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

Setting value of indicator DataSeries member from a multi-timeframe strategy

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

    Setting value of indicator DataSeries member from a multi-timeframe strategy

    Strategy has two time periods:
    • 5-min (BarsInProgress == 0)
    • 1-min (BarsInProgress == 1)


    One indicator is added to the strategy, and it has a DataSeries as a member.

    In OnBarUpdate() of the strategy, the day-of-the-month is calculated, and the indicator’s DataSeries member set to this value.

    The value should remain constant during a given day, i.e. on July 1st, it should be "1". On July 2nd, it should be "2"....

    All works as expected for historical data.

    But for the first real-time bar, the value of the indicator’s DataSeries member is incorrect. It changes, but should not.

    Please see screenshot.
    Click image for larger version

Name:	MultiTimeframeFirstRealtimeBarError.png
Views:	1
Size:	198.4 KB
ID:	902961

    Any ideas what is happening or how to correct?

    So that you can verify this behaviour yourself, here is the code:

    STRATEGY:

    Code:
    [standard Using declarations]
    namespace NinjaTrader.Strategy
    {
        public class JHanTest : Strategy
        {
            protected override void Initialize()
            {
    			Add(jHanTestIndy());
    			Add(PeriodType.Minute, 1);
                CalculateOnBarClose = true;
            }
    
            protected override void OnBarUpdate()
            {
    			if (BarsInProgress != 0)
    				return;
    			
    			this.jHanTestIndy().Ds[0] = Time[0].Day;
    			this.jHanTestIndy().Update();
            }
        }
    }
    INDICATOR:
    Code:
    [standard Using declarations]
    namespace NinjaTrader.Indicator
    {
        public class jHanTestIndy : Indicator
        {
            #region Variables
    			private DataSeries ds = null; 
            #endregion
    
            protected override void Initialize()
            {
                Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
                Overlay				= false;
    			this.ds = new DataSeries(this);
            }
    
            protected override void OnBarUpdate()
            {
    			if (Ds[0] != null)
    				Plot0[0] = Ds[0];
    			else 
    				Plot0.Reset();
            }
    
            #region Properties
            [Browsable(false)]	
            [XmlIgnore()]		
    		public DataSeries Plot0
            {
                get { return Values[0]; }
            }
    
            [Browsable(false)]	
            [XmlIgnore()]		
    		public DataSeries Ds
            {
                get { return this.ds; }
    			set { this.ds = value; }
            }
            #endregion
        }
    }
    NB - The problem seems to be related to multi-timeframeness; if you remove the additional timeperiod, the indicator works as expected.

    #2
    Hello AnotherTrader,

    Thank you for your inquiry.

    If you are planning to use that second Bars object for something in the future, you should re-code your strategy's logic like so:
    Code:
    if (BarsInProgress == 0)
    {
         this.jHanTestIndy().Ds[0] = Times[0][0].Day;
         this.jHanTestIndy().Update();
    }
    else if (BarsInProgress == 1)
    {
         return;
    }
    The indicator will display the correct value when coded this way.
    Zachary G.NinjaTrader Customer Service

    Comment


      #3
      Thanks for your message.

      I changed the code, but it is still happening. That does not fix it.

      Here is a screenshot ...
      Click image for larger version

