Getting Started
Installation
Install DhanHQ-py using pip:
pip install dhanhq
Authentication
To use DhanHQ-py, you need:
- Client ID - Your Dhan client ID
- Access Token - Generated from Dhan web/mobile app
from dhanhq import DhanContext, dhanhq
dhan_context = DhanContext("client_id","access_token")
dhan = dhanhq(dhan_context)
Basic Usage
Place an Order
# Market order
order_id = dhan.place_order(
security_id="1333", # Reliance
exchange_segment="NSE_EQ",
transaction_type="BUY",
quantity=1,
order_type="MARKET",
product_type="INTRADAY"
)
print(f"Order placed with ID: {order_id}")
Get Holdings
holdings = dhan.get_holdings()
print(holdings)
Get Positions
positions = dhan.get_positions()
print(positions)
Market Feed Usage
from dhanhq import DhanContext, MarketFeed
# Define and use your dhan_context if you haven't already done so like below:
dhan_context = DhanContext("client_id","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(dhan_context, 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
sub_instruments = [(MarketFeed.NSE, "14436", MarketFeed.Ticker)]
data.subscribe_symbols(sub_instruments)
Unsubscribe instruments
unsub_instruments = [(MarketFeed.NSE, "1333", 16)]
data.unsubscribe_symbols(unsub_instruments)
Live Order Update Usage
from dhanhq import DhanContext, OrderUpdate
import time
# Define and use your dhan_context if you haven't already done so like below:
dhan_context = DhanContext("client_id","access_token")
def on_order_update(order_data: dict):
"""Optional callback function to process order data"""
print(order_data["Data"])
def run_order_update():
order_client = OrderUpdate(dhan_context)
# Optional: Attach a callback function to receive and process order data.
order_client.on_update = on_order_update
while True:
try:
order_client.connect_to_dhan_websocket_sync()
except Exception as e:
print(f"Error connecting to Dhan WebSocket: {e}. Reconnecting in 5 seconds...")
time.sleep(5)
run_order_update()
20 Level Market Depth Usage
from dhanhq import DhanContext, FullDepth
dhan_context = DhanContext(client_id, access_token)
instruments = [(1, "1333"),(2,"")]
try:
response = fulldepth.FullDepth(dhan_context, instruments)
response.run_forever()
while True:
response.get_data()
if response.on_close:
print("Server disconnection detected. Kindly try again.")
break
except Exception as e:
print(e)
Next Steps
- Explore this documentation for detailed explanation on the library usage.
- Check out examples in the github repository.
Changelog
Check python library release notes here.