File size: 14,871 Bytes
5024af9
 
 
 
 
2a14d16
b25337f
b72b034
5024af9
 
 
 
 
 
 
 
 
 
 
2a14d16
5024af9
 
 
 
 
0ff9cc9
5024af9
 
b72b034
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b25337f
5024af9
 
b25337f
5024af9
 
 
 
 
 
 
 
 
b72b034
5024af9
 
 
 
b72b034
5024af9
b72b034
 
 
 
 
5024af9
b72b034
5024af9
 
 
b72b034
0ff9cc9
b25337f
5024af9
 
 
 
 
b25337f
 
 
 
 
b72b034
5024af9
 
 
 
 
 
 
 
 
 
 
 
b25337f
5024af9
b72b034
0ff9cc9
 
 
 
 
 
 
 
5024af9
0ff9cc9
 
5024af9
 
 
b25337f
5024af9
 
 
b25337f
b72b034
b25337f
 
b72b034
b25337f
b72b034
b25337f
 
 
b72b034
b25337f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b72b034
 
 
 
 
b25337f
 
2cc56aa
5024af9
 
 
 
 
 
 
b25337f
 
 
 
 
 
 
 
 
 
5024af9
b72b034
5024af9
 
b25337f
 
 
 
 
 
 
5024af9
 
 
 
 
 
 
 
b25337f
b72b034
 
b25337f
 
 
 
 
 
 
 
 
 
5024af9
 
 
b25337f
 
 
 
 
 
 
 
 
 
5024af9
 
 
f1bfa96
5024af9
 
2cc56aa
2a14d16
b72b034
2a14d16
b72b034
2a14d16
 
 
 
 
 
 
b72b034
2a14d16
 
 
 
 
 
 
b72b034
 
 
2a14d16
 
 
 
 
b25337f
2a14d16
b72b034
 
2a14d16
 
 
b72b034
 
2a14d16
b72b034
 
 
 
 
 
 
 
2a14d16
 
 
2cc56aa
5024af9
b25337f
 
 
 
 
 
 
 
 
 
 
5024af9
b25337f
5024af9
b72b034
5024af9
 
f1bfa96
b25337f
 
 
 
 
 
 
 
 
 
 
5024af9
b72b034
 
 
5024af9
 
b25337f
 
 
 
 
 
 
 
 
 
 
 
b72b034
 
 
b25337f
 
 
 
 
 
 
 
 
 
b72b034
5024af9
b72b034
5024af9
b72b034
 
 
 
b25337f
5024af9
 
b72b034
 
 
9b5766e
2cc56aa
0c8ee71
 
 
2cc56aa
b72b034
 
c0ea652
b72b034
 
 
 
 
 
 
 
 
 
 
 
 
c0ea652
b72b034
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c8ee71
 
b72b034
 
c0ea652
b72b034
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5024af9
 
 
b25337f
 
 
 
 
 
 
 
 
 
 
b72b034
5024af9
 
 
 
 
 
b72b034
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
import gradio as gr
import pandas as pd
import requests
from prophet import Prophet
import logging
import plotly.graph_objs as go
import math
import numpy as np

logging.basicConfig(level=logging.INFO)

OKX_TICKERS_ENDPOINT = "https://www.okx.com/api/v5/market/tickers?instType=SPOT"
OKX_CANDLE_ENDPOINT = "https://www.okx.com/api/v5/market/candles"

TIMEFRAME_MAPPING = {
    "1m": "1m",
    "5m": "5m",
    "15m": "15m",
    "30m": "30m",
    "1h": "1H",
    "2h": "2H",
    "4h": "4H",
    "6h": "6H",
    "12h": "12H",
    "1d": "1D",
    "1w": "1W",
}

def calculate_technical_indicators(df):
    # Calculate RSI
    delta = df['close'].diff()
    gain = (delta.where(delta > 0, 0)).rolling(window=14).mean()
    loss = (-delta.where(delta < 0, 0)).rolling(window=14).mean()
    rs = gain / loss
    df['RSI'] = 100 - (100 / (1 + rs))
    
    # Calculate MACD
    exp1 = df['close'].ewm(span=12, adjust=False).mean()
    exp2 = df['close'].ewm(span=26, adjust=False).mean()
    df['MACD'] = exp1 - exp2
    df['Signal_Line'] = df['MACD'].ewm(span=9, adjust=False).mean()
    
    # Calculate Bollinger Bands
    df['MA20'] = df['close'].rolling(window=20).mean()
    df['BB_upper'] = df['MA20'] + 2 * df['close'].rolling(window=20).std()
    df['BB_lower'] = df['MA20'] - 2 * df['close'].rolling(window=20).std()
    
    return df

