• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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?
    Patrick H.NinjaTrader Customer Service

    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?
          Patrick H.NinjaTrader Customer Service

          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 shdbgvfhjf, Today, 05:57 AM
            0 responses
            4 views
            0 likes
            Last Post shdbgvfhjf  
            Started by stefanrvc, Today, 04:59 AM
            1 response
            12 views
            0 likes
            Last Post NinjaTrader_Marco  
            Started by 7robert, Yesterday, 08:30 PM
            2 responses
            16 views
            0 likes
            Last Post NinjaTrader_Marco  
            Started by pjsmith, Today, 01:13 AM
            1 response
            15 views
            0 likes
            Last Post NinjaTrader_ChrisSch  
            Started by yukashg, Yesterday, 09:56 PM
            1 response
            16 views
            0 likes
            Last Post NinjaTrader_ChrisSch  
            Working...
            X