Now Live: Live Market Feed on DhanHQ Trading APIs

Hello @Ana

Welcome to Dhan Community! Excited to know you are exploring Dhan APIs.

This may happen if you are running the code in Jupyter notebook. You can use this example code instead, which should help you out: Marketfeed api - sample python code error - #10 by Hardik

@Hardik
with historical data do we get access to old expired future contract data also? this helps in backtesting. for example- copper contract of Jan-Feb 2024 mcx

Hello @HIR

No, currently we do not provide Historical Options data via APIs. We understand the use case here, but again APIs are optimised for execution purposes.

i asked for futures data of mcx and not options data

Hello @HIR

For futures contract as well, currently expired contracts data is not available. Apologies to have missed that in previous message.

i have same problem if you have done then suggest me. how to solve it.

I am facing in determing the length of payload, kindly help me in that with a sample example.

Hello @Akash_Prasad

Welcome to Dhan Community!

Can you please share screenshot of the error message you are getting and highlight more here, to be able to assist you better?

Hi Hardik,

I am trying to get live data, right now commodity market is live but only one packet at a time is received. Why on second attempt Socket is getting disconnected ? and why its saying “Invalid Client ID” where it is valid on first attempt ?

Code is -

def init_socket(self, exchg, secid):

        print('Initializing socket !')

        # proxy
        network.set_proxy()

        # configuring session
        session = requests.Session()
        session.verify = False
        urllib3.disable_warnings()
        urllib3.disable_warnings(InsecureRequestWarning)

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

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


        print('APP_NAME: ', self.app_name)
        print('APP_KEY: ', self.api_key)

        instruments = [(exchg, secid)]
        subscription_code = marketfeed.Ticker
        feed = marketfeed.DhanFeed(self.app_name, self.api_key, instruments, subscription_code, on_connect=on_connect, on_message=on_message)
        
        feed.run_forever()

and output is -

RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Connected to websocket
Connected to websocket
Received: {'type': 'Ticker Data', 'exchange_segment': 5, 'security_id': 429076, 'LTP': '6.75', 'LTT': '21:43:42'}
Received: {'type': 'Previous Close', 'exchange_segment': 5, 'security_id': 429076, 'prev_close': '6.90', 'prev_OI': 1170395136}
Disconnected: Invalid Client ID
Received: None
Connection has been closed

Wow :astonished::astonished::astonished::astonished::astonished:

Hello @Pranav_Deshmukh

Over here, authentication of Client ID, active Data API plan and access token, all happens async. This means that the socket is connected and instruments are subscribed and meanwhile, on background authentication takes place.

You will need to check on the above three things if the socket is being disconnected abruptly. We have also mentioned the same in documentation:

Then why it is sending 1 packet first ! It says received data that means token is authenticated and thats why its sending 1 valid response

If that is the case then its a flaw in security that I will send any invalid token and I can get one packet for free !

Hi,

I have taken stock positions using MTF. And now my combined ledger is in negative -68k.

And the issue is that I cannot subscribe to Data API because of this. When I contacted support I was told that it was because my ledger was negative. And I should clear the debit of the trading ledger by either transferring funds or selling the shares. I think this should not be the case as this means I can’t have mtf positions when I want to subscribe to data API.

I think an option to pay for the Data API directly via a payment gateway could resolve this. One should not be forced to have a positive ledger balance with MTF positions active and simply selling the shares is not an option. To subscribe to an API which costs Rs. 499+GST.

Hope this is resolved soon.

Hello @Pranav_Deshmukh

As mentioned, yes, since the authorisation is done parallel to subscription of instruments and sending data, this scenario may arise, where you receive some packets and then the connection is closed.

On security flaw, we do have other measures in place, which will prevent such an incident, on multiple levels. The reason behind keeping authorisation asynchronous is to separate two modules and make sure data is pushed as soon as user connects on websockets.

@Hardik

Understood what you are saying. But my credentials are correct. Still why it says Invalid Client ID !

Even I tried generating new token and tested with that still getting same.
(Note : I have subscribed to DATA APIs as well 499/- so no issues on that front as well )

Do we have any documentation ? or promising way to find root cause and fix ?

Hello @Pranav_Deshmukh

Can you send your Client ID over DM, will take a look. Also, can you please try once again today, will ask team to check for logs to debug faster?

You mentioned this :

however github sample code has this comment

# Maximum 100 instruments can be subscribed, then use 'subscribe_symbols' function

@Hardik can you please clarify how many symbols can be subscribed in single websocket connection?

Also, if I am able to subscribe to let’s say 5k instruments, will I get response in single list/array per second OR I will get separate response for each instrument ?

Hello @PavanaCapital

On the subscription of symbols, on initial connection, only 100 symbols can be subscribed. But with subscribe_symbols function, you can subscribe the other 4900 symbols. The comment on python code is only on how to handle subscribing to instruments and not on maximum instruments which can be connected on a socket (which remains at 5000)

On the response, since the Market Feed updates as and when there is any update in a any instrument, it is not possible to provide the same in a single list/array. You will be getting separate response for each instrument.

just to give you reference, when I subscribe to 100 instruments in zerodha, I get single array which has tick data, it may have 90 ticks based on the changes, but the response is single array/list. I wanted to know if same is true for Dhan