File size: 18,078 Bytes
a4f3539
6781598
f26fad2
ed27171
9d08131
ed27171
effbece
 
ed27171
8146f18
0b9fb08
f26fad2
8146f18
 
 
 
 
 
 
 
 
 
ed27171
 
9d08131
ed27171
 
 
8146f18
ed27171
8146f18
9d08131
8146f18
 
ed27171
8146f18
9d08131
8146f18
 
 
9d08131
8146f18
 
 
9d08131
8146f18
 
 
9d08131
8146f18
 
ed27171
8146f18
 
ed27171
8146f18
 
 
 
 
 
 
ed27171
9d08131
ed27171
 
8146f18
 
ed27171
8146f18
9d08131
8146f18
 
 
9d08131
8146f18
 
 
0c2f6bc
8146f18
 
ed27171
8146f18
9d08131
8146f18
 
ed27171
 
 
 
9d08131
ed27171
 
 
 
9d08131
ed27171
 
8146f18
 
ed27171
8146f18
9d08131
8146f18
 
 
9d08131
8146f18
 
ed27171
 
 
8146f18
9d08131
8146f18
 
ed27171
 
8146f18
 
ed27171
8146f18
9d08131
8146f18
 
 
9d08131
8146f18
 
 
9d08131
8146f18
 
ed27171
8146f18
 
ed27171
8146f18
9d08131
8146f18
 
ed27171
8146f18
9d08131
8146f18
 
ed27171
 
8146f18
ed27171
 
8146f18
 
 
 
 
 
 
 
 
9d08131
8146f18
 
 
ed27171
8146f18
aeb8adb
 
8146f18
 
 
 
 
 
 
 
 
 
aeb8adb
a4f3539
f26fad2
ed27171
8146f18
ed27171
 
9d08131
ed27171
 
9d08131
ed27171
9d08131
ed27171
 
9d08131
ed27171
 
 
 
 
 
 
 
 
 
 
8146f18
f26fad2
8146f18
 
 
 
9d08131
a4f3539
8146f18
 
a4f3539
8146f18
 
 
a4f3539
 
8146f18
 
 
 
a4f3539
 
 
ed27171
8146f18
 
ed27171
 
899b04c
a4f3539
899b04c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed27171
899b04c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed27171
899b04c
 
 
 
 
 
 
 
 
 
 
 
 
a4f3539
899b04c
 
 
a4f3539
 
899b04c
 
 
 
a4f3539
 
 
ed27171
899b04c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed27171
899b04c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed27171
899b04c
 
 
 
ed27171
 
 
899b04c
ed27171
 
 
 
 
 
 
 
 
 
 
 
 
 
1f5d7f2
 
ed27171
 
 
0fd9ef8
ed27171
 
 
9d08131
1f5d7f2
899b04c
 
ed27171
899b04c
a4f3539
6781598
a4f3539
 
9d08131
 
 
 
 
 
6781598
 
9d08131
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4f3539
9d08131
 
 
 
 
a4f3539
 
 
9d08131
 
f4998b5
9d08131
40f1a26
a4f3539
9d08131
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4f3539
9d08131
 
 
 
 
a4f3539
 
462dfc6
9d08131
 
 
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
import plotly.graph_objects as go
import plotly.express as px
import pandas as pd
import logging
import matplotlib.pyplot as plt
from plotly.subplots import make_subplots
import numpy as np

logging.basicConfig(level=logging.INFO)

