Error UNPACKING NON ITERABLE NONETYPE OBJECT

cannot unpack non-iterable NoneType object
  File "C:\Users\ratna\OneDrive\Desktop\upserge algo tradng masterclass\Session 3\Options addition.py", line 56, in <module>
    CE_symbol_name, PE_symbol_name, CE_OTM_price, PE_OTM_price = tsl.OTM_Strike_Selection(Underlying= 'stock_name' , Expiry=0, OTM_count=2)
TypeError: cannot unpack non-iterable NoneType object
type or paste code here

while True:

current_time = datetime.now().time()
if current_time > EXIT_TIME:
	print(f"{current_time} Exiting the Algo")
	tsl.cancel_all_orders()
	break

if current_time < ENTRY_TIME:
	print(f"{current_time}Waiting for the market to open")
	continue


for stock_name in watchlist:
	print(f"{watchlist.index(stock_name)} Scanning {stock_name} :::: {traded_stocks}")

	try:
		chart        = tsl.get_historical_data(tradingsymbol=stock_name, exchange='NSE', timeframe="5")
		chart['RSI'] = talib.RSI(chart['close'], timeperiod=14)   # todo: use as it is
	except Exception as e:
		print(f"Error in getting historical data for {stock_name}: {e}")
		continue


	rsi          = chart.iloc[-2]['RSI']                      # todo: use as it is
	close        = chart.iloc[-2]['close']                    # todo: use as it is

	bc1 = (rsi > 70)
	bc2 = (close < 2000)
	bc3 = (len(traded_stocks) <= 2)
	bc4 = (stock_name not in traded_stocks)

	sc1 = rsi < 30
	sc2 = close < 2000
	sc3 = len(traded_stocks) <= 2
	sc4 = stock_name not in traded_stocks

	if bc1 and bc2 and bc3 and bc4:
		print("buy ", stock_name)


		# ----- check if my entry is in options as well  ------
		CE_symbol_name, PE_symbol_name, CE_OTM_price, PE_OTM_price = tsl.OTM_Strike_Selection(Underlying= 'stock_name' , Expiry=0, OTM_count=2)
		ce_options_chart          = tsl.get_historical_data(tradingsymbol=CE_symbol_name, exchange='NFO', timeframe="5")
		ce_options_chart['RSI']   = talib.RSI(ce_options_chart['close'], timeperiod=14)   # todo: use as it is

		bc5 = ce_options_chart.iloc[-1]['RSI'] > 60

		if bc5:
			lot_size  = tsl.get_lot_size(tradingsymbol = CE_symbol_name)
			first_ask = tsl.get_quote_data(names=[CE_symbol_name])[CE_symbol_name]['depth']['sell'][0]['price']
			tsl.order_placement(tradingsymbol=CE_symbol_name, exchange='NFO', quantity=lot_size, price=first_ask, trigger_price=0,order_type='LIMIT', transaction_type='BUY', trade_type='MIS', amo_time="OPEN",after_market_order=False)
			traded_stocks.append(stock_name)





	if sc1 and sc2 and sc3 and sc4:
		print("sell ", stock_name)

		# ----- check if my entry is in options as well  ------
		CE_symbol_name, PE_symbol_name, CE_OTM_price, PE_OTM_price = tsl.OTM_Strike_Selection(Underlying= 'stock_name', Expiry=0, OTM_count=2)
		pe_options_chart           = tsl.get_historical_data(tradingsymbol=PE_symbol_name, exchange='NFO', timeframe="5")
		pe_options_chart['RSI']    = talib.RSI(pe_options_chart['close'], timeperiod=14)   # todo: use as it is

		sc5 = pe_options_chart.iloc[-1]['RSI'] > 60

		if sc5:
			lot_size  = tsl.get_lot_size(tradingsymbol = PE_symbol_name)
			first_ask = tsl.get_quote_data(names=[PE_symbol_name])[PE_symbol_name]['depth']['sell'][0]['price']
			tsl.order_placement(tradingsymbol=PE_symbol_name, exchange='NFO', quantity=lot_size, price=first_ask, trigger_price=0,order_type='LIMIT', transaction_type='BUY', trade_type='MIS', amo_time="OPEN",after_market_order=False)
			traded_stocks.append(stock_name)

Dhan Data API showing as paid monthly subscription.

I have not paid & I am also getting the same error.

Hi @NikhilSR ,

Use the below code-

CE_symbol_name, PE_symbol_name, CE_OTM_price, PE_OTM_price = tsl.OTM_Strike_Selection(Underlying= stock_name, Expiry=0, OTM_count=2)

stock_name must be without any punctuation marks

