OrderNotify

Download script
Author: 2017, Tomas Hruby - Parbest.com
OrderNotify

This script is an automated monitoring tool designed for the MetaTrader 4 platform. Its primary purpose is to keep you informed of trading activity by sending real-time email notifications whenever a new trade is opened or an existing one is closed on your account.

How It Works

The script operates as a "listener" that constantly monitors your account's active positions. Here is the step-by-step logic it follows:

  • Initial Startup: When you first run the script, it performs a quick check to record how many trades are currently open. This creates a baseline so that it doesn't send unnecessary notifications for trades that were already active before the script started.
  • Real-Time Monitoring: The script continuously tracks the number of open orders. Every time the market updates (a "tick"), it compares the current number of open positions against the previous count.
  • Detecting New Trades: If the script detects that the number of open positions has increased, it identifies that a new trade has been opened. It automatically gathers specific details about this new trade—such as the currency pair, the type of order (e.g., BUY or SELL), the volume (lots), and the current profit—and sends you an email alert.
  • Detecting Closed Trades: Conversely, if the number of open positions decreases, the script recognizes that a trade has been closed. It then scans your trading history to find the most recent completed transaction and sends you an email summary including the same relevant details (symbol, order type, profit, etc.).
  • Account Summary: Every notification sent to your email is enriched with your overall account status. This includes your account number, current balance, and total floating profit/loss in your specific currency, allowing you to see the "big picture" of your portfolio at a glance without having to open the trading platform.

Key Features

  • Automated Tracking: You don't need to manually check your trades; the script pushes the information to your inbox as it happens.
  • Detailed Reporting: Instead of just saying "a trade was closed," the script provides specific figures like profit, swap, and account balance.
  • Smart Filtering: By comparing the "old" total to the "handle" (current) total, it ensures you only receive notifications for actual changes in your trading activity.
Orders Execution
Checks for the total of open ordersChecks for the total of closed orders
Miscellaneous
It sends emails
10 Views
0 Downloads
0 Favorites
OrderNotify
ÿþ//+--------------------- ---------------------------------------------+

//|                                                   OrderNotify.mq4 |

//|                                2017, Tomas Hruby / Parbest s.r.o. |

//|                                             http://tomashruby.com |

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

#property copyright   "2017, Tomas Hruby - Parbest.com"

#property link        "http://tomashruby.com" 

#property version     "1.0"; 

#property description "OrderNotify is Expert Advisor for email monitoring newly opened and closed orders.";

#property strict



int   OrdersTotalOld = 0;

int   OrdersTotalHandle = 0;

bool  first = true;



void OnTick(){



    OrdersTotalHandle = OrdersTotal();

 

    // If this is the first launch of the Expert, we don't know the amount of orders on the previous tick.

    // So just remeber it, mark that the first launch has been happened, and exit.

    if(first){

        OrdersTotalOld = OrdersTotalHandle;

        first = false;

    }else{ 

      // Compare the amount of positions on the previous tick with the current amount

      // If it has been changed, display message

       if(OrdersTotalHandle > OrdersTotalOld){

           SendMail("New order ",StringConcatenate(InfoOpened(),"\n",accountDetails()));

       }

       if(OrdersTotalHandle < OrdersTotalOld){

           SendMail("Closed order ",StringConcatenate(InfoClosed(),"\n",accountDetails()));

       }

       

      // Memorize the amount of positions

      OrdersTotalOld = OrdersTotalHandle;

   }

}



string InfoClosed(){

   string info;

   for(int i=OrdersHistoryTotal();i>=OrdersHistoryTotal()-1;i--)

   {

      if(OrderSelect(i, SELECT_BY_POS,MODE_HISTORY)){

         info = orderDetails();

      }

   }

   return(info);

}   



string InfoOpened(){

   string info;

   ulong orderOpenTime,lastOpenTime=0;

   int latestTicket=0;

   for(int r=0;r<OrdersTotal();r++){

      if(OrderSelect(r,SELECT_BY_POS,MODE_TRADES)==true){   

         orderOpenTime = OrderOpenTime();

         if(orderOpenTime > lastOpenTime){

            lastOpenTime = orderOpenTime;

            latestTicket = OrderTicket();

         }

      }

   }

   if(latestTicket>0){

      if(OrderSelect(latestTicket,SELECT_BY_TICKET,MODE_TRADES)){

         info = orderDetails(); 

      }

   }

   return(info);

}



string orderDetails(){

   string info = StringConcatenate(OrderSymbol()," ",CmdPrint(OrderType())," ",DoubleToStr(OrderLots(),2),": ",DoubleToStr(OrderProfit(),2)," (",DoubleToStr(OrderSwap(),2),") ",AccountCurrency());

   return(info);

}



string accountDetails(){

   string info = StringConcatenate("Account: ",AccountNumber(),", balance: ",DoubleToStr(AccountBalance(),2)," ",AccountCurrency(),", profit: ",DoubleToStr(AccountProfit(),2)," ",AccountCurrency());

   return(info);

}





string CmdPrint(int Cmd){

   if(Cmd==OP_BUY){

      return ("BUY");

   }else if(Cmd==OP_BUYSTOP){

         return ("BUY STOP");

   }else if(Cmd==OP_BUYLIMIT){

         return ("BUY LIMIT");

   }else if(Cmd==OP_SELL){

         return ("SELL");

   }else if(Cmd==OP_SELLSTOP){

         return ("SELL STOP");                        

   }else if(Cmd==OP_SELLLIMIT){

      return ("SELL LIMIT");

   }else{

      return("");

   }

}

Comments