def create_technical_charts(df):
    # Price and Bollinger Bands
    fig1 = go.Figure()
    fig1.add_trace(go.Candlestick(
        x=df['timestamp'],
        open=df['open'],
        high=df['high'],
        low=df['low'],
        close=df['close'],
        name='Price'
    ))
    fig1.add_trace(go.Scatter(x=df['timestamp'], y=df['BB_upper'], name='Upper BB', line=dict(color='gray', dash='dash')))
    fig1.add_trace(go.Scatter(x=df['timestamp'], y=df['BB_lower'], name='Lower BB', line=dict(color='gray', dash='dash')))
    fig1.update_layout(title='Price and Bollinger Bands', xaxis_title='Date', yaxis_title='Price')

    # RSI
    fig2 = go.Figure()
    fig2.add_trace(go.Scatter(x=df['timestamp'], y=df['RSI'], name='RSI'))
    fig2.add_hline(y=70, line_dash="dash", line_color="red")
    fig2.add_hline(y=30, line_dash="dash", line_color="green")
    fig2.update_layout(title='RSI Indicator', xaxis_title='Date', yaxis_title='RSI')

    # MACD
    fig3 = go.Figure()
    fig3.add_trace(go.Scatter(x=df['timestamp'], y=df['MACD'], name='MACD'))
    fig3.add_trace(go.Scatter(x=df['timestamp'], y=df['Signal_Line'], name='Signal Line'))
    fig3.update_layout(title='MACD', xaxis_title='Date', yaxis_title='Value')

    return fig1, fig2, fig3


def fetch_okx_symbols():
    """
    Fetch spot symbols from OKX.
    """
    logging.info("Fetching symbols from OKX Spot tickers...")
    try:
        resp = requests.get(OKX_TICKERS_ENDPOINT, timeout=30)
        resp.raise_for_status()
        json_data = resp.json()

        if json_data.get("code") != "0":
            logging.error(f"Non-zero code returned: {json_data}")
            return ["BTC-USDT"]  # Default fallback

        data = json_data.get("data", [])
        symbols = [item["instId"] for item in data if item.get("instType") == "SPOT"]
        if not symbols:
            return ["BTC-USDT"]
        
        # Ensure BTC-USDT is first in the list
        if "BTC-USDT" in symbols:
            symbols.remove("BTC-USDT")
            symbols.insert(0, "BTC-USDT")
            
        logging.info(f"Fetched {len(symbols)} OKX spot symbols.")
        return symbols

    except Exception as e:
        logging.error(f"Error fetching OKX symbols: {e}")
        return ["BTC-USDT"]

def fetch_okx_candles_chunk(symbol, timeframe, limit=300, after=None, before=None):
    params = {
        "instId": symbol,
        "bar": timeframe,
        "limit": limit
    }
    if after is not None:
        params["after"] = str(after)
    if before is not None:
        params["before"] = str(before)

    logging.info(f"Fetching chunk: symbol={symbol}, bar={timeframe}, limit={limit}")
    try:
        resp = requests.get(OKX_CANDLE_ENDPOINT, params=params, timeout=30)
        resp.raise_for_status()
        json_data = resp.json()

        if json_data.get("code") != "0":
            msg = f"OKX returned code={json_data.get('code')}, msg={json_data.get('msg')}"
            logging.error(msg)
            return pd.DataFrame(), msg

        items = json_data.get("data", [])
        if not items:
            return pd.DataFrame(), ""

        columns = ["ts", "o", "h", "l", "c", "vol", "volCcy", "volCcyQuote", "confirm"]
        df = pd.DataFrame(items, columns=columns)
        df.rename(columns={
            "ts": "timestamp",
            "o": "open",
            "h": "high",
            "l": "low",
            "c": "close"
        }, inplace=True)
        df["timestamp"] = pd.to_datetime(df["timestamp"], unit="ms")
        numeric_cols = ["open", "high", "low", "close", "vol", "volCcy", "volCcyQuote", "confirm"]
        df[numeric_cols] = df[numeric_cols].astype(float)

        return df, ""
    except Exception as e:
        err_msg = f"Error fetching candles chunk for {symbol}: {e}"
        logging.error(err_msg)
        return pd.DataFrame(), err_msg



