OnBarUpdate can have two execution behaviors.
- Calculate == Calculate.OnBarClose
or
Calculate == Calculate.OnEachTick && State == State.Historical && IsTickReplays[0] == null || IsTickReplays[0].Value == false
Under these conditions, IsFirstTickOfBar == true for each execution, and the CurrentBar references the last complete bar. 0 bars ago also refers to the last complete bar. - Calculate == Calculate.OnEachTick
and
State == State.Realtime || State == State.Historical && IsTickReplays[0].Value == true
Under these conditions. IsFirstTickOfBar == true only when it’s the first tick of a bar. CurrentBar - 1 refers to the last complete bar. 1 bars ago also refers to the last complete bar.
class LastCompleteBarTracker
{
public LastCompleteBarTracker(Indicator indicator)
{
this.indicator = indicator;
lastCompleteBar = Enumerable.Repeat(-1, indicator.BarsArray.Length).ToArray();
}
public int this[int index]
{
get { return lastCompleteBar[index]; }
}
public int BarsAgo(int barsInProgress)
{
return indicator.CurrentBar - lastCompleteBar[barsInProgress];
}
public int BarsAgo()
{
return BarsAgo(indicator.BarsInProgress);
}
public void OnBarUpdate()
{
if(indicator.IsFirstTickOfBar)
{
var barsInProgress = indicator.BarsInProgress;
if(indicator.Calculate == Calculate.OnBarClose)
{
lastCompleteBar[barsInProgress] = indicator.CurrentBars[barsInProgress];
}
else
{
if(indicator.State == State.Realtime ||
indicator.IsTickReplays[barsInProgress].HasValue && indicator.IsTickReplays[barsInProgress].Value)
{
lastCompleteBar[barsInProgress] = indicator.CurrentBars[barsInProgress] - 1;
}
else
{
lastCompleteBar[barsInProgress] = indicator.CurrentBars[barsInProgress];
}
}
}
}
Indicator indicator;
int[] lastCompleteBar;
}
To use it
public class MyIndicator: Indicator
{
protected override void OnStateChange()
{
switch(State)
{
case State.SetDefaults:
// Set your defaults:
break;
case State.DataLoaded:
lastCompleteBar = new LastCompleteBarTracker(this);
break;
}
}
protected override void OnBarUpdate()
{
lastCompleteBar.OnBarUpdate();
var theLastCompleteBar = lastCompleteBar[BarsInProgress];
var lastClose = Close[lastCompleteBar.BarsAgo()];
}
LastCompleteBarTracker lastCompleteBar;
}
If Calculate == Calculate.OnEachTick and tick replay is off, this is how OnBarUpdate, executes
OnBarUpdate CurrentBar = 0, Time = 1/26/2021 15:59:15, LastCompleteBar = 0, Close = 143.28 IsFirstTickOfBar OnBarUpdate CurrentBar = 1, Time = 1/26/2021 15:59:20, LastCompleteBar = 1, Close = 143.23 IsFirstTickOfBar OnBarUpdate CurrentBar = 2, Time = 1/26/2021 15:59:25, LastCompleteBar = 2, Close = 143.23 IsFirstTickOfBar OnBarUpdate CurrentBar = 3, Time = 1/26/2021 15:59:30, LastCompleteBar = 3, Close = 143.28 IsFirstTickOfBar OnBarUpdate CurrentBar = 4, Time = 1/26/2021 15:59:35, LastCompleteBar = 4, Close = 143.28 IsFirstTickOfBar OnBarUpdate CurrentBar = 5, Time = 1/26/2021 15:59:40, LastCompleteBar = 5, Close = 143.3 IsFirstTickOfBar OnBarUpdate CurrentBar = 6, Time = 1/26/2021 15:59:45, LastCompleteBar = 6, Close = 143.31 IsFirstTickOfBar OnBarUpdate CurrentBar = 7, Time = 1/26/2021 15:59:50, LastCompleteBar = 7, Close = 143.28 IsFirstTickOfBar OnBarUpdate CurrentBar = 8, Time = 1/26/2021 15:59:55, LastCompleteBar = 8, Close = 143.26 IsFirstTickOfBar OnBarUpdate CurrentBar = 9, Time = 1/26/2021 16:00:00, LastCompleteBar = 9, Close = 143.19 IsFirstTickOfBar
OnBarUpdate CurrentBar = 0, Time = 1/26/2021 15:59:15, LastCompleteBar = -1, Close = 143.2 IsFirstTickOfBar OnBarUpdate CurrentBar = 0, Time = 1/26/2021 15:59:15, LastCompleteBar = -1, Close = 143.21 OnBarUpdate CurrentBar = 0, Time = 1/26/2021 15:59:15, LastCompleteBar = -1, Close = 143.21 OnBarUpdate CurrentBar = 0, Time = 1/26/2021 15:59:15, LastCompleteBar = -1, Close = 143.21 OnBarUpdate CurrentBar = 0, Time = 1/26/2021 15:59:15, LastCompleteBar = -1, Close = 143.21 OnBarUpdate CurrentBar = 0, Time = 1/26/2021 15:59:15, LastCompleteBar = -1, Close = 143.21 OnBarUpdate CurrentBar = 0, Time = 1/26/2021 15:59:15, LastCompleteBar = -1, Close = 143.22 OnBarUpdate CurrentBar = 0, Time = 1/26/2021 15:59:15, LastCompleteBar = -1, Close = 143.22 OnBarUpdate CurrentBar = 0, Time = 1/26/2021 15:59:15, LastCompleteBar = -1, Close = 143.22 OnBarUpdate CurrentBar = 0, Time = 1/26/2021 15:59:15, LastCompleteBar = -1, Close = 143.22 ... OnBarUpdate CurrentBar = 1, Time = 1/26/2021 15:59:20, LastCompleteBar = 0, Close = 143.28 IsFirstTickOfBar OnBarUpdate CurrentBar = 1, Time = 1/26/2021 15:59:20, LastCompleteBar = 0, Close = 143.27 OnBarUpdate CurrentBar = 1, Time = 1/26/2021 15:59:20, LastCompleteBar = 0, Close = 143.27 OnBarUpdate CurrentBar = 1, Time = 1/26/2021 15:59:20, LastCompleteBar = 0, Close = 143.28 OnBarUpdate CurrentBar = 1, Time = 1/26/2021 15:59:20, LastCompleteBar = 0, Close = 143.28 OnBarUpdate CurrentBar = 1, Time = 1/26/2021 15:59:20, LastCompleteBar = 0, Close = 143.26 OnBarUpdate CurrentBar = 1, Time = 1/26/2021 15:59:20, LastCompleteBar = 0, Close = 143.26 OnBarUpdate CurrentBar = 1, Time = 1/26/2021 15:59:20, LastCompleteBar = 0, Close = 143.26 OnBarUpdate CurrentBar = 1, Time = 1/26/2021 15:59:20, LastCompleteBar = 0, Close = 143.26 OnBarUpdate CurrentBar = 1, Time = 1/26/2021 15:59:20, LastCompleteBar = 0, Close = 143.26 OnBarUpdate CurrentBar = 1, Time = 1/26/2021 15:59:20, LastCompleteBar = 0, Close = 143.26

Comment