def plot_indicators(df, ticker):
    try:
        fig = make_subplots(
            rows=7, cols=1, shared_xaxes=True, vertical_spacing=0.03,
            subplot_titles=(
                'Price & Moving Averages', 'Volume', 'MACD & RSI',
                'Stochastic & Williams %R', 'ADX & DI', 'ATR & CCI', 'Signal Strength'
            ),
            row_heights=[0.4, 0.1, 0.15, 0.15, 0.15, 0.15, 0.15]
        )

        # Price and Moving Averages
        fig.add_trace(
            go.Candlestick(
                x=df.index, open=df['Open'], high=df['High'], low=df['Low'], close=df['value'],
                name='Price', increasing_line_color='#00CC96', decreasing_line_color='#EF553B'
            ), row=1, col=1
        )
        for ma in ['sma_10', 'sma_20', 'sma_50', 'ema_12', 'ema_26', 'ema_50']:
            if ma in df:
                fig.add_trace(
                    go.Scatter(x=df.index, y=df[ma], name=ma.upper(), line=dict(width=1.5)),
                    row=1, col=1
                )
        if 'bbu_20_2' in df:
            fig.add_trace(
                go.Scatter(x=df.index, y=df['bbu_20_2'], name='BB Upper', line=dict(color='gray', dash='dot')),
                row=1, col=1
            )
            fig.add_trace(
                go.Scatter(x=df.index, y=df['bbm_20_2'], name='BB Middle', line=dict(color='gray')),
                row=1, col=1
            )
            fig.add_trace(
                go.Scatter(x=df.index, y=df["bbl_20_2"], name="BB Lower", line=dict(color="gray", dash="dot")),
                row=1, col=1
            )



        # Position Size and Risk Annotation
        if 'atr_14' in df:
            atr = df['atr_14'].iloc[-1]
            stop_distance = atr * 2
            position_size = (10000 * 0.01) / stop_distance
            fig.add_annotation(
                text=f"Position Size: {position_size:.0f} shares (1% risk, ATR {atr:.2f})",
                xref="paper", yref="paper", x=0.05, y=0.95, showarrow=False,
                font=dict(color="black", size=12)
            )

        # Volume
        fig.add_trace(
            go.Bar(x=df.index, y=df["Volume"], name="Volume", marker_color="blue", opacity=0.5),
            row=2, col=1
        )

        # MACD & RSI
        if 'macd_12_26_9' in df:
            fig.add_trace(
                go.Scatter(x=df.index, y=df['macd_12_26_9'], name='MACD', line=dict(color='blue')),
                row=3, col=1
            )
            fig.add_trace(
                go.Scatter(x=df.index, y=df["macds_12_26_9"], name="MACD Signal", line=dict(color="orange")),
                row=3, col=1
            )
            fig.add_trace(
                go.Bar(x=df.index, y=df["macdhist_12_26_9"], name="MACD Hist", marker_color="gray"),
                row=3, col=1
            )
        if 'rsi_14' in df:
            fig.add_trace(
                go.Scatter(x=df.index, y=df["rsi_14"], name="RSI 14", line=dict(color="purple")),
                row=3, col=1
            )
            fig.add_hline(y=70, line_dash="dash", line_color="red", row=3, col=1)
            fig.add_hline(y=30, line_dash="dash", line_color="green", row=3, col=1)
        if 'rsi_21' in df:
            fig.add_trace(
                go.Scatter(x=df.index, y=df["rsi_21"], name="RSI 21", line=dict(color="magenta", dash="dash")),
                row=3, col=1
            )
        if 'rsi_50' in df:
            fig.add_trace(
                go.Scatter(x=df.index, y=df["rsi_50"], name="RSI 50", line=dict(color="cyan", dash="dot")),
                row=3, col=1
            )

        # Stochastic & Williams %R
        if 'stochk_14_3_3' in df:
            fig.add_trace(
                go.Scatter(x=df.index, y=df["stochk_14_3_3"], name="Stoch %K", line=dict(color="blue")),
                row=4, col=1
            )
            fig.add_trace(
                go.Scatter(x=df.index, y=df["stochd_14_3_3"], name="Stoch %D", line=dict(color="orange")),
                row=4, col=1
            )
            fig.add_hline(y=80, line_dash="dash", line_color="red", row=4, col=1)
            fig.add_hline(y=20, line_dash="dash", line_color="green", row=4, col=1)
        if 'willr_14' in df:
            fig.add_trace(
                go.Scatter(x=df.index, y=df["willr_14"], name="Williams %R", line=dict(color="green")),
                row=4, col=1
            )
            fig.add_hline(y=-20, line_dash="dash", line_color="red", row=4, col=1)
            fig.add_hline(y=-80, line_dash="dash", line_color="green", row=4, col=1)

        # ADX & DI
        if 'adx_14' in df:
            fig.add_trace(
                go.Scatter(x=df.index, y=df['adx_14'], name='ADX', line=dict(color='blue')),
                row=5, col=1
            )
            fig.add_trace(
                go.Scatter(x=df.index, y=df.get('pdi_14'), name='+DI', line=dict(color='green')),
                row=5, col=1
            )
            fig.add_trace(
                go.Scatter(x=df.index, y=df.get('mdi_14'), name='-DI', line=dict(color='red')),
                row=5, col=1
            )
            fig.add_hline(y=25, line_dash="dash", line_color="black", row=5, col=1)

        # ATR & CCI
        if 'atr_14' in df:
            fig.add_trace(
                go.Scatter(x=df.index, y=df["atr_14"], name="ATR", line=dict(color="orange")),
                row=6, col=1
            )
        if 'cci_20' in df:
            fig.add_trace(
                go.Scatter(x=df.index, y=df["cci_20"], name="CCI", line=dict(color="purple")),
                row=6, col=1
            )
            fig.add_hline(y=100, line_dash="dash", line_color="red", row=6, col=1)
            fig.add_hline(y=-100, line_dash="dash", line_color="green", row=6, col=1)

        # Signal Strength Plot
        if all(col in df for col in ['RSI_Signal', 'MACD_Signal', 'ADX_Signal', 'Sentiment_Signal', 'Model_Signal']):
            signal_strength = (
                df['RSI_Signal'].abs() +
                df['MACD_Signal'].abs() +
                df['ADX_Signal'].abs() +
                df['Sentiment_Signal'].abs() +
                df['Model_Signal'].abs()
            )
            fig.add_trace(
                go.Scatter(
                    x=df.index, y=signal_strength, name='Signal Strength',
                    line=dict(color='teal'), fill='tozeroy'
                ), row=7, col=1
            )
            fig.add_hline(y=3, line_dash="dash", line_color="orange", row=7, col=1, annotation_text="Strong Signal Threshold")

        fig.update_layout(
            title=f"{ticker} Price and Technical Indicators",
            template="plotly_white",
            height=2400,
            width=1400,
            showlegend=True,
            xaxis_rangeslider_visible=False,
            margin=dict(l=50, r=50, t=100, b=50),
            xaxis=dict(tickformat="%Y-%m-%d", minor=dict(ticks="inside", showgrid=True), gridcolor="lightgrey"),
            plot_bgcolor="white",
            paper_bgcolor="white",
            hovermode="x unified"
        )
        return fig
    except Exception as e:
        logging.error(f"Plot indicators error: {e}")
        return None
