Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,24 +11,36 @@ server = app.server
|
|
| 11 |
# Setup MEXC exchange
|
| 12 |
exchange = ccxt.mexc()
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
symbols = ["BTC/USDT", "ETH/USDT", "
|
| 16 |
timeframes = ["1m", "5m", "15m", "1h", "4h", "1d"]
|
| 17 |
|
| 18 |
-
# Default
|
| 19 |
default_symbol = "BTC/USDT"
|
| 20 |
default_timeframe = "1h"
|
| 21 |
|
| 22 |
-
# Helper
|
| 23 |
def fetch_ohlcv(symbol, timeframe):
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
# Layout
|
| 30 |
app.layout = html.Div([
|
| 31 |
-
html.H1("📊 TraderX AI
|
|
|
|
| 32 |
html.Div([
|
| 33 |
html.Label("Symbol:"),
|
| 34 |
dcc.Dropdown(
|
|
@@ -44,43 +56,44 @@ app.layout = html.Div([
|
|
| 44 |
value=default_timeframe,
|
| 45 |
style={"width": "120px"}
|
| 46 |
),
|
| 47 |
-
], style={"display": "flex", "alignItems": "center", "marginBottom": "20px"}),
|
| 48 |
|
| 49 |
-
dcc.Graph(id="price-chart")
|
| 50 |
])
|
| 51 |
|
| 52 |
-
# Callback
|
| 53 |
@app.callback(
|
| 54 |
Output("price-chart", "figure"),
|
| 55 |
Input("symbol-dropdown", "value"),
|
| 56 |
Input("timeframe-dropdown", "value")
|
| 57 |
)
|
| 58 |
def update_chart(symbol, timeframe):
|
| 59 |
-
|
| 60 |
-
df = fetch_ohlcv(symbol, timeframe)
|
| 61 |
-
|
| 62 |
-
candle = go.Candlestick(
|
| 63 |
-
x=df["timestamp"],
|
| 64 |
-
open=df["open"],
|
| 65 |
-
high=df["high"],
|
| 66 |
-
low=df["low"],
|
| 67 |
-
close=df["close"],
|
| 68 |
-
name="Price"
|
| 69 |
-
)
|
| 70 |
-
|
| 71 |
-
layout = go.Layout(
|
| 72 |
-
title=f"{symbol} - {timeframe} Chart (MEXC)",
|
| 73 |
-
xaxis={"title": "Time", "rangeslider": {"visible": True}},
|
| 74 |
-
yaxis={"title": "Price (USDT)"},
|
| 75 |
-
height=600
|
| 76 |
-
)
|
| 77 |
-
|
| 78 |
-
return {"data": [candle], "layout": layout}
|
| 79 |
|
| 80 |
-
|
| 81 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
if __name__ == "__main__":
|
| 85 |
app.run(host="0.0.0.0", port=7860)
|
| 86 |
-
|
|
|
|
| 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(
|
|
|
|
| 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 |
|
| 73 |
+
if df.empty:
|
| 74 |
+
return {
|
| 75 |
+
"data": [],
|
| 76 |
+
"layout": go.Layout(title="⚠️ Error fetching data or empty chart returned")
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
trace = go.Candlestick(
|
| 80 |
+
x=df["timestamp"],
|
| 81 |
+
open=df["open"],
|
| 82 |
+
high=df["high"],
|
| 83 |
+
low=df["low"],
|
| 84 |
+
close=df["close"],
|
| 85 |
+
name="OHLC"
|
| 86 |
+
)
|
| 87 |
|
| 88 |
+
layout = go.Layout(
|
| 89 |
+
title=f"{symbol} - {timeframe}",
|
| 90 |
+
xaxis={"title": "Time", "rangeslider": {"visible": True}},
|
| 91 |
+
yaxis={"title": "Price (USDT)"},
|
| 92 |
+
height=600
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
return {"data": [trace], "layout": layout}
|
| 96 |
+
|
| 97 |
+
# Start server (for modern Dash versions)
|
| 98 |
if __name__ == "__main__":
|
| 99 |
app.run(host="0.0.0.0", port=7860)
|
|
|