/*
   Generated by EX4-TO-MQ4 decompiler V4.0.225.1g []
   Website: http://purebeam.biz
   E-mail : purebeam@gmail.com
*/
#include <stdlib.mqh>

extern string Expert_Name = "Money Manager";
extern string s1 = "-- StopLoss & TakeProfit ------------------------------------";
extern bool Automatic_SL = FALSE;
extern double Automatic_SL_Ratio = 2.0;
extern double Auto_Max_SL = 150.0;
extern double StopLoss = 30.0;
extern string s11 = "-------------------------------";
extern bool Automatic_TP = FALSE;
extern double Automatic_TP_Ratio = 2.0;
extern double TakeProfit = 60.0;
extern string s12 = "-------------------------------";
extern bool Use_First_TakeProfit = FALSE;
extern double First_TakeProfit_At = 15.0;
extern int First_TakeProfit_Percent = 50;
extern bool AlertSound = TRUE;
extern string s0 = "-- Trailing Step & Set Breakeven ----------------";
extern bool Use_TrailingStep = TRUE;
extern double Start_Trailing_At = 15.0;
extern double TrailingStep = 2.0;
extern string s01 = "-------------------------------";
extern bool Use_Set_BreakEven = FALSE;
extern double LockPips = 1.0;
extern double Set_BreakEvenAt = 10.0;
extern string s2 = "--------------------------------------------------------------------";
extern double MaxSlippage = 3.0;
extern string s3 = "--------------------------------------------------------------------";
extern bool Allow_Drag_PriceLines = FALSE;
extern string s4 = "--------------------------------------------------------------------";
extern bool AccountIsMicro = TRUE;
extern bool ShowInfo = TRUE;
string gs_dummy_276;
int gi_284 = 1;
double gd_288;
int g_bars_296 = -1;

int init() {
   Expert_Name = "Money Manager";
   if (AccountIsMicro) gi_284 = 2;
   else gi_284 = 1;
   gd_288 = Point_Broker();
   MaxSlippage *= SetBrokerX();
   if (Use_First_TakeProfit && First_TakeProfit_Percent < 1 || First_TakeProfit_Percent > 100) Alert("Error: First_TakeProfit_Percent setting value must be set between 1 and 100");
   if (IsExpertEnabled()) Comment("\n\n" + Expert_Name + " initialized\n\nWaiting for price data...");
   else Comment("\n\n" + Expert_Name + " is DISABLED...");
   g_bars_296 = -1;
   return (0);
}

int start() {
   string ls_unused_0;
   string ls_8;
   string ls_16;
   string ls_24;
   string ls_32;
   string ls_40;
   string ls_48;
   string l_dbl2str_56;
   if (g_bars_296 != Bars) {
      g_bars_296 = Bars;
      Ares();
   }
   if (ShowInfo) {
      ls_unused_0 = "";
      ls_8 = "";
      ls_16 = "";
      ls_24 = "";
      ls_32 = "";
      ls_40 = "";
      ls_48 = "";
      if (Digits == 5 || Digits == 3) l_dbl2str_56 = DoubleToStr(NormalizeDouble(MarketInfo(Symbol(), MODE_SPREAD) / 10.0, 1), 1);
      else l_dbl2str_56 = DoubleToStr(NormalizeDouble(MarketInfo(Symbol(), MODE_SPREAD), 0), 0);
      if (Use_TrailingStep) ls_8 = "\nTrailing Step: ON (Start at +" + DoubleToStr(Start_Trailing_At, 0) + " pips. Step " + DoubleToStr(TrailingStep, 0) + ")";
      else ls_8 = "\nTrailing Step: OFF";
      if (Use_Set_BreakEven) ls_16 = "\nSet Breakeven: ON (at +" + DoubleToStr(Set_BreakEvenAt, 0) + " pips)";
      else ls_16 = "\nSet Breakeven: OFF";
      if (Use_First_TakeProfit) ls_24 = "\nFirst TakeProfit: ON (" + DoubleToStr(First_TakeProfit_Percent, 0) + "% at +" + DoubleToStr(First_TakeProfit_At, 0) + " pips)";
      else ls_24 = "\nFirst TakeProfit: OFF";
      if (Automatic_SL) ls_32 = "\nStopLoss: " + DoubleToStr(StopLoss, 0) + " pips (automatic) ratio: " + DoubleToStr(Automatic_SL_Ratio, 0);
      else ls_32 = "\nStopLoss: " + DoubleToStr(StopLoss, 0) + " pips";
      if (Automatic_TP) ls_40 = "\nTakeProfit: " + DoubleToStr(TakeProfit, 0) + " pips (automatic) ratio: " + DoubleToStr(Automatic_TP_Ratio, 0);
      else ls_40 = "\nTakeProfit: " + DoubleToStr(TakeProfit, 0) + " pips";
      ls_40 = ls_40 
      + "\n---------------------------------------------------";
      if (IsTradeAllowed() == FALSE) ls_48 = "\nTrade NOT Allowed";
      Comment("\n          " + Expert_Name 
         + "\n" 
         + "---------------------------------------------------" + ls_8 + ls_16 + ls_24 + ls_32 + ls_40 
         + "\n" 
         + "Pip Value: " + DoubleToStr(MarketInfo(Symbol(), MODE_TICKVALUE), 2) 
         + "\n" 
         + "Spread: " + DoubleToStr(MarketInfo(Symbol(), MODE_SPREAD) / 10.0, 1) + " pips" 
         + "\n" 
         + "Swap Long: " + DoubleToStr(MarketInfo(Symbol(), MODE_SWAPLONG), 2) + " pips" 
         + "\n" 
         + "Swap Short: " + DoubleToStr(MarketInfo(Symbol(), MODE_SWAPSHORT), 2) + " pips" 
         + "\n" 
      + "---------------------------------------------------" + ls_48);
   } else Comment("");
   Check_TrailingStop();
   if (Allow_Drag_PriceLines) Drag_Lines();
   return (0);
}

int Check_TrailingStop() {
   double l_lots_0;
   double ld_8;
   double ld_16;
   int l_error_24;
   string l_dbl2str_28;
   double l_point_36;
   double ld_44;
   double ld_52;
   double ld_60;
   double ld_68;
   double ld_76;
   double ld_84;
   double l_price_92;
   for (int l_pos_100 = 0; l_pos_100 < OrdersTotal(); l_pos_100++) {
      OrderSelect(l_pos_100, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == 0) {
         if (MarketInfo(OrderSymbol(), MODE_POINT) == 0.00001) l_point_36 = 0.0001;
         else {
            if (MarketInfo(OrderSymbol(), MODE_POINT) == 0.001) l_point_36 = 0.01;
            else l_point_36 = MarketInfo(OrderSymbol(), MODE_POINT);
         }
         ld_44 = TrailingStep * l_point_36;
         ld_52 = Start_Trailing_At * l_point_36;
         ld_60 = LockPips * l_point_36;
         ld_68 = Set_BreakEvenAt * l_point_36;
         ld_76 = TakeProfit * l_point_36;
         ld_76 = NormalizeDouble(ld_76, Digits);
         if (OrderType() == OP_BUY) {
            if (Use_TrailingStep && Bid - OrderOpenPrice() >= ld_52) {
               if (Bid - OrderStopLoss() >= ld_52 + ld_44) {
                  Del_Lin();
                  OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss() + ld_44, OrderTakeProfit(), 0);
               }
            }
            if (Use_First_TakeProfit && OrderComment() == "") {
               if (Bid - OrderOpenPrice() >= First_TakeProfit_At * l_point_36) {
                  Del_Lin();
                  ld_8 = First_TakeProfit_Percent;
                  ld_16 = ld_8 / 100.0;
                  l_lots_0 = NormalizeDouble(OrderLots() * ld_16, gi_284);
                  if (OrderClose(OrderTicket(), l_lots_0, Bid, MaxSlippage, Yellow) == 0) {
                     l_error_24 = GetLastError();
                     l_dbl2str_28 = DoubleToStr(Bid, Digits);
                     Print("ERROR closing BUY order (FIRST TakeProfit) : (", l_error_24, ") " + ErrorDescription(l_error_24) + " | Lots: " + DoubleToStr(l_lots_0, gi_284) + " | at " +
                        l_dbl2str_28);
                     continue;
                  }
                  l_dbl2str_28 = DoubleToStr(OrderClosePrice(), Digits);
                  Print("FIRST TakeProfit close Buy order: " + DoubleToStr(l_lots_0, gi_284) + " Lots at " + l_dbl2str_28);
                  if (!(AlertSound)) continue;
                  PlaySound("news.wav");
                  continue;
               }
            }
            if (OrderStopLoss() == 0.0 && StopLoss != 0.0) {
               Del_Lin();
               ld_84 = OrderOpenPrice() - StopLoss * l_point_36;
               l_price_92 = ValidStopLoss(0, Bid, ld_84);
               l_price_92 = NormalizeDouble(l_price_92, Digits);
               if (OrderTakeProfit() == 0.0 && ld_76 != 0.0) OrderModify(OrderTicket(), OrderOpenPrice(), l_price_92, OrderOpenPrice() + ld_76, 0);
               else OrderModify(OrderTicket(), OrderOpenPrice(), l_price_92, OrderTakeProfit(), 0);
            }
            if (Use_Set_BreakEven && Bid - OrderOpenPrice() >= ld_68) {
               if (OrderStopLoss() < OrderOpenPrice()) {
                  Del_Lin();
                  OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() + ld_60, OrderTakeProfit(), 0);
               }
            }
         } else {
            if (OrderType() == OP_SELL) {
               if (Use_TrailingStep && OrderOpenPrice() - Ask >= ld_52) {
                  if (OrderStopLoss() - Ask >= ld_52 + ld_44) {
                     Del_Lin();
                     OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss() - ld_44, OrderTakeProfit(), 0);
                  }
               }
               if (Use_First_TakeProfit && OrderComment() == "") {
                  if (OrderOpenPrice() - Ask >= First_TakeProfit_At * l_point_36) {
                     Del_Lin();
                     ld_8 = First_TakeProfit_Percent;
                     ld_16 = ld_8 / 100.0;
                     l_lots_0 = NormalizeDouble(OrderLots() * ld_16, gi_284);
                     if (OrderClose(OrderTicket(), l_lots_0, Ask, MaxSlippage, Yellow) == 0) {
                        l_error_24 = GetLastError();
                        l_dbl2str_28 = DoubleToStr(Ask, Digits);
                        Print("ERROR closing SELL order (FIRST TakeProfit) : (", l_error_24, ") " + ErrorDescription(l_error_24) + " | Lots: " + DoubleToStr(l_lots_0, gi_284) + " | at " +
                           l_dbl2str_28);
                        continue;
                     }
                     l_dbl2str_28 = DoubleToStr(OrderClosePrice(), Digits);
                     Print("FIRST TakeProfit close Sell order: " + DoubleToStr(l_lots_0, gi_284) + " Lots at " + l_dbl2str_28);
                     if (!(AlertSound)) continue;
                     PlaySound("news.wav");
                     continue;
                  }
               }
               if (OrderStopLoss() == 0.0 && StopLoss != 0.0) {
                  Del_Lin();
                  ld_84 = OrderOpenPrice() + StopLoss * l_point_36;
                  l_price_92 = ValidStopLoss(1, Ask, ld_84);
                  l_price_92 = NormalizeDouble(l_price_92, Digits);
                  if (OrderTakeProfit() == 0.0 && ld_76 != 0.0) OrderModify(OrderTicket(), OrderOpenPrice(), l_price_92, OrderOpenPrice() - ld_76, 0);
                  else OrderModify(OrderTicket(), OrderOpenPrice(), l_price_92, OrderTakeProfit(), 0);
               }
               if (Use_Set_BreakEven && OrderOpenPrice() - Ask >= ld_68) {
                  if (OrderStopLoss() > OrderOpenPrice()) {
                     Del_Lin();
                     OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() - ld_60, OrderTakeProfit(), 0);
                  }
               }
            }
         }
      }
   }
   return (0);
}

