The why's and the what's of functions for a newbie

 

I am new to programming in MQL4. Does anyone have a flow chart describing the flow of MQL4 programs. For example when I start to run an indicator the programs firest runs

1. The preprocessors

2. The Globals variables and the Lib and etc...

But, under what condition does the --> int init() -- functions gets called?

1. What is it's main purpose? What does it initialize? Why here and not when the Preprocessor and globals variables do?

2. Does the MQL4 run through this function only once?

3. Or does it get called it every time a new price or bar comes in?

4. What can be put in the --> int init() --function?

5. Why put it in this function and not the --> int start() -- function?

6. The --> return(0) -- where does it return to?

7. After the return(0) what does it do next?

The --> int deinit() -- de-initializes the parameters I thing I understand some of it?

How often does the --> int start() function gets called?

1. Does MQL4 run the --> int start() -- everytime there is a new price or Bar?

1a. Does the MQL4 keeps calling it the --> int start() -- function?

2. The --> return(0) -- where it return to?

3. After the return(0) what does it do next?

Some of these question may be trivial to some but it will help me to understand and write indicators. Thank you so much.

 

init() is run once when MT4 initializes the indicator or EA, which occurs when it is attached to a chart, when MT4 starts, when the timeframe is changed, if the Symbol is changed, if extern parameters are changed, if the source is recompiled.

start() gets called by MT4 on every tick in an indicator or expert. When the code in start() is finished it returns control to MT4. start() is called only once when a script is run.

return() returns execution to the function that called the function you are returning from. return(x) will give that calling function the value of x. The next operation in the caller's code is executed then.

deinit() is called by MT4 when the indicator is removed, when timeframe is changed, when symbol is changed, etc.

.

https://docs.mql4.com/runtime/start

 
phy wrote >>

init() is run once when MT4 initializes the indicator or EA, which occurs when it is attached to a chart, when MT4 starts, when the timeframe is changed, if the Symbol is changed, if extern parameters are changed, if the source is recompiled.

start() gets called by MT4 on every tick in an indicator or expert. When the code in start() is finished it returns control to MT4. start() is called only once when a script is run.

return() returns execution to the function that called the function you are returning from. return(x) will give that calling function the value of x. The next operation in the caller's code is executed then.

deinit() is called by MT4 when the indicator is removed, when timeframe is changed, when symbol is changed, etc.

.

https://docs.mql4.com/runtime/start

phy,

Why write code in ini()? and what kind of code should be writting in the ini()? and when should code be writting in ini()?

Thanks

 

Tigger, I assume you mean init() rather than ini().


Take an EA for example.


There will be logic that you wish to perform every time a tick arrives. Generally speaking that logic will be about your EA building its picture of the market and deciding whether to trade. You'll trigger that logic from start().


And there will be logic that you wish only to perform once when you first add the EA to the chart. You'll trigger that logic from init(). The following are examples of logic you may wish to perform here:

- Reading parameters from external (user) input and/or from a recovery file and making decisions about what way the EA will subsequently operate

- Validation of above parameters

- Enquiring for data from platform/broker such as account number, symbol, lot step, number of digits etc. and setting the EA to run accordingly

- Sending an initialization email to inform about what parameters the EA has been started with

- Checking if an expiry date has passed

- etc.


CB

 
cloudbreaker wrote >>

Tigger, I assume you mean init() rather than ini().

Take an EA for example.

There will be logic that you wish to perform every time a tick arrives. Generally speaking that logic will be about your EA building its picture of the market and deciding whether to trade. You'll trigger that logic from start().

And there will be logic that you wish only to perform once when you first add the EA to the chart. You'll trigger that logic from init(). The following are examples of logic you may wish to perform here:

- Reading parameters from external (user) input and/or from a recovery file and making decisions about what way the EA will subsequently operate

- Validation of above parameters

- Enquiring for data from platform/broker such as account number, symbol, lot step, number of digits etc. and setting the EA to run accordingly

- Sending an initialization email to inform about what parameters the EA has been started with

- Checking if an expiry date has passed

- etc.

CB

Now, I think I understand it better, Thanks. I often saw code in that fucntion and I did not know its use. Now, the --> start() -- can a variable in that function retain a value form the previous bar? for example: if I would say --- PreviousBar = Time[i-1] the next time the start() gets called could I retreive the value of PreviousBar? Usually, a fucntions do not retain any values after it returns a value? Or, does the -- start() -- is always running and and does not return(0)? Thank you

 

Use "run once" code inside init() sparingly... Initialization is not finished yet.

 
tigger:

Now, I think I understand it better, Thanks. I often saw code in that fucntion and I did not know its use. Now, the --> start() -- can a variable in that function retain a value form the previous bar? for example: if I would say --- PreviousBar = Time[i-1] the next time the start() gets called could I retreive the value of PreviousBar? Usually, a fucntions do not retain any values after it returns a value? Or, does the -- start() -- is always running and and does not return(0)? Thank you


Any variables you wish to "transcend" a function - just declare them outside of all function blocks. CB
 
tigger wrote >>

I am new to programming in MQL4. Does anyone have a flow chart describing the flow of MQL4 programs. For example when I start to run an indicator the programs firest runs

1. The preprocessors

2. The Globals variables and the Lib and etc...

But, under what condition does the --> int init() -- functions gets called?

1. What is it's main purpose? What does it initialize? Why here and not when the Preprocessor and globals variables do?

2. Does the MQL4 run through this function only once?

3. Or does it get called it every time a new price or bar comes in?

4. What can be put in the --> int init() --function?

5. Why put it in this function and not the --> int start() -- function?

6. The --> return(0) -- where does it return to?

7. After the return(0) what does it do next?

The --> int deinit() -- de-initializes the parameters I thing I understand some of it?

How often does the --> int start() function gets called?

1. Does MQL4 run the --> int start() -- everytime there is a new price or Bar?

1a. Does the MQL4 keeps calling it the --> int start() -- function?

2. The --> return(0) -- where it return to?

3. After the return(0) what does it do next?

Some of these question may be trivial to some but it will help me to understand and write indicators. Thank you so much.

it's just plain c code the start is an object triggered by each new tik,

there are som simple ready made function, you can get along if are

familiar with simple c code and forex rules good luck

Reason: