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 Indicator

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

    Multi-time frame Indicator

    I've created an indicator that I want to check prices of previous daily bars while loading on shorter time frame charts. For some reason that I can't work out it's generating an error that there's not enough bars loaded. It's a simple loop that prints the closing prices of the daily bars. It seems to stop after 3 or 4 bars for the secondary series, and won't go back any further, even though the data is there. Any help would be appreciated.

    Code:
    for(int d = 0; d < lookback && !conditionOccurred; d++)
    						{
    								Print(Closes[1][d]);
    								
    						}
    Error on calling 'OnBarUpdate' method on bar 6134: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
    Last edited by Matts; 12-10-2016, 12:41 AM.

    #2
    Hello,

    Thank you for the post.

    To find the problem, it would be best to add some more information and print it out to find out what specifically is happening.

    A good place to start would be to print the CurrentBar and the d variable:

    Code:
    Print("Current Bar: " + CurrentBars[1] + " d: " + d);
    You are accessing a BarsAgo on bar 6134 that seems to be invalid, printing the d variable will help see how many bars ago you are requesting.

    You can also add a check in the for loop to break if the period is too great:

    Code:
    if(d >= CurrentBars[1]) break;

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      This is the output:

      Current Bar: 4 d: 0
      Closes 0.74401
      Current Bar: 4 d: 1
      Closes 0.74021
      Current Bar: 4 d: 2
      Closes 0.73871
      Current Bar: 4 d: 3
      Closes 0.73937
      Current Bar: 4 d: 4
      Closes 0.73571
      Current Bar: 4 d: 5
      Indicator 'HzPriceLine': Error on calling 'OnBarUpdate' method on bar 6136: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
      The bar of the primary series is at 6136, and this number varies slightly, but is always about 6000 on a 1 minute chart. This part doesn't seem invalid, but the primary series seems to be limiting how far back the secondary series will look. I should also mention that I'm using historical tick data replay, which could be the cause of the problem.

      Comment


        #4
        Hello Matts,

        Thanks for your post.

        You may want to check how many days of data are loaded on the chart. You will want to load as many days back as you are checking + 1.
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Is it possible to load the secondary series independently of the data loaded on the chart, such as loading 1 year worth of daily bars, with a few days of minute data on the chart?

          Comment


            #6
            Hello Matts,

            Thanks for your post.

            No, the chart's data series dictates how much data is loaded for all added dataseries.
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by Matts View Post
              Is it possible to load the secondary series independently of the data loaded on the chart, such as loading 1 year worth of daily bars, with a few days of minute data on the chart?
              This is a really interesting question.

              As for your original post, I was wondering if the following could provide you with what you were looking for........

              Code:
              protected override void OnBarUpdate()
              {
                   if (BarsInProgress == 1)
                   {
                        if(CurrentBars[1] != 0)
                        {
                        Print(Closes[1][0]);
                        }
                   }
              }

              Comment


                #8
                Thanks Paul, is there a way to make sure that enough bars are loaded on the primary series? I'd like to have a way of making sure that the user loads enough bars on the chart, by having a lookback parameter set the number of days to load, instead of having it return if there's not enough bars. Are there any performance issues with loading about 500,000 bars on a shorter timeframe?

                Assiduous

                Comment


                  #9
                  (edited)
                  Hello,

                  It is possible to request an arbitrary amount of data by doing a BarsRequest instead of adding a secondary series.


                  You can ensure that the script does not run in there is not enough data by adding a return.
                  For example:
                  if (CurrentBars[0] < 10 || CurrentBars[1] < 10)
                  return;

                  This would prevent the script from executing until both the primary and secondary series have 10 bars each.

                  You could also check the BarsPeriods[0].BarsPeriodType and BarsPeriods[0].Value and ensure the values here are what are expected, and prevent the script from processing if they are not.


                  The more data you load, the more resources it will take in memory and the more calculations the script must do. Each computer is different. We cannot predict if a certain amount of data will cause performance issues with your machine.

                  Was there a different behavior you wanted to happen other than the script not running if the data is not satisfied?
                  Last edited by NinjaTrader_ChelseaB; 12-13-2016, 04:17 PM.
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Hello Chelsea,

                    thanks for replying. I should firstly clear up that I’m not Assiduous, that was for the previous poster on the thread.

                    The behaviour that I want to happen, is that when the indicator is loaded an int variable would be set by the user for the number of days to load in the secondary series. The primary series would then load additional data if there wasn’t enough data on the chart. If preventing the script from running is the only option, I'd have to notify the user to load more data. The problem with this is that if more data isn't loaded the indicator simply won't work, and this will be expected for most shorter time frame charts, which will only have a few days of data loaded.

                    My question about performance is more related to the number of bars and what is generally a safe amount to load, as it needs to be able to run on any machine. Is data loaded on a 1 minute chart going to use a lot more resources than if loaded on a daily chart? I don't want to use something like BarsRequest that is instrument specific, as the indicator needs to work for any chart instrument. Can BarRequest be used to load the current instrument of the shorter time frame?

                    I noticed that it's possible to choose the barsToLoad parameter when adding a data series for a different instrument, but not when using the same instrument. It'd be good to have this option when adding data series for the same instrument.

                    Comment


                      #11
                      Hello Matts,

                      I'm not quite sure what you mean when you say a BarsRequest is instrument specific.
                      A bars request will work for any instrument you supply to the BarsRequest. This is an addon function for non synchronized data. Meaning, this will not use the Instrument of the script, it will use the instrument that you supply to it.

                      I cannot predict how many bars will be a safe amount on your machine.

                      When using AddDataSeries, it is not possible to set a Days to load or Bars to load. This is only possible when doing a BarsRequest.
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        What I meant was that it requires a string for the instrument name, and I don't know what instrument the chart will be loaded for, so it'd need to access the name of the instrument on the chart.

                        It looks like I can access the string through ChartBars.Bars.Instrument, so I'll try using BarsRequest with that.

                        * I ended up solving this by setting ChartBars.Properties.DaysBack to make sure there's enough data on the chart. It's a lot easier than BarRequest.
                        Last edited by Matts; 12-13-2016, 06:30 PM.

                        Comment


                          #13
                          Hello Matt,

                          To get the instrument name of the primary series use Instrument.FullName.


                          It is not supported to change the Data Series properties in this manner.
                          Chelsea B.NinjaTrader Customer Service

                          Comment

                          Latest Posts

                          Collapse

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