Variable Initialization

 
Please I want to know if variables declared at global scope are initialized at the entry of every tick.

But I thought they are initialized only once at the loading of the EA.

Why is my variable (at global scope) always returning zero even when I assigned a non-zero value to it?

Or do I have to declare it as static even though it has global scope?.
 
macpee:
Please I want to know if variables declared at global scope are initialized at the entry of every tick.

But I thought they are initialized only once at the loading of the EA.

Why is my variable (at global scope) always returning zero even when I assigned a non-zero value to it?

Or do I have to declare it as static even though it has global scope?.

global variables are intialised at startup not every tick.

show your code if you want an answer to the second part of your question, it could be any number of reasons.

 
Paul Anscombe #:

global variables are intialised at startup not every tick.

show your code if you want an answer to the second part of your question, it could be any number of reasons.

I don't mean Global Variable, I mean variable with global scope.

 
macpee #:

I don't mean Global Variable, I mean variable with global scope.

I know that it is confusing.

Global Variables that you are referring to are terminal Global Variables.

The other gv's that Paul is referring to are global scope variables (declared outside of any function).

 
Keith Watford #:

I know that it is confusing.

Global Variables that you are referring to are terminal Global Variables.

The other gv's that Paul is referring to are global scope variables (declared outside of any function).

Yea. thats right.

 
macpee:
Please I want to know if variables declared at global scope are initialized at the entry of every tick.

But I thought they are initialized only once at the loading of the EA.

Why is my variable (at global scope) always returning zero even when I assigned a non-zero value to it?

Or do I have to declare it as static even though it has global scope?.
  1. Yes, on load.
  2. Yes, on load.
  3. Do you really expect an answer? There are no mind readers here and our crystal balls are cracked. Always post all relevant code (using Code button) or attach the file.
         How To Ask Questions The Smart Way. (2004)
              Be precise and informative about your problem

    We can't see your broken code.

  4. It is already static.

Those are not assignments; they are initialization of a common (globally declared), or static variable with a constant. They work exactly the same way in MT4/MT5/C/C++.

  1. They are initialized once on program load.

  2. They don't update unless you assign to them.

  3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use).

    MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and

    Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:

    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

  4. Unlike indicators, EAs are not reloaded on chart change, so you must reinitialize them, if necessary.
              external static variable - MQL4 programming forum #2 (2013)

 
macpee:
Please I want to know if variables declared at global scope are initialized at the entry of every tick.

But I thought they are initialized only once at the loading of the EA.

Why is my variable (at global scope) always returning zero even when I assigned a non-zero value to it?

Or do I have to declare it as static even though it has global scope?.



In the OnInit function, initialise the variable with the desired value, and then it will not return zero. 

Suppose you initialise a variable with global scope with a non-zero value , but you do not initialise it with same or some other non-zero value inside the onInit function, then the value of the variable will return zero in some scenarios.

I am assuming that, In Oninit, the values of the variables in your code is getting reinitialised to zero.