If I subscribe to Niffy 50 websocket stream, the OHLC data I get has highs and lows for the whole day.
My problem is that I want to aggregate this data into the same 1 minute running candle that the chart represents. What is the algorithm to do that? How does the Kite Historical API aggregate it? It does it correctly because it matches the chart.
I think I need to take the last_price coming in each tick of the same minute to aggregate into the running 1 minute candle's OHLC values. Is that the right way?
Yes, you need to build logic for that yourself. If you want to represent that data in chart, then fetch the historical data for NIFTY 50 for 1 minute, then subscribe to socket for realtime update,
since the data from socket is in realtime, on every tick, check if it belongs to current minute. If yes, then keep a store of open, high, low, close and check if the current price is higher than high, update the high of that minute, same for low.
open is set only once when the minute has changed. close is updated on every tick.
What you say makes sense. But real data does not help.
Here is the dump when a candle starts and ends test: 2026-01-23T12:17:00.000Z last_price 25207.65 close 25289.9 test: 2026-01-23T12:17:00.000Z last_price 25208.2 close 25289.9 ... test: 2026-01-23T12:17:57.000Z last_price 25207.75 close 25289.9 test: 2026-01-23T12:17:57.000Z last_price 25208.7 close 25289.9 test: 2026-01-23T12:17:58.000Z last_price 25206.95 close 25289.9 test: 2026-01-23T12:17:58.000Z last_price 25207.75 close 25289.9 test: 2026-01-23T12:17:59.000Z last_price 25208.4 close 25289.9 test: 2026-01-23T12:17:59.000Z last_price 25208.5 close 25289.9
But if I see Kite chart for today for candle at 12:17, it has these OHLC values 25207.30, 25207.30, 25203.20, 25204.40
I have not verified High and Low but open and close have no relation to the above data. Mystified.
If you want to represent that data in chart, then fetch the historical data for NIFTY 50 for 1 minute,
then subscribe to socket for realtime update,
since the data from socket is in realtime, on every tick, check if it belongs to current minute. If yes, then keep a store of open, high, low, close and check if the current price is higher than high, update the high of that minute, same for low.
open is set only once when the minute has changed. close is updated on every tick.
Here is the dump when a candle starts and ends
test: 2026-01-23T12:17:00.000Z last_price 25207.65 close 25289.9
test: 2026-01-23T12:17:00.000Z last_price 25208.2 close 25289.9
...
test: 2026-01-23T12:17:57.000Z last_price 25207.75 close 25289.9
test: 2026-01-23T12:17:57.000Z last_price 25208.7 close 25289.9
test: 2026-01-23T12:17:58.000Z last_price 25206.95 close 25289.9
test: 2026-01-23T12:17:58.000Z last_price 25207.75 close 25289.9
test: 2026-01-23T12:17:59.000Z last_price 25208.4 close 25289.9
test: 2026-01-23T12:17:59.000Z last_price 25208.5 close 25289.9
But if I see Kite chart for today for candle at 12:17, it has these OHLC values
25207.30, 25207.30, 25203.20, 25204.40
I have not verified High and Low but open and close have no relation to the above data. Mystified.