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

Mountain chart type and OnBarUpdate...

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

    Mountain chart type and OnBarUpdate...

    Hello,

    In years past I created two indicators for use on a bar chart, worked fine.
    I am starting a new indicator and began with a bar chart and all was/is well. The new indicator needs to to run on a mountain chart and when I tried to use the new indicator the fun stopped.

    The chart has many "bars" or for the mountain chart, many close data points.
    Version 8.0.26.1 64-bit
    1 minute chart

    Code:
    double currentClose = Close[0];
    double previousClose = Close[1];​
    Error:

    Error on calling 'OnBarUpdate' method on bar 0: 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 question, how to get the current and previous data point for the chart.

    Ideas?

    Thanks,

    Mark

    #2
    Hello Mark,

    You can ignore the chart style as this is not involved.

    You are attempting to call Close[1], which is the close of the previous bar, on the very first bar.

    If there is only one bar processed when CurrentBar is 0, you cannot call 1 bar ago. You have to wait until 2 bars are processed.

    See the forum post below.
    Hello, I want to create an indicator that show data in a chart but calculate in other charttime different to the time of the chart where is showed. For example:
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post

      You are attempting to call Close[1], which is the close of the previous bar, on the very first bar.
      Thanks for the response but as I wrote in the post "The chart has many "bars" or for the mountain chart, many close data points."

      Click image for larger version  Name:	image.png Views:	0 Size:	45.0 KB ID:	1232536

      Comment


        #4
        Hello Mark,

        The historical data starts processing with the first bar, when CurrentBar is 0.

        After this it processes the next bar, CurrentBar is 1.

        This continues through every bar on the chart.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_ChelseaB View Post
          The historical data starts processing with the first bar, when CurrentBar is 0.
          After this it processes the next bar, CurrentBar is 1.
          This continues through every bar on the chart.
          Thanks for the reply.

          OK there is some confusion and it might be on my end.

          With a bar chart.

          Close[0] refers to the current bar.
          Close[1] refers to previous bar

          Only this code in OnBarUpdate, works without error in a bar chart:
          Code:
          Print("Bar 0 call");
          double previousClose = Close[0];
          Print("Bar 0: " + previousClose);
          
          Print("Bar 1 call");
          double currentClose = Close[1];
          Print("Bar 1: " + currentClose);
          
          Print("OnBarUpdate end");​
          Here is the output:

          Bar 0 call
          Bar 0: 4041.25
          Bar 1 call
          Bar 1: 4039.5
          OnBarUpdate end
          Bar 0 call
          Bar 0: 4041.25
          Bar 1 call
          Bar 1: 4039.5
          OnBarUpdate end​

          The exact code in a mountain chart, output:

          Bar 0 call
          Bar 0: 3986.75
          Bar 1 call
          Indicator 'Dangle': Error on calling 'OnBarUpdate' method on bar 0: 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.

          ​It seems OnBarUpdate for a mountain chart does not act the same as a "candlestick" chart.

          What am I missing?
          Last edited by Mark--; 01-24-2023, 03:24 PM.

          Comment


            #6
            Hello Mark,

            If you add the code I suggested, does the error still occur?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_ChelseaB View Post
              If you add the code I suggested, does the error still occur?
              The:
              Code:
              if (CurrentBar < 1)
              return;​
              No it does not and thanks. It does not explain the issue. The "candlestick" chart does not need that code. Why? What else will be different?

              The help for "CurrentBar" reads: "...the very first bar of the chart (left most bar) will be number 0 (zero)...", which is the opposite from Close[0], the active bar.

              You wrote in the forum "The script will start from the far left of the chart at the very beginning where the CurrentBar will equal 0 and then start working to the right..." ...

              Tell me you see the disconnect.

              CurrentBar is numbered (older to newer) 0,1,2... newest is the highest number.
              Close[] is numbered n,2,1,0, newest is lowest number.

              Am I missing something?.

              Comment


                #8
                Hello Mark,

                Doing a test and printing Time[1], it does appear there is a phantom bar that is not on the chart.

                I've reported this to our development.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_ChelseaB View Post
                  Doing a test and printing Time[1], it does appear there is a phantom bar that is not on the chart.I've reported this to our development.
                  OK, thanks, please post the results when available.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by 21laienoch, Today, 07:25 AM
                  0 responses
                  2 views
                  0 likes
                  Last Post 21laienoch  
                  Started by burtoninlondon, 04-28-2024, 12:38 AM
                  1 response
                  16 views
                  0 likes
                  Last Post burtoninlondon  
                  Started by MacDad, 02-25-2024, 11:48 PM
                  8 responses
                  175 views
                  0 likes
                  Last Post NinjaTrader_Erick  
                  Started by alancurry, Today, 06:53 AM
                  0 responses
                  6 views
                  0 likes
                  Last Post alancurry  
                  Started by Juanhuisman, Today, 06:51 AM
                  0 responses
                  6 views
                  0 likes
                  Last Post Juanhuisman  
                  Working...
                  X