0vergeared commited on
Commit
fbf1dcf
Β·
verified Β·
1 Parent(s): c793a8f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -53
app.py CHANGED
@@ -1,102 +1,106 @@
1
- import dash
2
- from dash import dcc, html, Input, Output
3
- import plotly.graph_objs as go
4
  import ccxt
5
  import pandas as pd
 
 
 
6
 
7
  # Initialize Dash app
8
- app = dash.Dash(__name__)
9
- server = app.server
10
 
11
- # Setup MEXC exchange
12
  exchange = ccxt.mexc()
13
 
14
- # Predefined pairs and timeframes
15
- symbols = ["BTC/USDT", "ETH/USDT", "DOGE/USDT", "SOL/USDT"]
16
  timeframes = ["1m", "5m", "15m", "1h", "4h", "1d"]
17
 
18
- # Default selections
19
- default_symbol = "BTC/USDT"
20
- default_timeframe = "1h"
21
-
22
- # Helper: Fetch OHLCV from MEXC
23
  def fetch_ohlcv(symbol, timeframe):
24
  try:
25
- print(f"πŸ“‘ Fetching {symbol} @ {timeframe} from MEXC...")
26
  ohlcv = exchange.fetch_ohlcv(symbol, timeframe)
27
-
28
- if not ohlcv or len(ohlcv) < 10:
29
- print("⚠️ Warning: No or little data received.")
30
- return pd.DataFrame()
31
-
32
  df = pd.DataFrame(ohlcv, columns=["timestamp", "open", "high", "low", "close", "volume"])
33
  df["timestamp"] = pd.to_datetime(df["timestamp"], unit="ms")
34
  return df
35
-
36
  except Exception as e:
37
- print(f"❌ Failed to fetch OHLCV: {e}")
38
- return pd.DataFrame()
39
 
40
- # Layout
41
  app.layout = html.Div([
42
- html.H1("πŸ“Š TraderX AI β€” MEXC Crypto Chart", style={"textAlign": "center"}),
43
-
44
  html.Div([
45
- html.Label("Symbol:"),
46
- dcc.Dropdown(
47
- id="symbol-dropdown",
48
- options=[{"label": s, "value": s} for s in symbols],
49
- value=default_symbol,
50
- style={"width": "200px"}
51
- ),
52
- html.Label("Timeframe:", style={"marginLeft": "20px"}),
53
- dcc.Dropdown(
54
- id="timeframe-dropdown",
55
- options=[{"label": tf, "value": tf} for tf in timeframes],
56
- value=default_timeframe,
57
- style={"width": "120px"}
58
- ),
59
- ], style={"display": "flex", "alignItems": "center", "justifyContent": "center", "marginBottom": "20px"}),
60
-
61
- dcc.Graph(id="price-chart", config={"scrollZoom": True})
 
 
 
 
 
62
  ])
63
 
64
- # Callback to update chart
65
  @app.callback(
66
  Output("price-chart", "figure"),
67
  Input("symbol-dropdown", "value"),
68
- Input("timeframe-dropdown", "value")
69
  )
70
  def update_chart(symbol, timeframe):
71
  df = fetch_ohlcv(symbol, timeframe)
72
- print("πŸ” DEBUG DATAFRAME HEAD:")
73
  print(df.head())
74
 
75
  if df.empty:
76
- print("❌ Empty dataframe received in callback.")
77
  return {
78
  "data": [],
79
- "layout": go.Layout(title="⚠️ No chart data available.")
80
  }
81
 
82
- trace = go.Candlestick(
83
  x=df["timestamp"],
84
  open=df["open"],
85
  high=df["high"],
86
  low=df["low"],
87
  close=df["close"],
88
- name="OHLC"
89
  )
90
 
91
  layout = go.Layout(
92
  title=f"{symbol} - {timeframe}",
93
- xaxis={"title": "Time", "rangeslider": {"visible": True}},
94
- yaxis={"title": "Price (USDT)"},
 
 
 
 
 
 
 
 
 
 
 
 
95
  height=600
96
  )
97
 
98
- return {"data": [trace], "layout": layout}
99
 
100
- # Start server (for modern Dash versions)
101
  if __name__ == "__main__":
102
  app.run(host="0.0.0.0", port=7860)
 
 
 
 
