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

My DataSeries do not work properly

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

    My DataSeries do not work properly

    Hello,

    The Bottom and Hat square lines work fine. But the FlipUp and FlipDown can not be referred to in the strategy.

    Thanks

    Code:
    namespace NinjaTrader.Indicator
    {
        /// <summary>
        /// High Low Activator
        /// </summary>
        [Description("High Low Activator")]
        public class AIndiTester : Indicator
        {
            #region Variables
            // Wizard generated variables
                private int period = 5; // Default setting for Period
            // User defined variables (add any user defined variables below)
    		    private double Havg,Lavg;
    		    private int swing,previous_swing;
    		    private DataSeries flipUp,flipDown; 
            #endregion
    
            /// <summary>
            /// This method is used to configure the indicator and is called once before any bar data is loaded.
            /// </summary>
            protected override void Initialize()
            {
                Add(new Plot(Color.FromKnownColor(KnownColor.OrangeRed), PlotStyle.Square, "Hat"));
                Add(new Plot(Color.FromKnownColor(KnownColor.Lime), PlotStyle.Square, "Bottom"));
    			
    			flipUp=new DataSeries(this);
    			flipDown=new DataSeries(this);
                Overlay				= true;
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
                if (CurrentBar< period+1 ) return;
    			
    			Havg=SMA(High,period)[1];
    			Lavg=SMA(Low,period)[1];
    			
    			if(Input[0]<Lavg) swing = -1;
    			if(Input[0]>Havg) swing = 1;
    			
    			if (swing==-1) 
    			{
    				FlipDown.Set(0);
    				Hat.Set(Havg);
    				if (previous_swing!=swing) FlipDown.Set(1);
    			}
    			
    			if (swing==1)  
    			{
    				FlipUp.Set(0);
    				Bottom.Set(Lavg);
    				if (previous_swing!=swing) FlipUp.Set(1);
    			}
    			
    			previous_swing=swing;
    			
            }
    
            #region Properties
            [Browsable(false)]	// this line prevents the data series from being displayed in the indicator properties dialog, do not remove
            [XmlIgnore()]		// this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
            public DataSeries Hat
            {
                get { return Values[0]; }
            }
    
            [Browsable(false)]	// this line prevents the data series from being displayed in the indicator properties dialog, do not remove
            [XmlIgnore()]		// this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
            public DataSeries Bottom
            {
                get { return Values[1]; }
            }
    
            [Description("")]
            [GridCategory("Parameters")]
            public int Period
            {
                get { return period; }
                set { period = Math.Max(1, value); }
            }
    		
    		public DataSeries FlipDown
    		{
    			get {return Values[3];}
    		}
    		
    		public DataSeries FlipUp
    		{
    			get {return Values[2];}
    		}
            #endregion
        }
    }

    #2
    Hello wolfcuring,

    Thank you for your post.

    You would need to expose the DataSeries for the flipUp and flipDown as public just as you did for the Bottom and Hat.

    We also have a reference sample on exposing values that are not plots at the following link: http://ninjatrader.com/support/forum...ead.php?t=4991

    Comment


      #3
      Dear Patrick,

      I copied part of the example code. But when I try to refer to the bool value, it still said FlipUp is not defined.

      Code:
      {
          /// <summary>
          /// High Low Activator
          /// </summary>
          [Description("High Low Activator")]
          public class AIndiTester : Indicator
          {
              #region Variables
              // Wizard generated variables
                  private int period = 5; // Default setting for Period
              // User defined variables (add any user defined variables below)
      		    private double Havg,Lavg, exposedVariable;
      		    private int swing,previous_swing;
      		    private BoolSeries flipUp,flipDown; 
              #endregion
      
              /// <summary>
              /// This method is used to configure the indicator and is called once before any bar data is loaded.
              /// </summary>
              protected override void Initialize()
              {
                  Add(new Plot(Color.FromKnownColor(KnownColor.OrangeRed), PlotStyle.Square, "Hat"));
                  Add(new Plot(Color.FromKnownColor(KnownColor.Lime), PlotStyle.Square, "Bottom"));
      			
      			flipUp=new BoolSeries(this);
      			flipDown=new BoolSeries(this);
                  Overlay				= true;
              }
      
              /// <summary>
              /// Called on each bar update event (incoming tick)
              /// </summary>
              protected override void OnBarUpdate()
              {
                  if (CurrentBar< period+1 ) return;
      			
      			Havg=SMA(High,period)[1];
      			Lavg=SMA(Low,period)[1];
      			
      			if(Input[0]<Lavg) swing = -1;
      			if(Input[0]>Havg) swing = 1;
      			
      			if (swing==-1) 
      			{
      				Hat.Set(Havg);
      				if (previous_swing!=swing) FlipDown.Set(true);
      			}
      			
      			if (swing==1)  
      			{
      				Bottom.Set(Lavg);
      				if (previous_swing!=swing) FlipUp.Set(true);
      			}
      			
      			FlipUp.Set(false);
      			FlipDown.Set(false);
      			previous_swing=swing;
      			exposedVariable = Close[0];
              }
      
              #region Properties
              [Browsable(false)]	// this line prevents the data series from being displayed in the indicator properties dialog, do not remove
              [XmlIgnore()]		// this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
              public DataSeries Hat
              {
                  get { return Values[0]; }
              }
      
              [Browsable(false)]	// this line prevents the data series from being displayed in the indicator properties dialog, do not remove
              [XmlIgnore()]		// this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
              public DataSeries Bottom
              {
                  get { return Values[1]; }
              }
      
              [Description("")]
              [GridCategory("Parameters")]
              public int Period
              {
                  get { return period; }
                  set { period = Math.Max(1, value); }
              }
      		
      		public BoolSeries FlipUp
      		{
      			get {return flipUp;}
      		}
      		
      		public BoolSeries FlipDown
      		{
      			get {return flipDown;}
      		}
      		
      		public double ExposedVariable
              {
      			// We need to call the Update() method to ensure our exposed variable is in up-to-date.
                  get { Update(); return exposedVariable; }
              }
              #endregion
          }
      }

      Comment


        #4
        Hello wolfcuring,

        You would need to set the items with lowercase in the OnBarUpdate as that is how the private Bool Series are set.

        For example:
        Code:
        			flipUp.Set(false);
        			flipDown.Set(false);

        Comment


          #5
          Thank you Patrick, finally it worked.

          Comment


            #6
            Hi Patrick,

            How can know that the bool values within this indicator is accessible? Because, I referred to this bool series in a strategy, but it seems that the value is never true, and no action is executed.

            Comment


              #7
              Hello wolfcuring,

              Thank you for your post.

              Try Printing the values to the output window (Tools > Output) when you run your strategy.

              For example:
              Code:
              Print(MyIndicator().MyBoolSeries[0].ToString());

              Comment


                #8
                Thanks, I will give a try. Actually, there is a logical mistake when I double checked the code of this indicator.

                Comment


                  #9
                  Dear Patrick,

                  I've been testing the indicator these days. And after many tests, I believe the indicator itself is sound, but when referred by other indicators or strategies, unexpected errors emerged. The code now is as follows:

                  Code:
                  // This namespace holds all indicators and is required. Do not change it.
                  namespace NinjaTrader.Indicator
                  {
                      /// <summary>
                      /// 
                      /// </summary>
                      [Description("")]
                      public class BoolHiLow : Indicator
                      {
                          #region Variables
                          // Wizard generated variables
                              private int period = 3; // Default setting for Period
                          // User defined variables (add any user defined variables below)
                  		private double Havg, Lavg, exposedVariable;
                          private int swing, previous_swing;
                          private BoolSeries flipUp, flipDown;
                  		
                  		
                          #endregion
                  
                          /// <summary>
                          /// This method is used to configure the indicator and is called once before any bar data is loaded.
                          /// </summary>
                          protected override void Initialize()
                          {
                              Add(new Plot(Color.FromKnownColor(KnownColor.OrangeRed), PlotStyle.Square, "Hat"));
                              Add(new Plot(Color.FromKnownColor(KnownColor.Lime), PlotStyle.Square, "Bottom"));
                  			
                  			flipUp=new BoolSeries(this);
                  			flipDown=new BoolSeries(this);
                  			
                              Overlay				= true;
                          }
                  
                          /// <summary>
                          /// Called on each bar update event (incoming tick)
                          /// </summary>
                          protected override void OnBarUpdate()
                          {
                              if (CurrentBar < period + 1) return;
                  
                              Havg = SMA(High, period)[1];
                              Lavg = SMA(Low, period)[1];
                  
                              if (Input[0] < Lavg) swing = -1;
                              if (Input[0] > Havg) swing = 1;
                  			
                  			if (swing==-1)
                  			{
                  				Hat.Set(Havg);
                  				if(previous_swing!=swing)
                  				{
                  					flipDown.Set(true);
                  				}
                  				else 
                  				{
                  					flipDown.Set(false);
                  				}
                  			}
                  			
                  			if(swing==1)
                  			{
                  				Bottom.Set(Lavg);
                  				if (previous_swing!=swing) 
                  				{
                  					flipUp.Set(true);
                  				}
                  				else 
                  				{
                  					flipUp.Set(false);
                  				}
                  			}
                  
                  			previous_swing=swing;
                  			exposedVariable=Close[0];
                  			
                          }
                  
                          #region Properties
                          [Browsable(false)]	// this line prevents the data series from being displayed in the indicator properties dialog, do not remove
                          [XmlIgnore()]		// this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
                          public DataSeries Hat
                          {
                              get { return Values[0]; }
                          }
                  
                          [Browsable(false)]	// this line prevents the data series from being displayed in the indicator properties dialog, do not remove
                          [XmlIgnore()]		// this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
                          public DataSeries Bottom
                          {
                              get { return Values[1]; }
                          }
                  
                          [Description("")]
                          [GridCategory("Parameters")]
                          public int Period
                          {
                              get { return period; }
                              set { period = Math.Max(3, value); }
                          }
                  		
                  		public BoolSeries FlipDown
                  		{
                  			get {return flipDown;}
                  		}
                  		
                  		public BoolSeries FlipUp
                  		{
                  			get{return flipUp;}
                  		}
                  		
                  		public double ExposedVariable
                  		{
                  			get{Update();return exposedVariable;}
                  		}
                  		
                          #endregion
                      }
                  }
                  For example, I tested it in another indicator as follows:

                  Code:
                    public class AIndiTester : Indicator
                      {
                          #region Variables
                          // Wizard generated variables
                              private int period = 5; // Default setting for Period
                          // User defined variables (add any user defined variables below)
                  		   
                          #endregion
                  
                          /// <summary>
                          /// This method is used to configure the indicator and is called once before any bar data is loaded.
                          /// </summary>
                          protected override void Initialize()
                          {
                              Add(new Plot(Color.FromKnownColor(KnownColor.OrangeRed), PlotStyle.Square, "Hat"));	
                  			
                  			Overlay=true;
                          }
                  
                          /// <summary>
                          /// Called on each bar update event (incoming tick)
                          /// </summary>
                          protected override void OnBarUpdate()
                          {
                              if (CurrentBar< period+1 ) return;
                  			
                  			if (BoolHiLow(period).FlipDown[0]) 
                  			{
                  				DrawArrowDown("FlipDown",0,High[0],Color.Yellow);
                  				
                  			}
                  			if (BoolHiLow(period).FlipUp[0]) DrawArrowUp("FlipUp",0,Low[0],Color.Blue);
                  			
                  		
                          }
                  
                          #region Properties
                          [Browsable(false)]	// this line prevents the data series from being displayed in the indicator properties dialog, do not remove
                          [XmlIgnore()]		// this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
                          public DataSeries Hat
                          {
                              get { return Values[0]; }
                          }
                  
                          [Description("")]
                          [GridCategory("Parameters")]
                          public int Period
                          {
                              get { return period; }
                              set { period = Math.Max(1, value); }
                          }
                  		
                  		#endregion
                      }
                  }
                  But, the arrow was only drawn once. as the picture shows below.

                  So, I am confused by what is happening.
                  Attached Files

                  Comment


                    #10
                    Hello wolfcuring,

                    Thank you for your follow up.

                    Your Drawing Objects use the same tag, so it is replacing the old object with the new. Try the following in your tags:
                    Code:
                    DrawArrowDown([B]"FlipDown " + CurrentBar[/B],0,High[0],Color.Yellow);
                    DrawArrowUp([B]"FlipUp " + CurrentBar[/B],0,Low[0],Color.Blue);

                    Comment


                      #11
                      Hi Patrick,

                      Now, I have confidence that the indicator is giving right values.

                      But when tested in a multi-time frame strategy, sometimes it works well, while sometimes not as expected.

                      The exit rule is :

                      Code:
                      if (Position.MarketPosition==MarketPosition.Long)
                      { 
                      					
                      	if (BoolHiLow(BarsArray[1],5).FlipDown[0])  ExitLong("FlipDown","KD Up");	
                      }
                      Where BarsArray[1] is 30 minutes. The exits executed when no flipUp or flipDown happened. I checked the BarsArray object and the parameter of period over. They are consistent, and BarsArray[1] is 30 mins, and calculating period is 5.

                      Thank for your effort on this thread.
                      Attached Files

                      Comment


                        #12
                        Hello wolfcuring,

                        Thank you for your response.

                        I am not following your last item. The entries and exits are occurring at the wrong times? Can you provide more details on what is occurring?

                        Comment


                          #13
                          Hi Patrick,

                          As tested in other indicator, the indicator seems to work well. But, in the strategy, where the exit rule is get out the market, when the BoolHiLow in 30 minutes(Primary BarsInProgress object is 5 minutes) flipUp(for a short position) or Down(for a long position). As the pictures in the attachments show, sometimes(not all ), the strategy exits, where there is no flip happened.

                          Comment


                            #14
                            Hello wolfcuring,

                            Thank you for your response.

                            Can you provide the script you are running?

                            You can attach your indicator to your response by going to File > Utilities > Export NinjaScript > Export selected source files > select your Indicator > select the right arrow > Export. The file will be located under (My) Documents\NinjaTrader 7\bin\Custom\ExportNinjaScript.

                            Comment


                              #15
                              Hi Patrick,

                              The code of the strategy is as follows:
                              Last edited by wolfcuring; 06-23-2016, 09:34 PM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by tsantospinto, 04-12-2024, 07:04 PM
                              5 responses
                              67 views
                              0 likes
                              Last Post tsantospinto  
                              Started by cre8able, Today, 03:20 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post cre8able  
                              Started by Fran888, 02-16-2024, 10:48 AM
                              3 responses
                              49 views
                              0 likes
                              Last Post Sam2515
                              by Sam2515
                               
                              Started by martin70, 03-24-2023, 04:58 AM
                              15 responses
                              115 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by The_Sec, Today, 02:29 PM
                              1 response
                              8 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Working...
                              X