def fetch_okx_candles(symbol, timeframe="1H", total=2000):
    """
    Fetch historical candle data
    """
    logging.info(f"Fetching ~{total} candles for {symbol} @ {timeframe}")

    calls_needed = math.ceil(total / 300.0)
    all_data = []
    after_ts = None

    for _ in range(calls_needed):
        df_chunk, err = fetch_okx_candles_chunk(
            symbol, timeframe, limit=300, after=after_ts
        )
        if err:
            return pd.DataFrame(), err
        if df_chunk.empty:
            break

        earliest_ts = df_chunk["timestamp"].iloc[-1]
        after_ts = int(earliest_ts.timestamp() * 1000 - 1)
        all_data.append(df_chunk)

        if len(df_chunk) < 300:
            break

    if not all_data:
        return pd.DataFrame(), "No data returned."

    df_all = pd.concat(all_data, ignore_index=True)
    df_all.sort_values(by="timestamp", inplace=True)
    df_all.reset_index(drop=True, inplace=True)
    
    # Calculate technical indicators
    df_all = calculate_technical_indicators(df_all)
    
    logging.info(f"Fetched {len(df_all)} rows for {symbol}.")
    return df_all, ""



def prepare_data_for_prophet(df):
    if df.empty:
        return pd.DataFrame(columns=["ds", "y"])
    df_prophet = df.rename(columns={"timestamp": "ds", "close": "y"})
    return df_prophet[["ds", "y"]]

def prophet_forecast(
    df_prophet,
    periods=10,
    freq="h",
    daily_seasonality=False,
    weekly_seasonality=False,
    yearly_seasonality=False,
    seasonality_mode="additive",
    changepoint_prior_scale=0.05,
):
    if df_prophet.empty:
        return pd.DataFrame(), "No data for Prophet."

    try:
        model = Prophet(
            daily_seasonality=daily_seasonality,
            weekly_seasonality=weekly_seasonality,
            yearly_seasonality=yearly_seasonality,
            seasonality_mode=seasonality_mode,
            changepoint_prior_scale=changepoint_prior_scale,
        )
        model.fit(df_prophet)
        future = model.make_future_dataframe(periods=periods, freq=freq)
        forecast = model.predict(future)
        return forecast, ""
    except Exception as e:
        logging.error(f"Forecast error: {e}")
        return pd.DataFrame(), f"Forecast error: {e}"




def prophet_wrapper(
    df_prophet,
    forecast_steps,
    freq,
    daily_seasonality,
    weekly_seasonality,
    yearly_seasonality,
    seasonality_mode,
    changepoint_prior_scale,
):
    if len(df_prophet) < 10:
        return pd.DataFrame(), "Not enough data for forecasting (need >=10 rows)."

    full_forecast, err = prophet_forecast(
        df_prophet,
        periods=forecast_steps,
        freq=freq,
        daily_seasonality=daily_seasonality,
        weekly_seasonality=weekly_seasonality,
        yearly_seasonality=yearly_seasonality,
        seasonality_mode=seasonality_mode,
        changepoint_prior_scale=changepoint_prior_scale,
    )
    if err:
        return pd.DataFrame(), err

    future_only = full_forecast.loc[len(df_prophet):, ["ds", "yhat", "yhat_lower", "yhat_upper"]]
    return future_only, ""



def create_forecast_plot(forecast_df):
    if forecast_df.empty:
        return go.Figure()

    fig = go.Figure()
    fig.add_trace(go.Scatter(
        x=forecast_df["ds"],
        y=forecast_df["yhat"],
        mode="lines",
        name="Forecast",
        line=dict(color="blue", width=2)
    ))

    fig.add_trace(go.Scatter(
        x=forecast_df["ds"],
        y=forecast_df["yhat_lower"],
        fill=None,
        mode="lines",
        line=dict(width=0),
        showlegend=True,
        name="Lower Bound"
    ))

    fig.add_trace(go.Scatter(
        x=forecast_df["ds"],
        y=forecast_df["yhat_upper"],
        fill="tonexty",
        mode="lines",
        line=dict(width=0),
        name="Upper Bound"
    ))

    fig.update_layout(
        title="Price Forecast",
        xaxis_title="Time",
        yaxis_title="Price",
        hovermode="x unified",
        template="plotly_white",
        legend=dict(
            yanchor="top",
            y=0.99,
            xanchor="left",
            x=0.01
        )
    )
    return fig



def predict(
    symbol,
    timeframe,
    forecast_steps,
    total_candles,
    daily_seasonality,
    weekly_seasonality,
    yearly_seasonality,
    seasonality_mode,
    changepoint_prior_scale,
):
    okx_bar = TIMEFRAME_MAPPING.get(timeframe, "1H")
    df_raw, err = fetch_okx_candles(symbol, timeframe=okx_bar, total=total_candles)
    if err:
        return pd.DataFrame(), pd.DataFrame(), err

    df_prophet = prepare_data_for_prophet(df_raw)
    freq = "h" if "h" in timeframe.lower() else "d"

    future_df, err2 = prophet_wrapper(
        df_prophet,
        forecast_steps,
        freq,
        daily_seasonality,
        weekly_seasonality,
        yearly_seasonality,
        seasonality_mode,
        changepoint_prior_scale,
    )
    if err2:
        return pd.DataFrame(), pd.DataFrame(), err2

    return df_raw, future_df, ""



