alert mailing system help please

 

So this code sends a mail message whenever an order is entered or deleted or closed. But, it also sends redundant messages whenever the MT4 trading platform is started up as it re-assesses the recently brought up state of the orders. Does anyone know how to fix this? (I was thinking along the lines of using some of static variables or what not)

Also, is there a way to bypass the need for a VPS for this code to work? I mean, if pending orders can be executed without the need for the platform to be left continuously on, why not other executions? There probably isn't a way (dealers always trying to scrap off money from the customers) but I thought I'd ask

int Orders;

//+------------------------------------------------------------------+
int start()
  {
   if(Orders>OrdersTotal()) SendMail("Order Closed", "");
   if(Orders<OrdersTotal()) SendMail("Order Opened", "");
   Orders=OrdersTotal();
   return(0);
  }
//+------------------------------------------------------------------+
 

You are making a lot of invalid assumptions.

Orders is ALREADY a static variable (technically 'global' but that's static).

You do NOT need VPS for this code to run, or not run. All VPS does is (hopefully) keep you connected more reliably than a home computer, and not consume your own electricity & bandwidth. I've also heard that the 'free VPS' offerings can have VERY slow processing & be late with actioning Buy & Sell.

The reason 'pending orders' run without your computer is because the Broker/Server has been informed of the conditions when you want the trade to happen and the order is sitting on the Broker's Server.
This is usually for SL, TP & Limit orders.
If your EA suddenly says, 'Hey! I'd like to place an order 'cos my 3 indicators are lined up in a row!', then that requires your (or VPS) computer to be running & able to send the order through.
If your EA is programmed to say 'When Price hits x.xxxx, then Buy', this can be loaded beforehand as SL or TP onto the Broker Server, and does not require your (or VPS) computer to be active for the order to happen

Finally, to stop the mail being sent on startup, maybe

 static bool firsttime = true;
 if(!firsttime)
 {
   if(Orders>OrdersTotal()) SendMail("Order Closed", "");
   if(Orders<OrdersTotal()) SendMail("Order Opened", "");
   firsttime = false;
 }

(code just typed, not compiled or checked)


 
  1. That won't work, firsttime never set to false. Also remember a chart change/refresh will not reset globals but it will call deinit/init.
    bool firsttime;
    int init(){firsttime = true;}
    int start(){
      if(!firsttime)
      {
        if(Orders>OrdersTotal()) SendMail("Order Closed", "");
        if(Orders<OrdersTotal()) SendMail("Order Opened", "");
      }
      else firsttime = false;
    

  2. Orders>OrdersTotal assumes that the EA is the only one running and only running on one chart. Compute the actual open orders count
    int oo.count=0;
    for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
        OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
    &&  OrderMagicNumber() == magic.number              // my magic number, and
    &&  OrderSymbol()      == Symbol() ){               // period and symbol
        oo.count++;
    }

Reason: