I am trying to use Bollinger Bands in my Strategy but I got error: "Indicator 'Std. dev.': Error on calling 'OnBarUpdate' method on bar 1169: Object reference not set to an instance of an object.".
After that I tried to create simple example using strategy builder and I still get this error: "Indicator 'Std. dev.': Error on calling 'OnBarUpdate' method on bar 1169: Object reference not set to an instance of an object.".
I will attach simple code with should work but somehow its not working. Code:
#region Using declarations using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; using System.Windows.Media; using System.Xml.Serialization; using NinjaTrader.Cbi; using NinjaTrader.Gui; using NinjaTrader.Gui.Chart; using NinjaTrader.Gui.SuperDom; using NinjaTrader.Gui.Tools; using NinjaTrader.Data; using NinjaTrader.NinjaScript; using NinjaTrader.Core.FloatingPoint; using NinjaTrader.NinjaScript.Indicators; using NinjaTrader.NinjaScript.DrawingTools; #endregion //This namespace holds Strategies in this folder and is required. Do not change it. namespace NinjaTrader.NinjaScript.Strategies { public class MyCustomStrategyBollingerBandsTest : Strategy { private Bollinger Bollinger1; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Enter the description for your new custom Strategy here."; Name = "MyCustomStrategyBollingerBandsTest"; Calculate = Calculate.OnEachTick; 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) { Bollinger1 = Bollinger(Close, 2, 14); Bollinger1.Plots[0].Brush = Brushes.Goldenrod; Bollinger1.Plots[1].Brush = Brushes.Goldenrod; Bollinger1.Plots[2].Brush = Brushes.Goldenrod; AddChartIndicator(Bollinger1); } } protected override void OnBarUpdate() { if (BarsInProgress != 0) return; if (CurrentBars[0] < 1) return; // Set 1 if (CrossAbove(Close, Bollinger1.Upper, 1)) { ExitShort(Convert.ToInt32(DefaultQuantity), "", ""); EnterLong(Convert.ToInt32(DefaultQuantity), ""); } // Set 2 if (CrossBelow(Close, Bollinger1.Lower, 1)) { ExitLong(Convert.ToInt32(DefaultQuantity), "", ""); EnterShort(Convert.ToInt32(DefaultQuantity), ""); } } } }
Waiting for your reply. Thanks
Comment