Can not subscribe to more than 100 instruments

Hi team, I have been trying to subscribe 200 instruments, but can not. Somehow dhanhq python library throws an error. I have tried with multiple instruments list and can not find any solution… Can you please help me resolve this. Thanks in advance.

My code -

# More basic code here, main code starts below

async def on_connect(instance):
    print("Connected!")
async def on_message(instance, message):
    print(message)
feed = marketfeed.DhanFeed(client_id=client_id,
    access_token=access_token,
    instruments=instruments[:100],
    subscription_code=subscription_code,
    on_connect=on_connect,
    on_message=on_message
    )

print(f'Total subscribed instruments right now: {len(feed.instruments)}. Subscribing more...')
feed.subscribe_symbols(subscription_code, instruments[99:200])
print(f'Total subscribed instruments right now: {len(feed.instruments)}. Going to run feed now.')

feed.run_forever()

My error:

C:\___DHANHQ_PACKAGE_LOCATION_HERE___\dhanhq\marketfeed.py:79: RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Total subscribed instruments right now: 100. Subscribing more...
Total subscribed instruments right now: 200. Going to run feed now.
Connected!
Connected!
Connection has been closed

Hello @lalitnmudgal

Just to check here, are you able to subscribe to first 100 instruments and get data for those with this same code?

Yes, I did that and it works very well.

@Hardik can you please check this once and replicate on your system?

Hello @lalitnmudgal

Sure, will take a look into this and update here.

Hi @Hardik, could you please share an update on this? Thanks.

Hello @lalitnmudgal

Yes, one thing here. Which function are you calling to subscribe to the next 100 instruments?

It is subscribe_symbols. Also mentioned in the code above.

@Hardik could you please share an update on this?

Hello @lalitnmudgal

Yes, we are working on an update to the DhanHQ Python library, which will fix this.

Hi @Hardik
Is this fixed now in the official github repo?

Hello @R.E.M

Yes, just reviewing this. There are open PRs for this, will merge from the same post testing and update. I will post it here as soon as it is reviewed and merged.

1 Like

Also, could you confirm what is the max limit (if any) that would be supported? Suppose I want to track all f&o stocks (~ 200) + their futures and ATM options, that means approx 1k-2k stocks. Will the websockets be the right choice?

Or are you guys also working on a polling mechanism for getting a large number of LTP and OI data since not everyone will need the whole stream and some may only need the data on on-demand basis when their script runs.

Hello @R.E.M

Yes, on the websockets, you can get live data stream of upto 5000 instruments on a single websocket, which is an efficient way when you are trying to poll data for large number of instruments at a single time.

On the LTP API, we are looking into feasibility of delivering data via this method.

Thanks @Hardik, look forward to the library update.

for the polling api, you could keep a very small rate limit (e.g. 10 times per minute), so that people don’t misuse it when the streaming api was a better choice.

1 Like