Invicto69 commited on
Commit
e1ef946
·
verified ·
1 Parent(s): ddc92c4

Synced repo using 'sync_with_huggingface' Github Action

Browse files
Files changed (3) hide show
  1. page/complete_backtest.py +10 -1
  2. page/single_backtest.py +19 -9
  3. utils.py +1 -18
page/complete_backtest.py CHANGED
@@ -61,13 +61,22 @@ def complete_backtest():
61
  st.success(f"Analysis finished in {round(time.time()-start, 2)} seconds")
62
 
63
  if "results" in st.session_state:
64
- st.write("⬇️ Select a row in index column to get detailed information of the respective stock run.")
 
 
 
 
65
  cols = ['stock', 'Start', 'End', 'Return [%]', 'Equity Final [$]', 'Buy & Hold Return [%]', '# Trades', 'Win Rate [%]', 'Best Trade [%]', 'Worst Trade [%]', 'Avg. Trade [%]']
66
  df = st.dataframe(st.session_state.results, hide_index=True, column_order=cols, on_select="rerun", selection_mode="single-row")
67
  df.selection.rows = 1
68
  if df.selection.rows:
69
  row = df.selection.rows
 
70
  plot = st.session_state.results['plot'].values[row]
 
 
 
 
71
  components.html(plot[0], height=1067)
72
 
73
  complete_backtest()
 
61
  st.success(f"Analysis finished in {round(time.time()-start, 2)} seconds")
62
 
63
  if "results" in st.session_state:
64
+ # st.write("⬇️ Select a row in index column to get detailed information of the respective stock run.")
65
+ st.markdown(f"""
66
+ ### :orange[Nifty50 stocks backtest result by using {strategy}]
67
+ ⬇️ Select a row in index column to get detailed information of the respective stock run.
68
+ """)
69
  cols = ['stock', 'Start', 'End', 'Return [%]', 'Equity Final [$]', 'Buy & Hold Return [%]', '# Trades', 'Win Rate [%]', 'Best Trade [%]', 'Worst Trade [%]', 'Avg. Trade [%]']
70
  df = st.dataframe(st.session_state.results, hide_index=True, column_order=cols, on_select="rerun", selection_mode="single-row")
71
  df.selection.rows = 1
72
  if df.selection.rows:
73
  row = df.selection.rows
74
+ ticker = st.session_state.results['stock'].values[row]
75
  plot = st.session_state.results['plot'].values[row]
76
+ color = "green" if st.session_state.results['Return [%]'].values[row][0] > 0 else "red"
77
+ st.markdown(f"""
78
+ ### :{color}[{ticker[0]} backtest plot] 📊
79
+ """)
80
  components.html(plot[0], height=1067)
81
 
82
  complete_backtest()
page/single_backtest.py CHANGED
@@ -7,7 +7,7 @@ from bokeh.plotting import figure
7
  from streamlit.components import v1 as components
8
 
9
  from indicators import SMC
10
- from utils import fetch, smc_plot_backtest, smc_ema_plot_backtest, smc_structure_plot_backtest
11
 
12
  def use_file_for_bokeh(chart: figure, chart_height=1067):
13
  # Function used to replace st.boken_chart, because streamlit doesn't support bokeh v3
@@ -73,6 +73,8 @@ def algorithmic_trading_dashboard():
73
  ema2 = st.number_input("Slow EMA Length", min_value=1, value=21)
74
  with c3:
75
  cross_close = st.checkbox("Close trade on EMA crossover", value=False)
 
 
76
 
77
  # Button to run the analysis
78
  if st.button("Run"):
@@ -95,18 +97,26 @@ def algorithmic_trading_dashboard():
95
  )
96
 
97
  # Generate backtest plot
98
- if strategy == "Order Block":
99
- backtest_plot = smc_plot_backtest(data, 'test.html', swing_hl)
100
- elif strategy == "Order Block with EMA":
101
- backtest_plot = smc_ema_plot_backtest(data, 'test.html', ema1, ema2, cross_close)
102
- elif strategy == "Structure trading":
103
- backtest_plot = smc_structure_plot_backtest(data, 'test.html', swing_hl)
 
 
104
 
105
  # Display plots
106
  st.write("### Signal Plot")
107
  st.plotly_chart(signal_plot, width=1200)
108
 
109
- st.write("### Backtesting Plot")
110
- st.bokeh_chart(backtest_plot)
 
 
 
 
 
 
111
 
112
  algorithmic_trading_dashboard()
 
7
  from streamlit.components import v1 as components
8
 
9
  from indicators import SMC
10
+ from utils import fetch, run_strategy
11
 
12
  def use_file_for_bokeh(chart: figure, chart_height=1067):
13
  # Function used to replace st.boken_chart, because streamlit doesn't support bokeh v3
 
73
  ema2 = st.number_input("Slow EMA Length", min_value=1, value=21)
74
  with c3:
