Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Incrementation in a for loop

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

    Incrementation in a for loop

    Hello!
    In the incrementation, the first object is fixed and compared to the following others. After the first object was compared to others, the second one is fixed and compared also to the following others (except to the first one which is the previous). This process continues until the two last objects of the series are compared.
    Now let imagine that I have five bars and I want to compare them. But here, I want to compare only two consecutive objects. In other words, I want to compare Object[0](first object of the series) to Object[1](second object of the series); From here, Object[1] in the previous comparison will be Object[0] and the third objet of the series will be Object[1]. The first object of the series is not more taken into consideration. We could have the following illustration for the comparison for the objects at positions: (0-1); (1-2); (2-3); (3-4); (4-5) and not: (0-1-2-3-4-5); (1-2-3-4-5); (2-3-4-5); (3-4-5); (4-5) of the simple incrementation.
    I have this code:
    HTML Code:
    for(int i = 0; i < 5; i++)
     { for (int j= i+1; j < 4; j++)
      {if(Close[i] > Close[j]
       {DoSomething;
         i++;
         j++
        }
      }
     }
    I am unfortunately NOT having the expected result.
    Any help would be very appreciated.
    Thanks!
    Last edited by Stanfillirenfro; 04-08-2022, 05:27 AM.

    #2
    Hello Stanfillirenfro,

    A couple of issues with what you provided:

    Code:
    for(int i = 0; i < 5; i++)
    {
        for (int j= i+1; j < 4; j++)
        {
            if(Close[i] > Close[j]
            {
                DoSomething;
    [B]i++;   //not needed[/B]
    [B]j++;   [/B][B]//not needed[/B]
            }
        }
    }
    The extra i and j ++ you have in loop is not needed, you already do that inside the top of the loop:

    Code:
    for(int i = 0; i < 5; [B]i++[/B])
    The i++ is the increment action for this loop.


    To do the type of condition you wanted the easiest way is to use a single loop:

    Code:
    if(CurrentBar < 5) return;
    for(int i = 0; i < 5; i++)
    {
        int firstBarsAgo = i;
        int secondBarsAgo = i + 1;
        if(Close[firstBarsAgo] > Close[secondBarsAgo]
        {
    
        }
    }
    Which would result in a pattern of (0-1); (1-2); (2-3); (3-4); (4-5)

    Comment


      #3
      Hello Jesse!

      Many thanks for your quick reply and help!

      Awsome!

      Thanks one more time!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      571 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
      548 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      549 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X