/*
   Generated by EX4-TO-MQ4 decompiler V4.0.224.1 []
   Website: http://purebeam.biz
   E-mail : purebeam@gmail.com
*/


string gs_76 = "eabbandslateral";
string gs_unused_84 = "v1.0";
extern int Slippage = 1;
int gi_96 = 1;
int gia_unused_100[1];
extern int StopLoss = 50;
extern int TakeProfit = 50;
extern int Period_ = 20;
extern int deviation = 3;
extern int HoraInicio = 23;
extern int HoraFin = 24;
extern double Lots = 0.1;
int g_magic_128;
int gi_132 = 20;
int gi_136 = -1;
int gi_unused_140 = 0;
int g_time_144 = 0;

int comprobarStopLoss(int ai_0) {
   int l_stoplevel_4 = MarketInfo(Symbol(), MODE_STOPLEVEL);
   if (ai_0 != 0 && ai_0 < l_stoplevel_4) {
      Alert("ERROR: El StopLoss m?nimo en ", Symbol(), " es: ", l_stoplevel_4);
      return (0);
   }
   return (1);
}

int comprobarTakeProfit(int ai_0) {
   int l_stoplevel_4 = MarketInfo(Symbol(), MODE_STOPLEVEL);
   if (ai_0 != 0 && ai_0 < l_stoplevel_4) {
      Alert("ERROR: El TakeProfit m?nimo en ", Symbol(), " es: ", l_stoplevel_4);
      return (0);
   }
   return (1);
}

double getSL(int ai_0, int ai_4, double a_price_8 = 0.0) {
   int li_40;
   if (ai_4 == 0) return (0);
   double ld_ret_16 = 0;
   double ld_24 = MarketInfo(Symbol(), MODE_STOPLEVEL);
   ld_24 *= Point;
   if (ai_0 == 0 || ai_0 == 2 || ai_0 == 4) {
      if (a_price_8 == 0.0) {
         RefreshRates();
         a_price_8 = Ask;
      }
      ld_ret_16 = a_price_8 - ai_4 * Point;
      if (ld_ret_16 > a_price_8 - ld_24) ld_ret_16 = a_price_8 - ld_24;
   }
   if (ai_0 == 1 || ai_0 == 3 || ai_0 == 5) {
      if (a_price_8 == 0.0) {
         RefreshRates();
         a_price_8 = Bid;
      }
      ld_ret_16 = a_price_8 + ai_4 * Point;
      if (ld_ret_16 < a_price_8 + ld_24) ld_ret_16 = a_price_8 + ld_24;
   }
   double l_ticksize_32 = MarketInfo(Symbol(), MODE_TICKSIZE);
   if (l_ticksize_32 != 0.0) {
      li_40 = ld_ret_16 / Point;
      li_40 = li_40 / (l_ticksize_32 / Point);
      ld_ret_16 = li_40 * l_ticksize_32;
   }
   return (ld_ret_16);
}

double getTP(int ai_0, int ai_4, double a_price_8 = 0.0) {
   int li_40;
   if (ai_4 == 0) return (0);
   double ld_ret_16 = 0;
   double ld_24 = MarketInfo(Symbol(), MODE_STOPLEVEL);
   ld_24 *= Point;
   if (ai_0 == 0 || ai_0 == 2 || ai_0 == 4) {
      if (a_price_8 == 0.0) {
         RefreshRates();
         a_price_8 = Ask;
      }
      ld_ret_16 = a_price_8 + ai_4 * Point;
      if (ld_ret_16 < a_price_8 + ld_24) ld_ret_16 = a_price_8 + ld_24;
   }
   if (ai_0 == 1 || ai_0 == 3 || ai_0 == 5) {
      if (a_price_8 == 0.0) {
         RefreshRates();
         a_price_8 = Bid;
      }
      ld_ret_16 = a_price_8 - ai_4 * Point;
      if (ld_ret_16 > a_price_8 - ld_24) ld_ret_16 = a_price_8 - ld_24;
   }
   ld_ret_16 = NormalizeDouble(ld_ret_16, Digits);
   double l_ticksize_32 = MarketInfo(Symbol(), MODE_TICKSIZE);
   if (l_ticksize_32 != 0.0) {
      li_40 = ld_ret_16 / Point;
      li_40 = li_40 / (l_ticksize_32 / Point);
      ld_ret_16 = li_40 * l_ticksize_32;
   }
   return (ld_ret_16);
}

