Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 =
|
| 9 |
-
server = app.server
|
| 10 |
|
| 11 |
-
#
|
| 12 |
exchange = ccxt.mexc()
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
symbols = ["BTC/USDT", "ETH/USDT", "
|
| 16 |
timeframes = ["1m", "5m", "15m", "1h", "4h", "1d"]
|
| 17 |
|
| 18 |
-
#
|
| 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"β
|
| 38 |
-
return pd.DataFrame()
|
| 39 |
|
| 40 |
-
#
|
| 41 |
app.layout = html.Div([
|
| 42 |
-
html.
|
| 43 |
-
|
| 44 |
html.Div([
|
| 45 |
-
html.
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
])
|
| 63 |
|
| 64 |
-
# Callback
|
| 65 |
@app.callback(
|
| 66 |
Output("price-chart", "figure"),
|
| 67 |
Input("symbol-dropdown", "value"),
|
| 68 |
-
Input("timeframe-
|
| 69 |
)
|
| 70 |
def update_chart(symbol, timeframe):
|
| 71 |
df = fetch_ohlcv(symbol, timeframe)
|
| 72 |
-
print("π
|
| 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
|
| 80 |
}
|
| 81 |
|
| 82 |
-
|
| 83 |
x=df["timestamp"],
|
| 84 |
open=df["open"],
|
| 85 |
high=df["high"],
|
| 86 |
low=df["low"],
|
| 87 |
close=df["close"],
|
| 88 |
-
name="
|
| 89 |
)
|
| 90 |
|
| 91 |
layout = go.Layout(
|
| 92 |
title=f"{symbol} - {timeframe}",
|
| 93 |
-
xaxis=
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
height=600
|
| 96 |
)
|
| 97 |
|
| 98 |
-
return {"data": [
|
| 99 |
|
| 100 |
-
#
|
| 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)
|