Update utils.py
Browse files
utils.py
CHANGED
@@ -97,16 +97,29 @@ def random_test(strategy: str, period: str, interval: str, no_of_stocks: int = 5
|
|
97 |
for i in random_indices:
|
98 |
# Fetching ohlc of random ticker_symbol.
|
99 |
ticker_symbol = nifty50['YahooEquiv'].values[i]
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
|
111 |
with open("bokeh_graph.html", 'r', encoding='utf-8') as f:
|
112 |
plot = f.read()
|
|
|
97 |
for i in random_indices:
|
98 |
# Fetching ohlc of random ticker_symbol.
|
99 |
ticker_symbol = nifty50['YahooEquiv'].values[i]
|
100 |
+
retries = 3
|
101 |
+
for i in range(retries):
|
102 |
+
try:
|
103 |
+
data = fetch(ticker_symbol, period, interval)
|
104 |
+
except:
|
105 |
+
raise Exception(f"{ticker_symbol} data fetch failed")
|
106 |
+
|
107 |
+
if len(data) == 0:
|
108 |
+
raise Exception(f"{ticker_symbol} ohlc is empty")
|
109 |
+
else:
|
110 |
+
break
|
111 |
+
|
112 |
+
try:
|
113 |
+
if strategy == "Order Block":
|
114 |
+
backtest_results = smc_backtest(data, kwargs['swing_hl'])
|
115 |
+
elif strategy == "Order Block with EMA":
|
116 |
+
backtest_results = smc_ema_backtest(data, kwargs['ema1'], kwargs['ema2'], kwargs['cross_close'])
|
117 |
+
elif strategy == "Structure trading":
|
118 |
+
backtest_results = smc_structure_backtest(data, kwargs['swing_hl'])
|
119 |
+
else:
|
120 |
+
raise Exception('Strategy not found')
|
121 |
+
except:
|
122 |
+
raise Exception(f"{ticker_symbol} strategy run failed")
|
123 |
|
124 |
with open("bokeh_graph.html", 'r', encoding='utf-8') as f:
|
125 |
plot = f.read()
|