Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

CurrentBar in a for loop

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

    CurrentBar in a for loop

    Hi!
    I really do not understand what is happening with the for loop here in Ninjatrader.
    I am writing the code in protected override void OnBarUpdate().
    I have done the following:

    HTML Code:
    int Period = 15;
    
    if(CurrentBar < 0)
    return;
    
    for(int i = 0; i < 5; i++)
    {  double l_high = MAX(High, Period)[i];
       double l_low = MIN(Low, Period)[i];
    }
    On the output window, I have the error message: "Error on calling 'OnBarUpdate' method on bar 0:"
    I have repeated the same code by replacing 5 by Currentbar and this time I could have the values of l_high and l_low.
    I have then replaced 5 by Period and the error message was on bar 15.

    What is the problem when I have the number 5 or 15?
    What is the secret here in Ninjatrader to overcome this problem? This problem is receurrent for me and I really do not know what to do.
    I would really appreciate any help.

    #2
    Hello Stanfillirenfro,
    You need below to fix it:
    Code:
    if (CurrentBar < 4)
    return;
    In for loop you're using MAX()[4] so you must skip that many bars. You must have these bars on chart so it can be processed, 4 in your case.
    Hope it helps!

    Comment


      #3
      Hello S. Kinra and many thanks for your reply. The problem here is that when I increase the number to 500 for example,, I must set the condition to
      HTML Code:
      if(CurrentBar < 500)
      return.
      In this case the calculation is very slow and I do not have my indicator on the chart.
      Also, if I have something such as:
      HTML Code:
      for(j= i; j <= i + 3; j++)
      {
             Code
      }
      It is impossible here to have bars at position i+3

      Is there any idea to solve this problem?
      Last edited by Stanfillirenfro; 07-15-2021, 09:05 AM.

      Comment


        #4
        Hi Stanfillirenfro, thanks for posting.

        If the CurrentBar property is > i+3 then it will not throw an error because there are enough bars on the chart to process the requested index. e.g. i = 1, x = 1+3 = 4 if CurrentBar > 4 then you will have no problem accessing Close[x], High[x], Open[x], etc. Now, if you are using a function like MAX and MIN, CurrentBar must be > the Period value. You can use Visual Studio debugging to break at the specific line causing the problem, which can help to identify what bar index is invalid:
        https://ninjatrader.com/support/help..._debugging.htm

        Kind regards,
        -ChrisL

        Comment


          #5
          Hello Stanfillirenfro,
          I don't have a work around in that case, this is the only working way I could figure out as of now. Hope someone from NT could help further. BTW do you really need that many bars back, maybe there is some other better way.

          Comment


            #6
            Many thanks ChrisLfor the explanation and also thanks to S.Kinra.
            ChrisL please could you provide me a link to downoading visual studio? I have tried do download a visual studio but it seemed to be very big with some features not very usefull for me and I cancelled the installation.

            S.Kinra with 500 I was testing to see the behaviour of the code with the increase of the number of bars since with CurrentBar it seemed to work except that the caalculation was indefinitely.and I could not have any indicator on the chart.

            Best Regards

            Comment


              #7
              Hi Stanfillirenfro, thanks for your reply.

              When you pick features for visual studio, just select the .NET desktop development with C#.

              Visual Studio dev tools & services make app development easy for any developer, on any platform & language. Develop with our code editor or IDE anywhere for free.


              Kind regards,
              -ChrisL

              Comment


                #8
                Many thanks ChrisL!

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                566 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                330 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                101 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                547 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                548 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X