I use the below code in the calculate() method of a custom indicator to check if we have been asked to calculate a value for the currently forming bar.
The indicator does not display any values for the currently forming bar, so..
- it returns no values if startIndex is the currently forming bar; and
- it resets the value of endIndex to the 2nd last bar (ie. the bar before the currently forming bar) if endIndex refers to the currently forming bar.
Unfortunately I rely on the IIndicatorContect.getHistory() method to do this. This is not available in production, only in demo.
How can I re-write this code to exclude calculating values for the currently forming candle in production?
..
this.barHistory = indiContext.getHistory();
..
// avoid processing in progress candle
int futureCandlesAvoided = 0;
try
{
long inProgressBarTime = barHistory.getStartTimeOfCurrentBar(this.instrument, this.period);
if (inputBars[startIndex].getTime() >= inProgressBarTime)
return new IndicatorResult(0, 0); // do not calculate in progress bar
if (inputBars[startIndex].getTime() < inProgressBarTime && inputBars[endIndex].getTime() >= inProgressBarTime)
{
// move endIndex so that we do not calculate in progress bar
int count;
for (count = startIndex; count <= endIndex; count++)
{
if (inputBars[count].getTime() == inProgressBarTime)
{
break;
}
}
futureCandlesAvoided = endIndex - (count-1);
endIndex = count-1;
}
}
catch (Exception e)
{
...
}
..
indiRes = new IndicatorResult (startIndex, endIndex-startIndex + 1 + futureCandlesAvoided);
..
return indiRes;