declaring global arrays gives erros

 


double ema_21[];
double ema_55[];
double ema_233[];

double ema_21_m5[];
double ema_55_m5[];
double ema_233_m5[];

double ema_21_h4[];
double ema_55_h4[];
double ema_233_h4[];

ArraySetAsSeries(ema_21, true);
ArraySetAsSeries(ema_55, true);
ArraySetAsSeries(ema_233, true);

ArraySetAsSeries(ema_21_m5, true);
ArraySetAsSeries(ema_55_m5, true);
ArraySetAsSeries(ema_233_m5, true);

ArraySetAsSeries(ema_21_h4, true);
ArraySetAsSeries(ema_55_h4, true);
ArraySetAsSeries(ema_233_h4, true);
      
int OnInit()
  {

   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {

   
  }

void OnTick()
  {

   
  }
 
"ArraySetAsSeries" is a function! It has to be called from within a function. It cannot be used in the global scope of the code reserved for declaration of global variables. It can only be used locally inside another function, for example inside the OnInit event handler function.
 
Well, it is possible to call it on global scope.

But you need to assign it's result to another variable.

bool arr1_asseries = ArraySetAsSeries(ema_21, true);

This is fully legal, and the compiler will not complain.
 
Dominik Christian Egert #:Well, it is possible to call it on global scope. But you need to assign it's result to another variable. bool arr1_asseries = ArraySetAsSeries(ema_21, true); This is fully legal, and the compiler will not complain.

Yes, you are correct, but does it actually function by setting the array as a series?

And what happens when an Indicator/EA is restarted on a Chart, does it keep it set as series?

 
Fernando Carreiro #:

Yes, you are correct, but does it actually function by setting the array as a series?

And what happens when an Indicator/EA is restarted on a Chart, does it keep it set as series?

as ser

I don't know. I guess that would need some testing.

But if I should guess, I would say, yes. Because if the data of that array is preserved, why should the hidden structure to an array be reinitialized.

But it's just a guess.
 
Dominik Christian Egert #: as ser . I don't know. I guess that would need some testing. But if I should guess, I would say, yes. Because if the data of that array is preserved, why should the hidden structure to an array be reinitialized. But it's just a guess.
Please note I am not "testing" you. I was just wondering what will happen because I have never done it that way before. Because MQL is sometimes strange the way it works and because MetaQuotes sometimes change it's behaviour without any explanation. So, I personally would not code it that way in the Global scope. I would prefer doing it in the OnInit(), just to be on the safe side.
 
Fernando Carreiro #:
Please note I am not "testing" you. I was just wondering what will happen because I have never done it that way before. Because MQL is sometimes strange the way it works and because MetaQuotes sometimes change it's behaviour without any explanation. So, I personally would not code it that way in the Global scope. I would prefer doing it in the OnInit(), just to be on the safe side.
Yes, and I don't feel tested. Ha.

I wouldn't do it that way either, I just wanted to point out, it is possible.

Also declaring variables only to execute a function does not really make sense.

Although, I do use this technique sometimes for const static variable initialisation.




 

Dominik Christian Egert #: bool arr1_asseries = ArraySetAsSeries(ema_21, true);  This is fully legal, and the compiler will not complain.

Dominik Christian Egert #: Although, I do use this technique sometimes for const static variable initialisation.

That is not an assignment; it's 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. A 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)

Per № 3 It may compile, it just may not work. Unless the array is created first, call fails. Making the array a buffer (in OnInit) could change the result.

 
William Roeder #:

That is not an assignment; it's 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. A 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)

Per № 3 It may compile, it just may not work. Unless the array is created first, call fails. Making the array a buffer (in OnInit) could change the result.

Yes, I agree.

I was referring to the return value of the function ArraySetAsSeries, when saying assign. Assigning the return value of that function.

It is not good practice to declare and initialize a variable with a function on global scope.




Reason: