/*
   
*/

extern double Lots = 0.01;
double Gd_84 = 45.0;
double Gd_92 = 1.7;
int Gi_100 = 31;
double Gd_104 = 0.0;
int Gi_112 = 0;
int Gi_116 = 8;
int Gi_120 = 0;
int Gi_124 = 24;
int Gi_128 = 55;
int Gi_132 = 0;
int Gi_136 = 5;
int G_slippage_140 = 30;
int G_magic_144;
double G_lots_148;
int G_period_156 = 7;
int Gi_160 = 0;
int G_ma_method_164 = MODE_LWMA;
int G_applied_price_168 = PRICE_WEIGHTED;
double Gd_172 = 0.25;
double Gd_180 = 0.2;
bool Gi_188 = FALSE;
int Gi_192;
bool Gi_unused_196 = FALSE;
string Gs_dummy_200;
int Gi_unused_216 = 0;
int Gi_unused_220 = 1;
int Gi_unused_224 = 3;
int Gi_unused_228 = 250;

// 52D46093050F38C27267BCE42543EF60
int deinit() {
   return (0);
}

// E37F0136AA3FFAF149B351F6A4C948E9
int init() {
   if (Digits == 3) {
      Gd_104 = 10.0 * Gd_84;
      Gi_112 = 10.0 * Gi_100;
   } else {
      if (Digits == 5) {
         Gd_104 = 10.0 * Gd_84;
         Gi_112 = 10.0 * Gi_100;
      } else {
         Gd_104 = Gd_84;
         Gi_112 = Gi_100;
      }
   }
   if (Symbol() == "AUDCADm" || Symbol() == "AUDCAD" || Symbol() == "AUDCAD.") G_magic_144 = 133701;
   if (Symbol() == "AUDJPYm" || Symbol() == "AUDJPY" || Symbol() == "AUDJPY.") G_magic_144 = 133702;
   if (Symbol() == "AUDNZDm" || Symbol() == "AUDNZD" || Symbol() == "AUDNZD.") G_magic_144 = 133703;
   if (Symbol() == "AUDUSDm" || Symbol() == "AUDUSD" || Symbol() == "AUDUSD.") G_magic_144 = 133704;
   if (Symbol() == "CHFJPYm" || Symbol() == "CHFJPY" || Symbol() == "CHFJPY.") G_magic_144 = 133705;
   if (Symbol() == "EURAUDm" || Symbol() == "EURAUD" || Symbol() == "EURAUD.") G_magic_144 = 133706;
   if (Symbol() == "EURCADm" || Symbol() == "EURCAD" || Symbol() == "EURCAD.") G_magic_144 = 133707;
   if (Symbol() == "EURCHFm" || Symbol() == "EURCHF" || Symbol() == "EURCHF.") G_magic_144 = 133708;
   if (Symbol() == "EURGBPm" || Symbol() == "EURGBP" || Symbol() == "EURGBP.") G_magic_144 = 133709;
   if (Symbol() == "EURJPYm" || Symbol() == "EURJPY" || Symbol() == "EURJPY.") G_magic_144 = 133710;
   if (Symbol() == "EURUSDm" || Symbol() == "EURUSD" || Symbol() == "EURUSD.") G_magic_144 = 133711;
   if (Symbol() == "GBPCHFm" || Symbol() == "GBPCHF" || Symbol() == "GBPCHF.") G_magic_144 = 133712;
   if (Symbol() == "GBPJPYm" || Symbol() == "GBPJPY" || Symbol() == "GBPJPY.") G_magic_144 = 133713;
   if (Symbol() == "GBPUSDm" || Symbol() == "GBPUSD" || Symbol() == "GBPUSD.") G_magic_144 = 133714;
   if (Symbol() == "NZDJPYm" || Symbol() == "NZDJPY" || Symbol() == "NZDJPY.") G_magic_144 = 133715;
   if (Symbol() == "NZDUSDm" || Symbol() == "NZDUSD" || Symbol() == "NZDUSD.") G_magic_144 = 133716;
   if (Symbol() == "USDCHFm" || Symbol() == "USDCHF" || Symbol() == "USDCHF.") G_magic_144 = 133717;
   if (Symbol() == "USDJPYm" || Symbol() == "USDJPY" || Symbol() == "USDJPY.") G_magic_144 = 133718;
   if (Symbol() == "USDCADm" || Symbol() == "USDCAD" || Symbol() == "USDCAD.") G_magic_144 = 133719;
   if (G_magic_144 == 0) G_magic_144 = 133799;
   Gi_192 = MathRound((-MathLog(MarketInfo(Symbol(), MODE_LOTSTEP))) / 2.302585093);
   return (0);
}

