Need Help - DhanHQ Order Placement using Python


Did your code work next day???

I did not try as I had to travel. Will try in coming week and give you an update.

Hi @bsjhala,

Finally I got some time today to test the code once again, was quite busy since Monday due to tight deadlines in office.

The code worked without any issues and am able to send the orders to Dhan platform. Below is the screenshot of the code (Image 1). Please ignore the failure status at the end, one leg failed due to insufficient funds but the other one went through. Image 2 is the snapshot from Dhan Web showing the orders. Orders were placed with SL-L and entry prices set by our codes are 87.6 and 79.2, same prices you can see in the Dhan Web orders screenshot.

Image 1:

Image 2:





Can you pls share complete code…

I am also planning to shift from other Algo platforms to my own coded strategies here on Dhan.

Further,I also request, dhan team to add sample codes in their documentation, as other Popular brokers are providing.

Documentation at Dhan Python documentation is minimal that too not correct.

Market live feed data is also must.

Pls Prioritise …


Hi @bsjhala

Sure, will share the complete code through GitHub either tomorrow or on Sunday and will update you here.



Hello @bsjhala

We have already added sample code to jumpstart with Python here. We will be making it more elaborate with time.

For Market Feed data, have written about our approach here : Live Market Feed API - #2 by Hardik



Thanks a lot in Advance…

It will be very useful to all Dhan users who are interested in Automating their traded.

DhanHQ Python Documentation are very minimal…

1 Like

Sample codes provided are minimal and detailed documentation are required.

Hi @bsjhala and @Gangavarapu

As promised, sharing the codes. Please check below GitHub link to access the Python code having custom functions created to get live NSE option chain data of any Index/Stock Option derivative and security id dynamically.

Python Code for FNO order placements using DhanHQ

Our code is very easy to understand and use with Dhan platform. It has following custom functions;

  1. get_option_chain_data() - To fetch live option chain data (in .json format) from NSE website
  2. process_option_chain_raw_data() - To process .json format raw data and return option chain as a Pandas data frame
  3. get_security_ID_and_LTP() - This is the only function that we need to call and above two functions are called automatically inside this one. After getting option chain as data frame, this function fetches data from api-scrip-master.csv file from Dhan servers and returns the security_id and it’s last trading price as output (in tuple format)

How to use the code? It’s simple, explained using two examples below :slight_smile:

Example 1 - Placing Nifty 18100 CE Intraday Limit Buy Order (10-Nov-2022 expiry):

security_id, security_ltp = get_security_ID_and_LTP(symbol='NIFTY', expiry_date='2022-11-10', strike_price=18100, option_type='CE')

dhan.place_order(security_id=security_id, exchange_segment=dhan.FNO, transaction_type= dhan.BUY, quantity=50, order_type=dhan.LIMIT, 
                 product_type= dhan.INTRA, price=security_ltp)

Example 2 - Placing ITC 347.5 PE Intraday Limit Sell Order (24-Nov-2022 expiry):

security_id, security_ltp = get_security_ID_and_LTP(symbol='ITC', expiry_date='2022-11-24', strike_price=347.5, option_type='PE')

dhan.place_order(security_id=security_id, exchange_segment=dhan.FNO, transaction_type= dhan.SELL, quantity=3200, order_type=dhan.LIMIT, 
                 product_type= dhan.INTRA, price=security_ltp)


  1. symbols must be in capital letters and should be as per NSE standards like NIFTY, BANKNIFTY, FINNIFTY, ITC, HDFCBANK etc
  2. expiry_date must be in ‘YYYY-MM-DD’ format
  3. strike_price must be a numerical value like 18100, 41200, 347.5 etc
  4. quantity parameter in dhan.place_order() function must be in multiples of the scrip lot size. For example, single lot of ITC has 3200 quantity and two lots have 3200x2=6400 quantity. Similarly, single lot of BANKNIFTY has 25 quantity and two lots have 25x2=50 quantity.

Hope this helps. :slight_smile:

Let me know in case of any queries or issues while accessing or executing the codes.



Thanks a lot

@Naman @ravi_krishna @Hardik @Gangavarapu