1
  import ccxt
2
  import pandas as pd
3
+ from datetime import datetime
4
+ import plotly.graph_objs as go
5
+ from dash import Dash, dcc, html, Input, Output
6
 
7
  # Initialize Dash app
8
+ app = Dash(__name__)
 
9
 
10
+ # MEXC exchange setup
11
  exchange = ccxt.mexc()
12
 
13
+ # Available pairs and timeframes
14
+ symbols = ["BTC/USDT", "ETH/USDT", "SOL/USDT", "BNB/USDT"]
15
  timeframes = ["1m", "5m", "15m", "1h", "4h", "1d"]
16
 
17
+ # Fetch OHLCV data from MEXC
 
 
 
 
18
  def fetch_ohlcv(symbol, timeframe):
19
  try:
 
20
  ohlcv = exchange.fetch_ohlcv(symbol, timeframe)
 
 
 
 
 
21
  df = pd.DataFrame(ohlcv, columns=["timestamp", "open", "high", "low", "close", "volume"])
22
  df["timestamp"] = pd.to_datetime(df["timestamp"], unit="ms")
23
  return df
 
24
  except Exception as e:
25
+ print(f"❌ Error fetching OHLCV for {symbol} ({timeframe}): {e}")
26
+ return pd.DataFrame(columns=["timestamp", "open", "high", "low", "close", "volume"])
27
 
28
+ # Dash app layout
29
  app.layout = html.Div([
30
+ html.H2("πŸ“Š Crypto Signal Chart - Powered by MEXC"),
31
+
32
  html.Div([
33
+ html.Div([
34
+ html.Label("Select Pair:"),
35
+ dcc.Dropdown(
36
+ id="symbol-dropdown",
37
+ options=[{"label": s, "value": s} for s in symbols],
38
+ value="BTC/USDT",
39
+ clearable=False
40
+ )
41
+ ], style={"width": "30%", "display": "inline-block"}),
42
+
43
+ html.Div([
44
+ html.Label("Timeframe:"),
45
+ dcc.RadioItems(
46
+ id="timeframe-buttons",
47
+ options=[{"label": t, "value": t} for t in timeframes],
48
+ value="1h",
49
+ labelStyle={"display": "inline-block", "margin-right": "10px"}
50
+ )
51
+ ], style={"width": "65%", "display": "inline-block", "padding-left": "20px"})
52
+ ], style={"margin-bottom": "20px"}),
53
+
54
+ dcc.Graph(id="price-chart")
55
  ])
56
 
57
+ # Callback for chart update
58
  @app.callback(
59
  Output("price-chart", "figure"),
60
  Input("symbol-dropdown", "value"),
61
+ Input("timeframe-buttons", "value")
62
  )
63
  def update_chart(symbol, timeframe):
64
  df = fetch_ohlcv(symbol, timeframe)
65
+ print("πŸ” Data sample:")
66
  print(df.head())
67
 
68
  if df.empty:
 
69
  return {
70
  "data": [],
71
+ "layout": go.Layout(title="⚠️ No data available for selected pair/timeframe.")
72
  }
73
 
74
+ candlestick = go.Candlestick(
75
  x=df["timestamp"],
76
  open=df["open"],
77
  high=df["high"],
78
  low=df["low"],
79
  close=df["close"],
80
+ name="Price"
81
  )
82
 
83
  layout = go.Layout(
84
  title=f"{symbol} - {timeframe}",
85
+ xaxis=dict(
86
+ title="Time",
87
+ rangeslider=dict(visible=True),
88
+ type="date",
89
+ rangeselector=dict(
90
+ buttons=[
91
+ dict(count=1, label="1d", step="day", stepmode="backward"),
92
+ dict(count=7, label="1w", step="day", stepmode="backward"),
93
+ dict(count=1, label="1m", step="month", stepmode="backward"),
94
+ dict(step="all")
95
+ ]
96
+ )
97
+ ),
98
+ yaxis=dict(title="Price (USDT)"),
99
  height=600
100
  )
101
 
102
+ return {"data": [candlestick], "layout": layout}
103
 
104
+ # Run app (for Docker Space)
105
  if __name__ == "__main__":
106
  app.run(host="0.0.0.0", port=7860)