Invicto69 commited on
Commit
5048888
·
verified ·
1 Parent(s): 45a96c2

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +23 -10
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
- data = fetch(ticker_symbol, period, interval)
101
-
102
- if strategy == "Order Block":
103
- backtest_results = smc_backtest(data, kwargs['swing_hl'])
104
- elif strategy == "Order Block with EMA":
105
- backtest_results = smc_ema_backtest(data, kwargs['ema1'], kwargs['ema2'], kwargs['cross_close'])
106
- elif strategy == "Structure trading":
107
- backtest_results = smc_structure_backtest(data, kwargs['swing_hl'])
108
- else:
109
- raise Exception('Strategy not found')
 
 
 
 
 
 
 
 
 
 
 
 
 
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()