Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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 inanazsocial, Today, 01:15 AM
    1 response
    5 views
    0 likes
    Last Post NinjaTrader_Jason  
    Started by rocketman7, Today, 02:12 AM
    0 responses
    5 views
    0 likes
    Last Post rocketman7  
    Started by dustydbayer, Today, 01:59 AM
    0 responses
    1 view
    0 likes
    Last Post dustydbayer  
    Started by trilliantrader, 04-18-2024, 08:16 AM
    5 responses
    22 views
    0 likes
    Last Post trilliantrader  
    Started by Davidtowleii, Today, 12:15 AM
    0 responses
    3 views
    0 likes
    Last Post Davidtowleii  
    Working...
    X