// A88FE8FAEB651132C0CA6446B53FDF9B
int f0_3() {
   if (Gi_120 < Gi_124 && TimeHour(TimeCurrent()) < Gi_120 || TimeHour(TimeCurrent()) >= Gi_124) return (0);
   if (Gi_120 > Gi_124 && (TimeHour(TimeCurrent()) < Gi_124 && TimeHour(TimeCurrent()) >= Gi_124)) return (0);
   if (Gi_124 == 0) Gi_124 = 24;
   if (Hour() == Gi_124 - 1 && Minute() >= Gi_128) return (0);
   return (1);
}

// A5475BEEA729819A4699A3F14CBC7B39
void f0_2() {
   int ticket_0;
   if (!GlobalVariableCheck(" ")) {
      GlobalVariableSet(" ", TimeCurrent());
      if (Gi_188 && f0_3() == 1 && DayOfWeek() >= Gi_132 && DayOfWeek() <= Gi_136) ticket_0 = OrderSend(Symbol(), OP_BUY, G_lots_148, Ask, G_slippage_140, 0, 0, " Buy", G_magic_144, 0, Green);
      else
         if (f0_3() == 1 && DayOfWeek() >= Gi_132 && DayOfWeek() <= Gi_136) ticket_0 = OrderSend(Symbol(), OP_BUY, G_lots_148, Ask, G_slippage_140, 0, Ask + Gd_104 * Point, "Buy", G_magic_144, 0, Green);
      GlobalVariableDel(" ");
   }
}

// B6832796D33422B67F3123C913057ADB
void f0_4() {
   int ticket_0;
   if (!GlobalVariableCheck("")) {
      GlobalVariableSet("", TimeCurrent());
      if (Gi_188 && f0_3() == 1 && DayOfWeek() >= Gi_132 && DayOfWeek() <= Gi_136) ticket_0 = OrderSend(Symbol(), OP_SELL, G_lots_148, Bid, G_slippage_140, 0, 0, "Sell", G_magic_144, 0, Pink);
      else
         if (f0_3() == 1 && DayOfWeek() >= Gi_132 && DayOfWeek() <= Gi_136) ticket_0 = OrderSend(Symbol(), OP_SELL, G_lots_148, Bid, G_slippage_140, 0, Bid - Gd_104 * Point, "Sell", G_magic_144, 0, Pink);
      GlobalVariableDel("");
   }
}

// CCB1059879745CEF2FE100D422C58CC5
void f0_6() {
   int datetime_0 = 0;
   double order_open_price_4 = 0;
   double order_lots_12 = 0;
   double order_takeprofit_20 = 0;
   int cmd_28 = -1;
   int ticket_32 = 0;
   int pos_36 = 0;
   for (pos_36 = 0; pos_36 < OrdersTotal(); pos_36++) {
      OrderSelect(pos_36, SELECT_BY_POS, MODE_TRADES);
      if (OrderMagicNumber() != G_magic_144 || OrderType() != OP_BUY) continue;
      if (OrderOpenTime() > datetime_0 && f0_3() == 1) {
         datetime_0 = OrderOpenTime();
         order_open_price_4 = OrderOpenPrice();
         cmd_28 = OrderType();
         ticket_32 = OrderTicket();
         order_takeprofit_20 = OrderTakeProfit();
      }
      if (OrderLots() > order_lots_12) order_lots_12 = OrderLots();
   }
   double isar_40 = iSAR(NULL, 0, Gd_172, Gd_180, 0);
   double ima_48 = iMA(NULL, 0, G_period_156, Gi_160, G_ma_method_164, G_applied_price_168, 0);
   int Li_56 = MathRound(MathLog(order_lots_12 / Lots) / MathLog(Gd_92)) + 1.0;
   if (Li_56 < 0) Li_56 = 0;
   G_lots_148 = NormalizeDouble(Lots * MathPow(Gd_92, Li_56), Gi_192);
   if ((Li_56 == 0 && isar_40 < ima_48 && DayOfWeek() < Gi_136) || (Li_56 == 0 && isar_40 < ima_48 && DayOfWeek() >= Gi_132 && DayOfWeek() <= Gi_136)) f0_2();
   if (order_open_price_4 - Ask > Gi_112 * Point && Li_56 < Gi_116) {
      f0_2();
      return;
   }
   for (pos_36 = 0; pos_36 < OrdersTotal(); pos_36++) {
      OrderSelect(pos_36, SELECT_BY_POS, MODE_TRADES);
      if (OrderMagicNumber() != G_magic_144 || OrderType() != OP_BUY || OrderTakeProfit() == order_takeprofit_20 || order_takeprofit_20 == 0.0) continue;
      OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), order_takeprofit_20, 0, Pink);
   }
}

