Questions from Beginners MQL5 MT5 MetaTrader 5 - page 440

 

Hello 2015.09.17_19:07 GMT+3 .I have set the dimensions of dynamic arrays in the EA program. And the compiler gives errors: "comma expected". And if there is a variable in the program text, -- gives warnings: "variable such-and-such hides variable declaration at global level". I don't get it. Everything seems to be correct, like in Help. Here are some code snippets:

//--- array of maximal bar prices

bool ArraySetAsSeries(double &High[],bool);

//---

bool ArraySetAsSeries(double &Low[],bool);

//--- set the array sizes with reserve (reserve)

int ArrayResize(double &mrate[],int 16,int 9);

int ArrayResize(double &maVal[],int 16,int 9);

int ArrayResize(double &fVal[],int 3,int 2);

int ArrayResize(double &zVal[],int 3,int 0);

int ArrayResize(double &High[],int 1,int 0);

int ArrayResize(double &Low[],int 1,int 0);

//+------------------------------------------------------------------+

//| Expert deinitialization function |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

//--- массив максимальных цен баров
bool ArraySetAsSeries(double &High[],bool);
//---  
bool ArraySetAsSeries(double &Low[],bool);
//--- устанавливаю размеры массивов с запасом (reserve)
int  ArrayResize(double &mrate[],int 16,int 9);
int  ArrayResize(double &maVal[],int 16,int 9);
int  ArrayResize(double &fVal[],int 3,int 2);
int  ArrayResize(double &zVal[],int 3,int 0);
int  ArrayResize(double &High[],int 1,int 0);
int  ArrayResize(double &Low[],int 1,int 0);  
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {The compiler generates errors

{

 
Николай Никитюк:

Hello 2015.09.17_19:07 GMT+3 .I have set the dimensions of dynamic arrays in the EA program. And the compiler gives errors: "comma expected". And if in the program text there is

  1. You can't declare arrays with names High[] and Low[], because these are reserved system names of arrays-timeseries.
  2. Wrong spelling of ArrayResize. It should be this way:
    //--- устанавливаю размеры массивов с запасом (reserve)
    ArrayResize(mrate,16,9);
    ArrayResize(maVal,16,9);
    ArrayResize(fVal,3,2);
    ArrayResize(zVal,3);
  3. And setting the timeseries flag for an array would look like this:
    //--- массив максимальных цен баров
    ArraySetAsSeries(High,true);
    //---  
    ArraySetAsSeries(Low,true);
 
2015.09.17_19:45 GMT+3. Attaching a .png file just in case :
 
Karputov Vladimir:
  1. You cannot declare arrays with names High[] and Low[], because these are reserved system names for arrays timeseries.
  2. Wrong spelling of ArrayResize. It should be written this way:
Thank you! You answered so quickly. I'm out at 8:00 p.m. Mm-hmm. I'll see the rest later. 19:54 MSC.
 
Karputov Vladimir:
If you mean my code - then yes, my code is checking for a new bar.
I'm interested in the code I attached - I want to understand what it does.
 
-Aleks-:
I am interested in the code I have attached - I want to understand what it does.

The code you gave doesn't do anything, or rather it doesn't work. Error:

if(TimeN==0)
      TimeN=TimeC;

This condition will only work once - the first time you run the program. At this point, the static variable will be initialized.

The condition below is meaningless to check at all since the variable TimeN in your code will be equal to eternal zero (or rather the date January 1, 1970) and this eternal zero will be compared to the variable TimeC which equals the time the current bar opens):

   if(TimeN==TimeC)
      return;
 
Karputov Vladimir:

The code you gave doesn't do anything, or rather it doesn't work. Error:

This condition will only work once - the first time you run the program. At this point, the static variable will be initialized.

The condition below is meaningless to check at all since the variable TimeN in your code will be equal to eternal zero (or rather it will be the date January 1, 1970) and this eternal zero will be compared to the variable TimeC which equals the opening time of the current bar):

Now I wonder why I should have written it... into Expert Advisor.

Thanks for the clarification!

 
Karputov Vladimir:

If the new bar is on the M1 timeframe, then print the message:

Turns out I didn't give the full code, the correct one was

 

int init()

  { 

   static datetime TimeN=0;

   return(INIT_SUCCEEDED);

  }

 

int start()

  { 

   datetime TimeC=iTime(NULL,TF,0);

   if(TimeN==0)TimeN=TimeC;

   if(TimeN==TimeC) return(0);

   TimeN=TimeC;

  // Код программы исполняется при появлении нового бара

   return(INIT_SUCCEEDED);

  } 

In that case it works, right?
 
-Aleks-:

Turns out I didn't give the full code, the correct code was

 

int init()

  { 

   static datetime TimeN=0;

   return(INIT_SUCCEEDED);

  }

 

int start()

  { 

   datetime TimeC=iTime(NULL,TF,0);

   if(TimeN==0)TimeN=TimeC;

   if(TimeN==TimeC) return(0);

   TimeN=TimeC;

  // Код программы исполняется при появлении нового бара

   return(INIT_SUCCEEDED);

  } 

In that case, it works, right?
Have you tried to compile this code? There is an error here: the TimeN variable is declared in OnInit() and when you exit OnInit(), this variable will be destroyed. That is why there is an error in the OnTick() function
'TimeN' - undeclared identifier

Here's the working code:

//+------------------------------------------------------------------+
//|                                                         Test.mq4 |
//|                        Copyright 2015, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   static datetime TimeN;
   datetime TimeC=iTime(NULL,PERIOD_M1,0);
   if(TimeN==TimeC)
      return;
   TimeN=TimeC;
// новый бар, выполняем код
   Print("New bar");
   return;
  }
//+------------------------------------------------------------------+
 
Karputov Vladimir:
Have you tried to compile this code? There is an error here: the variable TimeN is declared in OnInit() and on exit from OnInit() this variable will be destroyed. Therefore, an error occurs in the OnTick() function

Here is the working code:

I wrote static datetime TimeN=0; in the area beforeint OnInit() - where external and other variables are declared.

Reason: