Q:how long can a program stay in "int start()"?? HELP

 

Hi everyone, I am new to MQL4 programing. And I have few questions bothering me a lot. I really hope that someone can help me out.

(I want write a program can run a long period)

Question 1:How long can a program stay in "int start()" stage: I know the "int start()" will start only when the program can detect a new tick. But

after that, will the program keep running?? or it will stop and run again in new coming new tick, again and again until I stop the whole program?

Question 2:Do the "int init()" and "int deinit()" only run once during the whole program execution?

Thanks so much for your time. If you know the answers, please reply me or sent me a email(L_J_L_100200@hotmail.com). Again, thanks.

 

Answer 2. Yes, only once.

Answer 1. You can force the program to sleep on Tick forever, however that is not how it's supposed to work as you miss all the later ticks (they become obsolete data and you don't want that. you can renew tick data during sleep cycle as well, but why make a thread sleep, if you can allow it to finish, cause it will restart on the next tick anyway?). You need to code your EA in a way that allows it to follow its cycles. So globally declared vars and static vars are the flavour (since doing all the math and every calculation on each and every tick is time and cpu consuming and foremost - unneeded).

Welcome to MQL4 and mql4 forums!

 
  1. As long as you want. Normal EA's return and wait for the next tick.
    Some Sleep(), loop, RefreshRates(), and check for changes. Needed only for special needs such as determining/handling connection loss durations, or reading external inputs (either file or http based) for trading independent of ticks.
    Any new ticks occuring while the EA was running start() are lost.
    Note: Only EA's and scripts can run start() indefinitely, indicators must return and can not sleep.
  2. init/deinit can happen many times. Every EA compile, or change of pair, timeframe, EA parameters, or chart refresh, etc will run a deinit/init cycle. See UninitializeReason()
    Note global and static variables are not reset to their initial values since the EA is not reloaded. Beware of code like
    int gv = 1;
    init(){ if(condition) gv *=10; }

 

Holy s*it I didnt know that 0.o. I thought globals get reset on init/deinit... Thanks!