File size: 4,662 Bytes
7d8ad6e |
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 |
from dash import Dash, Input, Output, State, callback, dcc, html
import dash_bootstrap_components as dbc
import pandas as pd
from indicators import SMC
from data_fetcher import fetch
from strategies import smc_plot_backtest, smc_ema_plot_backtest
# Load symbols data
symbols = pd.read_csv('data/Ticker_List_NSE_India.csv')
# Initialize the app with a Bootstrap theme
app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
# Function to create the layout
app.layout = dbc.Container([
dbc.Row([
html.H1("Algorithmic Trading Dashboard", className="text-center mb-4")
]),
dbc.Row([
html.Label("Select Company Name", className="form-label"),
dcc.Dropdown(
id="name",
options=[{"label": name, "value": name} for name in symbols['NAME OF COMPANY'].unique()],
value='',
placeholder="Select a company",
className="mb-3"
),
html.Label("Select Strategy", className="form-label"),
dcc.Dropdown(
id="strategy",
options=['SMC', 'SMC with EMA'],
value='',
placeholder="Select Strategy",
className="mb-3"
),
html.Label("Swing High/Low Window Size", className="form-label"),
dcc.Input(
id="window",
type="number",
value=10,
placeholder="Enter window size",
className="form-control mb-3"
),
]),
html.Div([
dbc.Row([
dbc.Col([
html.Label("Fast EMA Length: ", className="form-label"),
dcc.Input(
id="ema1",
type="number",
value=9,
placeholder="Enter EMA Length",
# className="form-control mb-3"
className = "text-nowrap"
),
], md=8),
dbc.Col([
html.Label("Slow EMA Length: ", className="form-label"),
dcc.Input(
id="ema2",
type="number",
value=21,
placeholder="Enter EMA size",
# className="form-control mb-3"
className="text-nowrap"
),
dbc.Col([dcc.Checklist(['Close on EMA crossover'], id='closecross', className="text-nowrap")]),
]),
]),
], style={'display': 'block'}, id='smc_ema'
),
dbc.Button("Run", id="submit-button", color="primary", className="w-100 mb-4"),
dbc.Row([
html.H5("Order Block Chart", className="text-center mb-3"),
html.Iframe(
src="assets/SMC.html",
style={"height": "450px", "width": "95%", "border": "none"},
className="mb-4"
),
html.H5("Backtest Results", className="text-center mb-3"),
html.Iframe(
src="assets/backtest_results.html",
style={"height": "1067px", "width": "95%", "border": "none"}
),
])
], fluid=True)
@callback(
Output("smc_ema", 'style'),
Input("strategy", 'value')
)
def update_layout(strategy):
if strategy=='SMC with EMA':
return {'display':'block'}
else:
return {'display':'none'}
# Callback for updating the visualizations
@callback(
Input("submit-button", "n_clicks"),
State("name", "value"),
State("window", "value"),
State("strategy", "value"),
State("ema1", "value"),
State("ema2", "value"),
State("closecross", "value")
)
def update_visuals(n_clicks, name, window, strategy, ema1, ema2, closecross):
if n_clicks <= 0 or not name:
return
# Clear existing files
open('assets/backtest_results.html', 'w').close()
open('assets/SMC.html', 'w').close()
ticker = symbols[symbols['NAME OF COMPANY'] == name]['YahooEquiv'].values[0]
data = fetch(ticker, '1mo', '15m')
fig = SMC(data=data, swing_hl_window_sz=window).plot(show=False).update_layout(title=dict(text=ticker))
print(strategy)
if strategy=='SMC':
smc_plot_backtest(data, 'assets/backtest_results.html', swing_hl=window)
elif strategy=='SMC with EMA':
smc_ema_plot_backtest(data, 'assets/backtest_results.html', ema1, ema2, closecross)
fig.write_html('assets/SMC.html')
if __name__ == "__main__":
# Clear initial files
open('assets/backtest_results.html', 'w').close()
open('assets/SMC.html', 'w').close()
app.run(debug=True) |