Name:	MultiTimeframeFirstRealtimeBarError.Still.png
Views:	1
Size:	167.5 KB
ID:	873957

      Here is the code as it currently stands ...

      Strategy:
      Code:
      [standard Using directives]
      namespace NinjaTrader.Strategy
      {
          public class JHanTest : Strategy
          {
              protected override void Initialize()
              {
      			Add(jHanTestIndy());
      			Add(PeriodType.Minute, 1);
                  CalculateOnBarClose = true;
              }
      
              protected override void OnBarUpdate()
              {
      			if (BarsInProgress == 0)
      			{
      				this.jHanTestIndy().Ds[0] = Times[0][0].Day;
      				this.jHanTestIndy().Update();
      			}
      			else if (BarsInProgress == 1)
      			{
      				return;
      			}
              }
          }
      }

      Indicator:
      Code:
      [standard Using directives]
      namespace NinjaTrader.Indicator
      {
          public class jHanTestIndy : Indicator
          {
              #region Variables
      			private DataSeries ds = null; 
              #endregion
      
              protected override void Initialize()
              {
                  Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
                  Overlay				= false;
      			this.ds = new DataSeries(this);
              }
      
              protected override void OnBarUpdate()
              {
      			if (Ds[0] != null)
      				Plot0[0] = Ds[0];
      			else 
      				Plot0.Reset();
              }
      
              #region Properties
              [Browsable(false)]	
              [XmlIgnore()]		
      		public DataSeries Plot0
              {
                  get { return Values[0]; }
              }
      
              [Browsable(false)]	
              [XmlIgnore()]		
      		public DataSeries Ds
              {
                  get { return this.ds; }
      			set { this.ds = value; }
              }
              #endregion
          }
      }
      Have you tried it?

      The problem occurs at the close of the first real-time bar. All is fine for historical bars ...

      Comment


        #4
        Hello AnotherTrader,

        I was able to reproduce the issue on my end.

        This is likely due to the passing of values from your strategy to your indicator and the Update() method you are using. I will be looking into this further for you, but it may take some time for me to return with some findings.
        Zachary G.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_ZacharyG View Post
          Hello AnotherTrader,

          I was able to reproduce the issue on my end.

          This is likely due to the passing of values from your strategy to your indicator and the Update() method you are using. I will be looking into this further for you, but it may take some time for me to return with some findings.
          Hi, Zachary

          Thanks for your message. Any sense of when you might have a response or some feedback?

          Comment


            #6
            Originally posted by AnotherTrader View Post
            Hi, Zachary

            Thanks for your message. Any sense of when you might have a response or some feedback?
            Your update is happening after the indicator is plotted. You are too late.

            Comment


              #7
              Originally posted by AnotherTrader View Post
              Hi, Zachary

              Thanks for your message. Any sense of when you might have a response or some feedback?
              You also do not have enough bars loaded in history for it to be another value.

              Try loading 5 full days. It should be the previous day's value.

              Comment


                #8
                Originally posted by AnotherTrader View Post
                Hi, Zachary

                Thanks for your message. Any sense of when you might have a response or some feedback?
                Use an Integer and not the DataSeries to communicate between the two here.

                The DataSeries is being advanced BEFORE your Set/Assignment & Update.

                The Plot has already been assigned the default value that NT uses (and it looks like it is coming from the back of the, bar 256 if it exists, 0 if it hasn't been set as you've seen). If you up it to 5 days like I suggested, you'll see them rolling over. If you make it to Infinite look back - you'll see 0's again.

                I've tested the int being passed/assigned, and it works - because there is no advancing of the DataSeries to change it.

                As to why historical is different than "live" or market replay - NT would have to dig into their main loop processing.

                Comment


                  #9
                  Hi, Sledge

                  I am very grateful to you for taking the time to look at the issue and post suggestions.

                  Originally posted by sledge View Post
                  You also do not have enough bars loaded in history for it to be another value.

                  Try loading 5 full days. It should be the previous day's value.
                  The issue does not relate to the number of days loaded.

                  Herebelow I have re-run the code on two charts, both with approximately 20 trading-days of data.

                  Click image for larger version

Name:	20_trading_days.png
Views:	1
Size:	172.7 KB
ID:	873963

                  Click image for larger version

Name:	20_trading_days_ES.png
Views:	1
Size:	159.3 KB
ID:	873964

                  The issue still occurs; namely that upon the close of the first real-time bar, the dataseries value reverts to the value it held in a previous day rather than the value it is set to hold for the current day.

                  So, the issue does not relate to the number of days of data ...
                  Last edited by AnotherTrader; 07-07-2015, 03:03 AM.

                  Comment


                    #10
                    Hi, Sledge

                    Again, I am very grateful that you have taken the time to respond with suggestions ...

                    Originally posted by sledge View Post
                    Your update is happening after the indicator is plotted. You are too late.
                    This only appears to be so in a multi-timeframe case; all works as it should if the strategy is just a single timeframe.

                    To show this, I added Print() statements to both the strategy and indicator (see the code below) to compare what happens for historical data versus real-time data.

                    In the single-timeframe case, the indicator's dataseries holds the value it is expected to hold at the close of the first real-time bar. The Output window shows that first the strategy's OnBarUpdate() method is evalued, and next the indicator's OnBarUpdate() method is evalued. And this happens both for historical and real-time data.

                    But in the multi-timeframe case, the dataseries value holds the wrong value at the close of the first real-time bar. The Output window shows that the sequence for evaluating strategy and indicator is different for historical and real-time: for real-time data in the multi-timeframe case, the indicator is evaluated first. And hence (as you suggest) the dataseries does not yet reflect the value pushed to it later from the strategy ...

                    You can see all this from the Output traces shown below...

                    Click image for larger version

