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

Multi-Time Frame Scripts

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

    Multi-Time Frame Scripts

    Reading the help but have a few questions ...

    1) When adding a secondary bar object with a different interval does each primary bar index Closes[0][i] have a corresponding Closes[1][i]?

    a) if i >= 1
    b) if i == 0 with new bars arriving?

    2) Can BarsInProgress 1 process code for BarsInProgress 0 (or vice versa) and if so how should this be organised to avoid racing or timing issues?

    #2
    Hello Futurenets,

    Thank you for your post.

    1) Yes, they will both have the same i value that is being passed, However, in a backtest this may not be so. Take the first 5 bars for instance, if your Primary is 1 min and Secondary is 5 Min, when you try to get i value 3 you will get an Out of Index error for the Secondary as it is only on the 2nd bar for the time frame.

    You will want to have a current bar check before processing any of the data.
    Code:
    if(CurrentBars[0] < 3 || CurrentBars[1] < 3) return;
    2) Yes, you can process code from certain bar updates in the code by filtering out the portion using if(BarsInProgress == 0)
    {
    // Process any logic in here
    }

    There is no organization here but keeping it in Order will help assess for debugging.
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      so for primary 1 min & secondary 5 min then

      Closes[0][0] Closes[1][0] ok
      Closes[0][1] Closes[1][1] out-of-index error
      Closes[0][2] Closes[1][2] out-of-index error
      Closes[0][3] Closes[1][3] out-of-index error
      Closes[0][4] Closes[1][4] out-of-index error
      Closes[0][5] Closes[1][5] ok

      for primary = 60 min & secondary = 1 day

      Closes[0][0] Closes[1][0] ok
      Closes[0][1] Closes[1][1] out-of-index error
      ...
      Closes[0][23] Closes[1][23] out-of-index error
      Closes[0][24] Closes[1][24] ok

      for primary = 1 day & secondary = 1 day

      Closes[0][0] Closes[1][0] ok
      Closes[0][1] Closes[1][1] ok
      ...
      Closes[0][23] Closes[1][23] ok
      Closes[0][5] Closes[1][24] ok

      does anything influence this like session templates?
      Last edited by futurenets; 02-18-2015, 11:32 AM.

      Comment


        #4
        Futurenets,

        You need to ensure that you have enough data for all the data series you are adding to the script -
        Code:
        if(CurrentBars[0] < 5 || CurrentBars[1] < 5) return;
        http://www.ninjatrader.com/support/f...ead.php?t=3170
        Cal H.NinjaTrader Customer Service

        Comment


          #5
          ok thanks

          so if I have enough data on both data series to clarify ...

          for primary 1 min & secondary 5 min then

          Closes[0][0] Closes[1][0] ok
          Closes[0][1] Closes[1][1] out-of-index error
          Closes[0][2] Closes[1][2] out-of-index error
          Closes[0][3] Closes[1][3] out-of-index error
          Closes[0][4] Closes[1][4] out-of-index error
          Closes[0][5] Closes[1][5] ok

          for primary = 60 min & secondary = 1 day

          Closes[0][0] Closes[1][0] ok
          Closes[0][1] Closes[1][1] out-of-index error
          ...
          Closes[0][23] Closes[1][23] out-of-index error
          Closes[0][24] Closes[1][24] ok

          for primary = 1 day & secondary = 1 day

          Closes[0][0] Closes[1][0] ok
          Closes[0][1] Closes[1][1] ok
          ...
          Closes[0][23] Closes[1][23] ok
          Closes[0][5] Closes[1][24] ok

          does anything influence this like session templates?

          Comment


            #6
            Futurenets,

            Yes, the added data series will adopt the primary data series session template that is set in the Data Series properties. Meaning if the primary uses US Equities RTH (9:30 AM to 4:00 PM EST) the secondary, tertiary and so forth, added data series will as well.

            As for your layout of the Closes[1][0]...
            Closes[0][0] Closes[1][0] ok - Expected
            Closes[0][1] Closes[1][1] out-of-index error - Expected
            Closes[0][2] Closes[1][2] out-of-index error - Expected
            Closes[0][3] Closes[1][3] out-of-index error - Expected
            Closes[0][4] Closes[1][4] out-of-index error - Expected
            Closes[0][5] Closes[1][5] ok - Not Expected, would expect to still receive another out of index error but Closes[1][1] would not.

            I recommend that you read the second section of the link below as it talks about how the data is referenced and what this means when trying to access the BarsAgo index of the price series.
            http://www.ninjatrader.com/support/h...nstruments.htm
            Cal H.NinjaTrader Customer Service

            Comment


              #7
              thank you very much Cal, 1st class support as usual.

              so using your example (1 min Primary & 3 min Secondary)

              COBC = True

              when CurrentBars[0] < 3
              cannot access Closes[1]

              when CurrentBars[0] = 3 to 5
              can only access Closes[1][0] (closed)

              when CurrentBars[0] = 6 to 8
              can only access Closes[1][0] (closed) & Closes[1][1] (closed)

              COBC = False

              when CurrentBars[0] < 3
              cannot access Closes[1]

              when CurrentBars[0] = 3 to 5
              can access Closes[1][0] (closed) & Closes[1][1] (not yet closed)

              when CurrentBars[0] = 6 to 8
              can access Closes[1][0] (closed), Closes[1][1] (closed) & Closes[1][3] (not yet closed)

              etc
              Last edited by futurenets; 02-19-2015, 02:47 AM.

              Comment


                #8
                so how is this organised with 60 min Primary & 1 day Secondary?

                Comment


                  #9
                  Futurenets,

                  Is this going to be the same setup for both or multiple data series. You need to ensure that you have enough bars for the amount that you are looking back in order to get around the Out-of-Index errors.

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

                  Even if its not three, use a value that you know will ensure you are not going back further than available data.

                  Example, if your script never looks past the 10th bar in indexing (Such as Closes[1][10]) then you want to ensure that your CurrentBar check is doing this -
                  Code:
                  if(CurrentBars[0] < 10 || CurrentBars[1] < 10) return;
                  Cal H.NinjaTrader Customer Service

                  Comment


                    #10
                    sorry I don't understand.

                    I thought perhaps we could create something similar to below for 60min primary & 1 day secondary?

                    e.g.

                    COBC = True

                    when CurrentBars[0] < 24
                    cannot access Closes[1]

                    when CurrentBars[0] = 24 to 47
                    can only access Closes[1][0] (closed)

                    when CurrentBars[0] = 48 to 71
                    can only access Closes[1][0] (closed) & Closes[1][1] (closed)

                    COBC = False

                    when CurrentBars[0] < 24
                    cannot access Closes[1]

                    when CurrentBars[0] = 24 to 47
                    can access Closes[1][0] (closed) & Closes[1][1] (not yet closed)

                    when CurrentBars[0] = 48 to 71
                    can access Closes[1][0] (closed), Closes[1][1] (closed) & Closes[1][3] (not yet closed)
                    Last edited by futurenets; 02-19-2015, 07:53 AM.

                    Comment


                      #11
                      Futurenets,

                      You're not checking the Secondary data series for the bar count.

                      // this needs to be at the beginning of OnBarUpdate() before processing anything else.
                      if(CurrentBars[1] < 10 || CurrentBars[0] < 10) return;

                      This check will ensure that if either data series has less than 10 bars for processing from historical data then we need to return the method so we don't try to access information outside of the available index.

                      //Check for both data series here, otherwise we will run into Indexing issues.
                      when CurrentBars[0] < 3 || CurrentBars[1] < 3
                      can access Closes[1]

                      So on...
                      Cal H.NinjaTrader Customer Service

                      Comment


                        #12
                        ok thanks but before I can check I need to know how its organised?

                        Comment


                          #13
                          Futurenets,

                          Apologies, as I do not follow along.

                          What needs to be organized in this matter?
                          Cal H.NinjaTrader Customer Service

                          Comment


                            #14
                            ok ...

                            we discussed 1 min primary and a 3 min secondary bar objects e.g. what closes[1] can be accessed.

                            if I have 60 min primary and 1 day secondary is the following true:

                            COBC = True

                            when CurrentBars[0] < 24
                            cannot access Closes[1]

                            when CurrentBars[0] = 24 to 47
                            can only access Closes[1][0] (closed)

                            when CurrentBars[0] = 48 to 71
                            can only access Closes[1][0] (closed) & Closes[1][1] (closed)

                            COBC = False

                            when CurrentBars[0] < 24
                            cannot access Closes[1]

                            when CurrentBars[0] = 24 to 47
                            can access Closes[1][0] (closed) & Closes[1][1] (not yet closed)

                            when CurrentBars[0] = 48 to 71
                            can access Closes[1][0] (closed), Closes[1][1] (closed) & Closes[1][3] (not yet closed)

                            Comment


                              #15
                              Originally posted by futurenets View Post
                              ok ...

                              we discussed 1 min primary and a 3 min secondary bar objects e.g. what closes[1] can be accessed.

                              if I have 60 min primary and 1 day secondary is the following true:

                              COBC = True

                              when CurrentBars[0] < 24
                              cannot access Closes[1]

                              when CurrentBars[0] = 24 to 47
                              can only access Closes[1][0] (closed)

                              when CurrentBars[0] = 48 to 71
                              can only access Closes[1][0] (closed) & Closes[1][1] (closed)

                              COBC = False

                              when CurrentBars[0] < 24
                              cannot access Closes[1]

                              when CurrentBars[0] = 24 to 47
                              can access Closes[1][0] (closed) & Closes[1][1] (not yet closed)

                              when CurrentBars[0] = 48 to 71
                              can access Closes[1][0] (closed), Closes[1][1] (closed) & Closes[1][3] (not yet closed)
                              Futurenets,

                              The organization here will not matter so long as you have the current bar check. The only issue you would run into is not checking that enough data has been loaded into the data series when starting the indicator

                              Have you tested your script using the sample code I have provided in numerous posts?
                              Cal H.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by geotrades1, Today, 10:02 AM
                              2 responses
                              5 views
                              0 likes
                              Last Post geotrades1  
                              Started by ender_wiggum, Today, 09:50 AM
                              1 response
                              5 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by rajendrasubedi2023, Today, 09:50 AM
                              1 response
                              12 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by bmartz, Today, 09:30 AM
                              1 response
                              10 views
                              0 likes
                              Last Post NinjaTrader_Erick  
                              Started by geddyisodin, Today, 05:20 AM
                              4 responses
                              28 views
                              0 likes
                              Last Post geddyisodin  
                              Working...
                              X