double ValidStopLoss(int ai_unused_0, double ad_unused_4, double ad_12) {
   return (ad_12);
}

int Drag_Lines() {
   string l_name_0;
   int li_8;
   int li_12;
   string ls_16 = Symbol();
   for (int l_pos_24 = 0; l_pos_24 <= OrdersTotal(); l_pos_24++) {
      if (OrderSelect(l_pos_24, SELECT_BY_POS, MODE_TRADES)) {
         if (OrderMagicNumber() == 0) {
            if (OrderSymbol() == ls_16) {
               if (OrderStopLoss() > 0.0) {
                  l_name_0 = "_SL" + OrderTicket();
                  li_8 = MathRound(OrderStopLoss() / Point);
                  if (ObjectFind(l_name_0) < 0) ObjectCreate(l_name_0, OBJ_HLINE, 0, TimeCurrent(), OrderStopLoss());
                  ObjectSet(l_name_0, OBJPROP_COLOR, Gold);
                  li_12 = MathRound(ObjectGet(l_name_0, OBJPROP_PRICE1) / Point);
                  ObjectSet(l_name_0, OBJPROP_PRICE1, li_12 * Point);
                  if (li_12 != li_8) {
                     if (!OrderModify(OrderTicket(), OrderOpenPrice(), li_12 * Point, OrderTakeProfit(), OrderExpiration())) {
                        Print("ERROR OrderModify(SL): ", ErrorDescription(GetLastError()));
                        ObjectSet(l_name_0, OBJPROP_PRICE1, li_8 * Point);
                     }
                  }
                  ObjectSet(l_name_0, OBJPROP_BACK, TRUE);
               }
               if (OrderTakeProfit() > 0.0) {
                  l_name_0 = "_TP" + OrderTicket();
                  li_8 = MathRound(OrderTakeProfit() / Point);
                  if (ObjectFind(l_name_0) < 0) ObjectCreate(l_name_0, OBJ_HLINE, 0, TimeCurrent(), OrderTakeProfit());
                  ObjectSet(l_name_0, OBJPROP_COLOR, Gold);
                  li_12 = MathRound(ObjectGet(l_name_0, OBJPROP_PRICE1) / Point);
                  ObjectSet(l_name_0, OBJPROP_PRICE1, li_12 * Point);
                  if (li_12 != li_8) {
                     if (!OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), li_12 * Point, OrderExpiration())) {
                        Print("ERROR OrderModify(TP): ", ErrorDescription(GetLastError()));
                        ObjectSet(l_name_0, OBJPROP_PRICE1, li_8 * Point);
                     }
                  }
                  ObjectSet(l_name_0, OBJPROP_BACK, TRUE);
               }
               if (OrderType() > OP_SELL) {
                  l_name_0 = "_OP" + OrderTicket();
                  li_8 = MathRound(OrderOpenPrice() / Point);
                  if (ObjectFind(l_name_0) < 0) ObjectCreate(l_name_0, OBJ_HLINE, 0, TimeCurrent(), OrderOpenPrice());
                  ObjectSet(l_name_0, OBJPROP_COLOR, Gold);
                  li_12 = MathRound(ObjectGet(l_name_0, OBJPROP_PRICE1) / Point);
                  ObjectSet(l_name_0, OBJPROP_PRICE1, li_12 * Point);
                  if (li_12 != li_8) {
                     if (!OrderModify(OrderTicket(), li_12 * Point, OrderStopLoss(), OrderTakeProfit(), OrderExpiration())) {
                        Print("ERROR OrderModify(OP): ", ErrorDescription(GetLastError()));
                        ObjectSet(l_name_0, OBJPROP_PRICE1, li_8 * Point);
                     }
                  }
                  ObjectSet(l_name_0, OBJPROP_BACK, TRUE);
               }
            }
         }
      }
   }
   for (l_pos_24 = ObjectsTotal() - 1; l_pos_24 >= 0; l_pos_24--) {
      l_name_0 = ObjectName(l_pos_24);
      if (StringSubstr(l_name_0, 0, 1) == "_") {
         if (OrderSelect(StrToInteger(StringSubstr(l_name_0, 3)), SELECT_BY_TICKET))
            if (OrderCloseTime() > 0) ObjectDelete(l_name_0);
      }
   }
   ObjectsRedraw();
   return (0);
}

