Sound Alert Entry Out 2

Author: Copyright © 2017-2021, Vladimir Karputov
Miscellaneous
It issuies visual alerts to the screenIt plays sound alerts
0 Views
0 Downloads
0 Favorites
Sound Alert Entry Out 2
ÿþ//+------------------------------------------------------------------+

//|                                      Sound Alert Entry Out 2.mq5 |

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

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

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

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

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

#property version   "2.000"

//---

enum ENUM_SOUNDS

  {

   alert       = 0,  // alert

   alert2      = 1,  // alert2

   connect     = 2,  // connect

   disconnect  = 3,  // disconnect

   email       = 4,  // email

   expert      = 5,  // expert

   news        = 6,  // news

   ok          = 7,  // ok

   request     = 8,  // request

   stops       = 9,  // stops

   tick        = 10, // tick

   timeout     = 11, // timeout

   wait        = 12, // wait

   none        = 13, // NONE sound

  };

//---

input ENUM_SOUNDS InpSoundName      = alert2;   // Sound Name (if 'NONE sound' -> sound OFF)

input bool        InpAlert          = true;     // Use Alert

input bool        InpNotification   = true;     // Use Push notifications

input bool        Inp_I_am_live     = true;     // Use message "I am live" every hour

input bool        InpDailyResult    = true;     // Use daily trading account result

//---

string filename="";

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

//| Expert initialization function                                   |

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

int OnInit()

  {

//---

   switch(InpSoundName)

     {

      case 0:

         filename="alert.wav";

         break;

      case 1:

         filename="alert2.wav";

         break;

      case 2:

         filename="connect .wav";

         break;

      case 3:

         filename="disconnect.wav";

         break;

      case 4:

         filename="email.wav";

         break;

      case 5:

         filename="expert.wav";

         break;

      case 6:

         filename="news.wav";

         break;

      case 7:

         filename="ok.wav";

         break;

      case 8:

         filename="request.wav";

         break;

      case 9:

         filename="stops.wav";

         break;

      case 10:

         filename="tick.wav";

         break;

      case 11:

         filename="timeout.wav";

         break;

      case 12:

         filename="wait.wav";

         break;

     }

//--- create timer

   ResetLastError();

   if(!EventSetTimer(60*60))

     {

      string text="EventSetTimer Error# "+IntegerToString(GetLastError());

      Alert(text);

      SendNotification(text);

     }

//---

   string text="INIT_SUCCEEDED";

   if(InpAlert)

      Alert(text);

   if(InpNotification)

      SendNotification(text);

//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---

   string text="";

   switch(reason)

     {

      case  REASON_PROGRAM:

         text="REASON_PROGRAM";

         break;

      case  REASON_REMOVE:

         text="REASON_REMOVE";

         break;

      case  REASON_RECOMPILE:

         text="REASON_RECOMPILE";

         break;

      case  REASON_CHARTCHANGE:

         text="REASON_CHARTCHANGE";

         break;

      case  REASON_CHARTCLOSE:

         text="REASON_CHARTCLOSE";

         break;

      case  REASON_PARAMETERS:

         text="REASON_PARAMETERS";

         break;

      case  REASON_ACCOUNT:

         text="REASON_ACCOUNT";

         break;

      case  REASON_TEMPLATE:

         text="REASON_TEMPLATE";

         break;

      case  REASON_INITFAILED:

         text="REASON_INITFAILED";

         break;

      case  REASON_CLOSE:

         text="REASON_CLOSE";

         break;

      default:

         text="NON REASON";

         break;

     }

//---

   if(InpAlert)

      Alert(text);

   if(InpNotification)

      SendNotification(text);

  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

//---

  }

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

//| Timer function                                                   |

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

void OnTimer()

  {

//---

   if(InpDailyResult)

     {

      string text=TimeToString(TimeCurrent())+": I am live";

      if(InpAlert)

         Alert(text);

      if(InpNotification)

         SendNotification(text);

     }

  }

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

//| 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_type=0;

      long     deal_entry=0;

      double   deal_volume=0.0;

      double   deal_commission=0.0;

      double   deal_swap=0.0;

      double   deal_profit=0.0;

      string   deal_symbol="NON";

      if(HistoryDealSelect(trans.deal))

        {

         deal_type      = HistoryDealGetInteger(trans.deal,DEAL_TYPE);

         deal_entry     = HistoryDealGetInteger(trans.deal,DEAL_ENTRY);

         deal_volume    = HistoryDealGetDouble(trans.deal,DEAL_VOLUME);

         deal_commission= HistoryDealGetDouble(trans.deal,DEAL_COMMISSION);

         deal_swap      = HistoryDealGetDouble(trans.deal,DEAL_SWAP);

         deal_profit    = HistoryDealGetDouble(trans.deal,DEAL_PROFIT);

         deal_symbol    = HistoryDealGetString(trans.deal,DEAL_SYMBOL);

        }

      else

         return;

      if(deal_entry!=DEAL_ENTRY_IN)

        {

         string day_profit="";

         if(InpDailyResult)

            day_profit=DoubleToString(RequestTradeHistory(),3);

         if(InpSoundName!=none)

            PlaySound(filename);

         if(InpAlert || InpNotification)

           {

            string text="deal #"+IntegerToString(trans.deal);

            string str_type="NON";

            if(deal_type==DEAL_TYPE_BUY)

               str_type=" buy ";

            else

               if(deal_type==DEAL_TYPE_SELL)

                  str_type=" sell ";

            text=text+str_type+DoubleToString(deal_volume,2)+" "+deal_symbol+

                 ", commission: "+DoubleToString(deal_commission,2)+

                 ", swap: "+DoubleToString(deal_swap,2)+

                 ", profit: "+DoubleToString(deal_profit,2)+" "+AccountInfoString(ACCOUNT_CURRENCY);

            if(InpDailyResult)

               text=text+", day profit: "+day_profit;

            if(InpAlert)

               Alert(text);

            if(InpNotification)

               SendNotification(text);

           }

        }

     }

  }

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

//| Request trade history                                            |

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

double RequestTradeHistory()

  {

   double result=0.0;

//--- request trade history

   datetime from_date         = iTime(Symbol(),PERIOD_D1,0);

   datetime to_date           = TimeCurrent()+86400;

   HistorySelect(from_date,to_date);

   uint total_deals           = HistoryDealsTotal();

   ulong ticket_history_deal  = 0;

   if(total_deals>0)

     {

      //--- for all deals

      for(uint i=0; i<total_deals; i++)

        {

         //--- try to get deals ticket_history_deal

         if((ticket_history_deal=HistoryDealGetTicket(i))>0)

           {

            long     deal_type         = HistoryDealGetInteger(ticket_history_deal,DEAL_TYPE);

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

              {

               long     deal_entry        = HistoryDealGetInteger(ticket_history_deal,DEAL_ENTRY);

               if(deal_entry!=DEAL_ENTRY_IN)

                 {

                  double   deal_commission   = HistoryDealGetDouble(ticket_history_deal,DEAL_COMMISSION);

                  double   deal_swap         = HistoryDealGetDouble(ticket_history_deal,DEAL_SWAP);

                  double   deal_profit       = HistoryDealGetDouble(ticket_history_deal,DEAL_PROFIT);

                  //---

                  result=result+deal_commission+deal_swap+deal_profit;

                 }

              }

           }

        }

     }

//---

   return(result);

  }

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

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---