int se?alInversa?(int ai_0) {
   if (gi_136 == -1) gi_136 = ai_0;
   if (gi_136 != ai_0 && ai_0 != -1) {
      gi_136 = ai_0;
      return (1);
   }
   return (0);
}

int abrirPosicion(string a_symbol_0, int a_cmd_8, double ad_12 = 0.0, double ad_20 = 0.0, double ad_28 = 0.0, double a_minlot_36 = 0.0, string a_comment_44 = "", int a_datetime_52 = 0) {
   double l_price_56;
   color l_color_64;
   int l_ticket_68;
   int l_error_72;
   double ld_76;
   double ld_84;
   int li_128;
   if (a_comment_44 == "") a_comment_44 = gs_76 + " " + getPeriodAsString(Period()) + ";";
   double l_digits_92 = MarketInfo(a_symbol_0, MODE_DIGITS);
   double l_point_100 = MarketInfo(a_symbol_0, MODE_POINT);
   double ld_108 = (MarketInfo(a_symbol_0, MODE_STOPLEVEL) + 1.0) * l_point_100;
   int li_116 = gi_132 * 2;
   ad_12 = NormalizeDouble(ad_12, l_digits_92);
   double l_ticksize_120 = MarketInfo(a_symbol_0, MODE_TICKSIZE);
   if (l_ticksize_120 != 0.0) {
      li_128 = ad_12 / l_point_100;
      li_128 = li_128 / (l_ticksize_120 / l_point_100);
      ad_12 = li_128 * l_ticksize_120;
   }
   while (li_116 > 0) {
      RefreshRates();
      if (a_cmd_8 == OP_BUY || a_cmd_8 == OP_BUYLIMIT || a_cmd_8 == OP_BUYSTOP) {
         l_price_56 = NormalizeDouble(MarketInfo(a_symbol_0, MODE_ASK), l_digits_92);
         l_color_64 = Green;
      }
      if (a_cmd_8 == OP_SELL || a_cmd_8 == OP_SELLLIMIT || a_cmd_8 == OP_SELLSTOP) {
         l_price_56 = NormalizeDouble(MarketInfo(a_symbol_0, MODE_BID), l_digits_92);
         l_color_64 = Red;
      }
      if (ad_12 == 0.0) ad_12 = l_price_56;
      if (a_minlot_36 == 0.0) a_minlot_36 = MarketInfo(a_symbol_0, MODE_MINLOT);
      else a_minlot_36 = NormalizeLots(a_minlot_36, a_symbol_0);
      if (a_cmd_8 != OP_BUY && a_cmd_8 != OP_SELL) {
         if (ad_12 < l_price_56 && a_cmd_8 == OP_BUYSTOP) a_cmd_8 = 2;
         if (ad_12 > l_price_56 && a_cmd_8 == OP_BUYLIMIT) a_cmd_8 = 4;
         if (ad_12 > l_price_56 && a_cmd_8 == OP_SELLSTOP) a_cmd_8 = 3;
         if (ad_12 < l_price_56 && a_cmd_8 == OP_SELLLIMIT) a_cmd_8 = 5;
         if (a_cmd_8 == OP_BUYLIMIT || a_cmd_8 == OP_BUYSTOP || a_cmd_8 == OP_SELLLIMIT || a_cmd_8 == OP_SELLSTOP) {
            if (MathAbs(l_price_56 - ad_12) > ld_108) l_price_56 = ad_12;
            else {
               if (a_cmd_8 == OP_BUYLIMIT || a_cmd_8 == OP_BUYSTOP) {
                  a_cmd_8 = 0;
                  a_datetime_52 = 0;
               }
               if (a_cmd_8 == OP_SELLLIMIT || a_cmd_8 == OP_SELLSTOP) {
                  a_cmd_8 = 1;
                  a_datetime_52 = 0;
               }
            }
         }
      }
      if (ad_20 != 0.0) ld_76 = ad_20;
      else ld_76 = 0;
      if (ad_28 != 0.0) ld_84 = ad_28;
      else ld_84 = 0;
      if (a_cmd_8 == OP_BUY || a_cmd_8 == OP_BUYLIMIT || a_cmd_8 == OP_BUYSTOP) {
         if (ld_76 != 0.0 && l_price_56 - ld_76 < ld_108) {
            if (l_price_56 - ld_76 > 0.0) {
               ld_76 = NormalizeDouble(l_price_56 - ld_108, l_digits_92);
               Print("Cambiando el StopLoss al m?nimo de la plataforma");
            } else {
               ld_76 = 0;
               Print("Eliminando StopLoss incorrecto.");
            }
         }
         if (ld_84 != 0.0 && ld_84 - l_price_56 < ld_108) {
            if (ld_84 - l_price_56 > 0.0) {
               ld_84 = NormalizeDouble(l_price_56 + ld_108, l_digits_92);
               Print("Cambiando el TakeProfit al m?nimo de la plataforma");
            } else {
               ld_84 = 0;
               Print("Eliminando TakeProfit incorrecto");
            }
         }
      }
      if (a_cmd_8 == OP_SELL || a_cmd_8 == OP_SELLLIMIT || a_cmd_8 == OP_SELLSTOP) {
         if (ld_76 != 0.0 && ld_76 - l_price_56 < ld_108) {
            if (ld_76 - l_price_56 > 0.0) {
               ld_76 = NormalizeDouble(l_price_56 + ld_108, l_digits_92);
               Print("Cambiando el StopLoss al m?nimo de la plataforma");
            } else {
               Print("Eliminando StopLoss incorrecto");
               ld_76 = 0;
            }
         }
         if (ld_84 != 0.0 && l_price_56 - ld_84 < ld_108) {
            if (l_price_56 - ld_84 > 0.0) {
               ld_84 = NormalizeDouble(l_price_56 - ld_108, l_digits_92);
               Print("Cambiando el TakeProfit al m?nimo de la plataforma");
            } else {
               ld_84 = 0;
               Print("Eliminando TakeProfit incorrecto");
            }
         }
      }
      while (!IsTradeAllowed() || IsTradeContextBusy()) Sleep(1000);
      l_ticket_68 = OrderSend(a_symbol_0, a_cmd_8, a_minlot_36, l_price_56, Slippage, 0, 0, a_comment_44, g_magic_128, a_datetime_52, l_color_64);
      if (l_ticket_68 < 0) {
         l_error_72 = GetLastError();
         Print("OrderSend fall? con error #", l_error_72);
         Print("Orden: ", a_cmd_8, " L=", a_minlot_36, " pa=", l_price_56, " precio=" + ad_12 + " sp=", Slippage, " sl=", ld_76, " tp=", ld_84, " c=", a_comment_44, " stoplevel=", ld_108, " Bid=", MarketInfo(a_symbol_0, MODE_BID));
         li_116--;
         Sleep(500);
      } else {
         modificarSLyTP(l_ticket_68, ld_76, ld_84);
         return (l_ticket_68);
      }
   }
   Alert("Se intent? operar durante " + gi_132 + " segundos. Error #" + l_error_72);
   return (-1);
}

