Close[0] very weird behavior

So in a strategy that is calculating on price change, I am simply need to access Close[0] to get the current price of the instrument (ES) and running some checks and ‘if’ statements on it.

However it is returning very strange results. Right as the strategy is activated, Close[0] floods the logs with random numbers much higher than the current price. It starts at a high number and then dives down to the eventual correct current price.

Any ideas on what is causing this or what this is?

Add Time[0] and you will see that it is running through all the historical data. So that very first one SHOULD be the very first close. If you only want it to run in real time you can add

if (State != State.Realtime)
{
return;
}

I use that for my strategy to disable my ability to hit buttons before it’s ready, but I think you can use that in OnBarUpdate.

1 Like

Yeah that was it. I had it on my other if’s, didnt occur to me on this. Still weird to me that function returns that. Close[0] is supposed to be the current bar.

When the chart is building, from the first bar (bar 0), each bar is closed and triggers a Close[0].

1 Like

Yes. Technically it is from a historical point of view. It populates the bars one at a time in order, so at the time, Close[0] is that current bar. It loads everything and acts as if it were realtime, aside from how it would actually act if it were OnEachTick or OnPriceChange, since the historic bars only have the OHLC info. For the historic portion, it treats it kinda like OnBarClose. If you are coding your own strategy that’s just something you need to remember with how it works on loading the indicator/ strategy.

1 Like