Not able to fetch open interest data using dhan api

I’m unable to fetch open interest data for nifty and banknifty using dhan api and the code doesn’t even throw an error. I’m not getting what’s wrong with the code.

from dhanhq import dhanhq
import pandas as pd
import websockets

from dhanhq import marketfeed

import websockets
import asyncio
import json
import struct
from datetime import datetime

# Define the WebSocket URL
WS_URL = "wss://api-feed.dhan.co"

client_id = "my client id"
access_token = "my access token"

dhan = dhanhq(client_id, access_token)

QUOTE_PACKET = 17
MARKET_DEPTH_PACKET = 19

nifty_symbol = "NIFTY"
banknifty_symbol = "BANKNIFTY"


def get_instrument_token():
    df = pd.read_csv("csv/api-scrip-master.csv", low_memory=False)
    data_dict = {}
    for index, row in df.iterrows():
        trading_symbol = row["SEM_TRADING_SYMBOL"]
        exm_exch_id = row["SEM_EXM_EXCH_ID"]
        if trading_symbol not in data_dict:
            data_dict[trading_symbol] = {}
        data_dict[trading_symbol][exm_exch_id] = row.to_dict()
    return data_dict

token_dict = get_instrument_token()

def get_security_id(symbol, exchange):
    if symbol in token_dict and exchange in token_dict[symbol]:
        return int(token_dict[symbol][exchange]["SEM_SMST_SECURITY_ID"])
    else:
        return None

nifty_id = get_security_id(nifty_symbol, "NSE")
banknifty_id = get_security_id(banknifty_symbol, "NSE")

if nifty_id and banknifty_id:
    instruments = [(0, nifty_id), (0, banknifty_id)]

    async def on_connect(instance):
        print("Connected to the server")

    async def on_message(ws, message):
        print(f"Received message length: {len(message)}")
        
        if len(message) >= 50:  
            exchange_segment, security_id, ltp, ltq, ltt, avg_price, volume, oi = struct.unpack('<BHBIfHIfI', message[:30])
            open_price, close_price, high_price, low_price = struct.unpack('ffff', message[30:46])

            print(f"Security ID: {security_id}, LTP: {ltp}, Open Interest: {oi}, LTT: {ltt}")
            print(f"Open: {open_price}, High: {high_price}, Low: {low_price}, Close: {close_price}")
        else:
            print("Incomplete or unexpected data received for Quote Packet")

    def create_subscription_packet(instruments, subscription_code):
        num_instruments = len(instruments)
        num_instruments_bytes = struct.pack('<I', num_instruments)
        instrument_info = b""

        for exchange_segment, security_id in instruments:
            instrument_info += struct.pack('<B20s', int(exchange_segment), str(security_id).encode('utf-8'))

        subscription_packet = num_instruments_bytes + instrument_info
        return subscription_packet

    async def main():
        feed = marketfeed.DhanFeed(
            client_id,
            access_token,
            instruments,
            QUOTE_PACKET,
            on_connect=on_connect,
            on_message=on_message
        )

        feed.create_subscription_packet = create_subscription_packet

        await feed.connect()

    asyncio.run(main())

else:
    print("Could not find the security IDs for NIFTY or BANKNIFTY.")

Output:

Hello @Stavan_Shah

Welcome to MadeForTrade community!

Would request you to use the DhanHQ library itself to avoid this issue. You can check out the same on GitHub: DhanHQ Python Library.