int cerrarPosicion(int a_ticket_0) {
   bool li_12;
   int l_error_4 = -1;
   if (a_ticket_0 < 1) return (0);
   int l_count_8 = 0;
   while (true) {
      if (OrderSelect(a_ticket_0, SELECT_BY_TICKET, MODE_TRADES)) {
         RefreshRates();
         while (!IsTradeAllowed() || IsTradeContextBusy()) Sleep(1000);
         if (OrderType() == OP_BUY || OrderType() == OP_SELL) li_12 = OrderClose(a_ticket_0, OrderLots(), NormalizeDouble(OrderClosePrice(), Digits), Slippage, Yellow);
         else li_12 = OrderDelete(a_ticket_0);
         if (!li_12) {
            l_error_4 = GetLastError();
            if (OrderType() == OP_BUY || OrderType() == OP_SELL) Print("OrderClose fall? con error #", l_error_4);
            else Print("OrderDelete fall? con error #", l_error_4);
            if (l_error_4 == 4108/* INVALID_TICKET */ || l_error_4 == 4106/* UNKNOWN_SYMBOL */ || l_error_4 == 133/* TRADE_DISABLED */) return (0);
            Sleep(500);
            continue;
         }
         return (1);
      }
      l_count_8++;
      if (l_count_8 <= 10) continue;
      break;
   }
   return (0);
}

