First of all, 9.81.1 is not the latest TWS API version. :
- Stable API version: is 10.19 (Release Date:Nov 16 2022)
- Latest API version is 10.20 (Release Date Nov 4 2022)
You might want to move towards one of the Version 10 APIs.
I am not following your approach completely and am wondering whether you make it too complicated.
The API documentation does not say so explicitly, but "bar time" is the time for the open value of the bar (the start of the bar). The bar is assembled by IBKR at (or shortly after) "bar time + 5 seconds" and takes a few 100 milliseconds (up to a second) until it reaches you:
If bar time for a 5 second bar is is 14:44:55, for example:
- OPEN price for the bar is the first trade at or after 14:44:55
- CLOSE price is the last trade before 14:45:00
- You receive the bar very shortly after 14:45:00 and this is the end of the 14:44 minute.
If you want to make your own 1minute bar from 5second bars, you'd:
- Start with a bar that has a bar time of a full minute (1676321100 or 14:45:00 below) and use the OPEN price of that bar as the open price for your 1 minute bar
- Remember low and high from the first bar as well as volume and count if you are interested in those.
- For the next eleven bars (e.g. bars with a time of < first bar time + 60 seconds) check whether their low is lower or high is higher and add up volume and count.
- Once you have received bar #12 you can finish your 1 minute bar from OPEN, the lowest low you found, the highest high you found, the sum of volumes and counts, and the CLOSE price of bar #12
You should have that 1minute bar in hand a second or less after the end of the minute.
Hope this helps,
´³¨¹°ù²µ±ð²Ô
