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 can I create two instances of the same secondary data series?

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

    how can I create two instances of the same secondary data series?

    hi,

    So I have a multiple data series strategy. I added a secondary and third data series. And yes they are the same data series.
    Code:
    AddDataSeries(BarsPeriodType.Minute, 30);
    AddDataSeries(BarsPeriodType.Minute, 30);
    Whenever I process data of the secondary data, the logic for the third data series get updated as well, which is NOT what I want. So how can I separate these series into two different instances?

    #2
    You only need 1

    explain what it is you are trying to do with the 2nd data set?
    apply a different indicator to it?

    Comment


      #3
      Originally posted by affilife View Post
      Whenever I process data of the secondary data, the logic for the third data series get updated as well, which is NOT what I want. So how can I separate these series into two different instances?
      Did you try using BarsInProgress?

      Code:
      if (BarsInProgress == 0)      // <-- primary data series on chart
          { ... }
      else if (BarsInProgress == 1) // <-- 1st secondary data series
          { ... }
      else if (BarsInProgress == 2) // <-- 2nd secondary data series
          { ... }

      Comment


        #4
        Originally posted by affilife View Post
        hi,

        So I have a multiple data series strategy. I added a secondary and third data series. And yes they are the same data series.
        Code:
        AddDataSeries(BarsPeriodType.Minute, 30);
        AddDataSeries(BarsPeriodType.Minute, 30);
        Whenever I process data of the secondary data, the logic for the third data series get updated as well, which is NOT what I want. So how can I separate these series into two different instances?
        Thank you for your post.

        "Whenever I process data of the secondary data, the logic for the third data series get updated as well, which is NOT what I want."
        What do you mean by this? What is the third data series and what logic are you using that shows it gets updated as well? OnBarUpdate() may only be called by one data series at a time; you could print BarsInProgress to see which bars object is calling OnBarUpdate() each time it is processed:


        For a general overview of items to keep in mind with multi-series scripts:


        Thank you for using NinjaTrader.
        Emily C.NinjaTrader Customer Service

        Comment


          #5
          Yes, I have applied BarsInProgress == 2 separately as well. And it still gets updated from the other dataset.
          Let me try to explain what I do.

          Add 30min data series. This is BarsInProgress == 1. Called it series 1
          Add 30min data series again. This is BarsInProgress == 2. Called it series 2
          Add 1 day data series. This is BarsInProgress == 3 . Called it series 3

          I have a custom indicator.
          StepA: Apply the custom indicator on the data series 1 when a condition is met on dataseries 3. Process some logic on the indicator output. Get my data. I don't want this data to be updated unless the condition is met on dataseries 3.
          StepB: Apply the same custom indicator on data series 2. This will get updated every 30min when BarsInProgress == 2 and IsFirstTickOfBar is true.

          However, everytime StepB get updated, StepA get updated as well because it's using the same indicator. If I remove StepB from my strategy, then StepA get updated properly only when the condition is met.

          So I think I need to have two different instances of the same data series to prevent StepA being updated involuntarily. (And yes, I do have different instances of my indicator as follows)
          Code:
          private AaaCustomIndi myRecentIndiData;
          private AaaCustomIndi myStaticIndiData;​
          Edit:
          I think it will be a hack if I create another copy of the custom indicator and name it differently. So that it won't get updated involuntarily. So I want to see if there is a way to get StepA stay put when StepB get updated
          Last edited by affilife; 02-16-2024, 02:36 PM.

          Comment


            #6
            Hello affilife,

            Thank you for your reply.

            StepA: Apply the custom indicator on the data series 1 when a condition is met on dataseries 3. Process some logic on the indicator output. Get my data. I don't want this data to be updated unless the condition is met on dataseries 3.
            StepB: Apply the same custom indicator on data series 2. This will get updated every 30min when BarsInProgress == 2 and IsFirstTickOfBar is true.​
            What are the conditions in question? You should be able to consolidate these to use the same 30 minute data series; one condition can utilize data from the 1 day series and process logic on the 30 minute series. What exactly do you mean "I don't want this data to be updated unless the condition is met on dataseries 3?" Sharing the conditions in question would help to clarify this so I can assist you more accurately.
            Step B should be possible by checking if the BarsInProgress is the 30 minute series and if IsFirstTickOfBar is true.

            For additional insight on which bar data is known/updated while processing multiple series on bar close vs. on each tick/on price change, please see the section "How Bars Data is Referenced​" on this page:


            I look forward to your clarification.
            Emily C.NinjaTrader Customer Service

            Comment


              #7
              The condition is very simple. When (BarsInProgress == 3 and IsFirstTickOfBar is true), then get data from the indicator applied on 30min series. This is a static data, meaning it should not be updated every 30min although it's applied on 30min dataseries. But then when StepB runs, this static data get updated from stepA. How can I prevent that?

              Edit: again to solve this, I think I need to have two different instances of the same dataseries. If so, how do I create 2 instances of the same data series?
              Last edited by affilife; 02-16-2024, 03:00 PM.

              Comment


                #8
                Originally posted by affilife View Post
                The condition is very simple. When (BarsInProgress == 3 and IsFirstTickOfBar is true), then get data from the indicator applied on 30min series. This is a static data, meaning it should not be updated every 30min although it's applied on 30min dataseries. But then when StepB runs, this static data get updated from stepA. How can I prevent that?
                The indicator will be calculated based on the strategy's Calculate property since the strategy is the parent/host script. If the strategy is calculating On Price Change or On Each Tick, the indicator will be updated intrabar as well rather than being updated On Bar Close. With that said, you could create one instance of the indicator and then you could create a custom Series<double> object that is only updated on the first tick of each bar with the indicator's Close value. For example:

                Code:
                private AaaCustomIndi myRecentIndiData;
                private Series<double> myStaticIndiDataSeries;
                
                protected override void OnStateChange()
                {
                    if (State == State.Configure)
                    {
                        // this added series will be BarsInProgress 1
                        AddDataSeries(BarsPeriodType.Minute, 30);
                    }
                    else if (State == State.DataLoaded)
                    {
                        // "BarsArray[1]" refers to the first data series added to the script with AddDataSeries.
                        myStaticIndiDataSeries= new Series<double>(BarsArray[1]);
                    }
                }​
                
                protected override void OnBarUpdate()
                {
                // if the BarsInProgress calling OBU is the added 30 minute series and it is the first tick of the bar, update our custom data series
                if (BarsInProgress == 1 && IsFirstTickOfBar)
                    myStaticIndiDataSeries[0] = Close[0];
                }​
                In this snippet, the myStaticIndiDataSeries value for the current bar is only updated once per 30-minute bar when the first tick of the bar is true. I believe this is what you are looking to achieve.

                Please let us know if we may be of further assistance.
                Emily C.NinjaTrader Customer Service

                Comment


                  #9
                  Thanks Emily. I think this does the trick. However, one question. You have
                  Code:
                  protected override void OnBarUpdate()
                  {
                  // if the BarsInProgress calling OBU is the added 30 minute series and it is the first tick of the bar, update our custom data series
                  if (BarsInProgress == 1 && IsFirstTickOfBar)
                  myStaticIndiDataSeries[0] = Close[0];
                  }​​
                  instead, is that supposed to be ?
                  Code:
                  myStaticIndiDataSeries[1] = Close[1];
                  to get the Static dataseries updated properly as this is a Calculate.OnEachTick strategy?

                  Comment


                    #10
                    Originally posted by affilife View Post
                    Thanks Emily. I think this does the trick. However, one question. You have
                    Code:
                    protected override void OnBarUpdate()
                    {
                    // if the BarsInProgress calling OBU is the added 30 minute series and it is the first tick of the bar, update our custom data series
                    if (BarsInProgress == 1 && IsFirstTickOfBar)
                    myStaticIndiDataSeries[0] = Close[0];
                    }​​
                    instead, is that supposed to be ?
                    Code:
                    myStaticIndiDataSeries[1] = Close[1];
                    to get the Static dataseries updated properly as this is a Calculate.OnEachTick strategy?
                    Yes; you are correct and thank you for pointing that out. Also, my example updates the custom series to equal the Close price. I forgot to modify that to equal the indicator value. Here is an updated snippet:

                    Code:
                    private AaaCustomIndi myRecentIndiData;
                    private Series<double> myStaticIndiDataSeries;
                    
                    protected override void OnStateChange()
                    {
                    if (State == State.Configure)
                    {
                    // this added series will be BarsInProgress 1
                    AddDataSeries(BarsPeriodType.Minute, 30);
                    }
                    else if (State == State.DataLoaded)
                    {
                    // "BarsArray[1]" refers to the first data series added to the script with AddDataSeries.
                    myStaticIndiDataSeries= new Series<double>(BarsArray[1]);
                    myRecentIndiData = AaaCustomIndi();
                    }
                    }​
                    
                    protected override void OnBarUpdate()
                    {
                    // if the BarsInProgress calling OBU is the added 30 minute series and it is the first tick of the bar, update our custom data series so the value for 1 bar ago is equal to the custom indicator value from 1 bar ago
                    if (BarsInProgress == 1 && IsFirstTickOfBar)
                    myStaticIndiDataSeries[1] = myRecentIndiData[1];
                    }​​
                    Please let me know if I may be of further assistance.
                    Emily C.NinjaTrader Customer Service

                    Comment


                      #11
                      Hi Emily,

                      Just want to confirm. If I add data series and assign it to Series<double>
                      Code:
                      protected override void OnStateChange()
                      {
                      if (State == State.Configure)
                      {
                      // this added series will be BarsInProgress 1
                      AddDataSeries(BarsPeriodType.Minute, 30);
                      }
                      else if (State == State.DataLoaded)
                      {
                         // "BarsArray[1]" refers to the first data series added to the script with AddDataSeries.
                         myDataSeries= new Series<double>(BarsArray[1]);
                      
                      }
                      }​​
                      Do I need to update myDataSeries data in OnBarUpdate for the dataseries to be updated? I read it somewhere saying that it's automatically synchronized as the 30min bar updates?

                      Comment


                        #12
                        Hello affilife,

                        Thank you for your reply.

                        "Do I need to update myDataSeries data in OnBarUpdate for the dataseries to be updated?"
                        You do need to update the series by assigning values to it in OnBarUpdate(). What other update are you referring to?

                        "I read it somewhere saying that it's automatically synchronized as the 30min bar updates?"
                        By being synchronized with a series, the documentation means that the custom series has the same number of slots/indexes as the data series. If it is synchronized with the 30-minute series, there will be just as many data points in the custom series as there are bars in the 30-minute series. 5 bars ago on the 30 minute series will sync up with 5 bars ago from the custom series.

                        Please let me know if I may be of further assistance.
                        Emily C.NinjaTrader Customer Service

                        Comment


                          #13
                          Thanks Emily. I think I am still confused. Are you saying that myDataSeries is empty with the same length as BarsArray[1] per my previous code if I dont update it in OnBarUpdate()? So if I run the following code, there is nothing for my indicator to process?
                          Code:
                          private AaaCustomIndi myAaaCustomIndi;
                          private Series<double> myDataSeries;
                          ​
                          protected override void OnStateChange()
                          {
                          if (State == State.Configure)
                          {
                          // this added series will be BarsInProgress 1
                          AddDataSeries(BarsPeriodType.Minute, 30);
                          }
                          else if (State == State.DataLoaded)
                          {
                          // "BarsArray[1]" refers to the first data series added to the script with AddDataSeries.
                          myDataSeries= new Series<double>(BarsArray[1]);
                          myAaaCustomIndi = AaaCustomIndi(myDataSeries);
                          
                          }
                          }​​​
                          so myAaaCustomIndi will return nothing?

                          Comment


                            #14
                            I ran a quick test and I saw myAaaCustomIndi has values in it although I don't update any data from myDataSeries in OnBarUpdate(). Is this a bug or an intended behavior?

                            Comment


                              #15
                              After a lot of digging into my code and NT8 guide, I found an issue with my code. I use Update() to get the most recent data from indicator. However, this will Update all data series, which I don't want. I found out about it when reading up on the guide​ on Update(). So I essentially dont need a different instance for the data series as it wont help fixing the problem

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Balage0922, Today, 07:38 AM
                              0 responses
                              2 views
                              0 likes
                              Last Post Balage0922  
                              Started by JoMoon2024, Today, 06:56 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post JoMoon2024  
                              Started by Haiasi, 04-25-2024, 06:53 PM
                              2 responses
                              19 views
                              0 likes
                              Last Post Massinisa  
                              Started by Creamers, Today, 05:32 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post Creamers  
                              Started by Segwin, 05-07-2018, 02:15 PM
                              12 responses
                              1,786 views
                              0 likes
                              Last Post Leafcutter  
                              Working...
                              X