Now Live: Live Market Feed on DhanHQ Trading APIs

any update on it?

I guess not. But they have a new “cute” thread here :roll_eyes:

I need to get the LTP of nifty continuously
instruments = [(0, ‘13’)]
subscription_code = marketfeed.Ticker
ltp_value = None # Initialize a variable to store the LTP

async def on_connect(instance):
print(“Connected! in callback”)

async def on_close(instance):
print(“###C LOSED ############”)

async def on_message(ws, message):
global ltp_value
if message is not None and ‘LTP’ in message:
ltp_value = float(message[‘LTP’])
print(“Current price (on_message):”, ltp_value)
print(“Current price (on_message):”, ltp_value)

def get_ltp():
global ltp_value
return ltp_value

user_ID = <MY_USER_ID>
access_token =<ACCESS_TOKEN

print(“Subscription code :”, subscription_code)
feed = marketfeed.DhanFeed(user_ID,
access_token,
instruments,
subscription_code,
on_connect=on_connect,
on_message=on_message,
on_close=on_close)
feed.run_forever()
asyncio.create_task(feed.run_forever())

But after 1-2 iteration the connection is getting closed…
Connected! in callback
Connected! in callback
Current price (on_message): 22502.0
Current price (on_message): 22502.0
Current price (on_message): 22502.0
Disconnected: Subscribe to Data APIs to continue
Current price (on_message): 22502.0
Connection has been closed
Connected! in callback
Connected! in callback
Current price (on_message): 22502.0
Current price (on_message): 22502.0
Current price (on_message): 22502.0
Disconnected: Subscribe to Data APIs to continue
Current price (on_message): 22502.0
Connection has been closed
Traceback (most recent call last):
File “/Users/raviverma/Desktop/python/marketfeed.py”, line 46, in
asyncio.create_task(feed.run_forever())
File “/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/tasks.py”, line 381, in create_task
loop = events.get_running_loop()
^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: no running event loop

Please help me how can i keep on getting the Nifty LTP continuously.

Hello @testuser

Over here, looks like you haven’t subscribed to Data APIs itself, hence the connection is getting closed. Since the authorisation happens on sockets asynchronously, you start receiving ticks and then the connection is closed by the server as the subscription is not active.

Kindly subscribe to Data APIs first to be able to access Live Market Feed uninterruptedly.

Hi, i am trying to use market feed to get ltp after receiving data for some time i got the below error. it happened 2-3 times in a day. how to handle this
Connection has been closed
Traceback (most recent call last):
File “C:\Users\Administrator\Documents\dhanhq.venv\lib\site-packages\websockets\legacy\client.py”, line 647, in await_impl_timeout
return await self.await_impl()
File “C:\Users\Administrator\Documents\dhanhq.venv\lib\site-packages\websockets\legacy\client.py”, line 651, in await_impl
_transport, _protocol = await self._create_connection()
File “C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py”, line 1017, in create_connection
infos = await self._ensure_resolved(
File “C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py”, line 1396, in _ensure_resolved
return await loop.getaddrinfo(host, port, family=family, type=type,
File “C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py”, line 856, in getaddrinfo
return await self.run_in_executor(
asyncio.exceptions.CancelledError

@Hardik Any update on this.

Hello @Vithal_Patil

Sorry to have missed this earlier. Yes, you can subscribe to Data APIs even if you have negative ledger balance (in case where you do have MTF holdings).

Can you send me a screenshot of 'DhanHQ Trading APIs` page along with your Client ID over DM, so that I can ask team to take a look.

Hello @ram_krishna

Welcome to Dhan Community!

Can you confirm if you are using jupyter notebook to run market feed or google collab? If not, then are there any additional asyncio function that you have used in your code?

Hey @rahulbajaj did you manage to create connections using Java/Spring to Dhan websockets? I am trying to build some utility using java but making a connection to dhanhq had been a difficult task. Any help/examples to get around this will be appreciated. thanks.

Hello @pnsudesh

Welcome to Dhan Community!

Can you tell me where exactly are you facing issues? You can post in detail and if someone from community can help you out here, they will.

Asked a similar question on another thread. @Hardik Dhan API - Java implementation of data apis

1 Like

Hello @Rishikesh_Gaikwad

Welcome to Dhan Community!

Looks like you are pasting your mobile number instead of Client ID. You can go to web.dhan.co > My Profile to see your Client ID. Do try with this and let us know if this works.

1 Like

@Hardik Kindly please share an example of subscribing data to your web socket wss://api-feed.dhan.co using javascript.

Hello @Developer_Finsicle

We do not have sample code for JS, you can refer to the core websocket documentation itself to get an idea on the implementation : Live Market Feed - DhanHQ Ver 1.0 / API Document

Is there an easy way to get SECURITY_ID using an API call instead to looking up from CSV?

something like get_security_id(SEM_TRADING_SYMBOL)

Hi @Hardik

  1. Once a websocket is open and the live feed is streaming, is there a possibility to add more instruments/ symbols to the existing open connection?

  2. Is there a possibility to place a trailing stop loss order through Dhan API programmatically?

Please share details for each of them if doable.

1 Like

Hello @PrasadP @Rishikesh_Gaikwad

This is not available. The idea with Security ID list is to give all details about instruments in a single place.

Yes, you can use subscribe_symbols function for this.

You can build Trailing Stop Loss at your end, which looks at live market price and updates the stop loss order. This is not available directly on API.

I am also facing the same issue, Here is the screenshot of the issue

How to resolve this issue?

Hello @sathishmuppudathi

Welcome to MadeForTrade community!

Can you please check if your Data API subscription is active and the access token used is also valid?

Yes, I already checked both are active ad valid, But still disconnecting and showinf invalid client data