Name:	Strat_indy_sequence_when_strat_is_single_timeframe.png
Views:	1
Size:	314.5 KB
ID:	873965

                    Click image for larger version

Name:	Strat_indy_sequence_when_strat_is_multi_timeframe.png
Views:	1
Size:	325.6 KB
ID:	873966

                    Here is the code as modified (with added Print() statements):

                    STRATEGY
                    Code:
                    standard Using declarations
                    namespace NinjaTrader.Strategy
                    {
                        public class JHanTest : Strategy
                        {
                            protected override void Initialize()
                            {
                    			Add(jHanTestIndy());
                    			//Add(PeriodType.Minute, 1);
                                CalculateOnBarClose = true;
                            }
                    
                            protected override void OnBarUpdate()
                            {
                    			if (BarsInProgress == 0)
                    			{
                    				this.jHanTestIndy().Ds[0] = Times[0][0].Day;
                    				[B]if (this.Historical) Print(CurrentBar.ToString() + " Strat (historical): Update()");[/B]
                    				[B]if (!this.Historical) Print(CurrentBar.ToString() + " Strat (real-time ): Update()");[/B]
                    				this.jHanTestIndy().Update();
                    			}
                    			else if (BarsInProgress == 1)
                    			{
                    				return;
                    			}
                            }
                        }
                    }
                    INDICATOR
                    Code:
                    standard Using declarations
                    namespace NinjaTrader.Indicator
                    {
                        public class jHanTestIndy : Indicator
                        {
                            #region Variables
                    			private DataSeries ds = null; 
                            #endregion
                    
                            protected override void Initialize()
                            {
                                Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
                                Overlay				= false;
                    			this.ds = new DataSeries(this);
                            }
                    
                            protected override void OnBarUpdate()
                            {
                    			if (Ds[0] != null)
                    				Plot0[0] = Ds[0];
                    			else 
                    				Plot0.Reset();
                    			
                    			[B]if (this.Historical) Print(CurrentBar.ToString() + " Indy (historical): Ds[0] - " + Ds[0].ToString());[/B]
                    			[B]if (!this.Historical) Print(CurrentBar.ToString() + " Indy (real-time ): Ds[0] - " + Ds[0].ToString());[/B]
                            }
                    
                            #region Properties
                            [Browsable(false)]	
                            [XmlIgnore()]		
                    		public DataSeries Plot0
                            {
                                get { return Values[0];}
                            }
                    
                            [Browsable(false)]	
                            [XmlIgnore()]		
                    		public DataSeries Ds
                            {
                                get { return this.ds; }
                    			set { this.ds = value; }
                            }
                            #endregion
                        }
                    }
                    Last edited by AnotherTrader; 07-07-2015, 04:10 AM.

                    Comment


                      #11
                      Originally posted by sledge View Post
                      Use an Integer and not the DataSeries to communicate between the two here.
                      ...

                      I've tested the int being passed/assigned, and it works - because there is no advancing of the DataSeries to change it.
                      ...
                      Yes, that seems to have done it.

                      Replace indicator's DataSeries member by an int member... and Bob's your uncle!

                      Many thanks!

                      Click image for larger version

