After way too much troubleshooting and removing sections of code for testing I found that the else if (State == State.DataLoaded) section is sometimes running multiple times. In this section I have a calculation that takes a user defined property of trendDays which is say 1 and multiplies it by 26. Basically if the 1 day trend is positive then buy. It multiplies by 26 as the strategy is run on 15 minute bars and 26 bars is one day so after dataloaded trendDays changes from 1 to 26 where in OnBarUpdate trendDays is the int value for some SMAs.
if (State == State.SetDefaults) { trendDays = 1; } else if (State == State.DataLoaded) { trendDays = trendDays *26; } protected override void OnBarUpdate() { if (SMA(Close, trendDays)[0] > SMA(Close, trendDays)[1]) EnterLong();
I've resolved the issue by removing the calc entirely and directly using the number of bars for the trendDays variable instead but this isn't ideal. I can put this code somewhere else if need be but I'm not sure where. It only needs to run once and according to the NT Guide in OnStateChange the dataloaded section is only suppose to run once. IsInstantiatedOnEachOptimizationIteration is set to true.
Any suggestions?
Comment