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-timeframes vs Sessions vs barsAgo

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

    Multi-timeframes vs Sessions vs barsAgo

    Code similar to the following ...
    Code:
    protected override void Initialize()
    {
      CalculateOnBarClose = true;
      MaximumBarsLookBack = MaximumBarsLookBack.Infinite ;
      Add("$EURUSD",PeriodType.Second, 1,MarketDataType.Bid) ; // 1 sec
    }
    ...
    Print ("Highs[1].Count = "+Highs[1].Count);
    Print ("Highs[1].BarsSinceSession = "+Highs[1].BarsSinceSession);
    Print ("Highs[1][Count-10] = "+Highs[1][Highs[1].Count-10]);
    ... produces output similar to the following ...

    Highs[1].Count = 5432
    Highs[1].BarsSinceSession= 158

    ... and gets an error at that point:
    "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.".
    Why?

    I assume (it seems incorrectly) that if a data series has a bar count of 5432 and one attempts to access bar # 5422 (i.e Count-10), it should not be a problem ... but it seems the maximum bar one can access is determined by BarsSinceSession, since if one loops from bar # 0 backwards, there is no problem until one reaches bar # BarsSinceSession+1, at which point the error occurs.

    Advice most appreciated. Thanks!
    Multi-Dimensional Managed Trading
    jeronymite
    NinjaTrader Ecosystem Vendor - Mizpah Software

    #2
    Try printing out CurrentBars[1] as well. That should tell you what bar you are on when trying to call Highs[1][Count - 10]

    My assumption is that you are getting this error early on in the historical data

    The .Count you are getting is just the total amount in that list/array but the indicator will start at bar 1 and start working its way up to the most recent bar when applied to a chart.

    Comment


      #3
      Multi-timeframes vs Sessions vs barsAgo

      First, this is strategy code, not indicator code, and I'm running the strategy independently of a chart.

      Also, I did also print CurrentBars[1] and its identical to BarsSinceSession[1].

      Thanks.
      Multi-Dimensional Managed Trading
      jeronymite
      NinjaTrader Ecosystem Vendor - Mizpah Software

      Comment


        #4
        Strategy and Indicator will process historical data the same way with OnBarUpdate(). There's no difference here.

        Second, you state that the CurrentBars[1] is the same value as the Highs[1].BarsSinceSession, which means that you are on bar 158 out of 5432 and trying to get values from an index that doesn't exist yet, hence the error "accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart."

        You will need to either add a CurrentBar check to ensure that you have enough data before accessing those values or use a different value there.

        Comment


          #5
          Multi-timeframes vs Sessions vs barsAgo

          Hmmmm. This is perplexing. I can count 5432 bars, albeit historical bars. But I can only access the most recent 158 bars?

          So, let's get back to the what I want, rather than what I'm doing. I want the strategy to access all current and historical bars that are countable at startup.

          The strategy Initializes, and then processes all the historical data in OnStartUp, before even entering OnBarUpdate. In other words, at the conclusion of the startup of the strategy, all historical data, as defined by the added data series and the lookback, has already been processed. In this case, 5432 bars. Note that this strategy will never be applied to a chart -- it runs from the added data series only.

          How does one achieve that? Thanks again.
          Last edited by jeronymite; 04-18-2015, 12:30 AM.
          Multi-Dimensional Managed Trading
          jeronymite
          NinjaTrader Ecosystem Vendor - Mizpah Software

          Comment


            #6
            Originally posted by jeronymite View Post
            Hmmmm. This is perplexing. I can count 5432 bars, albeit historical bars. But I can only access the most recent 158 bars?

            So, let's get back to the what I want, rather than what I'm doing. I want the strategy to access all current and historical bars that are countable at startup.

            The strategy Initializes, and then processes all the historical data in OnStartUp, before even entering OnBarUpdate. In other words, at the conclusion of the startup of the strategy, all historical data, as defined by the added data series and the lookback, has already been processed. In this case, 5432 bars. Note that this strategy will never be applied to a chart -- it runs from the added data series only.

            How does one achieve that? Thanks again.

            You are right that the bars are loaded in OnStartUp() but they get processed one by one in OnBarUpdate().

            Whats the full message of the error? It should have something like "Error in OnBarUpdate on bar xx:"

            Also, if you want all the data accessible from the count of OnStartUp for your OnBarUpdate() then you need to have a CurrentBar check for this.

            Try using if(CurrentBars[1] < Highs[1].Count - 10) return;

            This tells the OnBarUpdate that as long as the data is not available to process your logic, end the method and move on to the next bar.

            Comment


              #7
              Hello jeronymite,

              Thank you for your post.

              I would recommend running a CurrentBars check as well to verify NinjaTrader has processed enough bars from left to right. Even though we have a full count the CurrentBar is the one being processed and if we don't have enough bars processed before then we will see this type of error.

              For information on CurrentBars please visit the following link: http://www.ninjatrader.com/support/h...urrentbars.htm
              For information on ensuring you have enough bars processed please visit the following link: http://www.ninjatrader.com/support/f...ead.php?t=3170

              Please let me know if I may be of further assistance.

              Comment


                #8
                Forcing OnBarUpdate in a strategy

                Thanks for all the advice.

                Let me try to provide a simple synopsis of what I want to achieve. From a strategy that is not associated with any charts and does not call any indicators, I want to access all historical and current bars from multiple data series, even when no market sessions are active, to conduct data analysis.

                The challenges/complications seem to be:
                • Not associated with any charts
                • Does not call any indicators
                • Must run successfully even when no market sessions are active (e.g. 3am Sunday)

                I have put the CurrentBars[1] check in OnBarUpdate, but because I am testing this and want to run it, amongst other times, on a Sunday when no markets are active, OnBarUpdate is not being invoked. I have tried to add Update() to force OnBarUpdate, but it is a method in the Indicator namespace apparently, and I have not been able to work out the magic to invoke that successfully in a strategy ... assuming that is the appropriate approach.

                Any further advice/solutions most welcome! Thanks.
                Multi-Dimensional Managed Trading
                jeronymite
                NinjaTrader Ecosystem Vendor - Mizpah Software

                Comment


                  #9
                  Originally posted by jeronymite View Post
                  Thanks for all the advice.

                  Let me try to provide a simple synopsis of what I want to achieve. From a strategy that is not associated with any charts and does not call any indicators, I want to access all historical and current bars from multiple data series, even when no market sessions are active, to conduct data analysis.

                  The challenges/complications seem to be:
                  • Not associated with any charts
                  • Does not call any indicators
                  • Must run successfully even when no market sessions are active (e.g. 3am Sunday)

                  I have put the CurrentBars[1] check in OnBarUpdate, but because I am testing this and want to run it, amongst other times, on a Sunday when no markets are active, OnBarUpdate is not being invoked. I have tried to add Update() to force OnBarUpdate, but it is a method in the Indicator namespace apparently, and I have not been able to work out the magic to invoke that successfully in a strategy ... assuming that is the appropriate approach.

                  Any further advice/solutions most welcome! Thanks.
                  Everything that you have described as your scenario can be run in OnBarUpdate() successfully. Maybe I am misunderstanding? Are you saying that you want tp process everything in OnStartUp() rather than OBU? Is that what I am misunderstanding?

                  Comment


                    #10
                    OnStartup to load all bars ... or ad hoc on demand

                    Thanks, koganam.

                    I have been able to get this working in OBU, but doing this in OSU (or even better, the undocumented OnStart()) is very much preferred. I want to avoid the code being cycled on every bar, even though a little boolean exclusion wrapper prevents that in the main. It is essentially "on demand" data analysis, initiated at startup, but updated ad hoc on demand through a custom interactive interface.

                    All advice/enlightenment welcome with many thanks!
                    Multi-Dimensional Managed Trading
                    jeronymite
                    NinjaTrader Ecosystem Vendor - Mizpah Software

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Perr0Grande, Today, 08:16 PM
                    0 responses
                    2 views
                    0 likes
                    Last Post Perr0Grande  
                    Started by elderan, Today, 08:03 PM
                    0 responses
                    5 views
                    0 likes
                    Last Post elderan
                    by elderan
                     
                    Started by algospoke, Today, 06:40 PM
                    0 responses
                    10 views
                    0 likes
                    Last Post algospoke  
                    Started by maybeimnotrader, Today, 05:46 PM
                    0 responses
                    12 views
                    0 likes
                    Last Post maybeimnotrader  
                    Started by quantismo, Today, 05:13 PM
                    0 responses
                    7 views
                    0 likes
                    Last Post quantismo  
                    Working...
                    X