Alert Position Open 2

Author: Copyright © 2019-2021, Vladimir Karputov
Miscellaneous
It issuies visual alerts to the screenIt sends emailsIt plays sound alerts
0 Views
0 Downloads
0 Favorites
Alert Position Open 2
ÿþ//+------------------------------------------------------------------+

//|                                        Alert Position Open 2.mq5 |

//|                         Copyright © 2019-2021, Vladimir Karputov |

//|                     https://www.mql5.com/ru/market/product/43516 |

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

#property copyright "Copyright © 2019-2021, Vladimir Karputov"

#property link      "https://www.mql5.com/ru/market/product/43516"

#property version   "2.003"

#property description "At the moment, 'TRADE_TRANSACTION_DEAL_ADD' comes first "

#property description "and only then 'TRADE_TRANSACTION_HISTORY_ADD'. "

#property description "Help taken from 'TypePendingOrderTriggered.mq5' code"

//--- input parameters

input bool     InpAlert          = false;          // Alert

input bool     InpEmail          = true;           // Use send email

input bool     InpNotification   = true;           // Use Push notifications

input string   InpSound          = "expert.wav";   // Play Sound ("" -> off)

input bool     InpExperts        = false;          // Tab Experts

input bool     InpComment        = true;           // Comment on Chart

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

//| Structure Catch Order                                            |

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

struct CATCH_ORDER

  {

   ulong             ul_catch_deal;          // catch deal ticket

   ulong             ul_catch_order;         // catch order ticket

   //--- Constructor

                     CATCH_ORDER()

     {

      ul_catch_deal              = 0;

      ul_catch_order             = 0;

     }

  };

CATCH_ORDER SCatchOrder[];

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

//| Expert initialization function                                   |

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

int OnInit()

  {

//--- create timer

   EventSetTimer(1);

//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//--- destroy timer

   EventKillTimer();

   if(InpComment)

      Comment("");

  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

//---

  }

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

//| Timer function                                                   |

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

void OnTimer()

  {

//---

   int size=ArraySize(SCatchOrder);

   for(int i=0; i<size; i++)

     {

      if(Sending(SCatchOrder[i].ul_catch_deal,SCatchOrder[i].ul_catch_order))

         ArrayRemove(SCatchOrder,i,1);

     }

  }

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

//| TradeTransaction function                                        |

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

void OnTradeTransaction(const MqlTradeTransaction &trans,

                        const MqlTradeRequest &request,

                        const MqlTradeResult &result)

  {

//--- get transaction type as enumeration value

   ENUM_TRADE_TRANSACTION_TYPE type=trans.type;

//--- if transaction is result of addition of the transaction in history

   if(type==TRADE_TRANSACTION_DEAL_ADD)

     {

      long deal_order=0;

      if(HistoryDealSelect(trans.deal))

        {

         //---

         long deal_type          = HistoryDealGetInteger(trans.deal,DEAL_TYPE);

         long deal_entry         = HistoryDealGetInteger(trans.deal,DEAL_ENTRY);

         ENUM_DEAL_ENTRY enum_deal_entry=(ENUM_DEAL_ENTRY)deal_entry;

         if(deal_type==DEAL_TYPE_BUY || deal_type==DEAL_TYPE_SELL)

            if(deal_entry==DEAL_ENTRY_IN)

              {

               deal_order=HistoryDealGetInteger(trans.deal,DEAL_ORDER);

               int size=ArraySize(SCatchOrder);

               ArrayResize(SCatchOrder,size+1);

               SCatchOrder[size].ul_catch_deal=trans.deal;

               SCatchOrder[size].ul_catch_order=deal_order;

              }

        }

      else

        {

         Print("ERROR: HistoryDealSelect(",trans.deal,")");

         return;

        }

     }

  }

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

//| Sending                                                          |

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

