This forum is in read-only mode now. You may discuss your questions on forums of MQL4.community and MQL5.community

Beginner's question: help with globals


Back to topics list
avatar
4
Lou 2010.05.20 01:43 

I understand that MT handles global variables, so. I have a misunderstanding that I hope someone might help clear up. I have understood that the EA runs top to bottom each tick. If so, how does one maintain persistent global values when the variables are initialized each time?


Is it that only the start() section reruns and that the init/deint sections run once?


Do my own functions need to be within start() or can they be written along side?


Thanks for any help.

avatar
80
cloudbreaker 2010.05.20 18:20 

init() runs once.


deinit() runs once for a maximum of 2500 ms.


start() runs on each tick on condition that it is idle when the tick arrives.


Your functions are coded outside of all the other function blocks. The order does not matter. Where you call them from obviously matters.


Variables declared outside of all functions are available to all functions.


Variables declared within a function are available only to that function.


Within a function if you wish to stop the variable being initialized each time that function runs, you can declare it as static - then it will only be initialized on first run of the function.


There is another (even more global) type of variable which are shared across MT4 platform instances and they have their own functions to initialize, set, query etc.


CB


Back to topics list  
Download MetaTrader 5 (450 Kb, web installer) — a new terminal for financial markets