/*
   Generated by EX4-TO-MQ4 decompiler V4.0.224.1 []
   Website: http://purebeam.biz
   E-mail : purebeam@gmail.com
*/
#property copyright "Fx_s"
#property link      ""

extern double Lots = 0.1;
double gd_84 = 1.667;
extern double TakeProfit = 10.0;
double g_pips_100 = 500.0;
double gd_108 = 10.0;
double gd_116 = 10.0;
extern double PipStep = 30.0;
int gi_132 = 0;
int gi_136 = 0;
extern int MaxTrades = 8;
bool gi_144 = FALSE;
double gd_148 = 20.0;
bool gi_156 = FALSE;
bool gi_160 = FALSE;
double gd_164 = 48.0;
extern double slip = 3.0;
extern int MagicNumber = 12345;
double g_price_184;
double gd_192;
double gd_unused_200;
double gd_unused_208;
double g_price_216;
double g_bid_224;
double g_ask_232;
double gd_240;
double gd_248;
double gd_264;
bool gi_272;
string gs_276 = "Fx_s";
int g_time_284 = 0;
int gi_288;
int g_ord_total_292 = 0;
double gd_296;
int g_pos_304 = 0;
double gd_312 = 0.0;
bool gi_320 = FALSE;
bool gi_324 = FALSE;
bool gi_328 = FALSE;
int gi_332;
bool gi_336 = FALSE;
double gd_340;
double gd_348;

int init() {
   gd_264 = MarketInfo(Symbol(), MODE_SPREAD) * Point;
   return (0);
}

int deinit() {
   return (0);
}