// B69C187B5B28537387FD39F3050D06F5
void f0_5() {
   int datetime_0 = 0;
   double order_open_price_4 = 0;
   double order_lots_12 = 0;
   double order_takeprofit_20 = 0;
   int cmd_28 = -1;
   int ticket_32 = 0;
   int pos_36 = 0;
   for (pos_36 = 0; pos_36 < OrdersTotal(); pos_36++) {
      OrderSelect(pos_36, SELECT_BY_POS, MODE_TRADES);
      if (OrderMagicNumber() != G_magic_144 || OrderType() != OP_SELL) continue;
      if (OrderOpenTime() > datetime_0 && f0_3() == 1) {
         datetime_0 = OrderOpenTime();
         order_open_price_4 = OrderOpenPrice();
         cmd_28 = OrderType();
         ticket_32 = OrderTicket();
         order_takeprofit_20 = OrderTakeProfit();
      }
      if (OrderLots() > order_lots_12) order_lots_12 = OrderLots();
   }
   double isar_40 = iSAR(NULL, 0, Gd_172, Gd_180, 0);
   double ima_48 = iMA(NULL, 0, G_period_156, Gi_160, G_ma_method_164, G_applied_price_168, 0);
   int Li_56 = MathRound(MathLog(order_lots_12 / Lots) / MathLog(Gd_92)) + 1.0;
   if (Li_56 < 0) Li_56 = 0;
   G_lots_148 = NormalizeDouble(Lots * MathPow(Gd_92, Li_56), Gi_192);
   if ((Li_56 == 0 && isar_40 > ima_48 && DayOfWeek() < Gi_136) || (Li_56 == 0 && isar_40 > ima_48 && DayOfWeek() >= Gi_132 && DayOfWeek() <= Gi_136)) f0_4();
   if (Bid - order_open_price_4 > Gi_112 * Point && order_open_price_4 > 0.0 && Li_56 < Gi_116) {
      f0_4();
      return;
   }
   for (pos_36 = 0; pos_36 < OrdersTotal(); pos_36++) {
      OrderSelect(pos_36, SELECT_BY_POS, MODE_TRADES);
      if (OrderMagicNumber() != G_magic_144 || OrderType() != OP_SELL || OrderTakeProfit() == order_takeprofit_20 || order_takeprofit_20 == 0.0) continue;
      OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), order_takeprofit_20, 0, Pink);
   }
}

// EA2B2676C28C0DB26D39331A336C6B92
int start() {
   if (Gi_188) {
      for (int pos_0 = 0; pos_0 < OrdersTotal(); pos_0++) {
         if (OrderSelect(pos_0, SELECT_BY_POS)) {
            if (OrderMagicNumber() == G_magic_144) {
               if (OrderTakeProfit() == 0.0) {
                  if (OrderType() == OP_BUY) {
                     OrderModify(OrderTicket(), 0, OrderStopLoss(), OrderOpenPrice() + Gd_104 * Point, 0, White);
                     continue;
                  }
                  if (OrderType() == OP_SELL) OrderModify(OrderTicket(), 0, OrderStopLoss(), OrderOpenPrice() - Gd_104 * Point, 0, White);
               }
            }
         }
      }
   }
   if (f0_0() != 0) {
      f0_6();
      f0_5();
      f0_7();
      return (0);
   }
   return (0);
}

// E2E55FE76D835B9574F0F0BBDE4389D4
void f0_7() {
   string dbl2str_0 = DoubleToStr(f0_1(2), 2);
   Comment("2-Pairs-Trader - www.mql54.com ", 
      "\nAccount Leverage  :  " + "1 : " + AccountLeverage(), 
      "\nAccount Type  :  " + AccountServer(), 
      "\nServer Time  :  " + TimeToStr(TimeCurrent(), TIME_SECONDS), 
      "\nAccount Equity  = ", AccountEquity(), 
      "\nFree Margin     = ", AccountFreeMargin(), 
   "\nDrawdown  :  ", dbl2str_0, "%\n");
}

// 060BF2D587991D8F090A1309B285291C
int f0_0() {
   return (1);
}

// 0E1322D82872392ADED74DAACDDE6704
double f0_1(int Ai_0) {
   double Ld_ret_4;
   if (Ai_0 == 2) {
      Ld_ret_4 = (AccountEquity() / AccountBalance() - 1.0) / (-0.01);
      if (Ld_ret_4 <= 0.0) return (0);
      return (Ld_ret_4);
   }
   if (Ai_0 == 1) {
      Ld_ret_4 = 100.0 * (AccountEquity() / AccountBalance() - 1.0);
      if (Ld_ret_4 <= 0.0) return (0);
      return (Ld_ret_4);
   }
   return (0.0);
}