Optimizing Real-Time Option Data Pipeline: 5-Min LTP → 42 Strikes → 150 OHLC Points

I’m building a real-time system that:

Workflow Sequence :

  1. Waits for 5-minute candle close → fetches fresh LTP → calculates new ATM strike.
  2. Dynamically selects 42 strikes (21CE+21PE) around this just-calculated ATM.
  3. Pulls 150 OHLC points per option (rolling window, 5-min candles).
  4. Cannot pre-cache/pre-fetch: The ATM (and thus strikes) is unknown until the 5-minute window closes. Every cycle depends on the newest LTP.

Bottlenecks:

  1. API Latency: ~10 sec to fetch all 42 strikes (DhanHQ rate limits).
  2. No Caching Possible: ATM changes every cycle → strikes change → symbol mappings differ.
  3. Pre-fetching: Cannot predict next ATM until LTP updates.

Are there any best practice that can be beneficially used?
Are there any specific Dhan API optimisation ?

@Arun86 The flow is impressive and I appreciate you sharing it with the community.

In a naive way, though you are waiting for the 5-minute candle close to handle the 10 sec fetch issue, I would rather start the fetch at the 51st second of the 4th minute, considering that the 9-second fetch will not have changes and even if it does, it will fall under the accuracy thresholds. This will make the system execute at an ATM with some accuracy thresholds at the 1st second after the 5-minute candle.

Thanks for sharing your input, but I have a doubt:

how can I get 5-minute data before the candle completion time?

For example, if I need data from 09:15-09:20, I currently execute at 09:20 to get the data. But how can I execute at 09:19:51 to get data instead?

It depends on how you execute at 9:20. I assume you have a cron to run every 5 minutes(300 seconds) starting from 9:15. Get the data, store and accordingly execute. You need to change it to run every 51 seconds and then cap it to 5 minutes. Something like the following

51 4-59/5 * * * *

I use gocron, but how to get the 5 min api call?

It’s not about getting the 5-minute api call. It’s about executing the API when the cron is triggered to retrieve the prices at that instant, i.e., the current prices.

yup i completely agree, but the main problem is the Rate limit 5/sec

@Arun86 I am not sure how the rate limit causes an issue? If you are doing it for an Index, i.e Nifty and Bank Nifty, it’s just 2 calls per 290 seconds. If you are running it for stocks then you will need to batch it i.e so each batch’s 5-minute differs by 1 second each and each batch has 5 stocks. Are you running it for all FnO stocks at once?

1 Like

i am retrieving for 42 options instrument s

If you are using Dhan’s data api, you will get it in one call. Refer https://docs.dhanhq.co/docs/v2/option-chain/. Which api are you calling?