int start() {
   double l_iclose_20;
   double l_iclose_28;
   if (gi_156) TrailingAlls(gd_108, gd_116, g_price_216);
   if (gi_160) {
      if (TimeCurrent() >= gi_288) {
         CloseThisSymbolAll();
         Print("Closed All due to TimeOut");
      }
   }
   if (g_time_284 == Time[0]) return (0);
   g_time_284 = Time[0];
   double ld_0 = CalculateProfit();
   if (gi_144) {
      if (ld_0 < 0.0 && MathAbs(ld_0) > gd_148 / 100.0 * AccountEquityHigh()) {
         CloseThisSymbolAll();
         Print("Closed All due to Stop Out");
         gi_336 = FALSE;
      }
   }
   int l_ord_total_8 = OrdersTotal();
   int l_count_12 = 0;
   for (int l_pos_16 = 0; l_pos_16 < l_ord_total_8; l_pos_16++)
      if (OrderSelect(l_pos_16, SELECT_BY_POS, MODE_TRADES) != FALSE) l_count_12++;
   l_ord_total_8 = CountTrades();
   if (l_ord_total_8 == 0) gi_272 = FALSE;
   for (g_pos_304 = OrdersTotal() - 1; g_pos_304 >= 0; g_pos_304--) {
      OrderSelect(g_pos_304, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
         if (OrderType() == OP_BUY) {
            if (gi_132 != 0 || gi_136 != 0) {
               if (gi_132 == l_count_12 || gi_136 == l_count_12) {
                  gi_324 = TRUE;
                  gi_328 = FALSE;
                  break;
               }
               gi_324 = FALSE;
               gi_328 = TRUE;
               break;
            }
            gi_324 = TRUE;
            gi_328 = FALSE;
            break;
         }
      }
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
         if (OrderType() == OP_SELL) {
            if (gi_132 != 0 || gi_136 != 0) {
               if (gi_132 == l_count_12 || gi_136 == l_count_12) {
                  gi_324 = FALSE;
                  gi_328 = TRUE;
                  break;
               }
               gi_324 = TRUE;
               gi_328 = FALSE;
               break;
            }
            gi_324 = FALSE;
            gi_328 = TRUE;
            break;
         }
      }
   }
   if (l_ord_total_8 > 0 && l_ord_total_8 <= MaxTrades) {
      RefreshRates();
      gd_240 = FindLastBuyPrice();
      gd_248 = FindLastSellPrice();
      if (gi_324 && gd_240 - Ask >= PipStep * Point) gi_320 = TRUE;
      if (gi_328 && Bid - gd_248 >= PipStep * Point) gi_320 = TRUE;
   }
   if (l_ord_total_8 < 1) {
      gi_328 = FALSE;
      gi_324 = FALSE;
      gi_320 = TRUE;
      gd_192 = AccountEquity();
   }
   if (gi_320) {
      gd_240 = FindLastBuyPrice();
      gd_248 = FindLastSellPrice();
      if (gi_328) {
         g_ord_total_292 = l_ord_total_8;
         gd_296 = NormalizeDouble(Lots * MathPow(gd_84, g_ord_total_292), 1);
         RefreshRates();
         gi_332 = OpenPendingOrder(1, gd_296, Bid, slip, Ask, 0, 0, gs_276 + "-" + g_ord_total_292, MagicNumber, 0, HotPink);
         if (gi_332 < 0) {
            Print("Error: ", GetLastError());
            return (0);
         }
         gd_248 = FindLastSellPrice();
         gi_320 = FALSE;
         gi_336 = TRUE;
      } else {
         if (gi_324) {
            g_ord_total_292 = l_ord_total_8;
            gd_296 = NormalizeDouble(Lots * MathPow(gd_84, g_ord_total_292), 1);
            gi_332 = OpenPendingOrder(0, gd_296, Ask, slip, Bid, 0, 0, gs_276 + "-" + g_ord_total_292, MagicNumber, 0, Lime);
            if (gi_332 < 0) {
               Print("Error: ", GetLastError());
               return (0);
            }
            gd_240 = FindLastBuyPrice();
            gi_320 = FALSE;
            gi_336 = TRUE;
         }
      }
   }
   if (gi_320 && l_ord_total_8 < 1) {
      l_iclose_20 = iClose(Symbol(), 0, 2);
      l_iclose_28 = iClose(Symbol(), 0, 1);
      g_bid_224 = Bid;
      g_ask_232 = Ask;
      if (!gi_328 && !gi_324) {
         g_ord_total_292 = l_ord_total_8;
         gd_296 = NormalizeDouble(Lots * MathPow(gd_84, g_ord_total_292), 1);
         if (l_iclose_20 > l_iclose_28) {
            gi_332 = OpenPendingOrder(1, gd_296, g_bid_224, slip, g_bid_224, 0, 0, gs_276 + "-" + g_ord_total_292, MagicNumber, 0, HotPink);
            if (gi_332 < 0) {
               Print("Error: ", GetLastError());
               return (0);
            }
            gd_240 = FindLastBuyPrice();
            gi_336 = TRUE;
         } else {
            gi_332 = OpenPendingOrder(0, gd_296, g_ask_232, slip, g_ask_232, 0, 0, gs_276 + "-" + g_ord_total_292, MagicNumber, 0, Lime);
            if (gi_332 < 0) {
               Print("Error: ", GetLastError());
               return (0);
            }
            gd_248 = FindLastSellPrice();
            gi_336 = TRUE;
         }
         if (gi_332 > 0) gi_288 = TimeCurrent() + 60.0 * (60.0 * gd_164);
         gi_320 = FALSE;
      }
   }
   l_ord_total_8 = CountTrades();
   g_price_216 = 0;
   double ld_36 = 0;
   for (g_pos_304 = OrdersTotal() - 1; g_pos_304 >= 0; g_pos_304--) {
      OrderSelect(g_pos_304, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
         if (OrderType() == OP_BUY || OrderType() == OP_SELL) {
            g_price_216 += OrderOpenPrice() * OrderLots();
            ld_36 += OrderLots();
         }
      }
   }
   if (l_ord_total_8 > 0) g_price_216 = NormalizeDouble(g_price_216 / ld_36, Digits);
   if (gi_336) {
      for (g_pos_304 = OrdersTotal() - 1; g_pos_304 >= 0; g_pos_304--) {
         OrderSelect(g_pos_304, SELECT_BY_POS, MODE_TRADES);
         if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
            if (OrderType() == OP_BUY) {
               g_price_184 = g_price_216 + TakeProfit * Point;
               gd_unused_200 = g_price_184;
               gd_312 = g_price_216 - g_pips_100 * Point;
               gi_272 = TRUE;
            }
         }
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
            if (OrderType() == OP_SELL) {
               g_price_184 = g_price_216 - TakeProfit * Point;
               gd_unused_208 = g_price_184;
               gd_312 = g_price_216 + g_pips_100 * Point;
               gi_272 = TRUE;
            }
         }
      }
   }
   if (gi_336) {
      if (gi_272 == TRUE) {
         for (g_pos_304 = OrdersTotal() - 1; g_pos_304 >= 0; g_pos_304--) {
            OrderSelect(g_pos_304, SELECT_BY_POS, MODE_TRADES);
            if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
            if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) OrderModify(OrderTicket(), g_price_216, OrderStopLoss(), g_price_184, 0, Yellow);
            gi_336 = FALSE;
         }
      }
   }
   return (0);
}

