Marketfeed api - sample python code error


There are some typos in sample code presented in dhanhq · PyPI

1 Like

Hello @api_expert

Welcome to Dhan Community!

Yes, we have corrected the README file today morning itself. Do check now and we would love to hear your feedback on the Live Market Feed as well.

1 Like

This is on pypi site:

Getting this frequently:
Disconnected: No. of active websocket connections exceeded
Connection has been closed
[Finished in 64.3s]


How many connections are you establishing currently? Also, thanks for highlighting the pypi docs, will update that.

Only one data api connection. Trading api token is used for order management.
on_error it should reconnect.

Typical websocket standard functions(Allows upto 3 connections with 1000 ltp subscriptions):

Callback for tick reception.

def on_tick(ws, ticks):“on tick - {}”.format(json.dumps(ticks)))

Callback for successful connection.

def on_connect(ws, response):“Successfully connected to WebSocket”)

def on_close(ws, code, reason):“WebSocket connection closed”)

def on_error(ws, code, reason):“Connection error: {code} - {reason}”.format(code=code, reason=reason))

Callback when reconnect is on progress

def on_reconnect(ws, attempts_count):“Reconnecting: {}”.format(attempts_count))

Assign the callbacks.

kws.on_tick = on_tick
kws.on_connect = on_connect
kws.on_close = on_close
kws.on_error = on_error
kws.on_reconnect = on_reconnect

Infinite loop on the main thread. Nothing after this will run.

You have to use the pre-defined callbacks to manage subscriptions.


Hey @api_expert

Over here, the connection is terminated if a specific error code is received such as the one that you shared an example of.

We do allow multiple connections per user, with no limits on number of instruments to be subscribed.

Hi @Hardik,

I am getting following error when i am trying to use the sample code provided in readme file

Hello @haricharan

Welcome to Dhan community! Glad to have you get started on Market Feed.

This error is primarily in case where Jupyter notebooks do not allow for multiple loops at once and we don’t have full control over the event loop. You can handle this using asyncio, adding reference for you here:

import nest_asyncio

import marketfeed
import asyncio

client_id = "Client ID"
access_token = "Access Token"

instruments = [(1, "1333"),(0,"13")]
subscription_code = marketfeed.Ticker

# Usage Example
async def on_connect(instance):
    print("Connected to websocket")

async def on_message(instance, message):
    print("Received:", message)

print("Subscription code :", subscription_code)
feed = marketfeed.DhanFeed(client_id, access_token, instruments, subscription_code,
                    on_connect=on_connect, on_message=on_message)



Let me know if this helps! Would love to have your feedback here as well.

How to get the exchange_segment which need to passed as part of instruments?

Hello @stanly_thomas

You can get exchange_segment enums from Documentation Annexure section - here

1 Like

Hi @stanly_thomas @haricharan @api_expert

We’ve just launched an API-based trading quiz!

Join in and don’t forget to share your insights on building efficient systems. Your thoughts matter!

Any idea why websocket is getting connected twice with the sample code given ?
And even I am getting a waring also.

Screenshot 2024-02-10 at 1.32.44 PM

Getting this error when callng subscribe_symbols
An error occurred: DhanFeed.create_subscription_packet() got an unexpected keyword argument ‘subscribe’

Hi @stanly_thomas @haricharan @api_expert

We’ve just launched an API-based trading quiz!

Join in and don’t forget to share your insights on building efficient systems. Your thoughts matter!

Hello @stanly_thomas

Over here, the socket is not getting connected twice. The library initiates connection and retries until connection is successful. On the error, it doesn’t affect actual connection and receiving data, just one module is skipped there, will try to fix this in further releases.

If you are worried about the data being displayed twice, both data packets are different if you look closely. You can see one is previous close packet and the other is first tick received on the socket.

Can you send the arguments being passed here?

Hello @Hardik , calling this is throwing the error.
self.feed.subscribe_symbols(marketfeed.Ticker, instruments)

I think even the unsubscribe_symbols will fail too.

Hey @stanly_thomas

Will have to try and replicate this. If required, will update the python library as well.