Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Days to Load for Multi Time Series Strategy

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

    Days to Load for Multi Time Series Strategy

    I am running a strategy that consists of two time series, the main time series is one minute bars and the second is one day bars. When running the strategy and accessing data on the secondary day bar time series I am presented with the error “You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.” Changing the value of “Days to load” to something ridiculously high seems to work but I shouldn’t need that much data when I am only going back as far as 5 days. All the threads I’ve read simply suggest adding the following code at the start of OnBarUpdate() to make sure there are enough bars generated before accessing the data .
    Code:
    			if (CurrentBars[1] < BarsRequired)
    			{
    				return;
    			}
    This code seems to work fine for the main time series but it just continues to loop when used for the secondary. When I tell it to print the value of “CurrentBars[1]” to the output window it just constantly returns 0 no matter what I try. It’s like it’s not loading the day bars at all. Am I doing something wrong here? Is there some kind of data limitation with Interactive Brokers that I’m not aware of?

    #2
    Hello cory360,

    Thank you for writing in.

    The issue is your text is trying to draw 10 bars back, however you need to check to ensure you have at least 10 bars on the chart. You should add the following to your code under OnBarUpdate and it should work as you expect. if(CurrentBar<10) return;

    An Example:

    protected override void OnBarUpdate()

    {

    if(CurrentBars[0 ] < 3 || CurrentBars[1] <3) return;

    if(Close[0]>Close[3])

    {

    DrawText("test","Text to draw",10,2000,Color.Blue);

    }


    Reference Example: Make sure you have enough bars in the data series you are accessing



    Please let us know if you need further assistance.
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      "CurrentBar" is only used for the main time series correct? If I want to check to ensure I have enough bars on my secondary time series I need to use "CurrentBars[1]". I have tried implementing a check for both and the one checking for the main time series works just fine but the secondary never gets past 10 or whatever number I put in. Because of this it just continues to "return;" without ever reaching the rest of the code.

      Comment


        #4
        Hello Cory360,

        Yes, CurrentBar would only apply to the main time series.

        What is the secondary time frame you are using?

        Who are you connected to?

        If you go to Control Center>Tools>Historical Data Manager is the data of that secondary time series listed?

        Can you open a chart with the same data you are using as part of your strategy?

        I look forward to your reply.
        Alan P.NinjaTrader Customer Service

        Comment


          #5
          The secondary time frame I am using is day bars.

          I am connected to InteractiveBrokers through their TradersWorkStation platform.

          There is data for the secondary time frame in the Historical Data Manager and I am able to access it fine when running a backtest from the Strategy Analyzer. It is only when I attempt to run the strategy from the Strategies tab on the Control Centre that I run into this problem.

          Lastly, I am able to open a chart with the secondary time frame just fine.

          Comment


            #6
            Hello Cory360,

            Without the code I’m unable to test on my end. If you’d like to share a copy of the code so I can see if anything pops out, please do so.

            I have provided a reference example using a secondary data series which might help you troubleshoot the issue.

            See Synchronizing a DataSeries object to a secondary time frame.
            Note: In NinjaTrader 8 It is no longer needed to use an indicator to sync a secondary series. This can be done directly from the Series&lt;T&gt; (https://ninjatrader.com/support/helpGuides/nt8/NT%20HelpGuide%20English.html?seriest.htm) constructor. This post is left for historical purposes. Series objects are useful for


            Please let us know if you need further assistance.
            Alan P.NinjaTrader Customer Service

            Comment


              #7
              I wrote just a simple strategy that yields the same results. It only needs to find data for 2 days back.

              Code:
               protected override void Initialize()
                      {
                          CalculateOnBarClose = true;
              			Add(PeriodType.Day, 1);
                      }
              
                      protected override void OnBarUpdate()
                      {
              			if (CurrentBars[0] < 5 || CurrentBars[1] < 5)
              			{
              				return;
              			}
              			
              			if (Closes[1][1] > Closes[1][2])
              			{
              				Print("Condition is true");
              			}
              			else
              			{
              				Print("Condition is false");
              			}
                      }
              As you can see I use the "CurrentBars[]" and "return;" to ensure I have the daily bars needed before the rest of the code accesses it. In the attached "Screenshot 1" you can see I have enough days to load, in this case its 10. When I run the strategy nothing happens, nothing gets printed to the output window even though my strategy should print something regardless of my condition being true or false. this can be seen in "Screenshot 2".

              Next I try modifying the code a little to print the values of "CurrentBars[0]" and "CurrentBars[1]" before the code reaches "return;".

              Code:
               protected override void Initialize()
                      {
                          CalculateOnBarClose = true;
              			Add(PeriodType.Day, 1);
                      }
              
                      protected override void OnBarUpdate()
                      {
              			if (CurrentBars[0] < 5 || CurrentBars[1] < 5)
              			{
              				Print("Minute Bars Loaded: " + CurrentBars[0]);
              				Print("Day Bars Loaded: " + CurrentBars[1]);
              				return;
              			}
              			
              			if (Closes[1][1] > Closes[1][2])
              			{
              				Print("Condition is true");
              			}
              			else
              			{
              				Print("Condition is false");
              			}
                      }
              You can see from the attached "Screenshot 3" that the value of the minute bars continues to increase but the daily bars stays at 0. Its like the day bars aren't even being loaded.
              Attached Files

              Comment


                #8
                Hello cory360,

                I was able to copy your first example, change the print statement to,

                Print(“Condition is True”)

                From,

                Print(‘Condition is True’)

                And the strategy put out prints.

                Please let us know if you need further assistance.
                Alan P.NinjaTrader Customer Service

                Comment


                  #9
                  What? My code already uses double quotes in the print statements. I don't see single quotes anywhere in either of the examples I posted. Well what happens if you run the second example code I shared? Does it return 0's for the number of day bars?

                  Comment


                    #10
                    Hello Cory360,

                    Yes, that code will print 0’s for day bars.

                    When applying the strategy, you have Min Bars Required set to 20. See Attachment.

                    If you change this value to 1 and enable the strategy you should see prints for day bars loaded starting at a value of 1.

                    Please let us know if you need further assistance.
                    Attached Files
                    Alan P.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by bortz, 11-06-2023, 08:04 AM
                    47 responses
                    1,602 views
                    0 likes
                    Last Post aligator  
                    Started by jaybedreamin, Today, 05:56 PM
                    0 responses
                    8 views
                    0 likes
                    Last Post jaybedreamin  
                    Started by DJ888, 04-16-2024, 06:09 PM
                    6 responses
                    18 views
                    0 likes
                    Last Post DJ888
                    by DJ888
                     
                    Started by Jon17, Today, 04:33 PM
                    0 responses
                    4 views
                    0 likes
                    Last Post Jon17
                    by Jon17
                     
                    Started by Javierw.ok, Today, 04:12 PM
                    0 responses
                    12 views
                    0 likes
                    Last Post Javierw.ok  
                    Working...
                    X