Error on calling 'OnStateChange' method: Object reference not set to an instance of an object.
Questions:
1. Why is it giving me this error when I'm not even coding by hand?
2. I have always struggled with having the target and stop set to a var that is set in the OnBarUpdate. Is that possible?
3. Once the stop and target are set, will I be able to reset or move them if they are located in the DataLoaded section of the code?
....a. If not which three methods can be called in the OnBarUpdate that work together in unison?
........- ExitLongStopMarket(MyStop);
........- ExitLongLimit(MyTarget);
........- EnterLong()?
namespace NinjaTrader.NinjaScript.Strategies { public class BollingerTest : Strategy { private EMA EMA1; private Bollinger Bollinger1; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Enter the description for your new custom Strategy here."; Name = "BollingerTest"; Calculate = Calculate.OnBarClose; EntriesPerDirection = 1; EntryHandling = EntryHandling.AllEntries; IsExitOnSessionCloseStrategy = true; ExitOnSessionCloseSeconds = 30; IsFillLimitOnTouch = false; MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix; OrderFillResolution = OrderFillResolution.Standard; Slippage = 0; StartBehavior = StartBehavior.WaitUntilFlat; TimeInForce = TimeInForce.Gtc; TraceOrders = false; RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose; StopTargetHandling = StopTargetHandling.PerEntryExecution; BarsRequiredToTrade = 20; // Disable this property for performance gains in Strategy Analyzer optimizations // See the Help Guide for additional information IsInstantiatedOnEachOptimizationIteration = true; } else if (State == State.Configure) { } else if (State == State.DataLoaded) { EMA1 = EMA(Close, 20); Bollinger1 = Bollinger(Close, 2, 14); SetStopLoss("", CalculationMode.Currency, Bollinger1.Lower[0], false); SetProfitTarget("", CalculationMode.Currency, Bollinger1.Upper[0]); } } protected override void OnBarUpdate() { if (BarsInProgress != 0) return; if (CurrentBars[0] < 1) return; // Set 1 if (Close[0] > EMA1[0]) { EnterLong(Convert.ToInt32(DefaultQuantity), ""); } } } }
Comment