Skip to content

DhanHQ-py : v2.1.0

Getting Started

alt

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) 2025. Licensed under the MIT License

Documentation

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:
Before This Version After This Release
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.
Before This Version After This Release
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:
Before This Version After This Release
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.

v2.0 - What's New

DhanHQ v2 extends execution capability with live order updates, market quotes and forever orders on superfast APIs. Some of the key highlights from this version are:

  • Fetch LTP, Quote (with OI) and Market Depth data directly on API, for upto 1000 instruments at once with Market Quote API.

  • Option Chain API which gives OI, greeks, volume, top bid/ask and price data of all strikes of a particular underlying.

  • Place, modify and manage your Forever Orders, including single and OCO orders to manage risk and trade efficiently with Forever Order API.

  • Order Updates are sent in real time via websockets, which will update order status of all your orders placed via any platform - order_update.

  • Intraday Minute Data now provides OHLC with Volume data for last 5 trading days across timeframes such as 1 min, 5 min, 15 min, 25 min and 60 min - intraday_minute_data.

  • Full Packet in Live Market Feed (marketfeed).

  • Margin Calculator (margin_calculator) and Kill Switch (kill_switch) APIs.

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.

Generating Token for Individual Use

Individual traders can directly get their Access Token from web.dhan.co. All Dhan users are eligible to get free access to Trading APIs. Here's how to get your Access Token:

  • Login to web.dhan.co

  • Click on My Profile and navigate to 'Access DhanHQ APIs'

Errors

Error responses come with the error code and message generated internally by the system. The sample structure of error response is shown below.

{
    "errorType": "",
    "errorCode": "",
    "errorMessage": ""
}

You can find detailed error code and message under Annexure.