Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

"Error on calling 'OnStateChange' method: Out-of-range" Why??

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    "Error on calling 'OnStateChange' method: Out-of-range" Why??

    Hello! I´m coding this strategy:

    Code:
    [SIZE="2"]{
    	public class KLL391v2 : Strategy
    	{
    		private ATR ATR1;
    
    		protected override void OnStateChange()
    		{
    			if (State == State.SetDefaults)
    			{
    				Description									= @"Enter the description for your new custom Strategy here.";
    				Name										= "KLL391v2";
    				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							= 200;
    				// Disable this property for performance gains in Strategy Analyzer optimizations
    				// See the Help Guide for additional information
    				IsInstantiatedOnEachOptimizationIteration	= true;
    				PeriodoATR					= 20;
    				Multipos					= 0.5;
    				Multineg					= -0.5;
    				Multicero					= 0.0;
    			}
    			else if (State == State.Configure)
    			{
    				ATR1				= ATR(Convert.ToInt32(PeriodoATR));
    				SetStopLoss(@"", CalculationMode.Currency, ATR1[0], false);
    			}
    		}
    
    		protected override void OnBarUpdate()
    		{
    			if (CurrentBars[0] < 200)
    			return;
    
    			 // Set 1
    			if (((Open[50]-Low[1]) > (Multipos * ATR1[0]))
    				 && ((Close[25]-Open[32]) > (Multicero * ATR1[0])))
    			{
    				EnterLong(Convert.ToInt32(DefaultQuantity), "");
    			}
    			 // Set 2
    			if (((Open[35]-Open[5]) < (Multineg * ATR1[0]))
    				 && ((High[25]-Close[57]) < (Multicero * ATR1[0])))
    			{
    				EnterShort(Convert.ToInt32(DefaultQuantity), "");
    			}
    			
    		}
    
    		#region Properties
    		[NinjaScriptProperty]
    		[Range(1, int.MaxValue)]
    		[Display(ResourceType = typeof(Custom.Resource), Name="PeriodoATR", Order=1, GroupName="NinjaScriptStrategyParameters")]
    		public int PeriodoATR
    		{ get; set; }
    
    		[NinjaScriptProperty]
    		[Range(-1, double.MaxValue)]
    		[Display(ResourceType = typeof(Custom.Resource), Name="Multipos", Description="Multiplicador positivo", Order=2, GroupName="NinjaScriptStrategyParameters")]
    		public double Multipos
    		{ get; set; }
    
    		[NinjaScriptProperty]
    		[Range(-1, double.MaxValue)]
    		[Display(ResourceType = typeof(Custom.Resource), Name="Multineg", Description="Multiplicador negativo", Order=3, GroupName="NinjaScriptStrategyParameters")]
    		public double Multineg
    		{ get; set; }
    		
    		[NinjaScriptProperty]
    		[Range(-1, double.MaxValue)]
    		[Display(ResourceType = typeof(Custom.Resource), Name="Multicero", Description="Multiplicador cero", Order=3, GroupName="NinjaScriptStrategyParameters")]
    		public double Multicero
    		{ get; set; }
    		#endregion
    
    	}
    }[/SIZE]
    It compiles fine, but when backtesting I get his error message as output:

    Code:
    [SIZE="2"]Strategy 'KLL391v2': Error on calling 'OnStateChange' method: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.[/SIZE]

    I´ve tried changing different things but nothing works. There´s enoguh bars to load, so I´m lost wondering what can be wrong in my simple strategy code.

    Any clues??

    Thank you in advance

    #2
    Hello tomaslolo,

    Thank you for writing in.

    If you comment out,

    SetStopLoss(@"", CalculationMode.Currency, ATR1[0],false);

    The strategy will not produce the error.

    The culprit is passing “ATR1[0]” as a price, which in state.configure does not exist yet.

    I would suggest moving SetStopLoss to,

    Code:
    protected override void OnExecutionUpdate(Cbi.Execution execution, string executionId, double price, int quantity,
    
    Cbi.MarketPosition marketPosition, string orderId, DateTime time)
    
     {
    
    SetStopLoss(@"", CalculationMode.Currency, ATR1[0],false);
    
     }
    Where you’ll paste protected override… right after the last } of onbar update.

    See OnExecutionUpdate section of our helpguide,



    Please let us know if you need further assistance.
    Alan P.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Mindset, 04-21-2026, 06:46 AM
    0 responses
    87 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by M4ndoo, 04-20-2026, 05:21 PM
    0 responses
    132 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by M4ndoo, 04-19-2026, 05:54 PM
    0 responses
    68 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by cmoran13, 04-16-2026, 01:02 PM
    0 responses
    118 views
    0 likes
    Last Post cmoran13  
    Started by PaulMohn, 04-10-2026, 11:11 AM
    0 responses
    67 views
    0 likes
    Last Post PaulMohn  
    Working...
    X