FunnySounds

Author: Copyright 2023, Yevhenii Levchenko
Orders Execution
Checks for the total of closed ordersChecks for the total of open orders
Miscellaneous
It plays sound alerts
0 Views
0 Downloads
0 Favorites
FunnySounds
ÿþ//+------------------------------------------------------------------+

//|                                                  FunnySounds.mq4 |

//|                               Copyright 2023, Yevhenii Levchenko |

//|                          https://www.mql5.com/ru/users/levchenko |

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

#property copyright "Copyright 2023, Yevhenii Levchenko"

#property link      "https://www.mql5.com/ru/users/levchenko"

#property version   "1.00"

#property strict

#property indicator_chart_window

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

//| Custom indicator initialization function                         |

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

input string InputOpenPositionSound = "\\Files\\Sounds\\mortal_kombat_choose.wav",

             InputTakeProfitSound = "\\Files\\Sounds\\mario_coin_sound.wav",

             InputStopLossSound = "\\Files\\Sounds\\mario_lost_a_life.wav",

             InputAllInSound = "\\Files\\Sounds\\mortal_combat.wav",

             InputMarginCallSound = "\\Files\\Sounds\\mario_castle_theme.wav",

             InputStopoutSound = "\\Files\\Sounds\\mortal_combat_fatality.wav",

             StopOutPrefix = "[stopout]";



datetime playTimeDelayInSeconds = 3;



enum ENUM_POSITION_RESULT {PROFIT,LOSS,STOPOUT,NOT_TRADE_OPERATION};



int positionsTotal, positionsHistoryTotal;

datetime playTime, marginCallEventTime, allInEventTime;

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

//|                                                                  |

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

int OnInit() {

   positionsTotal = positionsTotal();

   positionsHistoryTotal = OrdersHistoryTotal();

   return(INIT_SUCCEEDED);

}

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

//|                                                                  |

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

void OnDeinit(const int reason) {

   playSound("");

   Comment("");

   positionsTotal = 0;

   positionsHistoryTotal = 0;

}

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

//| Custom indicator iteration function                              |

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

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const datetime &time[],

                const double &open[],

                const double &high[],

                const double &low[],

                const double &close[],

                const long &tick_volume[],

                const long &volume[],

                const int &spread[]) {



   if(positionsTotal < positionsTotal()) {

      playSound(InputOpenPositionSound);

   }

   positionsTotal = positionsTotal();

   if(isAllIn()) {

      if(playSound(InputAllInSound))

         allInEventTime = TimeCurrent();

   }



   if(isMarginCall()) {

      if(playSound(InputMarginCallSound))

         marginCallEventTime = TimeCurrent();

   }



   if(positionsHistoryTotal < OrdersHistoryTotal()) {

      bool success = false;

      switch(checkHistory()) {

      case PROFIT:

         success = playSound(InputTakeProfitSound);

         break;

      case LOSS:

         success = playSound(InputStopLossSound);

         break;

      case STOPOUT:

         success = playSound(InputStopoutSound);

         break;

      }

      if (success)

         positionsHistoryTotal = OrdersHistoryTotal();

   }



   return(rates_total);

}

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

//| Only open positions are counted (without pending orders)         |

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

int positionsTotal() {

   int total = OrdersTotal();

   int counter = 0;

   for(int i = 0; i < total && !IsStopped(); i++) {

      if(OrderSelect(i,SELECT_BY_POS)) {

         if(OrderType() == OP_BUY || OrderType() == OP_SELL )

            counter++;

      }

   }

   return counter;

}



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

//| Checking last deal result                                        |

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

ENUM_POSITION_RESULT checkHistory() {

   int total = OrdersHistoryTotal();

   ENUM_POSITION_RESULT result = NOT_TRADE_OPERATION;



   int i = 0;

   if(total > 10) i = total - 10;

   for(i = 0; i < total && !IsStopped(); i++) {

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

         if(OrderType() == OP_BUY || OrderType() == OP_SELL ) {

            if(StringFind(OrderComment(),StopOutPrefix,0) >= 0) // search for a marker of forced closing of a position due to reaching the stop out level

               result = STOPOUT;

            else if(OrderProfit() + OrderSwap() + OrderCommission() > 0)

               result = PROFIT;

            else if (OrderProfit() + OrderSwap() + OrderCommission() <= 0)

               result = LOSS;

         } else result = NOT_TRADE_OPERATION;

      }

   }

   return result;

}

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

//| Playing sound                                                    |

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

bool playSound(string sound) {

//Comment("Playing "+sound);

   if(MQLInfoInteger(MQL_TESTER) == false && TimeCurrent() - playTime >= playTimeDelayInSeconds) {

      if(!PlaySound(sound)) {

         printf("%s error:",__FUNCTION__,GetLastError());

         return false;

      } else {

         playTime = TimeCurrent();

         return true;

      }

   }

   return false;

}

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

//| Checking for margin call event                                   |

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

bool isMarginCall() {

   if(TimeCurrent() - marginCallEventTime < PeriodSeconds(PERIOD_M30)) return false; // this is necessary in order to avoid too frequent detection of the event

   if (AccountInfoInteger(ACCOUNT_MARGIN_SO_MODE) == ACCOUNT_STOPOUT_MODE_MONEY) {

      return AccountInfoDouble(ACCOUNT_EQUITY) <= AccountInfoDouble(ACCOUNT_MARGIN_SO_CALL);

   }

   if(AccountInfoDouble(ACCOUNT_MARGIN_LEVEL) <= 0) return false;

   return AccountInfoDouble(ACCOUNT_MARGIN_LEVEL) <= AccountInfoDouble(ACCOUNT_MARGIN_SO_CALL);

}



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

//| Checking for "all in" event :)                                   |

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

bool isAllIn() {

   if(TimeCurrent() - allInEventTime < PeriodSeconds(PERIOD_M30)) return false; // this is necessary in order to avoid too frequent detection of the event

   if(AccountInfoDouble(ACCOUNT_MARGIN_LEVEL) <= 0) return false;

   int leverage = (int)AccountInfoInteger(ACCOUNT_LEVERAGE);

   if(leverage >= 500) return AccountInfoDouble(ACCOUNT_MARGIN_LEVEL) <= 300;

   else if(leverage >= 300) return AccountInfoDouble(ACCOUNT_MARGIN_LEVEL) <= 250;

   else if(leverage >= 100) return AccountInfoDouble(ACCOUNT_MARGIN_LEVEL) <= 125;



   return AccountInfoDouble(ACCOUNT_MARGIN_LEVEL) <= 110;

}

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

Comments