def plot_future_forecast(df, result, indicators):
    fig, ax = plt.subplots(figsize=(10, 6))
    ax.plot(df.index, df["value"], label="Historical Value", color="blue", linewidth=2)
    for ind in indicators:
        if ind in df.columns:
            ax.plot(df.index, df[ind], label=ind, linestyle='--')
    if "latest_prediction" in result:
        last_date = df.index[-1]
        horizon = len(result["latest_prediction"])
        future_dates = pd.date_range(start=last_date + pd.Timedelta(days=1), periods=horizon, freq='B')
        ax.plot(future_dates, result["latest_prediction"], label="Forecast Value", color="orange", linestyle="--", linewidth=2)
        for i, val in enumerate(result["latest_prediction"]):
            ax.text(future_dates[i], val, f"{val:.2f}", color="orange")
    ax.legend()
    ax.set_title("Historical Data, Indicators, and Future Forecast")
    ax.set_xlabel("Date")
    ax.set_ylabel("Value")
    ax.grid(True)
    plt.tight_layout()
    return fig
    
# Other plotting functions remain unchanged
def plot_forecast(result, df):
    try:
        actual = result.get("actual", [])
        forecast = result.get("forecast", [])
        if not actual or not forecast:
            return None
        dates = df.index[-len(actual):]
        fig = go.Figure()
        fig.add_trace(go.Scatter(x=dates, y=actual, name='Actual', line=dict(color='blue')))
        fig.add_trace(go.Scatter(x=dates, y=forecast, name='Forecast', line=dict(color='orange')))
        fig.update_layout(
            title="Backtest: Actual vs Forecast",
            template="plotly_white",
            height=600,
            xaxis_title="Date",
            yaxis_title="Price",
            xaxis=dict(tickformat="%Y-%m-%d", minor=dict(ticks="inside", showgrid=True), gridcolor="lightgrey"),
            yaxis=dict(gridcolor="lightgrey"),
            plot_bgcolor="white",
            paper_bgcolor="white"
        )
        return fig
    except Exception as e:
        logging.error(f"Plot forecast error: {e}")
        return None