int CountTrades() {
   int l_count_0 = 0;
   for (int l_pos_4 = OrdersTotal() - 1; l_pos_4 >= 0; l_pos_4--) {
      OrderSelect(l_pos_4, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
         if (OrderType() == OP_SELL || OrderType() == OP_BUY) l_count_0++;
   }
   return (l_count_0);
}

void CloseThisSymbolAll() {
   for (int l_pos_0 = OrdersTotal() - 1; l_pos_0 >= 0; l_pos_0--) {
      OrderSelect(l_pos_0, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol()) {
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
            if (OrderType() == OP_BUY) OrderClose(OrderTicket(), OrderLots(), Bid, slip, Blue);
            if (OrderType() == OP_SELL) OrderClose(OrderTicket(), OrderLots(), Ask, slip, Red);
         }
         Sleep(1000);
      }
   }
}

int OpenPendingOrder(int ai_0, double a_lots_4, double a_price_12, int a_slippage_20, double ad_24, int ai_32, int ai_36, string a_comment_40, int a_magic_48, int a_datetime_52, color a_color_56) {
   int l_ticket_60 = 0;
   int l_error_64 = 0;
   int l_count_68 = 0;
   int li_72 = 100;
   switch (ai_0) {
   case 2:
      for (l_count_68 = 0; l_count_68 < li_72; l_count_68++) {
         l_ticket_60 = OrderSend(Symbol(), OP_BUYLIMIT, a_lots_4, a_price_12, a_slippage_20, StopLong(ad_24, ai_32), TakeLong(a_price_12, ai_36), a_comment_40, a_magic_48, a_datetime_52, a_color_56);
         l_error_64 = GetLastError();
         if (l_error_64 == 0/* NO_ERROR */) break;
         if (!(l_error_64 == 4/* SERVER_BUSY */ || l_error_64 == 137/* BROKER_BUSY */ || l_error_64 == 146/* TRADE_CONTEXT_BUSY */ || l_error_64 == 136/* OFF_QUOTES */)) break;
         Sleep(1000);
      }
      break;
   case 4:
      for (l_count_68 = 0; l_count_68 < li_72; l_count_68++) {
         l_ticket_60 = OrderSend(Symbol(), OP_BUYSTOP, a_lots_4, a_price_12, a_slippage_20, StopLong(ad_24, ai_32), TakeLong(a_price_12, ai_36), a_comment_40, a_magic_48, a_datetime_52, a_color_56);
         l_error_64 = GetLastError();
         if (l_error_64 == 0/* NO_ERROR */) break;
         if (!(l_error_64 == 4/* SERVER_BUSY */ || l_error_64 == 137/* BROKER_BUSY */ || l_error_64 == 146/* TRADE_CONTEXT_BUSY */ || l_error_64 == 136/* OFF_QUOTES */)) break;
         Sleep(5000);
      }
      break;
   case 0:
      for (l_count_68 = 0; l_count_68 < li_72; l_count_68++) {
         RefreshRates();
         l_ticket_60 = OrderSend(Symbol(), OP_BUY, a_lots_4, Ask, a_slippage_20, StopLong(Bid, ai_32), TakeLong(Ask, ai_36), a_comment_40, a_magic_48, a_datetime_52, a_color_56);
         l_error_64 = GetLastError();
         if (l_error_64 == 0/* NO_ERROR */) break;
         if (!(l_error_64 == 4/* SERVER_BUSY */ || l_error_64 == 137/* BROKER_BUSY */ || l_error_64 == 146/* TRADE_CONTEXT_BUSY */ || l_error_64 == 136/* OFF_QUOTES */)) break;
         Sleep(5000);
      }
      break;
   case 3:
      for (l_count_68 = 0; l_count_68 < li_72; l_count_68++) {
         l_ticket_60 = OrderSend(Symbol(), OP_SELLLIMIT, a_lots_4, a_price_12, a_slippage_20, StopShort(ad_24, ai_32), TakeShort(a_price_12, ai_36), a_comment_40, a_magic_48, a_datetime_52, a_color_56);
         l_error_64 = GetLastError();
         if (l_error_64 == 0/* NO_ERROR */) break;
         if (!(l_error_64 == 4/* SERVER_BUSY */ || l_error_64 == 137/* BROKER_BUSY */ || l_error_64 == 146/* TRADE_CONTEXT_BUSY */ || l_error_64 == 136/* OFF_QUOTES */)) break;
         Sleep(5000);
      }
      break;
   case 5:
      for (l_count_68 = 0; l_count_68 < li_72; l_count_68++) {
         l_ticket_60 = OrderSend(Symbol(), OP_SELLSTOP, a_lots_4, a_price_12, a_slippage_20, StopShort(ad_24, ai_32), TakeShort(a_price_12, ai_36), a_comment_40, a_magic_48, a_datetime_52, a_color_56);
         l_error_64 = GetLastError();
         if (l_error_64 == 0/* NO_ERROR */) break;
         if (!(l_error_64 == 4/* SERVER_BUSY */ || l_error_64 == 137/* BROKER_BUSY */ || l_error_64 == 146/* TRADE_CONTEXT_BUSY */ || l_error_64 == 136/* OFF_QUOTES */)) break;
         Sleep(5000);
      }
      break;
   case 1:
      for (l_count_68 = 0; l_count_68 < li_72; l_count_68++) {
         l_ticket_60 = OrderSend(Symbol(), OP_SELL, a_lots_4, Bid, a_slippage_20, StopShort(Ask, ai_32), TakeShort(Bid, ai_36), a_comment_40, a_magic_48, a_datetime_52, a_color_56);
         l_error_64 = GetLastError();
         if (l_error_64 == 0/* NO_ERROR */) break;
         if (!(l_error_64 == 4/* SERVER_BUSY */ || l_error_64 == 137/* BROKER_BUSY */ || l_error_64 == 146/* TRADE_CONTEXT_BUSY */ || l_error_64 == 136/* OFF_QUOTES */)) break;
         Sleep(5000);
      }
   }
   return (l_ticket_60);
}