Hi @vishal_rajgor ,

Could you elaborate your issue?

corrected the stock_name as per syntax , still the error continues happening .

Hi @NikhilSR ,

Can you share the code and mention for which stock name error is occuring?

tsl           = Tradehull(client_code,token_id)
watchlist     = list(filter(None, live_status.range('A2:A501').value))
ENTRY_TIME    = config['B4'].value
ENTRY_TIME    = time(int(ENTRY_TIME.split(":")[0]), int(ENTRY_TIME.split(":")[1]))

EXIT_TIME     = config['B5'].value
EXIT_TIME     = time(int(EXIT_TIME.split(":")[0]), int(EXIT_TIME.split(":")[1]))

rsi_timeperiod          = int(config.range('B6').value)
rsi_bullish_entry_level = int(config.range('B7').value)
rsi_bearish_entry_level = int(config.range('B8').value)
max_no_of_trades        = int(config.range('B9').value)
min_ltp_required        = int(config.range('B10').value)
max_loss_percentage     = float(config.range('B11').value)


traded_stocks = []


while True:

	current_time = datetime.now().time()
	if current_time > EXIT_TIME:
		print(f"{current_time} Exiting the Algo")
		break

	if current_time < ENTRY_TIME:
		print(f"{current_time}Waiting for the market to open")
		continue
	

	for stock_name in watchlist:
		print(f"{watchlist.index(stock_name)} Scanning {stock_name} :::: {traded_stocks}")

		try:
			chart        = tsl.get_historical_data(tradingsymbol=stock_name, exchange='NSE', timeframe="5")
			chart['RSI'] = talib.RSI(chart['close'], timeperiod=rsi_timeperiod)   # todo: use as it is
		except Exception as e:
			print(f"Error in getting historical data for {stock_name}: {e}")
			continue


		rsi          = float(chart.iloc[-2]['RSI'])                      # todo: use as it is
		close        = float(chart.iloc[-2]['close'])                    # todo: use as it is

		bc1 = (rsi > rsi_bullish_entry_level)
		bc2 = (close < min_ltp_required)
		bc3 = (len(traded_stocks) <= max_no_of_trades)
		bc4 = (stock_name not in traded_stocks)

		sc1 = rsi < rsi_bearish_entry_level
		sc2 = close < min_ltp_required
		sc3 = len(traded_stocks) <= max_no_of_trades
		sc4 = stock_name not in traded_stocks



		# ----------- Send info to excel -----------
		row_no = watchlist.index(stock_name) + 2

		live_status.range(f'B{row_no}').value = [stock_name, close, rsi, bc1, bc2, bc3, bc4, sc1, sc2, sc3, sc4]

PS C:\Users\ratna\OneDrive\Desktop\upserge algo tradng masterclass\Session 4\Algo with Dashboard> & C:/Users/ratna/AppData/Local/Programs/Python/Python38/python.exe "c:/Users/ratna/OneDrive/Desktop/upserge algo tradng masterclass/Session 4/Algo with Dashboard/Options addition.py"
Codebase Version 3
-----Logged into Dhan-----
reading existing file all_instrument 2025-12-03.csv
Got the instrument file
0 Scanning HINDALCO :::: []
buy  HINDALCO
Exception at getting Expiry list as {'status': 'failure', 'remarks': {'error_code': None, 'error_type': None, 'error_message': None}, 'data': {'data': {'808': 'Authentication Failed - Client ID or Token invalid'}, 'status': 'failed'}}
Unable to find the correct Expiry for HINDALCO
Error in getting historical data for HINDALCO: cannot unpack non-iterable NoneType object
Traceback (most recent call last):
  File "c:/Users/ratna/OneDrive/Desktop/upserge algo tradng masterclass/Session 4/Algo with Dashboard/Options addition.py", line 92, in <module>
    CE_symbol_name, PE_symbol_name, CE_OTM_price, PE_OTM_price = tsl.OTM_Strike_Selection(Underlying=stock_name, Expiry=0, OTM_count=2)
TypeError: cannot unpack non-iterable NoneType object

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:/Users/ratna/OneDrive/Desktop/upserge algo tradng masterclass/Session 4/Algo with Dashboard/Options addition.py", line 99, in <module>
    if  bc5:
NameError: name 'bc5' is not defined

Hi @NikhilSR ,

The error is Client ID or Token is invalid. Token needs to be regenerated every 24 hours. Do check if it is expired

Exception at getting Expiry list as {'status': 'failure', 'remarks': {'error_code': None, 'error_type': None, 'error_message': None}, 'data': {'data': {'808': 'Authentication Failed - Client ID or Token invalid'}, 'status': 'failed'}}