Name:	Replace_DataSeries_with_Int.png
Views:	1
Size:	307.2 KB
ID:	873967


                      STRATEGY
                      Code:
                      standard Using declarations
                      namespace NinjaTrader.Strategy
                      {
                          public class JHanTest : Strategy
                          {
                              protected override void Initialize()
                              {
                      			Add(jHanTestIndy());
                      			Add(PeriodType.Minute, 1);
                                  CalculateOnBarClose = true;
                              }
                      
                              protected override void OnBarUpdate()
                              {
                      			if (BarsInProgress == 0)
                      			{
                      				//this.jHanTestIndy().Ds[0] = Times[0][0].Day;
                      				this.jHanTestIndy().StratInt = Times[0][0].Day;
                      				if (this.Historical) Print(CurrentBar.ToString() + " Strat (historical): Update()");
                      				if (!this.Historical) Print(CurrentBar.ToString() + " Strat (real-time ): Update()");
                      				this.jHanTestIndy().Update();
                      			}
                      			else if (BarsInProgress == 1)
                      			{
                      				return;
                      			}
                              }
                          }
                      }

                      INDICATOR
                      Code:
                      standard Using declarations
                      namespace NinjaTrader.Indicator
                      {
                          public class jHanTestIndy : Indicator
                          {
                              #region Variables
                      			//private DataSeries ds = null; 
                      			private int stratInt = 0;
                              #endregion
                      
                              protected override void Initialize()
                              {
                                  Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
                                  Overlay				= false;
                      			//this.ds = new DataSeries(this);
                              }
                      
                              protected override void OnBarUpdate()
                              {
                      			if (this.StratInt != 0/*Ds[0] != null*/)
                      				Plot0[0] = this.StratInt /*Ds[0]*/;
                      			else 
                      				Plot0.Reset();
                      			
                      			if (this.Historical) Print(CurrentBar.ToString() + " Indy (historical):  - " + this.StratInt.ToString()/*Ds[0].ToString()*/);
                      			if (!this.Historical) Print(CurrentBar.ToString() + " Indy (real-time ):  - " + this.StratInt.ToString()/*Ds[0].ToString()*/);
                              }
                      
                              #region Properties
                              [Browsable(false)]	
                              [XmlIgnore()]		
                      		public DataSeries Plot0
                              {
                                  get { return Values[0];}
                              }
                      
                              /*[Browsable(false)]	
                              [XmlIgnore()]		
                      		public DataSeries Ds
                              {
                                  get { return this.ds; }
                      			set { this.ds = value; }
                              }*/
                      		
                      		[Browsable(false)]	
                              [XmlIgnore()]		
                      		public int StratInt
                              {
                                  get { return this.stratInt; }
                      			set { this.stratInt = Math.Max(0, value); }
                              }
                              #endregion
                          }
                      }

                      Comment


                        #12
                        Originally posted by AnotherTrader View Post


                        The issue does not relate to the number of days loaded.

                        Herebelow I have re-run the code on two charts, both with approximately 20 trading-days of

                        The issue still occurs; namely that upon the close of the first real-time bar, the dataseries value reverts to the value it held in a previous day rather than the value it is set to hold for the current day.

                        So, the issue does not relate to the number of days of data ...
                        But it's no longer zero

                        It looks like NT defaults the whole memory area to 0 for int, and after 256 of them (default) it loops back over and
                        Ends up with the previous value for that location in memory.

                        Comment


                          #13
                          Originally posted by AnotherTrader View Post



                          This only appears to be so in a multi-timeframe case; all works as it should if the strategy is just a single timeframe.
                          There might be a case to make to NT about this difference with historical and real time going forward.

                          But I doubt they will want to change this so late in the game. Who knows how many other people have a work around or expect this behavior.

                          There might be a previous post here about this or something similar.

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by jeronymite, 04-12-2024, 04:26 PM
                          3 responses
                          40 views
                          0 likes
                          Last Post jeronymite  
                          Started by bill2023, Today, 08:51 AM
                          2 responses
                          15 views
                          0 likes
                          Last Post bill2023  
                          Started by sidlercom80, 10-28-2023, 08:49 AM
                          167 responses
                          2,260 views
                          0 likes
                          Last Post jeronymite  
                          Started by warreng86, 11-10-2020, 02:04 PM
                          7 responses
                          1,362 views
                          0 likes
                          Last Post NinjaTrader_Manfred  
                          Started by Perr0Grande, Today, 08:16 PM
                          0 responses
                          5 views
                          0 likes
                          Last Post Perr0Grande  
                          Working...
                          X