int getMagicNumber(bool ai_0 = FALSE) {
   int li_ret_4;
   if (!IsTesting()) li_ret_4 = WindowHandle(Symbol(), 0);
   else
      if (!IsVisualMode()) li_ret_4 = 23;
   if (ai_0) {
      for (int l_pos_8 = OrdersTotal() - 1; l_pos_8 >= 0; l_pos_8--) {
         OrderSelect(l_pos_8, SELECT_BY_POS);
         if (OrderSymbol() == Symbol()) {
            if (StringSubstr(OrderComment(), 0, StringLen(gs_76)) == gs_76 && StringFind(OrderComment(), " ", StringLen(gs_76) + 1) == -1 || StringSubstr(OrderComment(), StringLen(gs_76) +
               1, StringLen(getPeriodAsString(Period()))) == getPeriodAsString(Period())) {
               Print("Retomando orden " + OrderTicket());
               li_ret_4 = OrderMagicNumber();
               se?alInversa?(OrderType());
               break;
            }
         }
      }
   }
   return (li_ret_4);
}

double getFixedLot(double ad_0) {
   return (NormalizeLots(ad_0));
}

string getPeriodAsString(int a_timeframe_0 = 0) {
   if (a_timeframe_0 == 0) a_timeframe_0 = Period();
   switch (a_timeframe_0) {
   case PERIOD_M1:
      return ("M1");
   case PERIOD_M5:
      return ("M5");
   case PERIOD_M15:
      return ("M15");
   case PERIOD_M30:
      return ("M30");
   case PERIOD_H1:
      return ("H1");
   case PERIOD_H4:
      return ("H4");
   case PERIOD_D1:
      return ("D1");
   case PERIOD_W1:
      return ("W1");
   case PERIOD_MN1:
      return ("MN1");
   }
   return ("");
}

double NormalizeLots(double ad_0, string a_symbol_8 = "") {
   if (a_symbol_8 == "") a_symbol_8 = Symbol();
   double ld_16 = ad_0;
   double l_minlot_24 = MarketInfo(a_symbol_8, MODE_MINLOT);
   if (ld_16 < l_minlot_24) return (0);
   double l_maxlot_32 = MarketInfo(a_symbol_8, MODE_MAXLOT);
   if (ld_16 > l_maxlot_32) return (l_maxlot_32);
   double l_lotstep_40 = MarketInfo(a_symbol_8, MODE_LOTSTEP);
   int li_48 = MathRound(ld_16 / l_lotstep_40);
   ld_16 = l_lotstep_40 * li_48;
   return (NormalizeDouble(ld_16, 2));
}

