I checked different examples and documentation and find this version worked check this link of dhanhq python: dhanhq · PyPI in that go and check marketfeed example which worked perfectly. This is the example which you can modify as per your use
from dhanhq import marketfeed
Add your Dhan Client ID and Access Token
client_id = “Dhan Client ID”
access_token = “Access Token”
Structure for subscribing is (exchange_segment, “security_id”, subscription_type)
instruments = [(marketfeed.NSE, “1333”, marketfeed.Ticker), # Ticker - Ticker Data
(marketfeed.NSE, “1333”, marketfeed.Quote), # Quote - Quote Data
(marketfeed.NSE, “1333”, marketfeed.Full), # Full - Full Packet
(marketfeed.NSE, “11915”, marketfeed.Ticker),
(marketfeed.NSE, “11915”, marketfeed.Full)]
version = “v2” # Mention Version and set to latest version ‘v2’
In case subscription_type is left as blank, by default Ticker mode will be subscribed.
try:
data = marketfeed.DhanFeed(client_id, access_token, instruments, version)
while True:
data.run_forever()
response = data.get_data()
print(response)
except Exception as e:
print(e)
Close Connection
data.disconnect()
Subscribe instruments while connection is open
sub_instruments = [(marketfeed.NSE, “14436”, marketfeed.Ticker)]
data.subscribe_symbols(sub_instruments)
Unsubscribe instruments which are already active on connection
unsub_instruments = [(marketfeed.NSE, “1333”, 16)]
data.unsubscribe_symbols(unsub_instruments)