# ... (other plotting functions like plot_future_forecast, plot_metrics_r2, etc., remain as provided)

def plot_metrics_r2(result):
    try:
        metrics = result.get("metrics", {})
        if not metrics:
            return None
        fig = go.Figure()
        fig.add_trace(go.Bar(
            x=['R²', 'MAPE'],
            y=[metrics.get('R2', 0), metrics.get('MAPE', 0)],
            marker_color=['#1E90FF', '#FF6347']
        ))
        fig.update_layout(
            title="R² and MAPE Metrics",
            template="plotly_white",
            height=600,
            xaxis_title="Metric",
            yaxis_title="Value",
            xaxis=dict(minor=dict(ticks="inside", showgrid=True), gridcolor="lightgrey"),
            yaxis=dict(gridcolor="lightgrey"),
            plot_bgcolor="white",
            paper_bgcolor="white"
        )
        return fig
    except Exception as e:
        logging.error(f"Plot R2 error: {e}")
        return None

def plot_metrics_errors(result):
    try:
        metrics = result.get("metrics", {})
        if not metrics:
            return None
        fig = go.Figure()
        fig.add_trace(go.Bar(
            x=['RMSE', 'MAE'],
            y=[metrics.get('RMSE', 0), metrics.get('MAE', 0)],
            marker_color=['#32CD32', '#9370DB']
        ))
        fig.update_layout(
            title="Error Metrics",
            template="plotly_white",
            height=600,
            xaxis_title="Metric",
            yaxis_title="Value",
            xaxis=dict(minor=dict(ticks="inside", showgrid=True), gridcolor="lightgrey"),
            yaxis=dict(gridcolor="lightgrey"),
            plot_bgcolor="white",
            paper_bgcolor="white"
        )
        return fig
    except Exception as e:
        logging.error(f"Plot metrics errors: {e}")
        return None

def plot_metrics_precision_recall(result):
    try:
        metrics = result.get("metrics", {})
        if not metrics:
            return None
        fig = go.Figure()
        fig.add_trace(go.Bar(
            x=['Precision', 'Recall'],
            y=[metrics.get('Precision', 0), metrics.get('Recall', 0)],
            marker_color=['#1E90FF', '#FF6347']
        ))
        fig.update_layout(
            title="Precision and Recall Metrics",
            template="plotly_white",
            height=600,
            xaxis_title="Metric",
            yaxis_title="Value",
            xaxis=dict(minor=dict(ticks="inside", showgrid=True), gridcolor="lightgrey"),
            yaxis=dict(gridcolor="lightgrey"),
            plot_bgcolor="white",
            paper_bgcolor="white"
        )
        return fig
    except Exception as e:
        logging.error(f"Plot precision recall error: {e}")
        return None

def plot_metrics_risk(result):
    try:
        metrics = result.get("metrics", {})
        if not metrics:
            return None
        fig = go.Figure()
        fig.add_trace(go.Bar(
            x=['MASE', 'Sharpe', 'Volatility'],
            y=[metrics.get('MASE', 0), metrics.get('Sharpe', 0), metrics.get('Volatility', 0)],
            marker_color=['#32CD32', '#9370DB', '#FFD700']
        ))
        fig.update_layout(
            title="Risk Metrics",
            template="plotly_white",
            height=600,
            xaxis_title="Metric",
            yaxis_title="Value",
            xaxis=dict(minor=dict(ticks="inside", showgrid=True), gridcolor="lightgrey"),
            yaxis=dict(gridcolor="lightgrey"),
            plot_bgcolor="white",
            paper_bgcolor="white"
        )
        return fig
    except Exception as e:
        logging.error(f"Plot risk metrics error: {e}")
        return None

