Best way to determine new bar from API

I’m subscribing to a renko chart and need to be able to reliably know when a new bar is formed. I was under the impression that when the md/getchart endpoint returns a chart frame with 2 bars, the bar with index of 1 was the new bar. Is this not correct? So far, playing with the replay against my app to compare the differences shows inconsistencies with having my app paint a new bar onto my chart when the above condition is met. Is this the proper way to consume a new bar or is there a different/better way to go about this?

What I think is causing my problems is the “projected renko bar” situation as shown here:

In this example, the websocket api returns a frame for a chart bar as expected, but when the bar disappears, the next bar in the frame received shows the previous bar? This is confusing as I was under the assumption that all bars received from md/getchart would be either the same timestamp as the last bar or a bar with a timestamp AFTER the last bar?

Haven’t used RENKO but faced this issue with other candle types like tick and range ones.

The algorithm I finally use is such that.

  1. Keep track of old time stamp of an ongoing candle which is in process to form.

  2. When a new data point is received, check each candle. If timestamp matches the old time stamp, update the forming candle to this latest candle (OHLC Values updated).

  3. If new timestamp is observed, check if it is really newer? TimeNew > TimeOld. If yes, then we can conclude that whichever OHLC values we have for old timestamp form the latest closed candle.

  4. Move this completely formed candle to database/other variable and start forming the new candle with new timestamp and associated OHLC values.

This works fine so far. I have been using this for more than few months now and faced no issue.
Haven’t used replay though.

Thanks and Regards,

@PRATIK_BABHULKAR thank you so much for replying. I came to the same conclusion before you replied and I’m glad that I wasn’t the only one running into this and it’s good that someone else came to the same conclusion and has a fix in place :+1:

I implemented a similar algo and the replay seems to be lining up with my chart!

1 Like

Index 0 is most likely the first bar. Arrays start from 0 in every programming language I know. I don’t know if this will help you though. Also time series data tends to be added to the front of arrays rather than the back. Sometimes it’s preferable to use index 1 in algos order to ignore the forming bar, but if you’re charting the data you probably want 0 if you want to see the live bar.