Learn Algo Trading with Python | Codes | Youtube Series

Hi @vinothsaravanan ,

Rate limit issues may occur while fetching data. One way to handle this is by creating multiple APIs to increase the overall rate limit.

Tagging @Dhan for multiple API creation.

@Tradehull_Imran sir,

API market exit got converted to limit ₹20 and didn’t execute, while manual market exit worked at ₹18.95.

Facing execution issue with API + MPP. Need clarity:

  1. What is the exact logic used to convert API market orders into limit prices under MPP?

  2. In my case, limit was set to ₹20 while traded range was ₹18.40–₹18.90 — why such a mismatch?

  3. Is there any way to control or reduce MPP for API users?

  4. What’s the recommended way to ensure reliable exits in options via API (since market orders are converted)?

  5. Are IOC or aggressive limit strategies supported/recommended for such scenarios?

Hi @Rakesh_Durgam

  1. MPP conversion logic:
    In Dhan API, market orders are not sent as pure market orders. They are converted into limit orders using MPP based on the reference/LTP price at that time and the allowed protection percentage.

  2. Why ₹20 limit when market traded around ₹18.40–₹18.90:
    The ₹20 limit was most likely calculated from the reference price at the time the API order was placed. After that, the option price moved down. Since it became a SELL LIMIT at ₹20, it could not execute at ₹18.95 or below.

  3. Can we control/reduce MPP from API?
    As per current API behavior, there is no direct field to set or reduce MPP percentage from our side. It is handled by Dhan/exchange internally.

  4. Recommended reliable exit method:
    For option exits through API, instead of relying on MARKET orders, we should use aggressive LIMIT orders with IOC validity. For SELL exit, place limit slightly below current bid/LTP. For BUY exit, place limit slightly above current ask/LTP. Then check filled quantity and retry remaining quantity if required.

In this case, the pending SELL LIMIT at ₹20 will execute only if buyers are available at ₹20 or above. It will not execute at ₹18.95.

There is a mistake in code in 3rd Algo. conditions are created (breakout_c1, breakout_c2, breakout_c3) but they are not really used in decision making

Request to start Backtesting Series

Kindly start a detailed backtesting series. Many of us are eager to learn and understand strategy validation in a structured way. It would be extremely helpful for improving our trading approach.

Looking forward to your guidance!

Hi Team,

I have enabled Data APIs in my Dhan account, but I am not receiving any market data.

Details:

  • API calls are successful (status: success)
  • But response is always empty: { “data”: {} }
  • Tested with valid instruments (e.g., NSE_EQ: 11536, NSE_INDEX: 13)
  • Tested both /quote and /ltp endpoints

Is it because it is outside trading hours? what can be the issues?

Thanks

getting below error while trying to get historical data

data = tsl.get_historical_data(tradingsymbol=‘NIFTY’, exchange=‘INDEX’, timeframe=“DAY”)

Exception: dhanhq.convert_to_date_time() missing 1 required positional argument: ‘epoch’

getting response now- had to uninstall dhanhq and re-install

Hi @Bishwapati_Nandi ,

we have updated the version use

pip install Dhan-Tradehull==3.2.2

Dhan quote apis are heavily limited in this regard compared to say Z or K. You will have to retry quotes often, and also use filelock while running mutiple scripts…

1 Like

Hi @siddhesh22 ,

Kindly share the youtube link to verify it .

Hi @Suman_Saurabh ,

Refer this link -

Hi @sharath_Chandra ,

Refer the photo after login it will give us the data -

Refer the link for code and Update the codebase -

pip install Dhan-Tradehull==3.2.2

Use this code to update the Dhan_Tradehull

can I get access to this link @Tradehull_Imran Sir

Hi @Rakesh_Durgam ,

Access has been granted to the link.

1 Like

@Tradehull_Imran sir

Facing a design question for algo + payoff logic. Need clarity:

I’m building an options algo using Dhan Data API and want to use payoff-based logic (PnL vs spot range) for adjustments.

Couple of questions:

Already handling execution via API, now working on payoff-based adjustment logic

1.Is there any way to fetch payoff data (PnL across spot range) via Dhan API?

2.Or is payoff always computed internally in the Options Trader UI?

3.If no API is available, is it expected that we build our own payoff engine for multi-leg strategies?

4.Any recommended approach from Dhan for using payoff/risk zones in algo decision-making?

Goal: use payoff zones to control adjustments

(e.g., avoid adjusting

already safe side).

Hi @Rakesh_Durgam
Payoff is computed internally in Dhan options traders strategy builder. we can build our own payoff graphs using opstrat library.

link : opstrat · PyPI
install by : pip install opstrat

example code

op.single_plotter(spot=24107, strike=24300, op_type='c', tr_type='b', op_pr=126)

Parameter description
spot  = 24107
strike = 24300
option type = "call"
trade type = "buy"
options_price = 126

see its example payoff graph output

Hi @Tradehull_Imran sir,

I am facing the below error repeatedly. Please give your feedback

Scanning NIFTY at 23:20:29.963766
[DEBUG] First rows of NIFTY data:
open high low close volume timestamp open_interest
4364 24171.00 24250.85 24164.55 24175.80 18986851.0 2026-05-06 09:15:00+05:30 0.0
4365 24179.20 24201.95 24156.95 24185.20 8595561.0 2026-05-06 09:20:00+05:30 0.0
4366 24186.15 24195.30 24164.95 24171.60 6184442.0 2026-05-06 09:25:00+05:30 0.0
4367 24174.60 24185.25 24161.55 24162.75 6207446.0 2026-05-06 09:30:00+05:30 0.0
4368 24163.75 24167.05 24150.75 24154.00 5421620.0 2026-05-06 09:35:00+05:30 0.0
[DEBUG] NIFTY: bearish_candle=False,buy_ce_setup=False, buy_ce=True, fund_required=True
[DEBUG] NIFTY: candle_size=True, bullish_candle=True, buy_ce1_setup=False, buy_ce1=True ,fund_required=True
starting while Loop

Scanning NIFTY at 23:20:35.452643
[DEBUG] First rows of NIFTY data:
open high low close volume timestamp open_interest
4364 24171.00 24250.85 24164.55 24175.80 18986851.0 2026-05-06 09:15:00+05:30 0.0
4365 24179.20 24201.95 24156.95 24185.20 8595561.0 2026-05-06 09:20:00+05:30 0.0
4366 24186.15 24195.30 24164.95 24171.60 6184442.0 2026-05-06 09:25:00+05:30 0.0
4367 24174.60 24185.25 24161.55 24162.75 6207446.0 2026-05-06 09:30:00+05:30 0.0
4368 24163.75 24167.05 24150.75 24154.00 5421620.0 2026-05-06 09:35:00+05:30 0.0
Exception at calling ltp as {‘status’: ‘failure’, ‘remarks’: {‘error_code’: None, ‘error_type’: None, ‘error_message’: None}, ‘data’: ‘’}
[ERROR] NIFTY: LTP data empty for NIFTY 12 MAY 24400 CALL — skipping.
starting while Loop

Is this issue resolved? I am also trying with Python 3.12.

I am also facing same problem which you have posted initially. please help!