DhanHQ-py : v2.2.0

With the release of v2.2.0, there are breaking changes. You can install previous stable version by using the below code:
pip install dhanhq==2.0.2
The official Python client for communicating with the Dhan API
DhanHQ-py Rest API is used to automate investing and trading. Execute orders in real time along with position management, live and historical data, tradebook and more with simple API collection.
Not just this, you also get real-time market data via DhanHQ Live Market Feed.
Dhan (c) 2026. Licensed under the MIT License
Documentation
v2.2.0 - What's new
- You can now access the entire full market depth (200 Level) via DhanHQ APIs and part of the python library.
- Expired Options Data are now directly available on the library and you can fetch for all NSE & BSE instruments
- Super Orders - smart orders for managing risk and profits is now introduced with DhanHQ
- You can set, modify and change IP for your account, right from the python library - the code for the same is available under example.
And a lot more is available directly on DhanHQ Python Library now.
v2.1.0 - What's new
- 20 level market depth is now available on DhanHQ APIs and part of the python library.
DhanHQ v2.1.0 is more modular and secure.
-
The project is restructured to contemporary "best practices" in the python world. How does this affect you? Your imports would change like below:
| from dhanhq import dhanhq | from dhanhq import dhanhq |
| from dhanhq import marketfeed.MarketFeed | from dhanhq import MarketFeed |
| from dhanhq import orderupdate.OrderUpdate | from dhanhq import OrderUpdate |
-
The constants that were earlier part of modules marketfeed and orderupdate are now moved to its respective classes contained in the modules for better developer experience.
| marketfeed.NSE | MarketFeed.NSE |
| marketfeed.Ticker | MarketFeed.Ticker |
Note: This improves developer experience to not knowing the entire package hierarchy and stay productive to know the interfaces he is working with.
-
You no longer have to repeat and spread the client-id and access-token around their code based in using our APIs. With this release, you defined it once for DhanContext and pass on this to different classes of the SDK instead of the raw credential strings, making your codebase much secure from data leaks and your coding a lot easier by defining DhanContext just once and use that context for other API classes. Quick glance of how affected code initialization is below:
dhanhq('client_id','access_token') | dhanhq('dhan_context') |
MarketFeed('client_id','access_token',instruments) | MarketFeed('dhan_context',instruments) |
OrderUpdate('client_id','access_token') | OrderUpdate('dhan_context') |
Note: The Hands-on API section is updated to reflect this change, for your convenience.
-
Code coverage improvements with robust unit & integration tests for safe and speed delivery of features. Will strengthen even more in upcoming releases.
You can read about all other updates from DhanHQ V2 here: DhanHQ Releases.
Features
-
Order Management
The order management APIs lets you place a new order, cancel or modify the pending order, retrieve the order status, trade status, order book & tradebook.
-
Live Market Feed
Get real-time market data to power your trading systems, with easy to implement functions and data across exchanges.
-
Market Quote
REST APIs based market quotes which given you snapshot of ticker mode, quote mode or full mode.
-
Option Chain
Single function which gives entire Option Chain across exchanges and segments, giving OI, greeks, volume, top bid/ask and price data.
-
Forever Order
Place, modify or delete Forever Orders, whether single or OCO to better manage your swing trades.
-
Portfolio Management
With this set of APIs, retrieve your holdings and positions in your portfolio as well as manage them.
-
Historical Data
Get historical candle data for the desired scrip across segments & exchange, both multiple minute timeframe OHLC and Daily OHLC.
-
Fund Details
Get all information of your trading account like balance, margin utilised, collateral, etc as well margin required for any order.
-
eDIS Authorisation
To sell holding stocks, one needs to complete the CDSL eDIS flow, generate T-PIN & mark stock to complete the sell action.
Quickstart
You can install the package via pip
pip install dhanhq
Authentication
You can now generate access tokens using the DhanLogin class.
Method 1: OAuth Flow
from dhanhq import DhanLogin
dhan_login = DhanLogin("YOUR_CLIENT_ID")
app_id = "YOUR_APP_ID"
app_secret = "YOUR_APP_SECRET"
consent_id = dhan_login.generate_login_session(app_id, app_secret)
token_id = "TOKEN_ID_FROM_REDIRECT_URL"
access_token = dhan_login.consume_token_id(token_id, app_id, app_secret)
print(access_token)
Method 2: PIN & TOTP Flow
from dhanhq import DhanLogin
dhan_login = DhanLogin("YOUR_CLIENT_ID")
pin = "YOUR_PIN"
totp = "YOUR_TOTP"
access_token_data = dhan_login.generate_token(pin, totp)
print(access_token_data)
Renew Token
dhan_login.renew_token(access_token)
User Profile
user_info = dhan_login.user_profile(access_token)
print(user_info)
IP Management
You can manage your Static IP (whitelisting) using set_ip, modify_ip, and get_ip.
response = dhan_login.set_ip(access_token, "10.200.10.10", "PRIMARY")
print(response)
response = dhan_login.modify_ip(access_token, "10.200.10.11", "PRIMARY")
print(response)
ip_list = dhan_login.get_ip(access_token)
print(ip_list)
Hands-on API
from dhanhq import DhanContext, dhanhq
dhan_context = DhanContext("client_id","access_token")
dhan = dhanhq(dhan_context)
dhan.place_order(security_id='1333',
exchange_segment=dhan.NSE,
transaction_type=dhan.BUY,
quantity=10,
order_type=dhan.MARKET,
product_type=dhan.INTRA,
price=0)
dhan.place_order(security_id='52175',
exchange_segment=dhan.NSE_FNO,
transaction_type=dhan.BUY,
quantity=550,
order_type=dhan.MARKET,
product_type=dhan.INTRA,
price=0)
dhan.get_order_list()
dhan.get_order_by_id(order_id)
dhan.modify_order(order_id, order_type, leg_name, quantity, price, trigger_price, disclosed_quantity, validity)
dhan.cancel_order(order_id)
dhan.get_order_by_correlationID(correlationID)
dhan.fetch_security_list("compact")
dhan.get_positions()
dhan.get_holdings()
dhan.intraday_minute_data(security_id, exchange_segment, instrument_type, from_date, to_date)
dhan.historical_daily_data(security_id, exchange_segment, instrument_type, from_date, to_date)
dhan.expired_options_data(
security_id=13,
exchange_segment="NSE_FNO",
instrument_type="INDEX",
expiry_flag="MONTH",
expiry_code=1,
strike="ATM",
drv_option_type="CALL",
required_data=["open", "high", "low", "close", "volume"],
from_date="2023-01-01",
to_date="2023-01-31"
)
dhan.convert_to_date_time(epoch_date)
dhan.get_trade_book(order_id)
dhan.get_trade_history(from_date,to_date,page_number=0)
dhan.get_fund_limits()
dhan.generate_tpin()
dhan.open_browser_for_tpin(isin='INE00IN01015',
qty=1,
exchange='NSE')
dhan.edis_inquiry(isin='INE00IN01015')
dhan.expiry_list(
under_security_id=13,
under_exchange_segment="IDX_I"
)
dhan.option_chain(
under_security_id=13,
under_exchange_segment="IDX_I",
expiry="2024-10-31"
)
dhan.ohlc_data(
securities = {"NSE_EQ":[1333]}
)
dhan.place_forever(
security_id="1333",
exchange_segment= dhan.NSE,
transaction_type= dhan.BUY,
order_type=dhan.LIMIT,
product_type=dhan.CNC,
quantity= 10,
price= 1900,
trigger_Price= 1950
)
Market Feed Usage
from dhanhq import DhanContext, MarketFeed
dhan_context = DhanContext("client_id","access_token")
instruments = [(MarketFeed.NSE, "1333", MarketFeed.Ticker),
(MarketFeed.NSE, "1333", MarketFeed.Quote),
(MarketFeed.NSE, "1333", MarketFeed.Full),
(MarketFeed.NSE, "11915", MarketFeed.Ticker),
(MarketFeed.NSE, "11915", MarketFeed.Full)]
version = "v2"
try:
data = MarketFeed(dhan_context, instruments, version)
data.run_forever()
while True:
response = data.get_data()
print(response)
except Exception as e:
print(e)
# Close Connection
data.close_connection()
# 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)
Live Order Update Usage
from dhanhq import DhanContext, OrderUpdate
import time
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)
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()
Full Market Depth
from dhanhq import DhanContext, FullDepth
dhan_context = DhanContext(client_id, access_token)
instruments = [(1, "1333")]
depth_level = 200
try:
response = FullDepth(dhan_context, instruments, depth_level)
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)
Changelog
Check release notes