int Del_Lin() {
   string l_name_0;
   if (Allow_Drag_PriceLines == FALSE) return (0);
   for (int li_8 = ObjectsTotal() - 1; li_8 >= 0; li_8--) {
      l_name_0 = ObjectName(li_8);
      if (StringSubstr(l_name_0, 0, 1) == "_") ObjectDelete(l_name_0);
   }
   ObjectsRedraw();
   return (0);
}

int SetBrokerX() {
   double ld_ret_0;
   if (Digits == 5 || Digits == 3) ld_ret_0 = 10;
   else ld_ret_0 = 1;
   return (ld_ret_0);
}

double Point_Broker() {
   double l_point_0;
   if (MarketInfo(Symbol(), MODE_POINT) == 0.00001) l_point_0 = 0.0001;
   else {
      if (MarketInfo(Symbol(), MODE_POINT) == 0.001) l_point_0 = 0.01;
      else l_point_0 = MarketInfo(Symbol(), MODE_POINT);
   }
   return (l_point_0);
}

int Ares() {
   double ld_0;
   double ld_8;
   HideTestIndicators(TRUE);
   if (Automatic_SL == TRUE) {
      ld_0 = 0.75 * iATR(NULL, 0, 100, 1) / gd_288;
      StopLoss = ld_0 * Automatic_SL_Ratio;
      if (StopLoss > Auto_Max_SL) StopLoss = Auto_Max_SL;
   }
   if (Automatic_TP == TRUE) {
      ld_8 = 0.75 * iATR(NULL, 0, 100, 1) / gd_288;
      TakeProfit = ld_8 / 2.0 * Automatic_TP_Ratio;
   }
   HideTestIndicators(FALSE);
   return (0);
}