int modificarSLyTP(int a_ticket_0, double a_price_4, double a_price_12) {
   int li_36;
   int li_40;
   bool l_bool_44;
   if (a_price_4 == 0.0 && a_price_12 == 0.0) return (0);
   double ld_20 = MarketInfo(Symbol(), MODE_STOPLEVEL) * Point;
   OrderSelect(a_ticket_0, SELECT_BY_TICKET);
   a_price_4 = NormalizeDouble(a_price_4, Digits);
   double l_ticksize_28 = MarketInfo(Symbol(), MODE_TICKSIZE);
   if (l_ticksize_28 != 0.0) {
      li_36 = a_price_4 / Point;
      li_36 = li_36 / (l_ticksize_28 / Point);
      a_price_4 = li_36 * l_ticksize_28;
   }
   if (a_price_4 > 0.0) {
      if (OrderType() == OP_BUY)
         if (a_price_4 > OrderClosePrice() - ld_20) return (0);
      if (OrderType() == OP_BUYLIMIT || OrderType() == OP_BUYSTOP)
         if (a_price_4 > OrderOpenPrice()) return (0);
      if (OrderType() == OP_SELL)
         if (a_price_4 < OrderClosePrice() - ld_20) return (0);
      if (OrderType() == OP_SELLLIMIT || OrderType() == OP_SELLSTOP)
         if (a_price_4 < OrderOpenPrice()) return (0);
   }
   a_price_12 = NormalizeDouble(a_price_12, Digits);
   if (l_ticksize_28 != 0.0) {
      li_36 = a_price_12 / Point;
      li_36 = li_36 / (l_ticksize_28 / Point);
      a_price_12 = li_36 * l_ticksize_28;
   }
   if (a_price_12 > 0.0) {
      if (NormalizeDouble(a_price_4, Digits) == NormalizeDouble(OrderStopLoss(), Digits) && NormalizeDouble(a_price_12, Digits) == NormalizeDouble(OrderTakeProfit(), Digits)) return;
      if (OrderType() == OP_BUY)
         if (a_price_12 < OrderClosePrice() - ld_20) return (0);
      if (OrderType() == OP_BUYLIMIT || OrderType() == OP_BUYSTOP)
         if (a_price_12 < OrderOpenPrice()) return (0);
      if (OrderType() == OP_SELL)
         if (a_price_12 > OrderClosePrice() - ld_20) return (0);
      if (OrderType() == OP_SELLLIMIT || OrderType() == OP_SELLSTOP)
         if (a_price_12 > OrderOpenPrice()) return (0);
   }
   if (OrderStopLoss() == 0.0 || (OrderStopLoss() < a_price_4 && OrderType() == OP_BUY) || (OrderStopLoss() > a_price_4 && OrderType() == OP_SELL)) {
      if (OrderTakeProfit() == 0.0 || (OrderTakeProfit() > a_price_12 && OrderType() == OP_BUY) || (OrderTakeProfit() < a_price_12 && OrderType() == OP_SELL) && MathAbs(OrderClosePrice() - a_price_12) > ld_20) {
         li_40 = gi_132 * 2;
         while (li_40 > 0) {
            while (!IsTradeAllowed() || IsTradeContextBusy()) Sleep(1000);
            l_bool_44 = OrderModify(OrderTicket(), OrderOpenPrice(), a_price_4, a_price_12, 0, Yellow);
            if (!l_bool_44) {
               Print("Error al modificar StopLoss. SL actual:" + OrderStopLoss() + " Nuevo SL:" + a_price_4 + ". Error " + GetLastError());
               li_40--;
               Sleep(500);
            } else return (1);
         }
      }
   }
   return (0);
}

int init() {
   g_magic_128 = getMagicNumber(1);
   if (Digits == 3 || Digits == 5) gi_96 = 10;
   else gi_96 = 1;
   StopLoss *= gi_96;
   if (!comprobarStopLoss(StopLoss)) {
      Alert("StopLoss: Se elimina StopLoss al ser menor que el mínimo para " + Symbol() + ": " + DoubleToStr(MarketInfo(Symbol(), MODE_STOPLEVEL), 0));
      StopLoss = 0;
   }
   TakeProfit *= gi_96;
   if (!comprobarTakeProfit(TakeProfit)) {
      Alert("TakeProfit: Se elimina TakeProfit al ser menor que el mínimo para " + Symbol() + ": " + DoubleToStr(MarketInfo(Symbol(), MODE_STOPLEVEL), 0));
      TakeProfit = 0;
   }
   return (0);
}

int deinit() {
   return (0);
}

int start() {
   if (AB_NuevaVela()) {
      if (AB_Horario(HoraInicio, 0, HoraFin, 0)) {
         if (iLow(NULL, 0, 0) <= iBands(NULL, 0, Period_, deviation, 0, PRICE_CLOSE, MODE_LOWER, 1)) AB_Comprar("buy", StopLoss, TakeProfit, getFixedLot(0.1), "");
         if (iHigh(NULL, 0, 0) >= iBands(NULL, 0, Period_, deviation, 0, PRICE_CLOSE, MODE_UPPER, 1)) AB_Vender("sell", StopLoss, TakeProfit, getFixedLot(0.1), "");
      }
      return (0);
   }
}