75
  cross_close = st.checkbox("Close trade on EMA crossover", value=False)
76
+ else:
77
+ ema1, ema2, cross_close = None, None, None
78
 
79
  # Button to run the analysis
80
  if st.button("Run"):
 
97
  )
98
 
99
  # Generate backtest plot
100
+ # if strategy == "Order Block":
101
+ # backtest_plot = smc_plot_backtest(data, 'test.html', swing_hl)
102
+ # elif strategy == "Order Block with EMA":
103
+ # backtest_plot = smc_ema_plot_backtest(data, 'test.html', ema1, ema2, cross_close)
104
+ # elif strategy == "Structure trading":
105
+ # backtest_plot = smc_structure_plot_backtest(data, 'test.html', swing_hl)
106
+
107
+ backtest_results = run_strategy(ticker, strategy, period, interval, swing_hl=swing_hl, ema1=ema1, ema2=ema2, cross_close=cross_close)
108
 
109
  # Display plots
110
  st.write("### Signal Plot")
111
  st.plotly_chart(signal_plot, width=1200)
112
 
113
+ st.write('### Backtest Results')
114
+ cols = ['stock', 'Start', 'End', 'Return [%]', 'Equity Final [$]', 'Buy & Hold Return [%]', '# Trades',
115
+ 'Win Rate [%]', 'Best Trade [%]', 'Worst Trade [%]', 'Avg. Trade [%]']
116
+ st.dataframe(backtest_results, hide_index=True, column_order=cols)
117
+
118
+ st.write("### Backtest Plot")
119
+ plot = backtest_results['plot']
120
+ components.html(plot[0], height=1067)
121
 
122
  algorithmic_trading_dashboard()
utils.py CHANGED
@@ -14,21 +14,6 @@ def fetch(symbol, period, interval):
14
  df.columns =df.columns.get_level_values(0)
15
  return df
16
 
17
- def smc_plot_backtest(data, filename, swing_hl, **kwargs):
18
- bt = Backtest(data, SMC_test, **kwargs)
19
- bt.run(swing_window=swing_hl)
20
- return bt.plot(filename=filename, open_browser=False)
21
-
22
- def smc_ema_plot_backtest(data, filename, ema1, ema2, closecross, **kwargs):
23
- bt = Backtest(data, SMC_ema, **kwargs)
24
- bt.run(ema1=ema1, ema2=ema2, close_on_crossover=closecross)
25
- return bt.plot(filename=filename, open_browser=False)
26
-
27
- def smc_structure_plot_backtest(data, filename, swing_hl, **kwargs):
28
- bt = Backtest(data, SMCStructure, **kwargs)
29
- bt.run(swing_window=swing_hl)
30
- return bt.plot(filename=filename, open_browser=False)
31
-
32
  def smc_backtest(data, filename, swing_hl, **kwargs):
33
  bt = Backtest(data, SMC_test, **kwargs)
34
  results = bt.run(swing_window=swing_hl)
@@ -47,7 +32,7 @@ def smc_structure_backtest(data, filename, swing_hl, **kwargs):
47
  bt.plot(filename=filename, open_browser=False)
48
  return results
49
 
50
- def run_strategy(ticker_symbol, strategy, period, interval, kwargs):
51
  # Fetching ohlc of random ticker_symbol.
52
  retries = 3
53
  for i in range(retries):
@@ -87,8 +72,6 @@ def run_strategy(ticker_symbol, strategy, period, interval, kwargs):
87
  backtest_results['plot'] = plot
88
 
89
  # Reordering columns.
90
- # cols = df.columns.tolist()
91
- # cols = cols[-1:] + cols[:-1]
92
  cols = ['stock', 'Start', 'End', 'Return [%]', 'Equity Final [$]', 'Buy & Hold Return [%]', '# Trades',
93
  'Win Rate [%]', 'Best Trade [%]', 'Worst Trade [%]', 'Avg. Trade [%]', 'plot']
94
  backtest_results = backtest_results[cols]
 
14
  df.columns =df.columns.get_level_values(0)
15
  return df
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  def smc_backtest(data, filename, swing_hl, **kwargs):
18
  bt = Backtest(data, SMC_test, **kwargs)
19
  results = bt.run(swing_window=swing_hl)
 
32
  bt.plot(filename=filename, open_browser=False)
33
  return results
34
 
35
+ def run_strategy(ticker_symbol, strategy, period, interval, **kwargs):
36
  # Fetching ohlc of random ticker_symbol.
37
  retries = 3
38
  for i in range(retries):
 
72
  backtest_results['plot'] = plot
73
 
74
  # Reordering columns.
 
 
75
  cols = ['stock', 'Start', 'End', 'Return [%]', 'Equity Final [$]', 'Buy & Hold Return [%]', '# Trades',
76
  'Win Rate [%]', 'Best Trade [%]', 'Worst Trade [%]', 'Avg. Trade [%]', 'plot']
77
  backtest_results = backtest_results[cols]