I have developed an unusual proprietary Indicator that works well and I am currently coding a Strategy to trade automatically based on its indications. Despite not having an error message during extensive testing of the finished Indicator attached directly to a chart, when accessed from a Strategy there was a startup error message complaining that the code was trying to reference 5 bars back with only 4 bars loaded.
The indicator only accesses 5 bars back, but needs to analyze patterns over a several hundred historic bars to be able to make reliable market indications. The number of historic bars needed depends complexly on various user settings, so I initially calculated and set a suitable BarsRequiredToPlot value in State.Configure rather than making the value a user setting in State.SetDefaults.
I thought calculating and setting the value that way might have something to do with the error when the Indicator was accessed from a Strategy, so I commented-out the setting in State.Configure, made BarsRequiredToPlot a user-setting in State.SetDefaults, and passed a suitable value from the Strategy. That didn't change anything. I still had the error message.
Then I put a Print statement in the indicator to print the BarsRequiredToPlot value and learned that when the Indicator was accessed from a Strategy the BarsRequiredToPlot value was always 20, regardless of the value passed from the Strategy or even hard-coded locally in the Indicator! However, when the Indicator was attached directly to a chart the printed BarsRequiredToPlot value was always whatever value I set and there was no error message.
Another strange fact is that when the Indicator is attached directly to a chart it runs without an error message with any BarsRequiredToPlot value greater than 4, but when accessed from a Strategy there is the error message even though the printed BarsRequiredToPlot value is 20 ???
I found experimentally that I could avoid the error and make the indicator work normally when accessed from a Strategy by setting a suitable BarsRequiredToPlot value in OnBarUpdate(), instead of in State.SetDefaults, like this:
protected override void OnBarUpdate() { if (BarsInProgress != 0) return; if (CurrentBar == 0) { BarsRequiredToPlot = 435; return; } ... Other processing }
Comment