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

For Loop Help With Current Check

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

    For Loop Help With Current Check

    Hi there,

    I have a for loop below to calculate heiken shi inside bars. It works for a few bars then gives the common error on bar 50. I am not sure why on bar 50 it can't compute anymore?

    The common check of BarsRequired did not work. What would make it work under these conditions?

    Error on calling 'OnBarUpdate' method for indicator 'itTestCode' on bar 50: 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.


    if (CurrentBar < BarsRequired)
    return;


    #region Inside Bars

    double currentBarRealBody = Math.Abs(HeikenAshi().HAClose[0] - HeikenAshi().HAOpen[0]);

    for (int i = 0; currentBarRealBody < Math.Abs(HeikenAshi().HAClose[i+1] - HeikenAshi().HAOpen[i+1]); i++)
    {
    if (i >= 3) // if there is 3 inside bars
    {
    DrawArrowUp("MyArrowUp2"+CurrentBar, 0, Low[0], Color.Black);
    }
    }

    #2
    Hello ij001,

    Thank you for your post.

    I do not see how this for loop would ever stop, are you using break at all?

    Comment


      #3
      No this is it. I don't follow why won't it stop once the currentBarRealBody is greater than the initial real body? Also where would break go and how does it work?

      Comment


        #4
        Originally posted by ij001 View Post
        No this is it. I don't follow why won't it stop once the currentBarRealBody is greater than the initial real body? Also where would break go and how does it work?
        Because whenever the CurrentBar is the smallest bar on the chart, your loop exit condition cannot be satisfied, and so your loop index will eventually exceed the barCount.

        You should not use an uncontrolled quantity as your loop exit condition. If you must insist on what you are doing, then use your current test condition inside the loop itself, and increment a counter.

        Keep your loop conditions kosher by limiting the loop iterations to the bars available to process, or less.
        Last edited by koganam; 11-23-2014, 06:06 PM. Reason: Corrected punctuation.

        Comment


          #5
          Hello ij001,

          Thank you for your response.

          You are correct, that it will continue as long as the condition is true - but this means it never stops if the condition is true. Is there a max number of inside bars you are looking for?

          Comment


            #6
            My bad, I didn't understand so I decided to go back and create an if statement and increment myself. It has worked.

            if (currentbar is inside previous bar [i+1])
            i++
            // draw
            else
            i=0;

            Thanks for the help!

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by DJ888, 04-16-2024, 06:09 PM
            4 responses
            12 views
            0 likes
            Last Post DJ888
            by DJ888
             
            Started by terofs, Today, 04:18 PM
            0 responses
            8 views
            0 likes
            Last Post terofs
            by terofs
             
            Started by nandhumca, Today, 03:41 PM
            0 responses
            6 views
            0 likes
            Last Post nandhumca  
            Started by The_Sec, Today, 03:37 PM
            0 responses
            3 views
            0 likes
            Last Post The_Sec
            by The_Sec
             
            Started by GwFutures1988, Today, 02:48 PM
            1 response
            9 views
            0 likes
            Last Post NinjaTrader_Clayton  
            Working...
            X