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

How to reference previous value on MTF ?

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

    How to reference previous value on MTF ?

    I have this code:

    Code:
     
    ....
    private DataSeries price_O_2	;
    ...
     protected override void Initialize()
            {
                Add(new Plot(Color.FromKnownColor(KnownColor.Green), PlotStyle.Line, "Bullish_top_line"));
                Add(new Plot(Color.FromKnownColor(KnownColor.Green), PlotStyle.Line, "Bullish_bottom_line"));
                Overlay				= true;
    			
                Add ( PeriodType.Second,30);
    			price_O_2	= new DataSeries(this);
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
    
    
    			// Executed on secondary bar updates only
    			if (BarsInProgress == 1)
    			{
    				price_O_2[0] =Open[0];
    				x1= price_O_2[0];
    				x2= price_O_2[1];
    			}
    			
    			
              	Bullish_top_line.Set(x1);
               	Bullish_bottom_line.Set(x2);
            }
    x1 is ploted correctly, but x2 always gets value 0:




    what is the problem?

    #2
    Hello selnomeria,

    Thanks for your post.

    The data series price_O_2 is created with the same number of slots for data as the primary series has bars.

    It appears your chart bars are faster than your added data series of 30 seconds thus the dataseries price_O_2 will have more slots than there are for the added dataseries meaning that when BarsInProgress == 1 the reference to price_O_2[1] will be pointing to a slot that has no data.

    To help clarify, if the chart bars are 1 second this means that the dataseries price_O_2 will have 30 slots for every 1 bar of the added 30 second data series. This would mean that the previous bar value was actually 30 bars/slots ago in the dataseries price_O_2.

    To resolve you will need to synchronize your dataseries price_O_2 to the added 30-second bars data series. Please see the reference example on how to "Synchronizing a DataSeries object to a secondary time frame" : http://ninjatrader.com/support/forum...ead.php?t=3572

    After synchronizing, the previous bar reference price_O_2[1] will point to the previously stored value.
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      thanks for excellent help. i looked into those files, and partially understand, but was still unable to fix this problem.

      can you just look through quickly and give me hints what is missing?



      (lets say, primary chart is 1 day)
      Last edited by ttodua; 08-17-2017, 06:42 AM.

      Comment


        #4
        Hello selnomeria,

        Thanks for your reply.

        From the reference strategy:

        /* Only need to sync DataSeries objects once. We couldn't do this sync in the Initialize() method because we
        cannot access the BarsArray property there. */
        if (secondarySeries == null)
        {
        /* Syncs another DataSeries object to the secondary bar object.
        We use an arbitrary indicator overloaded with an IDataSeries input to achieve the sync.
        The indicator can be any indicator. The DataSeries will be synced to whatever the
        BarsArray[] is provided.*
        /
        secondarySeries = new DataSeries(SMA(BarsArray[1], 50));
        }

        I've bolded a key part that I do not see in your example. Please don't let the use of the indicator concern you as the sole purpose is to create the data series with the same number of slots/bars as the added timeframe. You can use an SMA with 1 period instead of 50 if that is of concern.
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Maybe now i've understood...
          1) so, the only way to correctly "initialize" dataserie, is to use "any" indicator (actually we dont need any indi value at all, right?) so, it's like a "trick", right?

          2) is there any shorthand, that instead of this:
          Code:
          				price_O_2	= new DataSeries();
          				price_H_2	= new DataSeries(SMA(BarsArray[1], 3));
          				price_L_2	= new DataSeries(SMA(BarsArray[1], 3));
          				price_C_2	= new DataSeries(SMA(BarsArray[1], 3));
          i could use:
          Code:
          				xyz =SMA(BarsArray[1], 3); 
          				price_O_2	= new DataSeries(xyz );
          				price_H_2	= new DataSeries(xyz );
          				price_L_2	= new DataSeries(xyz );
          				price_C_2	= new DataSeries(xyz );
          Last edited by ttodua; 08-17-2017, 07:14 AM.

          Comment


            #6
            Hello selnomeria,

            Thanks for your reply.

            In order for you to accomplish your goal you will need to synchronize your dataseries to the secondary time frame and we have provided directions for doing so. I've not tested what you are showing but you can certainly test it yourself.
            Paul H.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by algospoke, Today, 06:40 PM
            0 responses
            2 views
            0 likes
            Last Post algospoke  
            Started by maybeimnotrader, Today, 05:46 PM
            0 responses
            6 views
            0 likes
            Last Post maybeimnotrader  
            Started by quantismo, Today, 05:13 PM
            0 responses
            6 views
            0 likes
            Last Post quantismo  
            Started by AttiM, 02-14-2024, 05:20 PM
            8 responses
            168 views
            0 likes
            Last Post jeronymite  
            Started by cre8able, Today, 04:22 PM
            0 responses
            8 views
            0 likes
            Last Post cre8able  
            Working...
            X