EventSetTimer + variable - flexible - mql4

 

Hello everyone,

I would appreciate your help since I am not so familiar with mql4.

My goal:

When the code starts EventSetTimer should be 0 as well as the variable x to get the close of bar 0 and bar 1 as "sell"/"buy" comment.

After that the EventSetTimer should work only 5 seconds after the candle of the 1h time frame has closed, giving me the close of bar 1 and bar 2  as "sell"/"buy" comment. 

The code you can find below. The code did not show any errors but the following warning at "int OnInit()" for t2 --> «possible loss of data due to type conversion»

Thank you in advance.

___________________________________________

#property strict

// inputs

int t1;
int t2;
int t;
int x;
int y;
string m;
 
int OnInit()  {
   
   x=0;
   t1=0;
   t2=iTime(Symbol(),Period(),0)+PeriodSeconds()+5;
   t=t1;
   return(INIT_SUCCEEDED);
  }
 
void OnDeinit(const int reason)
  {
  }
 
void OnTimer()  {
  
   EventSetTimer(t);
   y=x+1;
   
   double   closeH11    = iClose(Symbol(),    PERIOD_H1    ,x);
   double   closeH12    = iClose(Symbol(),    PERIOD_H1    ,y);
  
     if(closeH12>=closeH11) {
     m="sell";
     }
     else
     m="buy";
                                                              
     Comment (m);
     x=1;
     t=t2;
   return;
}
 
___________________________________________
 
  1. Please use the "</>" icon (Alt-S) to add code to your post. Don't just paste code as plain text.
  2. Don't access time-series data from the OnInit() even handler. The chart data may not be ready yet. Do in it in the OnTick() event handler.
  3. The iTime() function returns a datatime data-type and not an int. So declare the receiving variable as a datatime.
  4. Don't set the event timer on every tick. Set once only or when its duration needs to change.
  5. Set the event time based on a time duration and not a set datetime as you are doing.
Documentation on MQL5: Timeseries and Indicators Access / iTime
Documentation on MQL5: Timeseries and Indicators Access / iTime
  • www.mql5.com
iTime - Timeseries and Indicators Access - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5