Testing- OnTick() & OnTimer()

 

Hi


I've attached screenshots of my  Journal results from running this EA.

#property strict

// Global variables. EACH EA needs a different unique Magic Number
// extern means it can be changed in optimisation


//+------------------------------------------------------------------+
//|Initialisation is done once on loading EA to a chart.
            
//+------------------------------------------------------------------+
int OnInit()
  {
   EventSetTimer(3600);
   return(INIT_SUCCEEDED);
 
  }
//+------------------------------------------------------------------+
//| De-initialisation is done once on closing the EA on a chart      |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   EventKillTimer(); 
  }
//+------------------------------------------------------------------+
//| This is where your trading logic goes                            |
//+------------------------------------------------------------------+

void OnTimer()

{  OnTick(); }

void OnTick()

// THE START
// Has a new candle been formed?


{          
  // Check for a trade once at 9am. If there are zero trades open, see if London is calling         
            
                
                
                {
                  datetime time= TimeLocal();
                  string hoursandMinutes= TimeToString(time,TIME_MINUTES);   
                  
                
                        if(hoursandMinutes=="09:00");
                        {   Print(Bars);
                           Print (hoursandMinutes);
                           
                      
                        
                        }
                
               }
                
              
}

I'm trying to develop something simple based on entering a trade only at 9am local time on the H1 timeframe. As such, I'm trying to get Print() to simply print the time and number of bars at 9am only, to test I can get it to ignore at all other times.


My results show my EA seems to be running on tick when backtesting, rather than only at 9am, because the Print() is generating in Journals on every minor price movement (see attached screenshots).


I've tried running it in OnTimer() on its own, and in the testing it does not generate any results.


Any ideas?


Many thanks,

Steve

Files:
 
lancastersteve:

Hi


I've attached screenshots of my  Journal results from running this EA.

I'm trying to develop something simple based on entering a trade only at 9am local time on the H1 timeframe. As such, I'm trying to get Print() to simply print the time and number of bars at 9am only, to test I can get it to ignore at all other times.


My results show my EA seems to be running on tick when backtesting, rather than only at 9am, because the Print() is generating in Journals on every minor price movement (see attached screenshots).


I've tried running it in OnTimer() on its own, and in the testing it does not generate any results.


Any ideas?


Many thanks,

Steve

As I know, Timer() doesn't work in tester.
 
  1. Correct. See In backtest OnTimer() not performs (M. Ali) - MQL4 programming forum and work around Chart Event For MT4 Backtester (Migel) - MQL4 programming forum

  2. lancastersteve:
    int OnInit(){ {
       EventSetTimer(3600);
       return(INIT_SUCCEEDED);
    EST can fail. The default suggestion is to loop until you enable it.
              Build 1080 Cannot Set Timer error with EventSetTimer function - Indices - MQL4 programming forum #8

    I don't try to enable them in OnInit but instead I keep trying to enable them in OnTick/OnCalculate until they succeed. (No race condition, no CPU loop, no timer ticks before chart is updated.)
              Cannot set timer (60) - EA not working - MT4 - MQL4 programming forum

 
void OnTick()
{
       
            // What is the Hour of the current time?
            int h=TimeHour(TimeCurrent());
            int h1=-1;
            
            
            
            
            // Has a new hour started (i.e. is the hour from the current tick different from the hour from the previous tick?
            if(h1==h);
               {
               Print("h ",h);
               Print("h1 ",h1);
               }
               
               
               
            return;

Hi

Thanks, 

So I tried to create a simple check on the incoming Ticks to see if the hour displayed on the incoming tick equalled h1. So, in theory, this code should never run because h in the above will never equal -1. But, looking at the Journal results it just ran every single tick!

Is there something wrong with my code?

Files:
Untitled.jpg  450 kb
 
lancastersteve:

Hi

Thanks, 

So I tried to create a simple check on the incoming Ticks to see if the hour displayed on the incoming tick equalled h1. So, in theory, this code should never run because h in the above will never equal -1. But, looking at the Journal results it just ran every single tick!

Is there something wrong with my code?

            if(h1==h);
               {
               Print("h ",h);
               Print("h1 ",h1);
               }

Remove the ;

 
Keith Watford:

Remove the ;

Keith,


That's really helpful, thanks. Can't believe I missed that.


I tried to run the EA for one week in July and changed h1=9. So, in theory, it should run for all incoming ticks with an hour of 9, and should get 5 days' worth of Print()


The H1 candle graphs all look correct and how I'd expect them to look for that week. However, Print only ran for one of those days. Is this likely to be the 9am ticks for the other days were downloaded at a different server time?


Thanks again,

STeve

 
lancastersteve:

Do not double post.

I have deleted your duplicate topic.

 
lancastersteve:

Correct your code and show it and maybe somebody can help you

 

Keith, Sure. I'll put the new code up.

So after correcting the code to remove ";", I tried to run it for a 5 day period in Jul-19 in Tester with h1=9, to see if it would only Print @ 9am. However, it seemed to work, but only for one of the days in the period. Is this an issue with the code, or the tickdata in Tester in MT4?


Thanks

void OnTick()
{
       
            // What is the Hour of the current time?
            int h=TimeHour(TimeCurrent());
            int h1=9;
            
            
            
            
            // Has a new hour started (i.e. is the hour from the current tick different from the hour from the previous tick?
            if(h1==h)
               {
               Print("h ",h);
               Print("h1 ",h1);
               }
               
               
               
            return;
               
            
 
 
            
 
               
                
                
                    
}                      
 
lancastersteve:

Keith, Sure. I'll put the new code up.

So after correcting the code to remove ";", I tried to run it for a 5 day period in Jul-19 in Tester with h1=9, to see if it would only Print @ 9am. However, it seemed to work, but only for one of the days in the period. Is this an issue with the code, or the tickdata in Tester in MT4?


Thanks

Your code will print every tick of the 9th hour, not only when it is different from the previous tick. Try this.

   static int prev_hr=-1;
   int h=TimeHour(TimeCurrent());
   if(prev_hr!=h)
     {
      prev_hr=h;
      int h1=9;
      if(h1==h)
        {
         Print("h ",h);
         Print("h1 ",h1);
        }
     }
 

Hi,

I don't know if it is out of topic but it appears that the instruction "EventSetMillisecondTimer" which is supposed to work every millisecond actually cannot handle periods lower than 16ms. Is anyone familiar with that?

Thanks

Guillaume

Reason: