Sry about this since I know it's a common newbie mistake...I'm getting the out-of-bounds error but can't figure out why. Commented everything out in my custom indicator except for one print statement that is just a string, and still, the exception is thrown. I don't have a VS license. Any ideas?
This is from the log panel:
"okay init we got this far
8/31/2021 3:00:00 PM hi
8/31/2021 3:00:00 PM dema is 15609
Strategy 'SuperTrend': Error on calling 'OnBarUpdate' method on bar 0: Index was outside the bounds of the array."
Strategy 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 SuperTrend : Strategy { private DEMA dema; private bool moveStop = false; private _SuperTrend superTrend; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Enter the description for your new custom Strategy here."; Name = "SuperTrend"; Calculate = Calculate.OnBarClose; EntriesPerDirection = 1; EntryHandling = EntryHandling.AllEntries; IsExitOnSessionCloseStrategy = false; 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) { // Best practice is to instantiate indicators in State.DataLoaded. dema = DEMA(200); superTrend = _SuperTrend(14,3); } } protected override void OnBarUpdate() { Print(string.Format("{0} hi", Time[0])); Print(string.Format("{0} dema is {1}", Time[0], dema[0])); // error is thrown at this time, if I comment it out I'll get all print statements double ST = superTrend[0]; } } }
#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.DrawingTools; #endregion //This namespace holds Indicators in this folder and is required. Do not change it. namespace NinjaTrader.NinjaScript.Indicators { public class _SuperTrend : Indicator { // private ATR atr; // private double currentFinalLowerBand = 0; // private double currentFinalUpperBand = 0; // private double currentBasicUpperBand = 0; // private double currentBasicLowerBand = 0; // private double previousFinalUpperBand = 0; // private double previousFinalLowerBand = 0; // public double currentSuperTrend = 0; // private double previousSuperTrend = 0; public bool isDownTrend = true; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"_SuperTrend Bih"; Name = "_SuperTrend"; Calculate = Calculate.OnBarClose; IsOverlay = false; DisplayInDataBox = true; DrawOnPricePanel = true; DrawHorizontalGridLines = true; DrawVerticalGridLines = true; PaintPriceMarkers = true; ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right; //Disable this property if your indicator requires custom values that cumulate with each new market data event. //See Help Guide for additional information. IsSuspendedWhileInactive = true; // atr = ATR(14); Print(string.Format("okay init we got this far")); } else if (State == State.Configure) { } } protected override void OnBarUpdate() { Print("In bar update"); } // private void log(TimeSeries time, double basicUpper, double basicLower, double finalUpper, double finalLower, double prevST, double finalST, double high, double low, double close, double atr) // { // Print(string.Format("{0} high {1}", Time[0], high)); // Print(string.Format("{0} low {1}", Time[0], low)); // Print(string.Format("{0} close {1}", Time[0], close)); // Print(string.Format("{0} atr {1}", Time[0], atr)); // Print(string.Format("{0} basic upper {1}", Time[0], basicUpper)); // Print(string.Format("{0} basic lower {1}", Time[0], basicLower)); // Print(string.Format("{0} final upper {1}", Time[0], finalUpper)); // Print(string.Format("{0} final lower {1}", Time[0], finalLower)); //// Print(string.Format("{0} prev ST {1}", Time[0], prevST)); // Print(string.Format("{0} current ST {1}", Time[0], finalST)); // } #region Properties [NinjaScriptProperty] [Range(1, int.MaxValue)] [Display(Name="ATRPeriod", Description="ATR Period", Order=1, GroupName="Parameters")] public int ATRPeriod { get; set; } [NinjaScriptProperty] [Range(1, int.MaxValue)] [Display(Name="ATRMultiplier", Description="ATR Multiplier", Order=2, GroupName="Parameters")] public int ATRMultiplier { get; set; } #endregion } } #region NinjaScript generated code. Neither change nor remove. namespace NinjaTrader.NinjaScript.Indicators { public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase { private _SuperTrend[] cache_SuperTrend; public _SuperTrend _SuperTrend(int aTRPeriod, int aTRMultiplier) { return _SuperTrend(Input, aTRPeriod, aTRMultiplier); } public _SuperTrend _SuperTrend(ISeries<double> input, int aTRPeriod, int aTRMultiplier) { if (cache_SuperTrend != null) for (int idx = 0; idx < cache_SuperTrend.Length; idx++) if (cache_SuperTrend[idx] != null && cache_SuperTrend[idx].ATRPeriod == aTRPeriod && cache_SuperTrend[idx].ATRMultiplier == aTRMultiplier && cache_SuperTrend[idx].EqualsInput(input)) return cache_SuperTrend[idx]; return CacheIndicator<_SuperTrend>(new _SuperTrend(){ ATRPeriod = aTRPeriod, ATRMultiplier = aTRMultiplier }, input, ref cache_SuperTrend); } } } namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns { public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase { public Indicators._SuperTrend _SuperTrend(int aTRPeriod, int aTRMultiplier) { return indicator._SuperTrend(Input, aTRPeriod, aTRMultiplier); } public Indicators._SuperTrend _SuperTrend(ISeries<double> input , int aTRPeriod, int aTRMultiplier) { return indicator._SuperTrend(input, aTRPeriod, aTRMultiplier); } } } namespace NinjaTrader.NinjaScript.Strategies { public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase { public Indicators._SuperTrend _SuperTrend(int aTRPeriod, int aTRMultiplier) { return indicator._SuperTrend(Input, aTRPeriod, aTRMultiplier); } public Indicators._SuperTrend _SuperTrend(ISeries<double> input , int aTRPeriod, int aTRMultiplier) { return indicator._SuperTrend(input, aTRPeriod, aTRMultiplier); } } } #endregion
Comment