Search code examples
mql4metatrader4

EA sending a mass of "SendMessage" commands despite prevention code in place


Morning Fellow Stackoverflowers

I'm currently testing out some code which is based around time of day with which an EA is allowed to trade. How would I prevent the EA from sending a bazillion emails - one will obviously do.

Only one was sent in another EA I have by applying LastActiontime=Time[0];, however, having applied the LastActiontime=Time[0];, its pinging a mass of messages....

What changes do I need to make to prevent this from happening?

See code below

//+------------------------------------------------------------------+
//|                                     EAServerVerificationPing.mq4 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

datetime       LastActiontime;
int            timeOfDay;
int            hour;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   RefreshRates();
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
//---

   timeOfDay            =  DayOfWeek();
   hour                 =  Hour();
   
   if(timeOfDay == 1 && hour >= 2)
   if(LastActiontime!=Time[0])
   {
      //Code to execute once in the bar
      SendMail("Is your Trade Allowed? (FXVM)",
      "Yes, your trade is allowed"+
      "\n"+
      "\nPRIVACY NOTICE");
   }else{
      SendMail("Is your Trade Allowed? (FXVM)",
      "No, your trade is not allowed"+
      "\n"+
      "\nPRIVACY NOTICE");
   }  
   LastActiontime=Time[0]; <!------This should prevent a mass of messages from sending--->
}
  

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

Much obliged :)


Solution

  •   datetime       LastActiontime;
      int            timeOfDay;
      int            hour;
      
      int OnInit()
        {
      //---
         RefreshRates();   // <- useless
      //---
         return(INIT_SUCCEEDED);
        }
      void OnDeinit(const int reason)
        {
      //---
         
        }
      //+------------------------------------------------------------------+
      //| Expert tick function                                             |
      //+------------------------------------------------------------------+
      void OnTick()
      {
      //---
      
         timeOfDay            =  DayOfWeek();
         hour                 =  Hour();
         
         if(DayOfWeek()==MONDAY && Hour()>=2)
         {
            if(Time[0]>LastActiontime)
            {
               LastActiontime=Time[0];send();
            }
         }
      }
      void send()
      {
         if(true) //put your logic here
         {
            SendMail("Is your Trade Allowed? (FXVM)",
                  "Yes, your trade is allowed"+
                  "\n"+
                  "\nPRIVACY NOTICE");
         }
         else
         {
            SendMail("Is your Trade Allowed? (FXVM)",
               "No, your trade is not allowed"+
               "\n"+
               "\nPRIVACY NOTICE");
         }
      }