Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Probably something I am doing wrong

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

    Probably something I am doing wrong

    Building a strategy I get the following error when trying to load:

    Exception has been thrown by the target of an invocation.
    A hosted indicator tried to load additional data. All data must first loaded by the hosting NinjaScript in its configure state.
    This strategy worked in Beta 2 and Beta 1,

    Here is the code:

    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.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 SDY_PnF_Base : Strategy
    	{
    		protected override void OnStateChange()
    		{
    			if (State == State.SetDefaults)
    			{
    				Description					= @"Enter the description for your new custom Strategy here.";
    				Name						= "SDY_PnF_Base";
    				Calculate					= Calculate.OnPriceChange;
    				EntriesPerDirection			= 1;
    				EntryHandling				= EntryHandling.AllEntries;
    				IsExitOnCloseStrategy		= false;
    				ExitOnCloseSeconds			= 30;
    				IsFillLimitOnTouch			= false;
    				MaximumBarsLookBack			= MaximumBarsLookBack.TwoHundredFiftySix;
    				OrderFillResolution			= OrderFillResolution.High;
    				OrderFillResolutionType		= BarsPeriodType.Tick;
    				OrderFillResolutionValue	= 1;
    				Slippage					= 0;
    				StartBehavior				= StartBehavior.AdoptAccountPosition;
    				TimeInForce					= TimeInForce.Gtc;
    				TraceOrders					= false;
    				RealtimeErrorHandling		= RealtimeErrorHandling.StopCancelCloseIgnoreRejects;
    				StopTargetHandling			= StopTargetHandling.PerEntryExecution;
    				BarsRequiredToTrade			= 20;
    				period 						= 5;
    				//AddChartIndicator(SDY_PnF_MaxMin(period));
    				//AddChartIndicator(SDY_PnF_Breakout());
    			}
    			else if (State == State.Configure)
    			{
    			}
    		}
    
    		protected override void OnBarUpdate()
    		{
    			if(CurrentBar >= period+1)return;
    			
    			
    			if(Position.MarketPosition == MarketPosition.Flat)
    			{
    				if((BarsSinceExitExecution(0,"",0) >= 1 || BarsSinceExitExecution(0,"",0) == -1)
    					&& 	Close[0] >= SDY_PnF_Breakout().IHigh[0]
    					&&  Close[0] >= SDY_PnF_MaxMin(period).IHigh[0]
    					&&  SDY_PnF_XO().XO[0] == "X")
    				{
    					EnterLong();
    				}
    				
    				if((BarsSinceExitExecution(0,"",0) >= 1 || BarsSinceExitExecution(0,"",0) == -1)
    					&& 	Close[0] <= SDY_PnF_Breakout().ILow[0]
    					&&  Close[0] <= SDY_PnF_MaxMin(period).ILow[0]
    					&&  SDY_PnF_XO().XO[0] == "O")
    				{
    					EnterShort();
    				}
    				
    				
    			}
    			
    			if(Position.MarketPosition == MarketPosition.Long
    				&& (Close[0] <= SDY_PnF_Breakout().ILow[0] || (SDY_PnF_Count().Count[1] >= 10 && BarsSinceEntryExecution(0,"",0) >= 1)))
    			{
    				ExitLong();
    			}
    			
    			if(Position.MarketPosition == MarketPosition.Short
    				&& (Close[0] >= SDY_PnF_Breakout().IHigh[0] || (SDY_PnF_Count().Count[1] >= 10 && BarsSinceEntryExecution(0,"",0) >= 1)))
    			{
    				ExitShort();
    			}
    		}
    		#region Properties
    		[Range(1, int.MaxValue)]
    		[NinjaScriptProperty]
    		[Display(Name="Period", Order=1, GroupName="Parameters")]
    		public int period
    		{ get; set; }
    		#endregion
    	}
    }
    Whats is going on?

    Sody

    #2
    If your hosted indicator, the one you are calling in the strategy, is loading extra data in its configure state, then you need to do the same thing in the Strategy as well.

    Comment


      #3
      Based on the error, I am assuming SDY_PnF_XO is a multi-series indicator, correct?

      There were some design changes in response to other bugs which prompted the following requirement:

      If your added chart indicator is a multi time frame/instrument, you must also add that same time series/frame to the strategy.

      The "hosting NinjaScript" in the error is the Strategy. Meaning, from your strategy, you need to AddDataSeries() in State.Configure the same series you have in the indicator.

      Documentation is pending to reflect this requirement.
      MatthewNinjaTrader Product Management

      Comment


        #4
        Originally posted by NinjaTrader_Matthew View Post
        Based on the error, I am assuming SDY_PnF_XO is a multi-series indicator, correct?

        There were some design changes in response to other bugs which prompted the following requirement:

        If your added chart indicator is a multi time frame/instrument, you must also add that same time series/frame to the strategy.

        The "hosting NinjaScript" in the error is the Strategy. Meaning, from your strategy, you need to AddDataSeries() in State.Configure the same series you have in the indicator.

        Documentation is pending to reflect this requirement.
        Nope, SDY_PnF_XO is a very simple indicator that returns a string series, see below:

        Code:
        namespace NinjaTrader.NinjaScript.Indicators
        {
        	public class SDY_PnF_XO : Indicator
        	{
        		private Series<string> xo;
        
        		protected override void OnStateChange()
        		{
        			if (State == State.SetDefaults)
        			{
        				Description					= @"Enter the description for your new custom Indicator here.";
        				Name						= "SDY_PnF_XO";
        				Calculate					= Calculate.OnEachTick;
        				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;
        			}
        			else if (State == State.Configure)
        			{
        				xo = new Series<string>(this);
        			}
        		}
        
        		protected override void OnBarUpdate()
        		{
        			 if(CurrentBar == 0)
        				{
        					xo[0] = "Start";
        					return;
        				}
        			if(Open[0] < Close[0])
        				{
        					xo[0] = "X";
        				}
        			else xo[0] = "O";
        				
        			Draw.TextFixed(this,"XO",xo[0],TextPosition.TopLeft);
        		}
        		#region Properties
        
        		[Browsable(false)]
        		[XmlIgnore()]
        		public Series<string> XO
        		{
        			get { return xo; }
        		}
        
        		#endregion
        	}
        }

        Comment


          #5
          What about the other indicators?

          If you're not sure any multi-timeframe indicators, the error would be unexpected, but we would need to know how to reproduce if you can provide all the relevant code the strategy is attempting to load.
          MatthewNinjaTrader Product Management

          Comment


            #6
            Indicators:

            SDY_PnF_Count

            Code:
            namespace NinjaTrader.NinjaScript.Indicators
            {
            	public class SDY_PnF_Count : Indicator
            	{
            		private Series<int> PnF_Count;
            
            		protected override void OnStateChange()
            		{
            			if (State == State.SetDefaults)
            			{
            				Description					= @"Enter the description for your new custom Indicator here.";
            				Name						= "SDY_PnF_Count";
            				Calculate					= Calculate.OnBarClose;
            				IsOverlay					= true;
            				DisplayInDataBox			= true;
            				DrawOnPricePanel			= true;
            				DrawHorizontalGridLines		= true;
            				DrawVerticalGridLines		= true;
            				PaintPriceMarkers			= true;
            				ScaleJustification			= NinjaTrader.Gui.Chart.ScaleJustification.Right;
            				IsSuspendedWhileInactive	= false;
            			}
            			else if (State == State.Configure)
            			{
            				PnF_Count = new Series<int>(this);
            			}
            		}
            
            		protected override void OnBarUpdate()
            		{
            			if(CurrentBar <= 4)return;
            			PnF_Count[0] = (int) Math.Round((((High[0] - Low[0])/TickSize)/base.Bars.BarsPeriod.Value)+1,0);
            			
            			if(Open[0] < Close[0]) Draw.Text(this,"PnF_Count"+CurrentBar.ToString(),PnF_Count[0].ToString(),0,Close[0] + (base.Bars.BarsPeriod.Value*2*TickSize),Brushes.DimGray);
            			if(Open[0] > Close[0]) Draw.Text(this,"PnF_Count"+CurrentBar.ToString(),PnF_Count[0].ToString(),0,Close[0] - (base.Bars.BarsPeriod.Value*TickSize),Brushes.DimGray);	
            
            		}
            		
            		#region Properties
            		
            		[Browsable(false)]
            		[XmlIgnore()]
            		public Series<int> Count
            		{
            			get { return PnF_Count; }
            		}
            
            		#endregion
            	}
            SDY_PnF_XO:

            Code:
            namespace NinjaTrader.NinjaScript.Indicators
            {
            	public class SDY_PnF_XO : Indicator
            	{
            		private Series<string> xo;
            
            		protected override void OnStateChange()
            		{
            			if (State == State.SetDefaults)
            			{
            				Description					= @"Enter the description for your new custom Indicator here.";
            				Name						= "SDY_PnF_XO";
            				Calculate					= Calculate.OnEachTick;
            				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;
            			}
            			else if (State == State.Configure)
            			{
            				xo = new Series<string>(this);
            			}
            		}
            
            		protected override void OnBarUpdate()
            		{
            			 if(CurrentBar == 0)
            				{
            					xo[0] = "Start";
            					return;
            				}
            			if(Open[0] < Close[0])
            				{
            					xo[0] = "X";
            				}
            			else xo[0] = "O";
            				
            			Draw.TextFixed(this,"XO",xo[0],TextPosition.TopLeft);
            		}
            		#region Properties
            
            		[Browsable(false)]
            		[XmlIgnore()]
            		public Series<string> XO
            		{
            			get { return xo; }
            		}
            
            		#endregion
            SDY_PnF_MaxMin

            Code:
            namespace NinjaTrader.NinjaScript.Indicators
            {
            	public class SDY_PnF_MaxMin : Indicator
            	{
            		private Series<double> iHigh;
            		private Series<double> iLow;
            
            		protected override void OnStateChange()
            		{
            			if (State == State.SetDefaults)
            			{
            				Description					= @"";
            				Name						= "SDY_PnF_MaxMin";
            				Calculate					= Calculate.OnEachTick;
            				IsOverlay					= true;
            				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;
            				Period					= 10;
            			}
            			else if (State == State.Configure)
            			{
            				iHigh = new Series<double>(this);
            				iLow = new Series<double>(this);
            			}
            		}
            
            		protected override void OnBarUpdate()
            		{
            			if(CurrentBar <= Period)return;
            			iHigh[0] = MAX(High,Period)[1] + base.Bars.BarsPeriod.Value*TickSize;
            			iLow[0] = MIN(Low,Period)[1] - base.Bars.BarsPeriod.Value*TickSize;
            
            			//Draw the lines
            			Draw.Line(this,"iHigh",Period,iHigh[0],0,iHigh[0] ,Brushes.Green);
            			Draw.Line(this,"iLow",Period,iLow[0],0,iLow[0],Brushes.Red);
            			
            			//Draw the Text
            			Draw.Text(this,"hText",iHigh[0].ToString(),-2,iHigh[0] + (0.5* (base.Bars.BarsPeriod.Value*TickSize)),Brushes.DimGray);
            			Draw.Text(this,"lText",iLow[0].ToString(),-2,iLow[0] + (0.5* (base.Bars.BarsPeriod.Value*TickSize)),Brushes.DimGray);
            			
            			
            		}
            
            		#region Properties
            		[Range(1, int.MaxValue)]
            		[NinjaScriptProperty]
            		[Display(Name="Period", Order=1, GroupName="Parameters")]
            		public int Period
            		{ get; set; }
            
            		[Browsable(false)]
            		[XmlIgnore()]
            		public Series<double> IHigh 
            		{
            			get { return iHigh; }
            		}
            		
            		[Browsable(false)]
            		[XmlIgnore()]
            		public Series<double> ILow 
            		{
            			get { return iLow; }
            		}
            
            		#endregion
            SDY_PnF_Breakout

            Code:
            namespace NinjaTrader.NinjaScript.Indicators
            {
            	public class SDY_PnF_Breakout : Indicator
            	{
            		private Series<double> iHigh;
            		private Series<double> iLow;
            
            		protected override void OnStateChange()
            		{
            			if (State == State.SetDefaults)
            			{
            				Description					= @"";
            				Name						= "SDY_PnF_Breakout";
            				Calculate					= Calculate.OnEachTick;
            				IsOverlay					= true;
            				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;
            			}
            			else if (State == State.Configure)
            			{
            				iHigh = new Series<double>(this);
            				iLow = new Series<double>(this);
            			}
            		}
            
            		protected override void OnBarUpdate()
            		{
            			if(CurrentBar < 2) return;
            			if (IsFirstTickOfBar)
            			{
            				if(SDY_PnF_XO().XO[0] == "X")
            				{
            					iHigh[0] = SDY_PnF_MaxMin(2).IHigh[0];
            					iLow[0] = SDY_PnF_MaxMin(1).ILow[0];
            				}
            				
            				if(SDY_PnF_XO().XO[0] == "O")
            				{
            					iHigh[0] = SDY_PnF_MaxMin(1).IHigh[0];
            					iLow[0] = SDY_PnF_MaxMin(2).ILow[0];
            				}
            				
            				Draw.Line(this,"iHigh",2,iHigh[0],0,iHigh[0] ,Brushes.Green);
            				Draw.Line(this,"iLow",2,iLow[0],0,iLow[0],Brushes.Red);
            			}
            		}
            		
            		#region
            		[Browsable(false)]
            		[XmlIgnore()]
            		public Series<double> IHigh 
            		{
            			get { return iHigh; }
            		}
            		
            		[Browsable(false)]
            		[XmlIgnore()]
            		public Series<double> ILow 
            		{
            			get { return iLow; }
            		}
            		#endregion

            Comment


              #7
              Originally posted by NinjaTrader_Matthew View Post
              What about the other indicators?

              If you're not sure any multi-timeframe indicators, the error would be unexpected, but we would need to know how to reproduce if you can provide all the relevant code the strategy is attempting to load.
              My last post I listed all the indicators, The only thing that even closely represents a multi-timeframe situation is:

              Code:
              BarsSinceEntryExecution(0,"",0)
              because I am referencing barsArray 0, could this be causing it.. I don't see how how, but maybe...

              Sody

              Comment


                #8
                If it helps

                Here is the strategy if it helps

                Sody
                Attached Files
                Last edited by SodyTexas; 07-31-2015, 11:37 AM.

                Comment


                  #9
                  Thanks, I could reproduce with the snippets you posted. Will get back to you shortly.
                  MatthewNinjaTrader Product Management

                  Comment


                    #10
                    Thanks Matthew

                    Comment


                      #11
                      I have seen this error a few times as well. I think I resolved it by correcting a new Exception that occurred in an indicator. I will keep a closer eye on this one myself as I have seen it a few times.

                      Comment


                        #12
                        SodyTexas, how often are you seeing it? Every time? I was only able to reproduce a handful of time and having trouble isolating. Please let me know if you have some specific settings (time period, bars, strategy settings, etc) that your'e using if you can reproduce frequently.
                        MatthewNinjaTrader Product Management

                        Comment


                          #13
                          I built 4 strategies in Beta 1, all four of those are receiving the issue. But This strategy (SDY_PfF_Base) I built today under Beta 3.

                          Comment


                            #14
                            Sorry - I mean with the strategy you provided me, do you get the error message from post #1 every single time you start it? If so, exactly what settings are you using for the strategy?

                            - Instrument
                            - BarsType
                            - Period
                            - Any non-default strategy settings.

                            Any info you can provide to help me reproduce this our development environment should help us figure out what you and NJA_MC are running into
                            MatthewNinjaTrader Product Management

                            Comment


                              #15
                              Originally posted by NinjaTrader_Matthew View Post
                              Sorry - I mean with the strategy you provided me, do you get the error message from post #1 every single time you start it? If so, exactly what settings are you using for the strategy?

                              - Instrument
                              - BarsType
                              - Period
                              - Any non-default strategy settings.

                              Any info you can provide to help me reproduce this our development environment should help us figure out what you and NJA_MC are running into
                              - Number of contracts all replay (##-##)
                              - Mainly Point and figure
                              - Also, some 15 min bar
                              - strategy settings all done by code, full list is in the code provided in past posts

                              Hope this helps!

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              673 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              379 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              111 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              577 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              582 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X