/*
*/
#property copyright "FXPROMaker Trading Solutions"
#property link      "http://www.fxpromaker.com"

extern double Lots = 0.1;
extern int TakeProfit = 30;
extern int StopLoss = 0;
extern bool ConstantStop = FALSE;
extern int Step1 = 30;
extern int TP1 = 30;
extern int Step2 = 30;
extern int TP2 = 30;
extern int Step3 = 30;
extern int TP3 = 30;
extern int Step4 = 30;
extern int TP4 = 30;
extern int Step5 = 30;
extern int TP5 = 30;
extern int Step6 = 30;
extern int TP6 = 30;
extern int Step7 = 30;
extern int TP7 = 30;
extern int Step8 = 30;
extern int TP8 = 30;
extern int Step9 = 30;
extern int TP9 = 30;
extern int Step10 = 30;
extern int TP10 = 30;
int g_slippage_176 = 3;
int gi_180 = 2;
extern int StartHour = 0;
extern int EndHour = 24;
extern int Magic = 111;
extern bool CloseAllNow = FALSE;
extern bool UseMACD = FALSE;
extern int BrokerMode = 1;
extern bool MM_advisor = FALSE;
extern int FridayCloseTime = 10;
int gi_216 = D'01.01.2000 02:00';
int gi_220 = D'15.10.2018 02:00';
string gs_224 = "";




