My code is running very slow

Below code is working very slow…I am trying to scan nift 500 stocks for rsi > 60 , inside bar, breakout candle and no repeated order…is there anyway to speed this up…

for name in nifty500_stocks:
	print("scanning",name)
	# print(all_ltp_data[name])
	chart = tsl.get_historical_data(tradingsymbol = name,  exchange = "NSE",timeframe="DAY")
	chart["rsi"] = talib.RSI(chart['close'],timeperiod=14)
	# print(chart)
	bc = chart.iloc[-4] # base candle
	ic = chart.iloc[-2] # inside candle
	br = chart.iloc[-1] # breakout candle
	lcc = chart.iloc[-2]

	insidebar_pattern = (bc["high"] > ic["high"] ) and (bc["low"] < ic["low"])
	uptrend = lcc["rsi"] > 60
	breaout_pattern = True #br["close"] > bc["high"]

	no_repeat_order = stockname not in traded_stock
	if uptrend and insidebar_pattern and breaout_pattern and no_repeat_order:
		print(name, " is in uptrend with rsi", lcc["rsi"])
		traded_stock.append(name)

Hey @Anupam_Agarwal

Are you running this every minute? Given it takes historical data to scan, the results are slow because polling 500 instruments and checking for RSI is quite heavy.

Maybe @Tradehull_Imran can help you with alternate approach here.

1 Like