Simple Loop

 
I know I'm asking for a boot in the head, but it seems as thought the language has changed since I was coding last.  Trying to do a simple loop from 1 to 50, where the code stops at 50.  What I get is: 1 to 50 then continuously loops again from 1 to 50.  I want to stop at 50.  Would appreciate any ideas.  Hopefully no boots.  Thanks!   
int start()
   {
    int n = 51;
while(--n > 0 && n < 51)
{
   Alert("n = ",n);
}                                                   
 
//----
   return(0);
}

I've included a simple code.
 
Yellowbeard1881:
Trying to do a simple loop from 1 to 50, where the code stops at 50.
int OnStart()
   {
    for(int n = 50; n > 0; n--)
       Alert("n = ",n);
    return(0);
   }
 
Ryan L Johnson #:

i hate correcting anyone, but the op wants the loop so that "the code stops at 50", therefore the for loop would be like so, correct?

   for(int i; i<50; i++)
 
Michael Charles Schefe #:
i hate correcting anyone, but the op wants the loop so that "the code stops at 50", therefore the for loop would be like so, correct?

Not to be smug, but where i is less than 50, you only go up to 49 (0─49).

To the extent that I'm incrementing downwardly (50─1) and you're incrementing upwardly, you could be technically correct in that regard.

 
Thanks for your input(s).  int onstart() doesn't seem to be recognized with mql, though it still compiles.   int start() is what works.  The count begins with 0 because the current candle is 0.  for(int i; i<50; i++) does not stop at 50.  It seems that with every tick the loop starts again, even if you want it to stop at a specific number. (50)  The break operator also is ignored.  Trying to create a loop with n=n+1 effects the code as a whole instead of just one loop.  Thanks, again!
 
Yellowbeard1881 #:
Thanks for your input(s).  int onstart() doesn't seem to be recognized with mql, though it still compiles.   int start() is what works.  The count begins with 0 because the current candle is 0.  for(int i; i<50; i++) does not stop at 50.  It seems that with every tick the loop starts again, even if you want it to stop at a specific number. (50)  The break operator also is ignored.  Trying to create a loop with n=n+1 effects the code as a whole instead of just one loop.  Thanks, again!

Looks like you're in the wrong place. OnStart() is for MQL5. This is the main MT5 forum.

FYI, there's an MQL4 sub-forum.

 
MQL4: automated forex trading, strategy tester and custom indicators with MetaTrader
MQL4: automated forex trading, strategy tester and custom indicators with MetaTrader
  • www.mql4.com
MQL4: automated forex trading, strategy tester and custom indicators with MetaTrader
 
Yellowbeard1881 #:
https://www.mql4.com/ brings you to https://www.mql5.com/en/forum/

"Sub-forum" means within mql5.com─not parallel to it:

https://www.mql5.com/en/forum/mql4

 
Yellowbeard1881:
I know I'm asking for a boot in the head, but it seems as thought the language has changed since I was coding last.  Trying to do a simple loop from 1 to 50, where the code stops at 50.  What I get is: 1 to 50 then continuously loops again from 1 to 50.  I want to stop at 50.  Would appreciate any ideas.  Hopefully no boots.  Thanks!   

I've included a simple code.
The loop itself is fine, but start() (or OnTick()) triggers on every new price tick that comes in from your broker. 

Once your loop finishes, the next incoming price tick calls start() all over again from the beginning, resets n = 51, and repeats.

If you only want this to run once:

1. If it's a Script: use OnStart() instead:

 void OnStart()
  {
   for(int i = 1; i <= 50; i++)
     {
      Alert("n = ", i);
     }
  } 


2. If it's an EA: move the loop inside OnInit() so it only runs once when attached to the chart, or use a static flag inside start()/OnTick():

 int start()
  {
   static bool hasRun = false;
   if(hasRun) return(0);

   for(int i = 1; i <= 50; i++)
     {
      Alert("n = ", i);
     }

   hasRun = true;
   return(0);
  } 

 
Muhammad Fa'iq Abdi #:
The loop itself is fine, but start() (or OnTick()) triggers on every new price tick that comes in from your broker. 

Once your loop finishes, the next incoming price tick calls start() all over again from the beginning, resets n = 51, and repeats.

If you only want this to run once:

1. If it's a Script: use OnStart() instead:



2. If it's an EA: move the loop inside OnInit() so it only runs once when attached to the chart, or use a static flag inside start()/OnTick():


Muhammad, this code worked.  The loop stopped at 50.  My broker is forex.com.  I'm writing in MT4.  Always have. 
int start()
  {
   static bool hasRun = false;
   if(hasRun) return(0);

   for(int n = 50; n >= 0; n--)
     {
      Alert("n = ", n);
     }

   hasRun = true;
   return(0);
  }             

Thank you for your help!