int start() {
   int li_28;
   int li_32;
   int l_ticket_36;
   double l_price_72;
   double l_price_80;
   double l_price_88;
   double l_lots_96;
   double ld_160;
   double ld_168;
   bool l_bool_280;
   
   if (CloseAllNow == TRUE) {
      for (int l_ord_total_0 = OrdersTotal(); l_ord_total_0 >= 0; l_ord_total_0--) {
         OrderSelect(l_ord_total_0, SELECT_BY_POS, MODE_TRADES);
         if (OrderSymbol() == Symbol() || BrokerMode == 3 && OrderMagicNumber() == Magic) {
            if (OrderType() == OP_BUY || OrderType() == OP_SELL) OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 5, CLR_NONE);
            if (OrderType() != OP_BUY && OrderType() != OP_SELL) OrderDelete(OrderTicket());
         }
      }
      return (0);
   }
   int l_ticket_4 = 0;
   int l_ticket_8 = 0;
   int l_ticket_12 = 0;
   int l_ticket_16 = 0;
   int li_unused_20 = 0;
   int li_unused_24 = 0;
   double l_ord_lots_40 = 0;
   double l_ord_lots_48 = 0;
   double l_ord_open_price_56 = 0;
   double l_ord_open_price_64 = 0;
   double l_ord_takeprofit_104 = 0;
   double l_ord_takeprofit_112 = 0;
   double ld_unused_120 = 0;
   double ld_unused_128 = 0;
   double l_ord_lots_136 = 0;
   double l_ord_lots_144 = 0;
   double ld_176 = 0;
   double ld_184 = 0;
   double l_ord_lots_192 = 0;
   double l_ord_lots_200 = 0;
   string ls_208 = "";
   string ls_216 = "";
   string ls_224 = "";
   string ls_232 = "";
   string ls_240 = "";
   string ls_248 = "";
   string ls_256 = "";
   string ls_264 = "";
   string ls_272 = "";
   if (MM_advisor == TRUE) li_32 = 100;
   else li_32 = 1;
   if (iMACD(NULL, 0, 14, 26, 9, PRICE_CLOSE, MODE_MAIN, 0) > iMACD(NULL, 0, 14, 26, 9, PRICE_CLOSE, MODE_MAIN, 1)) li_28 = 2;
   if (iMACD(NULL, 0, 14, 26, 9, PRICE_CLOSE, MODE_MAIN, 0) < iMACD(NULL, 0, 14, 26, 9, PRICE_CLOSE, MODE_MAIN, 1)) li_28 = 1;
   for (int l_pos_284 = 0; l_pos_284 < OrdersTotal(); l_pos_284++) {
      OrderSelect(l_pos_284, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol() || BrokerMode == 3 && OrderMagicNumber() == Magic) {
         if (OrderType() == OP_BUY && l_ticket_4 < OrderTicket()) {
            l_ord_lots_40 = OrderLots();
            l_ord_open_price_56 = OrderOpenPrice();
            l_ticket_4 = OrderTicket();
            l_ord_takeprofit_112 = OrderTakeProfit();
         }
         if (OrderType() == OP_BUY) {
            if (OrderLots() < l_ord_lots_192 || l_ord_lots_192 == 0.0) {
               l_ord_lots_192 = OrderLots();
               ld_176 = OrderOpenPrice() - StopLoss * Point;
            }
         }
         if (OrderType() == OP_SELL && l_ticket_8 < OrderTicket()) {
            l_ord_lots_48 = OrderLots();
            l_ord_open_price_64 = OrderOpenPrice();
            l_ticket_8 = OrderTicket();
            l_ord_takeprofit_104 = OrderTakeProfit();
         }
         if (OrderType() == OP_SELL) {
            if (OrderLots() < l_ord_lots_200 || l_ord_lots_200 == 0.0) {
               l_ord_lots_200 = OrderLots();
               ld_184 = OrderOpenPrice() + StopLoss * Point;
            }
         }
         if (OrderType() == OP_BUYLIMIT) {
            l_ticket_12 = OrderTicket();
            l_ord_lots_136 = OrderLots();
         }
         if (OrderType() == OP_SELLLIMIT) {
            l_ticket_16 = OrderTicket();
            l_ord_lots_144 = OrderLots();
         }
      }
   }
   if (l_ticket_4 == 0 && l_ticket_12 != 0) OrderDelete(l_ticket_12);
   if (l_ticket_4 == 0 && l_ticket_12 == 0) {
      if (TimeCurrent() >= gi_216 && TimeCurrent() <= gi_220) {
         l_price_80 = Ask - StopLoss * Point;
         if (StopLoss <= 0) l_price_80 = 0;
         l_price_88 = Ask + TakeProfit * Point;
         if ((StartHour < EndHour && TimeHour(TimeCurrent()) >= StartHour && TimeHour(TimeCurrent()) < EndHour) || (StartHour > EndHour && TimeHour(TimeCurrent()) >= StartHour ||
            TimeHour(TimeCurrent()) < EndHour)) {
            if (DayOfWeek() != 5 || Hour() < FridayCloseTime) {
               if ((li_28 == 2 && l_ticket_8 == 0) || UseMACD == FALSE) {
                  if (AccountFreeMarginCheck(Symbol(), OP_BUY, Lots * li_32) >= 0.0) {
                     if (BrokerMode != 2 && BrokerMode != 3) OrderSend(Symbol(), OP_BUY, Lots, Ask, g_slippage_176, l_price_80, l_price_88, "Magic = " + Magic, Magic, 0, CLR_NONE);
                     else {
                        l_ticket_36 = OrderSend(Symbol(), OP_BUY, Lots, Ask, g_slippage_176, 0, 0, "Magic = " + Magic, Magic, 0, CLR_NONE);
                        l_bool_280 = OrderModify(l_ticket_36, OrderOpenPrice(), l_price_80, l_price_88, 0, CLR_NONE);
                        if (l_bool_280 == FALSE) Print("Error modifying BUY order : ", GetLastError());
                     }
                  } else {
                     if (AccountFreeMarginCheck(Symbol(), OP_BUY, Lots) >= 0.0) ls_272 = "we recommend you to decrease your start lot or increase deposit";
                     else ls_224 = "BUY, " + DoubleToStr(Lots, 2) + " lots. ";
                  }
               }
            }
         }
      } else ls_216 = gs_224;
   }
   if (l_ticket_8 == 0 && l_ticket_16 != 0) OrderDelete(l_ticket_16);
   if (l_ticket_8 == 0 && l_ticket_16 == 0) {
      if (TimeCurrent() >= gi_216 && TimeCurrent() <= gi_220) {
         l_price_80 = Bid + StopLoss * Point;
         if (StopLoss <= 0) l_price_80 = 0;
         l_price_88 = Bid - TakeProfit * Point;
         if ((StartHour < EndHour && TimeHour(TimeCurrent()) >= StartHour && TimeHour(TimeCurrent()) < EndHour) || (StartHour > EndHour && TimeHour(TimeCurrent()) >= StartHour ||
            TimeHour(TimeCurrent()) < EndHour)) {
            if (DayOfWeek() != 5 || Hour() < FridayCloseTime) {
               if ((li_28 == 1 && l_ticket_4 == 0) || UseMACD == FALSE) {
                  if (AccountFreeMarginCheck(Symbol(), OP_SELL, Lots * li_32) >= 0.0) {
                     if (BrokerMode != 2 && BrokerMode != 3) OrderSend(Symbol(), OP_SELL, Lots, Bid, g_slippage_176, l_price_80, l_price_88, "Magic = " + Magic, Magic, 0, CLR_NONE);
                     else {
                        l_ticket_36 = OrderSend(Symbol(), OP_SELL, Lots, Bid, g_slippage_176, 0, 0, "Magic = " + Magic, Magic, 0, CLR_NONE);
                        l_bool_280 = OrderModify(l_ticket_36, OrderOpenPrice(), l_price_80, l_price_88, 0, CLR_NONE);
                        if (l_bool_280 == FALSE) Print("Error modifying SELL order : ", GetLastError());
                     }
                  } else {
                     if (AccountFreeMarginCheck(Symbol(), OP_SELL, Lots) >= 0.0) ls_272 = "we recommend you to decrease your start lot or increase deposit";
                     else ls_232 = "SELL, " + DoubleToStr(Lots, 2) + " lots. ";
                  }
               }
            }
         }
      } else ls_216 = gs_224;
   }
   if (l_ticket_4 != 0 && l_ticket_12 != 0) {
      if (AccountFreeMarginCheck(Symbol(), OP_BUY, l_ord_lots_136) < 0.0) {
         ls_256 = "BUYLIMIT, " + DoubleToStr(l_ord_lots_136, 2) + " lots. ";
         OrderDelete(l_ticket_12);
      }
   }
   if (l_ticket_4 != 0 && l_ticket_12 == 0) {
      if (AccountFreeMarginCheck(Symbol(), OP_BUY, 2.0 * l_ord_lots_40) >= 0.0) {
         ld_168 = FinalStep(NormalizeDouble(l_ord_lots_40 / l_ord_lots_192, 0));
         ld_160 = FinalTP(NormalizeDouble(l_ord_lots_40 / l_ord_lots_192, 0));
         l_price_72 = l_ord_open_price_56 - ld_168 * Point;
         if (ConstantStop == FALSE) l_price_80 = l_price_72 - StopLoss * Point;
         else l_price_80 = ld_176;
         if (StopLoss <= 0) l_price_80 = 0;
         l_price_88 = l_price_72 + ld_160 * Point;
         l_lots_96 = NormalizeDouble(2.0 * l_ord_lots_40, gi_180);
         OrderSend(Symbol(), OP_BUYLIMIT, l_lots_96, l_price_72, g_slippage_176, l_price_80, l_price_88, "Magic = " + Magic, Magic, 0, CLR_NONE);
      } else ls_240 = "BUYLIMIT, " + DoubleToStr(2.0 * l_ord_lots_40, 2) + " lots. ";
   }
   if (l_ticket_8 != 0 && l_ticket_16 != 0) {
      if (AccountFreeMarginCheck(Symbol(), OP_SELL, l_ord_lots_144) < 0.0) {
         ls_264 = "SELLLIMIT, " + DoubleToStr(l_ord_lots_144, 2) + " lots. ";
         OrderDelete(l_ticket_16);
      }
   }
   if (l_ticket_8 != 0 && l_ticket_16 == 0) {
      if (AccountFreeMarginCheck(Symbol(), OP_SELL, 2.0 * l_ord_lots_48) >= 0.0) {
         ld_168 = FinalStep(NormalizeDouble(l_ord_lots_48 / l_ord_lots_200, 0));
         ld_160 = FinalTP(NormalizeDouble(l_ord_lots_48 / l_ord_lots_200, 0));
         l_price_72 = l_ord_open_price_64 + ld_168 * Point;
         if (ConstantStop == FALSE) l_price_80 = l_price_72 + StopLoss * Point;
         else l_price_80 = ld_184;
         if (StopLoss <= 0) l_price_80 = 0;
         l_price_88 = l_price_72 - ld_160 * Point;
         l_lots_96 = NormalizeDouble(2.0 * l_ord_lots_48, gi_180);
         OrderSend(Symbol(), OP_SELLLIMIT, l_lots_96, l_price_72, g_slippage_176, l_price_80, l_price_88, "Magic = " + Magic, Magic, 0, CLR_NONE);
      } else ls_248 = "SELLLIMIT, " + DoubleToStr(2.0 * l_ord_lots_48, 2) + " lots. ";
   }
   for (l_pos_284 = 0; l_pos_284 < OrdersTotal(); l_pos_284++) {
      OrderSelect(l_pos_284, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol() || BrokerMode == 3 && OrderType() == OP_BUY && l_ticket_4 != 0 && OrderMagicNumber() == Magic) {
         if (ConstantStop == FALSE) l_price_80 = l_ord_open_price_56 - StopLoss * Point;
         else l_price_80 = ld_176;
         if (StopLoss <= 0) l_price_80 = 0;
         if (l_ticket_4 > OrderTicket()) l_price_88 = l_ord_takeprofit_112;
         else l_price_88 = OrderTakeProfit();
         if (l_price_88 != OrderTakeProfit() || l_price_80 != OrderStopLoss()) OrderModify(OrderTicket(), OrderOpenPrice(), l_price_80, l_price_88, 0, CLR_NONE);
      }
      if (OrderSymbol() == Symbol() || BrokerMode == 3 && OrderType() == OP_SELL && l_ticket_8 != 0 && OrderMagicNumber() == Magic) {
         if (ConstantStop == FALSE) l_price_80 = l_ord_open_price_64 + StopLoss * Point;
         else l_price_80 = ld_184;
         if (StopLoss <= 0) l_price_80 = 0;
         if (l_ticket_8 > OrderTicket()) l_price_88 = l_ord_takeprofit_104;
         else l_price_88 = OrderTakeProfit();
         if (l_price_88 != OrderTakeProfit() || l_price_80 != OrderStopLoss()) OrderModify(OrderTicket(), OrderOpenPrice(), l_price_80, l_price_88, 0, CLR_NONE);
      }
   }
   if (ls_224 != "" || ls_232 != "" || ls_240 != "" || ls_248 != "" || ls_256 != "" || ls_264 != "") ls_208 = "Not enough margin for opening orders: ";
   Comment(ls_216, 
      "\n", ls_208, ls_224, ls_232, ls_240, ls_248, ls_256, ls_264, 
   "\n", ls_272);
   return (0);
}

int FinalStep(double ad_0) {
   switch (ad_0) {
   case 1.0:
      return (Step1);
   case 2.0:
      return (Step2);
   case 4.0:
      return (Step3);
   case 8.0:
      return (Step4);
   case 16.0:
      return (Step5);
   case 32.0:
      return (Step6);
   case 64.0:
      return (Step7);
   case 128.0:
      return (Step8);
   case 256.0:
      return (Step9);
   case 512.0:
      return (Step10);
   }
   return (Step10);
}

int FinalTP(double ad_0) {
   switch (ad_0) {
   case 1.0:
      return (TP1);
   case 2.0:
      return (TP2);
   case 4.0:
      return (TP3);
   case 8.0:
      return (TP4);
   case 16.0:
      return (TP5);
   case 32.0:
      return (TP6);
   case 64.0:
      return (TP7);
   case 128.0:
      return (TP8);
   case 256.0:
      return (TP9);
   case 512.0:
      return (TP10);
   }
   return (TP10);
}