I am Placing AMO Orders for bank Nifty though Dhan API from Excel.

Buy F&O orders are getting placed without any issue. But All the Sell F&O orders are not getting placed.

Following Error is coming :
{“errorCode”:“BAD_REQUEST_ERROR”,“httpStatus”:“BAD_REQUEST”,“internalErrorCode”:“RS-9005”,“internalErrorMessage”:“Insufficient Holding Quantity”}

Can Any one pls Help…

Hi @bsjhala
Can you please share the request structure?

“dhanClientId”: “string”,
“correlationId”: “string”,
“transactionType”: “SELL”,
“exchangeSegment”: “NSE_FNO”,
“productType”: “CNC”,
“orderType”: “LIMIT”,
“validity”: “DAY”,
“tradingSymbol”: “”,
“securityId”: “46789”,
“quantity”: 25,
“disclosedQuantity”: 25,
“price”: 25,
“triggerPrice”: “”,
“afterMarketOrder”: true,
“amoTime”: “OPEN”,
“boProfitValue”: “”,
“boStopLossValue”: “”,
“drvExpiryDate”: “2022-11-17”,
“drvOptionType”: “CALL”,
“drvStrikePrice”: “41200”

I am Using the above requests. I am able to place AMO Buy F&O orders but error coming in sell order.


1 Like

Hi @bsjhala

Product type should be either INTRADAY or MARGIN. Intraday as name suggests for intraday position and Margin for carryforward/overnight position.

In the above screenshot we can see that the finnifty option strike and banknifty option strike is having a different symbol format. The date part within the symbol is shwoing differently. For finnifty symbol, the month string is showing using just one character ‘N’ for november, while for banknify the month is represented using 3 characters as “NOV”. And the date and year is at different places in both symbols. Please have a look into this issue. This is causing issue in getting security id from the symbol.

1 Like

Hi @stanly_thomas Thank you for highlighting this challenge with FINNIFTY.

I have modified the code by adding “FinNifty_monthly” parameter in the get_security_ID_and_LTP() function to overcome this challenge with FINNIFTY and uploaded into GitHub

Link to GitHub

If using the code for FINNIFTY Monthly expiry, please pass the FinNifty_monthly = True as an argument.

security_id, security_ltp = get_security_ID_and_LTP(symbol='FINNIFTY', expiry_date='2022-11-29', strike_price=18550, option_type='CE', FinNifty_monthly=True)

dhan.place_order(security_id=security_id, exchange_segment= dhan.FNO, transaction_type= dhan.SELL, quantity=40, order_type=dhan.LIMIT, 
                 product_type= dhan.INTRA, price=security_ltp)

If using the code for anything else other than FINNIFTY Monthly expiry, we NO NEED to pass the FinNifty_monthly = True parameter

security_id, security_ltp = get_security_ID_and_LTP(symbol='FINNIFTY', expiry_date='2022-11-15', strike_price=18650, option_type='CE')

dhan.place_order(security_id=security_id, exchange_segment= dhan.FNO, transaction_type= dhan.SELL, quantity=40, order_type=dhan.LIMIT, 
                 product_type= dhan.INTRA, price=security_ltp)
1 Like

@PravinJ / @Naman / @Hardik

I am trying to get historical data using Dhan HQ, but there seems to be an issue with fetching Option related historical data.

I tried everything that is mentioned in the Dhan HQ API document, but it is working only for stock and index historical data and not for Option historical data.

Please find below image and let me know if it is a limitation or am I doing anything wrong with syntax? The first cell in the image contains code for fetching NIFTY historical data and it worked. Whereas the code to fetch historical data for BANKNIFTY24NOV2242300CE is returning a ‘success’ status, but not returning any data. I have even looked into the api_master_scrip.csv file to make sure I am using correct input arguments.


Hello @ravi_krishna

Options data is only available on Intraday Historical Data API currently. This is the reason you are not able to get data for the same.


Thank you for the response. Is there any plan to provide historical Options data in future? Or should I consider this as completely out of scope?

Hello @ravi_krishna

At Dhan, we are always building on our user’s feedback. We have noted this and will evaluate it to incorporate in our roadmap.

1 Like