2

I have an MQL4 script ( a script that runs on MetaTrader4 Terminal platform ) and I need to define an extern variable, named extractionDate of type datetime, so that the user can change its input value before the script starts.

I tried the conventional way to define the variable before the standard script's function start(), but it doesn't work. When I compile I, get the error message

['TimeLocal' - constant expected]

that means MQL4 wants a constant value for the variable. But this isn't my goal. I would like to show as default value the "Today" date, when the script starts and not a fixed predefined date value.

Is it possible to do this or not?

extern datetime extractionDate = TimeLocal();
int start()
{
   ......
   return(0);
}
user3666197
  • 1
  • 6
  • 50
  • 92
Andrea Caronello
  • 217
  • 2
  • 11

1 Answers1

3

No, compiler does not allow this directly

Compiler does not allow a way to assign a default value, that is not constant. It has to know the value, so an attempt to setup / assign an unknown / variable-value as a default one, will yield compilation error.

Yet, how to solve this?

My approach would be to give user instructions and a choice to setup any datetime, or setup a value of -1, which would be translated inside the OnInit() event handler code-block:

 void   OnInit(){
        ...
        if (  extractionData == -1 ) extractionDate = TimeLocal();
        ...
        }
user3666197
  • 1
  • 6
  • 50
  • 92