double StopLong(double ad_0, int ai_8) {
   if (ai_8 == 0) return (0);
   return (ad_0 - ai_8 * Point);
}

double StopShort(double ad_0, int ai_8) {
   if (ai_8 == 0) return (0);
   return (ad_0 + ai_8 * Point);
}

double TakeLong(double ad_0, int ai_8) {
   if (ai_8 == 0) return (0);
   return (ad_0 + ai_8 * Point);
}

double TakeShort(double ad_0, int ai_8) {
   if (ai_8 == 0) return (0);
   return (ad_0 - ai_8 * Point);
}

double CalculateProfit() {
   double ld_ret_0 = 0;
   for (g_pos_304 = OrdersTotal() - 1; g_pos_304 >= 0; g_pos_304--) {
      OrderSelect(g_pos_304, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
         if (OrderType() == OP_BUY || OrderType() == OP_SELL) ld_ret_0 += OrderProfit();
   }
   return (ld_ret_0);
}

void TrailingAlls(int ai_0, int ai_4, double a_price_8) {
   int li_16;
   double l_ord_stoploss_20;
   double l_price_28;
   if (ai_4 != 0) {
      for (int l_pos_36 = OrdersTotal() - 1; l_pos_36 >= 0; l_pos_36--) {
         if (OrderSelect(l_pos_36, SELECT_BY_POS, MODE_TRADES)) {
            if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
            if (OrderSymbol() == Symbol() || OrderMagicNumber() == MagicNumber) {
               if (OrderType() == OP_BUY) {
                  li_16 = NormalizeDouble((Bid - a_price_8) / Point, 0);
                  if (li_16 < ai_0) continue;
                  l_ord_stoploss_20 = OrderStopLoss();
                  l_price_28 = Bid - ai_4 * Point;
                  if (l_ord_stoploss_20 == 0.0 || (l_ord_stoploss_20 != 0.0 && l_price_28 > l_ord_stoploss_20)) OrderModify(OrderTicket(), a_price_8, l_price_28, OrderTakeProfit(), 0, Aqua);
               }
               if (OrderType() == OP_SELL) {
                  li_16 = NormalizeDouble((a_price_8 - Ask) / Point, 0);
                  if (li_16 < ai_0) continue;
                  l_ord_stoploss_20 = OrderStopLoss();
                  l_price_28 = Ask + ai_4 * Point;
                  if (l_ord_stoploss_20 == 0.0 || (l_ord_stoploss_20 != 0.0 && l_price_28 < l_ord_stoploss_20)) OrderModify(OrderTicket(), a_price_8, l_price_28, OrderTakeProfit(), 0, Red);
               }
            }
            Sleep(1000);
         }
      }
   }
}

double AccountEquityHigh() {
   if (CountTrades() == 0) gd_340 = AccountEquity();
   if (gd_340 < gd_348) gd_340 = gd_348;
   else gd_340 = AccountEquity();
   gd_348 = AccountEquity();
   return (gd_340);
}

double FindLastBuyPrice() {
   double l_ord_open_price_8;
   int l_ticket_24;
   double ld_unused_0 = 0;
   int l_ticket_20 = 0;
   for (int l_pos_16 = OrdersTotal() - 1; l_pos_16 >= 0; l_pos_16--) {
      OrderSelect(l_pos_16, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == OP_BUY) {
         l_ticket_24 = OrderTicket();
         if (l_ticket_24 > l_ticket_20) {
            l_ord_open_price_8 = OrderOpenPrice();
            ld_unused_0 = l_ord_open_price_8;
            l_ticket_20 = l_ticket_24;
         }
      }
   }
   return (l_ord_open_price_8);
}

double FindLastSellPrice() {
   double l_ord_open_price_8;
   int l_ticket_24;
   double ld_unused_0 = 0;
   int l_ticket_20 = 0;
   for (int l_pos_16 = OrdersTotal() - 1; l_pos_16 >= 0; l_pos_16--) {
      OrderSelect(l_pos_16, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == OP_SELL) {
         l_ticket_24 = OrderTicket();
         if (l_ticket_24 > l_ticket_20) {
            l_ord_open_price_8 = OrderOpenPrice();
            ld_unused_0 = l_ord_open_price_8;
            l_ticket_20 = l_ticket_24;
         }
      }
   }
   return (l_ord_open_price_8);
}