void AB_Comprar(string as_0, int ai_8, int ai_12, double ad_16, string as_24 = "") {
   string ls_40;
   double ld_48;
   if (as_24 == "") as_24 = Symbol();
   int l_ticket_32 = AB_GetTicketByName(as_0);
   bool li_36 = FALSE;
   if (l_ticket_32 <= 0) li_36 = TRUE;
   else {
      OrderSelect(l_ticket_32, SELECT_BY_TICKET);
      if (OrderType() == OP_SELL || OrderType() == OP_SELLLIMIT || OrderType() == OP_SELLSTOP) {
         cerrarPosicion(l_ticket_32);
         li_36 = TRUE;
      }
   }
   if (li_36) {
      ls_40 = gs_76 + " " + getPeriodAsString(Period()) + ";" + as_0;
      ld_48 = getSL(0, ai_8);
      abrirPosicion(as_24, OP_BUY, 0, ld_48, getTP(0, ai_12), NormalizeLots(ad_16), ls_40);
   }
}

void AB_Vender(string as_0, int ai_8, int ai_12, double ad_16, string as_24 = "") {
   string ls_40;
   double ld_48;
   if (as_24 == "") as_24 = Symbol();
   int l_ticket_32 = AB_GetTicketByName(as_0);
   bool li_36 = FALSE;
   if (l_ticket_32 <= 0) li_36 = TRUE;
   else {
      OrderSelect(l_ticket_32, SELECT_BY_TICKET);
      if (OrderType() == OP_BUY || OrderType() == OP_BUYLIMIT || OrderType() == OP_BUYSTOP) {
         cerrarPosicion(l_ticket_32);
         li_36 = TRUE;
      }
   }
   if (li_36) {
      ls_40 = gs_76 + " " + getPeriodAsString(Period()) + ";" + as_0;
      ld_48 = getSL(1, ai_8);
      abrirPosicion(as_24, OP_SELL, 0, ld_48, getTP(1, ai_12), NormalizeLots(ad_16), ls_40);
   }
}

bool AB_NuevaVela() {
   if (Time[0] != g_time_144) {
      g_time_144 = Time[0];
      return (TRUE);
   }
   return (FALSE);
}

int AB_GetTicketByName(string as_0) {
   if (as_0 == "") return (0);
   string ls_8 = gs_76 + " " + getPeriodAsString(Period()) + ";" + as_0;
   if (StringLen(ls_8) > 31) ls_8 = StringSubstr(ls_8, 0, 31);
   for (int l_pos_16 = 0; l_pos_16 < OrdersTotal(); l_pos_16++) {
      if ((OrderSelect(l_pos_16, SELECT_BY_POS) && OrderMagicNumber() == g_magic_128))
         if (OrderComment() == ls_8) return (OrderTicket());
   }
   return (0);
}

bool AB_Horario(int ai_0, int ai_4, int ai_8, int ai_12) {
   if (ai_0 < ai_8) {
      if (TimeHour(TimeCurrent()) > ai_0 && TimeHour(TimeCurrent()) < ai_8) return (TRUE);
      if (TimeHour(TimeCurrent()) == ai_0)
         if (TimeMinute(TimeCurrent()) >= ai_4) return (TRUE);
      if (TimeHour(TimeCurrent()) > ai_0)
         if (TimeMinute(TimeCurrent()) < ai_12) return (TRUE);
   }
   if (ai_0 > ai_8) {
      if (TimeHour(TimeCurrent()) > ai_0 || TimeHour(TimeCurrent()) < ai_8) return (TRUE);
      if (TimeHour(TimeCurrent()) == ai_0)
         if (TimeMinute(TimeCurrent()) >= ai_4) return (TRUE);
      if (TimeHour(TimeCurrent()) == ai_8)
         if (TimeMinute(TimeCurrent()) < ai_12) return (TRUE);
   }
   if (ai_0 == ai_8) {
      if (TimeHour(TimeCurrent()) == ai_0 && TimeHour(TimeCurrent()) == ai_8) {
         if (ai_4 < ai_12)
            if (TimeMinute(TimeCurrent()) >= ai_4 && TimeMinute(TimeCurrent()) < ai_12) return (TRUE);
         if (ai_4 > ai_12)
            if (TimeMinute(TimeCurrent()) >= ai_4 || TimeMinute(TimeCurrent()) < ai_12) return (TRUE);
         if (ai_4 != ai_12) return (FALSE);
         if (TimeMinute(TimeCurrent()) != ai_4) return (FALSE);
         return (TRUE);
      }
      if (ai_4 > ai_12) return (TRUE);
   }
   return (FALSE);
}