Introducing: Dhan Python API Library for Trading APIs

Hello Traders,

At Dhan, we always build to deliver a lightning-fast experience to our users, be it on our own platforms (Dhan App, Options Trading App, Dhan Web) or deeply integrated platforms (Dhan TradingView integration on tv.dhan.co, smallcase, stratzy, and more) or via our APIs that are used by partners or traders.

We use similar APIs formats and structures that we have extended to our Fast Trading APIs platform - Dhan HQ, we promised to deliver seamless integration to the Dhan experience. And it gives us immense delight to see new traders and platforms leveraging DhanHQ APIs every day to build better investing and trading experiences - many custom experiences that are built for systematic traders or who believe in building their own trading platforms to their choices.

Not just on Dhan, we are also improving on our APIs all this while. As we onboard more and more trade and tech geeks, we also recognize the need to diligently improvise our product to provide an effortless integration. We have already added comprehensive documentation for our APIs (here) and machine-generated code (here) along with documentation in 18 programming languages for our user’s convenience. Recently Dhan also introduced our API Bridge
for traders who want to trade with systems like Amibroker, MetaTrader, NinjaTrader and likes.

Introducing Dhan Python API Library

We are taking the next step in this direction. For our superfast users, we are minimising the steps between their codebase and our data & trading APIs. Today, we are announcing the launch of our first DhanHQ package for Python. This will help all our users and partners to install and start exploring the whole Dhan experience as quickly as they want.

Where can you find it?
We have added the python package to PyPI so that our users can search for and install the package with ease. This acts as a one-step integration that installs this package directly on your system and you can start communicating with the Dhan API within a matter of seconds.

Why a Python Package?
Python is considered to be the most accessible, high-level language which is used widely by traders. A Python library helps you to remove the step of adding and running individual APIs in your codebase. To integrate with Dhan HQ using the package, you will need Python 3.9 or above.

While building Dhan HQ and our API platforms, we also would like to acknowledge the benefits of FOSS and we intend to keep our library open for users to build on top of it. This shall also enable many individual traders to leverage APIs from Dhan HQ as we offer our trading APIs completely for free. If you are an open source developer and have ideas on building on top of these Python libraries and innovate or simplify further, we would love to connect with you and hope to build some good stuff together :slight_smile:

We are constantly bridging the gap for our users, enabling them to trade using our platform as well as their own, all built on the same core principles that we follow. This is a step forward, and we will also aim to bring another suite of APIs to you soon.

Meanwhile, load up your Python libraries and start experiencing Dhan.
Happy Trading!

Thank you

Naman.
-Product team

5 Likes

Algo Traders b328e8daa5c9383660502cbda7e6b2bf_w200

1 Like

Hi @naman

Is statement api available ?

Good to hear Dhan is advancing.

But want to know if WebSocket is ready?

Hi @sac777 Welcome to Dhan community. Are you referring to Markets Feeds APIs or something else?

Thanks. Please resolve this BUG. For positions API, both realized and unrealized fields are coming as zero for open position

‘realizedProfit’: 0.0, ‘unrealizedProfit’: 0.0,

Hi @ssn

These values need to be computed using the APIs itself on the frontend. Its simple sum of all the positions in either states. We will update the documentation to ensure this is communicated as well.

1 Like

@Naman @PravinJ @Dhan_Help

Is statement API available for use ?

1 Like

hi @t7support we usually release APIs after achieving product-market fit. At this point of time, we are enhancing a lot on backend systems and calculations and more. Will release once we are at a comfortable stage.

1 Like

K noted thanks @PravinJ

thanks @PravinJ . This comes for a single leg/position.

I am trying to use the Dhan Pyhton API for creating a Algo.

As per Documentation , I am trying to connect to API and getting the Holdings.
It is taking almost 1-2 minutes to give the Holdings.{Refer ScreenShot Below}

Further, when I am trying to place a Buy Order as per the Documentation following message is appearing:{refer screenShot below}

Whats the issue ??
Why results are displaying so late…

Documentation needs to be improved. Other Brokers are providing very good documentation for every function for easier understanding of API and implementation

1 Like

Hi, you can try the sample code below. There are couple of things in the Documentation that need to be fixed.

  1. exchange_seg= ‘NSE_EQ’ should be exchange_segment=‘NSE_EQ’
  2. There is no argument option for passing corelationID
from dhanhq import dhanhq


client_id = "Enter your client_id here "
access_token = "Enter your access_token here "
dhan = dhanhq(client_id, access_token)

# Place an order (hdfc bank- 1333)
market_order = dhan.place_order(
    security_id='1333',
    exchange_segment='NSE_EQ',
    transaction_type='BUY',
    order_type='MARKET',
    product_type='INTRADAY',
    quantity=10,
    price=0,
    trigger_price=0,
    validity='DAY',
    after_market_order='False'
)
print(market_order)

# Place limit order
limit_order = dhan.place_order(
    security_id='1333',
    exchange_segment='NSE_EQ',
    transaction_type='BUY',
    order_type='LIMIT',
    product_type='INTRADAY',
    quantity=10,
    price=100,
    trigger_price=0,
    validity='DAY',
    after_market_order='False'
)
print(limit_order)

# Place StopLoss Order
SL_Buy_order = dhan.place_order(
    security_id='1333',
    exchange_segment='NSE_EQ',
    transaction_type='BUY',
    order_type='STOP_LOSS',
    product_type='INTRADAY',
    quantity=1,
    price=110,
    trigger_price=100,
    validity='DAY',
    after_market_order='False'
)
print(SL_Buy_order)

# Intraday daily minute charts
intra_data = dhan.intraday_daily_minute_charts('11536', 'NSE_EQ', 'EQUITY')
print(intra_data)

# Fetch all orders
dhan.get_order_list()
print(dhan.get_order_list())

# Get positions
dhan.get_positions()

# Get holdings
dhan.get_holdings()

# Get fund limits
dhan.get_fund_limits()
print(dhan.get_fund_limits())

@Market_Wizard

Sample Code worked. But as suggested by you, Lot of corrections required in documentation.
Code as per recent documents not working.

One More thing why Code is running extrmely slow. Taking more than a minute …

What is the cause and solution for the same.

I ran the same sample code at my end and it took less than a second. You need to check at your end what is causing the delay.

Thank you @Market_Wizard for helping with the clarification here.

We have noted and will make the changes. For correlation ID, it is an optional field and can be added at user’s end if required.

yes, It is taking very long for first API call almost 1 min plus. subsequent API calls happening quickly

Hello @9985243221
Welcome to Dhan Community!

We have added some fixes in our python library - here. This will help with the delayed API response. Hence, we request you to reinstall the library in order to get updated version.