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); }
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!
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.
- www.mql4.com
"Sub-forum" means within mql5.com─not parallel to it:
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.
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); }
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():
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!
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I've included a simple code.