def plot_loss_curve(result):
    try:
        train_loss = result.get("train_loss", [])
        val_loss = result.get("val_loss", [])
        if not train_loss:
            return None
        epochs = list(range(1, len(train_loss) + 1))
        fig = go.Figure()
        fig.add_trace(go.Scatter(x=epochs, y=train_loss, mode='lines', name='Train Loss', line=dict(color='#00CC96')))
        fig.add_trace(go.Scatter(x=epochs, y=val_loss, mode='lines', name='Validation Loss', line=dict(color='#EF553B')))
        fig.update_layout(
            title="Training and Validation Loss",
            template="plotly_white",
            height=600,
            xaxis_title="Epoch",
            yaxis_title="Loss",
            xaxis=dict(minor=dict(ticks="inside", showgrid=True), gridcolor="lightgrey"),
            yaxis=dict(gridcolor="lightgrey"),
            plot_bgcolor="white",
            paper_bgcolor="white"
        )
        return fig
    except Exception as e:
        logging.error(f"Plot loss curve error: {e}")
        return None

def plot_model_architecture(result):
    try:
        summary_text = result.get("model_summary", "No model summary available.")
        if not summary_text or summary_text == "No model summary available.":
            summary_text = "Model architecture summary could not be generated."
        fig = go.Figure()
        fig.add_annotation(
            text=summary_text.replace('\n', '<br>'),
            xref="paper",
            yref="paper",
            x=0,
            y=1,
            showarrow=False,
            font=dict(size=14, family="Courier New", color="#1E90FF"),
            align="left",
            bgcolor="white",
            bordercolor="black",
            borderwidth=1,
            width=600,
            height=400
        )
        fig.update_layout(
            title="Model Architecture",
            template="plotly_white",
            height=600,
            showlegend=False,
            xaxis=dict(visible=False),
            yaxis=dict(visible=False),
            plot_bgcolor="white",
            paper_bgcolor="white"
        )
        return fig
    except Exception as e:
        logging.error(f"Plot model architecture error: {e}")
        return None

def plot_signals(signals_df, ticker):
    try:
        fig = go.Figure()
        fig.add_trace(go.Scatter(
            x=signals_df.index,
            y=signals_df['Price'],
            mode='lines',
            name='Price'
        ))
        buy_signals = signals_df[signals_df['Signal'] == 'Buy']
        sell_signals = signals_df[signals_df['Signal'] == 'Sell']

        fig.add_trace(go.Scatter(
            x=buy_signals.index,
            y=buy_signals['Price'],
            mode='markers',
            marker=dict(symbol='triangle-up', size=10, color='green'),
            name='Buy Signal'
        ))
        fig.add_trace(go.Scatter(
            x=sell_signals.index,
            y=sell_signals['Price'],
            mode='markers',
            marker=dict(symbol='triangle-down', size=10, color='red'),
            name='Sell Signal'
        ))

        fig.update_layout(
            title=f'{ticker} Trading Signals',
            xaxis_title='Date',
            yaxis_title='Price',
            template="plotly_white",
            xaxis_rangeslider_visible=False
        )
        return fig
    except Exception as e:
        logging.error(f"Plot signals error: {e}")
        return None

def plot_backtest(equity_df, trades_df, ticker):
    try:
        fig = go.Figure()
        fig.add_trace(go.Scatter(
            x=equity_df.index,
            y=equity_df['Equity'],
            mode='lines',
            name='Equity Curve'
        ))

        buy_trades = trades_df[trades_df['Type'] == 'Buy']
        sell_trades = trades_df[trades_df['Type'] == 'Sell']
        exit_trades = trades_df[trades_df['Type'] == 'Exit']

        fig.add_trace(go.Scatter(
            x=buy_trades['Date'],
            y=buy_trades['Price'],
            mode='markers',
            marker=dict(symbol='triangle-up', size=10, color='green'),
            name='Buy Trades'
        ))
        fig.add_trace(go.Scatter(
            x=sell_trades['Date'],
            y=sell_trades['Price'],
            mode='markers',
            marker=dict(symbol='triangle-down', size=10, color='red'),
            name='Sell Trades'
        ))
        fig.add_trace(go.Scatter(
            x=exit_trades['Date'],
            y=exit_trades['Price'],
            mode='markers',
            marker=dict(symbol='circle', size=8, color='blue'),
            name='Exit Trades'
        ))

        fig.update_layout(
            title=f'{ticker} Backtest Equity Curve and Trades',
            xaxis_title='Date',
            yaxis_title='Equity / Price',
            template="plotly_white",
            xaxis_rangeslider_visible=False
        )
        return fig
    except Exception as e:
        logging.error(f"Plot backtest error: {e}")
        return None