bool Sending(const ulong deal,const ulong order)

  {

   if(HistoryDealSelect(deal))

     {

      long deal_ticket        = HistoryDealGetInteger(deal,DEAL_TICKET);

      long deal_order         = HistoryDealGetInteger(deal,DEAL_ORDER);

      long deal_time          = HistoryDealGetInteger(deal,DEAL_TIME);

      long deal_time_msc      = HistoryDealGetInteger(deal,DEAL_TIME_MSC);

      long deal_type          = HistoryDealGetInteger(deal,DEAL_TYPE);

      long deal_entry         = HistoryDealGetInteger(deal,DEAL_ENTRY);

      long deal_magic         = HistoryDealGetInteger(deal,DEAL_MAGIC);

      long deal_reason        = HistoryDealGetInteger(deal,DEAL_REASON);

      long deal_position_id   = HistoryDealGetInteger(deal,DEAL_POSITION_ID);

      double deal_volume      = HistoryDealGetDouble(deal,DEAL_VOLUME);

      double deal_price       = HistoryDealGetDouble(deal,DEAL_PRICE);

      double deal_commission  = HistoryDealGetDouble(deal,DEAL_COMMISSION);

      double deal_swap        = HistoryDealGetDouble(deal,DEAL_SWAP);

      double deal_profit      = HistoryDealGetDouble(deal,DEAL_PROFIT);

      string deal_symbol      = HistoryDealGetString(deal,DEAL_SYMBOL);

      string deal_comment     = HistoryDealGetString(deal,DEAL_COMMENT);

      string deal_external_id = HistoryDealGetString(deal,DEAL_EXTERNAL_ID);

      if(HistoryOrderSelect(order))

        {

         long o_ticket              = HistoryOrderGetInteger(order,ORDER_TICKET);

         long o_time_setup          = HistoryOrderGetInteger(order,ORDER_TIME_SETUP);

         long o_type                = HistoryOrderGetInteger(order,ORDER_TYPE);

         long o_state               = HistoryOrderGetInteger(order,ORDER_STATE);

         long o_time_expiration     = HistoryOrderGetInteger(order,ORDER_TIME_EXPIRATION);

         long o_time_done           = HistoryOrderGetInteger(order,ORDER_TIME_DONE);

         long o_time_setup_msc      = HistoryOrderGetInteger(order,ORDER_TIME_SETUP_MSC);

         long o_time_done_msc       = HistoryOrderGetInteger(order,ORDER_TIME_DONE_MSC);

         long o_type_filling        = HistoryOrderGetInteger(order,ORDER_TYPE_FILLING);

         long o_type_time           = HistoryOrderGetInteger(order,ORDER_TYPE_TIME);

         long o_magic               = HistoryOrderGetInteger(order,ORDER_MAGIC);

         long o_reason              = HistoryOrderGetInteger(order,ORDER_REASON);

         long o_position_id         = HistoryOrderGetInteger(order,ORDER_POSITION_ID);

         long o_position_by_id      = HistoryOrderGetInteger(order,ORDER_POSITION_BY_ID);

         double o_volume_initial    = HistoryOrderGetDouble(order,ORDER_VOLUME_INITIAL);

         double o_volume_current    = HistoryOrderGetDouble(order,ORDER_VOLUME_CURRENT);

         double o_open_price        = HistoryOrderGetDouble(order,ORDER_PRICE_OPEN);

         double o_sl                = HistoryOrderGetDouble(order,ORDER_SL);

         double o_tp                = HistoryOrderGetDouble(order,ORDER_TP);

         double o_price_current     = HistoryOrderGetDouble(order,ORDER_PRICE_CURRENT);

         double o_price_stoplimit   = HistoryOrderGetDouble(order,ORDER_PRICE_STOPLIMIT);

         string o_symbol            = HistoryOrderGetString(order,ORDER_SYMBOL);

         string o_comment           = HistoryOrderGetString(order,ORDER_COMMENT);

         string o_extarnal_id       = HistoryOrderGetString(order,ORDER_EXTERNAL_ID);

         string str_o_time_setup       = TimeToString((datetime)o_time_setup,TIME_DATE|TIME_MINUTES|TIME_SECONDS);

         string str_o_type             = EnumToString((ENUM_ORDER_TYPE)o_type);

         string str_o_state            = EnumToString((ENUM_ORDER_STATE)o_state);

         string str_o_time_expiration  = TimeToString((datetime)o_time_expiration,TIME_DATE|TIME_MINUTES|TIME_SECONDS);

         string str_o_time_done        = TimeToString((datetime)o_time_done,TIME_DATE|TIME_MINUTES|TIME_SECONDS);

         string str_o_type_filling     = EnumToString((ENUM_ORDER_TYPE_FILLING)o_type_filling);

         string str_o_type_time        = TimeToString((datetime)o_type_time,TIME_DATE|TIME_MINUTES|TIME_SECONDS);

         string str_o_reason           = EnumToString((ENUM_ORDER_REASON)o_reason);

         //---

         string str_deal=(deal_type==DEAL_TYPE_BUY)?"buy":"sell";

         int digits=(int)SymbolInfoInteger(deal_symbol,SYMBOL_DIGITS);

         string text="deal #"+

                     IntegerToString(deal_ticket)+" "+

                     str_deal+" "+

                     DoubleToString(deal_volume,digits)+" "+

                     deal_symbol+" "+"at"+" "+

                     DoubleToString(deal_price,digits)+" ";

         if((deal_type==DEAL_TYPE_BUY && o_type!=ORDER_TYPE_BUY) ||(deal_type==DEAL_TYPE_SELL && o_type!=ORDER_TYPE_SELL))

           {

            string str_order="";

            if(o_type==ORDER_TYPE_BUY)

               str_order="buy";

            if(o_type==ORDER_TYPE_SELL)

               str_order="sell";

            if(o_type==ORDER_TYPE_BUY_LIMIT)

               str_order="buy limit";

            if(o_type==ORDER_TYPE_SELL_LIMIT)

               str_order="sell limit";

            if(o_type==ORDER_TYPE_BUY_STOP)

               str_order="buy stop";

            if(o_type==ORDER_TYPE_SELL_STOP)

               str_order="sell stop";

            if(o_type==ORDER_TYPE_BUY_STOP_LIMIT)

               str_order="buy stop limit";

            if(o_type==ORDER_TYPE_SELL_STOP_LIMIT)

               str_order="sell stop limit";

            if(o_type==ORDER_TYPE_CLOSE_BY)

               str_order="close by";

            text=text+" "+"[#"+IntegerToString(o_ticket)+" "+str_order+"]";

           }

         if(InpAlert)

            Alert(text);

         if(InpEmail)

            if(!SendMail("IN",text))

               Print("ERROR: SendMail");

         if(InpNotification)

            if(!SendNotification(text))

               Print("ERROR: SendNotification");

         if(InpSound!="")

            PlaySound(InpSound);

         if(InpExperts)

            Print(text);

         if(InpComment)

            Comment(text);

         //---

         return(true);

        }

      else

        {

         Print("ERROR: HistoryOrderSelect(",order,")");

         return(false);

        }

     }

   else

     {

      Print("ERROR: HistoryDealSelect(",deal,")");

      return(false);

     }

//---

   return(false);

  }

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

Comments