Synced repo using 'sync_with_huggingface' Github Action
Browse files- page/complete_backtest.py +3 -1
- utils.py +1 -60
page/complete_backtest.py
CHANGED
@@ -15,6 +15,8 @@ def complete_backtest():
|
|
15 |
"""
|
16 |
)
|
17 |
|
|
|
|
|
18 |
limits = pd.read_csv('data/yahoo_limits.csv')
|
19 |
period_list = ['1d', '5d', '1mo', '3mo', '6mo', '1y', '2y', '5y', '10y', 'ytd', 'max']
|
20 |
|
@@ -35,7 +37,7 @@ def complete_backtest():
|
|
35 |
limit = limits[limits['interval'] == interval]['limit'].values[0]
|
36 |
idx = period_list.index(limit)
|
37 |
period_options = period_list[:idx + 1] + ['max']
|
38 |
-
period = st.selectbox("Select Period", period_options, index=
|
39 |
|
40 |
# EMA parameters if "Order Block with EMA" is selected
|
41 |
if strategy == "Order Block with EMA":
|
|
|
15 |
"""
|
16 |
)
|
17 |
|
18 |
+
st.info("Strategy runs on most of the Nifty50 stocks", icon="ℹ️")
|
19 |
+
|
20 |
limits = pd.read_csv('data/yahoo_limits.csv')
|
21 |
period_list = ['1d', '5d', '1mo', '3mo', '6mo', '1y', '2y', '5y', '10y', 'ytd', 'max']
|
22 |
|
|
|
37 |
limit = limits[limits['interval'] == interval]['limit'].values[0]
|
38 |
idx = period_list.index(limit)
|
39 |
period_options = period_list[:idx + 1] + ['max']
|
40 |
+
period = st.selectbox("Select Period", period_options, index=2)
|
41 |
|
42 |
# EMA parameters if "Order Block with EMA" is selected
|
43 |
if strategy == "Order Block with EMA":
|
utils.py
CHANGED
@@ -58,7 +58,7 @@ def run_strategy(ticker_symbol, strategy, period, interval, kwargs):
|
|
58 |
|
59 |
if len(data) == 0:
|
60 |
if i < retries - 1:
|
61 |
-
print(f"Attempt{i+1}: {ticker_symbol} ohlc is empty")
|
62 |
else:
|
63 |
raise Exception(f"{ticker_symbol} ohlc is empty")
|
64 |
else:
|
@@ -95,65 +95,6 @@ def run_strategy(ticker_symbol, strategy, period, interval, kwargs):
|
|
95 |
|
96 |
return backtest_results
|
97 |
|
98 |
-
def random_test(strategy: str, period: str, interval: str, no_of_stocks: int = 5, **kwargs):
|
99 |
-
nifty50 = pd.read_csv("data/ind_nifty50list.csv")
|
100 |
-
ticker_list = pd.read_csv("data/Ticker_List_NSE_India.csv")
|
101 |
-
|
102 |
-
# Merging nifty50 and ticker_list dataframes to get 'YahooEquiv' column.
|
103 |
-
nifty50 = nifty50.merge(ticker_list, "inner", left_on=['Symbol'], right_on=['SYMBOL'])
|
104 |
-
|
105 |
-
# Generating random indices between 0 and len(nifty50).
|
106 |
-
random_indices = random.sample(range(0, len(nifty50)), no_of_stocks)
|
107 |
-
|
108 |
-
df = pd.DataFrame()
|
109 |
-
|
110 |
-
for i in random_indices:
|
111 |
-
# Fetching ohlc of random ticker_symbol.
|
112 |
-
ticker_symbol = nifty50['YahooEquiv'].values[i]
|
113 |
-
retries = 3
|
114 |
-
for i in range(retries):
|
115 |
-
try:
|
116 |
-
data = fetch(ticker_symbol, period, interval)
|
117 |
-
except:
|
118 |
-
raise Exception(f"{ticker_symbol} data fetch failed")
|
119 |
-
|
120 |
-
if len(data) == 0:
|
121 |
-
raise Exception(f"{ticker_symbol} ohlc is empty")
|
122 |
-
else:
|
123 |
-
break
|
124 |
-
|
125 |
-
try:
|
126 |
-
if strategy == "Order Block":
|
127 |
-
backtest_results = smc_backtest(data, kwargs['swing_hl'])
|
128 |
-
elif strategy == "Order Block with EMA":
|
129 |
-
backtest_results = smc_ema_backtest(data, kwargs['ema1'], kwargs['ema2'], kwargs['cross_close'])
|
130 |
-
elif strategy == "Structure trading":
|
131 |
-
backtest_results = smc_structure_backtest(data, kwargs['swing_hl'])
|
132 |
-
else:
|
133 |
-
raise Exception('Strategy not found')
|
134 |
-
except:
|
135 |
-
raise Exception(f"{ticker_symbol} strategy run failed")
|
136 |
-
|
137 |
-
with open("bokeh_graph.html", 'r', encoding='utf-8') as f:
|
138 |
-
plot = f.read()
|
139 |
-
|
140 |
-
# Converting pd.Series to pd.Dataframe
|
141 |
-
backtest_results = backtest_results.to_frame().transpose()
|
142 |
-
|
143 |
-
backtest_results['stock'] = ticker_symbol
|
144 |
-
|
145 |
-
# Reordering columns.
|
146 |
-
# cols = df.columns.tolist()
|
147 |
-
# cols = cols[-1:] + cols[:-1]
|
148 |
-
cols = ['stock', 'Start', 'End', 'Return [%]', 'Equity Final [$]', 'Buy & Hold Return [%]', '# Trades', 'Win Rate [%]', 'Best Trade [%]', 'Worst Trade [%]', 'Avg. Trade [%]']
|
149 |
-
backtest_results = backtest_results[cols]
|
150 |
-
|
151 |
-
df = pd.concat([df, backtest_results])
|
152 |
-
|
153 |
-
df = df.sort_values(by=['Return [%]'], ascending=False)
|
154 |
-
|
155 |
-
return df
|
156 |
-
|
157 |
def complete_test(strategy: str, period: str, interval: str, multiprocess=True, **kwargs):
|
158 |
nifty50 = pd.read_csv("data/ind_nifty50list.csv")
|
159 |
ticker_list = pd.read_csv("data/Ticker_List_NSE_India.csv")
|
|
|
58 |
|
59 |
if len(data) == 0:
|
60 |
if i < retries - 1:
|
61 |
+
print(f"Attempt{i + 1}: {ticker_symbol} ohlc is empty")
|
62 |
else:
|
63 |
raise Exception(f"{ticker_symbol} ohlc is empty")
|
64 |
else:
|
|
|
95 |
|
96 |
return backtest_results
|
97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
def complete_test(strategy: str, period: str, interval: str, multiprocess=True, **kwargs):
|
99 |
nifty50 = pd.read_csv("data/ind_nifty50list.csv")
|
100 |
ticker_list = pd.read_csv("data/Ticker_List_NSE_India.csv")
|