Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

for loop problem

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

    for loop problem

    I have a problem with "for loop" in NT.

    Let's take for example the following code:

    Code:
    int barsAgo;
    			int sum = 0;
    		
    			for (barsAgo = 0; Open[barsAgo] > Close[barsAgo]; barsAgo++)
    				
    			{
    				sum = sum + 1;
    					
    			}
    			
    			Value.Set(sum);
    It returns the number of consecutive bars for wich the condition is true. But here is a problem: if I change the condition and I want it to return the number of consecutive bars for wich the condition Open[barsAgo] < Close[barsAgo] is true, the indicator returns 0.

    Why is this working for Open[barsAgo] > Close[barsAgo] condition, but not for Open[barsAgo] < Close[barsAgo] condition?

    Thanks.

    #2
    If the condition is not true on the first iteration, it exits the loop, and sum is still equal to 0 because sum = sum + 1; never executed.
    Last edited by Crassius; 05-14-2011, 04:15 PM.

    Comment


      #3
      Originally posted by Crassius View Post
      If the condition is not true on the first iteration, it exits the loop, and sum is still equal to 0 because sum = sum + 1; never executed.
      Thank you for your answer.

      Yes, but there are constantly green and red candels on chart. The idea is that if I have a red candle (Open[barsAgo] > Close[barsAgo]), the indicator must return 1; if I have two red candels, the indicator must return number 2. It is working for green candels (Open[barsAgo] < Close[barsAgo]), but not for red candels.

      And there are red candels.

      Comment


        #4
        Originally posted by SymeBB View Post
        Thank you for your answer.

        Yes, but there are constantly green and red candels on chart. The idea is that if I have a red candle (Open[barsAgo] > Close[barsAgo]), the indicator must return 1; if I have two red candels, the indicator must return number 2. It is working for green candels (Open[barsAgo] < Close[barsAgo]), but not for red candels.

        And there are red candels.
        Your construct appears to be defective relative to its intent. A for loop is a conditional fixed count iteration, which exits when the exit condition is true.

        You seem to be wanting to determine the number of bars for which a condition is true, which is a different program construct.

        Maybe if you stated in words what you are trying to accomplish, someone might be able to write you the correct code snippet?

        Comment


          #5
          What you have now in english

          SymeBB,

          What you have now says,

          Starting at the first bar of the chart
          check to see if the open of the current bar is greater than the close

          if it is
          add 1 to Sum
          go to the next bar
          run the check again

          if it is not
          exit

          once it exits, the program goes to the next line of code following the for loops brackets, without executing what is inside the brackets.

          If the very first bar on the chart has an open that is greater than its close, the loop will run for that bar and add 1 to Sum, Sum now = 1.

          If the next bar's open is greater it will run a again and add 1 to Sum, Sum now = 2.

          The first bar it hits where the open is less than the close (or stated another way open is not greater than the close) the program exits the loop without adding anything to Sum.

          So, if the very first bar on the chart is Open < Close, it exits right then, without adding anything to Sum, and you will get the results you are getting.

          You can check this by loading a chart whose first bar meets the condition Open is greater than close.... in that case Sum must always be equal to at least 1.

          Now load a chart where the first bar's open is less than the close.... Sum will always be equal to 0 on that chart.

          Your near to whatever it is you want to do. Now if you take a better understanding of what is happening, maybe you can alter your code to get a little closer to the result you want.
          Last edited by Crassius; 05-14-2011, 04:34 PM.

          Comment


            #6
            As I understand it you are saying. I want to loop thru all the bars on the chart & perform some count or other action.
            So your loop could be either :-
            1. for 0 to CurrentBar -1. As that is an index over all the bars that are in the chart.
            2. foreach Close. As that would iterate thru the colllection.

            But it is likely that you are missing the point. The OnBarUpdate() method is called every bar (or tick). So you already have a loop which you can take advantage of. So try something like.

            In Variables
            int iDown = 0;
            int iUp = 0;

            in OnBarUpdate()

            if(CurrentBar < 2)
            return;

            // Only process entry signals on a bar by bar basis (not tick by tick)
            if (FirstTickOfBar)
            {
            if (Open[1] < Close[1])
            iUp++ ;
            else if (Open[1] > Close[1])
            iDown++;
            // otherwise Open == Close. Also note you are comparing Floating point numbers so it is safer to compare by subtracting the 2 numbers & checking to see if the difference is tiny rather than checking ==

            return;
            }


            These numbers will give you a total count of the Up & Down bars.
            -------------------------------
            Alternative 2: Use the Ninja CountIf function. eg:

            iUp = CountIf(delegate {return Close[1] > Open[1];}, CurrentBars)
            iDown= CountIf(delegate {return Close[1] < Open[1];}, CurrentBars)
            The only concern I have with this is that it would be expensive to recalculate every bar, when you could've kept the calculation from the prior bar in a local variable & just looked at the currentbar.
            ------
            Also note that the indexes change if you've set CalculateOnBarClose = true; then all my indexes above should be 0 & not 1.

            Hope this was somewhere close to your intent.
            Last edited by David Lean; 05-15-2011, 02:43 AM.

            Comment


              #7
              Thank you all for your answers.

              I will try to use your suggestions.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              583 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              338 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              103 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              554 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              552 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X