def display_forecast(
    symbol,
    timeframe,
    forecast_steps,
    total_candles,
    daily_seasonality,
    weekly_seasonality,
    yearly_seasonality,
    seasonality_mode,
    changepoint_prior_scale,
):
    logging.info(f"Processing forecast request for {symbol}")
    
    df_raw, forecast_df, error = predict(
        symbol,
        timeframe,
        forecast_steps,
        total_candles,
        daily_seasonality,
        weekly_seasonality,
        yearly_seasonality,
        seasonality_mode,
        changepoint_prior_scale,
    )
    
    if error:
        return None, None, None, None, f"Error: {error}"

    forecast_plot = create_forecast_plot(forecast_df)
    tech_plot, rsi_plot, macd_plot = create_technical_charts(df_raw)
    
    return forecast_plot, tech_plot, rsi_plot, macd_plot, forecast_df

def main():
    symbols = fetch_okx_symbols()
    
    with gr.Blocks(theme=gr.themes.Base()) as demo:
        with gr.Row():
            gr.Markdown("# CryptoVision")

        gr.HTML("""<a href="https://visitorbadge.io/status?path=https%3A%2F%2Fopenfree-CryptoVision.hf.space">
                   <img src="https://api.visitorbadge.io/api/visitors?path=https%3A%2F%2Fopenfree-CryptoVision.hf.space&countColor=%23263759" />
                   </a>""")
        
        with gr.Row():
            with gr.Column(scale=1):
                with gr.Group():
                    gr.Markdown("### Market Selection")
                    symbol_dd = gr.Dropdown(
                        label="Trading Pair",
                        choices=symbols,
                        value="BTC-USDT"
                    )
                    timeframe_dd = gr.Dropdown(
                        label="Timeframe",
                        choices=list(TIMEFRAME_MAPPING.keys()),
                        value="1h"
                    )
                    
            with gr.Column(scale=1):
                with gr.Group():
                    gr.Markdown("### Forecast Parameters")
                    forecast_steps_slider = gr.Slider(
                        label="Forecast Steps",
                        minimum=1,
                        maximum=100,
                        value=24,
                        step=1
                    )
                    total_candles_slider = gr.Slider(
                        label="Historical Candles",
                        minimum=300,
                        maximum=3000,
                        value=2000,
                        step=100
                    )



        with gr.Row():
            with gr.Column():
                with gr.Group():
                    gr.Markdown("### Advanced Settings")
                    with gr.Row():
                        daily_box = gr.Checkbox(label="Daily Seasonality", value=True)
                        weekly_box = gr.Checkbox(label="Weekly Seasonality", value=True)
                        yearly_box = gr.Checkbox(label="Yearly Seasonality", value=False)
                    seasonality_mode_dd = gr.Dropdown(
                        label="Seasonality Mode",
                        choices=["additive", "multiplicative"],
                        value="additive"
                    )
                    changepoint_scale_slider = gr.Slider(
                        label="Changepoint Prior Scale",
                        minimum=0.01,
                        maximum=1.0,
                        step=0.01,
                        value=0.05
                    )

        with gr.Row():
            forecast_btn = gr.Button("Generate Forecast", variant="primary", size="lg")

        with gr.Row():
            forecast_plot = gr.Plot(label="Price Forecast")
            
        with gr.Row():
            tech_plot = gr.Plot(label="Technical Analysis")
            rsi_plot = gr.Plot(label="RSI Indicator")
            
        with gr.Row():
            macd_plot = gr.Plot(label="MACD")
            
        with gr.Row():
            forecast_df = gr.Dataframe(
                label="Forecast Data",
                headers=["Date", "Forecast", "Lower Bound", "Upper Bound"]
            )

        forecast_btn.click(
            fn=display_forecast,
            inputs=[
                symbol_dd,
                timeframe_dd,
                forecast_steps_slider,
                total_candles_slider,
                daily_box,
                weekly_box,
                yearly_box,
                seasonality_mode_dd,
                changepoint_scale_slider,
            ],
            outputs=[forecast_plot, tech_plot, rsi_plot, macd_plot, forecast_df]
        )

    return demo

if __name__ == "__main__":
    app = main()
    app.launch()