
#property copyright "Automatic Forex Crusher"
#property link      "http://www.automaticforexcrusher.com"

extern bool Trade1 = TRUE;
extern double TradeLots1 = 0.1;
extern int TradeSL1 = 30;
extern int TradeTS1 = 50;
extern int TradeTP1 = 0;
extern bool Trade2 = TRUE;
extern double TradeLots2 = 0.1;
extern int TradeSL2 = 50;
extern int TradeTS2 = 80;
extern int TradeTP2 = 0;
extern bool Trade3 = TRUE;
extern double TradeLots3 = 0.1;
extern int TradeSL3 = 80;
extern int TradeTS3 = 120;
extern int TradeTP3 = 0;
extern bool Trade4 = TRUE;
extern double TradeLots4 = 0.1;
extern int TradeSL4 = 120;
extern int TradeTS4 = 160;
extern int TradeTP4 = 0;
extern int TrailingStop = 0;
extern int TrailingStep = 1;
extern int BreakEven = 0;
extern int Slippage = 3;
extern string Crusher_Warning = "Dont change any paramaters below-from this point";
extern int Repeat = 3;
extern int Periods = 5;
extern bool UseAlert = FALSE;
extern bool SendEmail = FALSE;
extern bool TradeAtCloseBar = TRUE;
extern int MagicNumber = 20090202;
extern string TradeLog = "ForexCrusher";
extern string Indicator_Setting = "----------Demark Indicator Setting-----------";
extern int showBars = 3000;
extern int LevDP = 2;
extern int qSteps = 1;
extern int BackStep = 0;
extern int startBar = 0;
extern bool TrendLine = FALSE;
extern bool HorizontLine = FALSE;
extern bool ChannelLine = FALSE;
extern bool TakeLines = FALSE;
extern bool Comments = FALSE;
extern bool Reverse = FALSE;
extern int Trend = 0;
extern bool ShowArrows = TRUE;
extern bool CustomFeatures = TRUE;
extern color UpTrendColor = Green;
extern color DownTrendColor = Red;
extern int TrendlineWidth = 3;
extern bool ShowAlerts = FALSE;
extern bool EmailAlert = FALSE;
extern string TradeC1 = "Trade1";
extern string TradeC2 = "Trade2";
extern string TradeC3 = "Trade3";
extern string TradeC4 = "Trade4";
int gi_unused_344 = 0;
double gd_unused_348 = 10.0;
int gi_356 = 0;
int g_datetime_360 = 0;
double gd_364 = 0.0;
double gd_372 = 0.0;
int gi_380 = 0;
double gd_384 = 300.0;
int gi_392 = 5;
int gi_396 = 1;
double gd_unused_400 = 0.0;
int gi_408 = 7;
int gi_unused_412 = 0;
string gs_unused_416 = "alert.wav";
double gd_424 = 0.0;
double gd_unused_432 = 0.0;
int gi_unused_440 = 0;
string g_name_444 = "";
string gs_452 = "ForexCrusher";
double g_icustom_460 = 0.0;
double g_icustom_468 = 0.0;
int g_time_476 = 0;

int init() {
   gi_356 = Repeat;
   return (0);
}

int deinit() {
   return (0);
}

int start() {
   if (TradeAtCloseBar) gi_380 = 1;
   else gi_380 = 0;
   g_name_444 = Symbol() + TradeLog + "-" + Month() + "-" + Day() + ".log";
   gi_unused_440 = MarketInfo(Symbol(), MODE_SPREAD);
   int l_pos_0 = 0;
   double ld_unused_4 = 0;
   double ld_unused_12 = 0;
   ld_unused_4 = 0;
   ld_unused_12 = 0;
   if (CntOrd(OP_BUY, MagicNumber) > 0) gi_unused_412 = 1;
   if (CntOrd(OP_SELL, MagicNumber) > 0) gi_unused_412 = -1;
   if (gd_424 > 0.0) ld_unused_4 = 1;
   if (gd_424 < 0.0) ld_unused_12 = 1;
   int l_pos_20 = 0;
   int l_count_24 = 0;
   int li_28 = 0;
   int li_32 = 0;
   bool li_36 = FALSE;
   bool li_40 = FALSE;
   double ld_unused_44 = 0;
   double ld_unused_52 = 0;
   double ld_unused_60 = 0;
   bool li_68 = FALSE;
   bool li_72 = TRUE;
   if (li_72 == TRUE) {
      li_72 = FALSE;
      g_time_476 = Time[0];
   }
   if (g_time_476 == Time[0] && TimeCurrent() - g_time_476 > gd_384) li_68 = FALSE;
   else {
      g_time_476 = Time[0];
      li_68 = TRUE;
   }
   bool li_76 = FALSE;
   bool li_80 = FALSE;
   if (TimeCurrent() >= Time[0] + 60 * Period() - gd_364) li_76 = TRUE;
   else li_76 = FALSE;
   if (TimeCurrent() >= Time[0] + 60 * Period() - gd_372) li_80 = TRUE;
   else li_80 = FALSE;
   if (gd_364 == 0.0) li_76 = TRUE;
   if (gd_372 == 0.0) li_80 = TRUE;
   l_count_24 = 0;
   for (l_pos_20 = 0; l_pos_20 < OrdersTotal(); l_pos_20++) {
      OrderSelect(l_pos_20, SELECT_BY_POS, MODE_TRADES);
      if (OrderType() == OP_SELL || OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber || MagicNumber == 0) l_count_24++;
   }
   if (l_count_24 >= 1) {
      li_28 = 0;
      li_32 = 0;
   }
   li_32 = 0;
   li_28 = 0;
   li_40 = FALSE;
   li_36 = FALSE;
   g_icustom_460 = iCustom(NULL, 0, "DeMark", showBars, LevDP, qSteps, BackStep, startBar, TrendLine, HorizontLine, ChannelLine, TakeLines, Comments, Trend, ShowArrows, CustomFeatures, UpTrendColor, DownTrendColor, TrendlineWidth, ShowAlerts, EmailAlert, 2, gi_380);
   g_icustom_468 = iCustom(NULL, 0, "DeMark", showBars, LevDP, qSteps, BackStep, startBar, TrendLine, HorizontLine, ChannelLine, TakeLines, Comments, Trend, ShowArrows, CustomFeatures, UpTrendColor, DownTrendColor, TrendlineWidth, ShowAlerts, EmailAlert, 3, gi_380);
   if (g_icustom_460 > 0.0 && g_icustom_460 != EMPTY_VALUE) {
      li_32 = 1;
      li_28 = 0;
   }
   if (g_icustom_468 > 0.0 && g_icustom_468 != EMPTY_VALUE) {
      li_28 = 1;
      li_32 = 0;
   }
   if (li_32 > 0 && li_28 > 0) {
      li_28 = 0;
      li_32 = 0;
   }
   bool li_84 = FALSE;
   if (Reverse) {
      li_84 = li_28;
      li_28 = li_32;
      li_32 = li_84;
   }
   if (li_28 > 0) li_40 = TRUE;
   if (li_32 > 0) li_36 = TRUE;
   subPrintDetails();
   for (l_pos_20 = 0; l_pos_20 < OrdersTotal(); l_pos_20++) {
      OrderSelect(l_pos_20, SELECT_BY_POS, MODE_TRADES);
      if (OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber || MagicNumber == 0) {
         if (li_40 == TRUE && li_80 == TRUE) {
            if (li_68 == TRUE && gi_380 > 0) {
               SetText(Time[0], High[0] + 1 * gi_408 * Point, "CloseBuy" + DoubleToStr(Time[0], 0), CharToStr(251), 16711935);
               PlaySound("alert.wav");
               OrdClose(OrderTicket(), OrderLots(), Bid, Slippage, Red);
               Alerts(0, 0, li_40, li_36, Bid, 0, 0, OrderTicket());
               return (0);
            }
            if (gi_380 == 0) {
               SetText(Time[0], High[0] + 1 * gi_408 * Point, "CloseBuy" + DoubleToStr(Time[0], 0), CharToStr(251), 16711935);
               PlaySound("alert.wav");
               OrdClose(OrderTicket(), OrderLots(), Bid, Slippage, Red);
               Alerts(0, 0, li_40, li_36, Bid, 0, 0, OrderTicket());
               return (0);
            }
         }
      }
      if (OrderType() == OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber || MagicNumber == 0) {
         if (li_36 == TRUE && li_80 == TRUE) {
            if (li_68 == TRUE && gi_380 > 0) {
               SetText(Time[0], High[0] - 0.3 * gi_408 * Point, "CloseSell" + DoubleToStr(Time[0], 0), CharToStr(251), 16711935);
               PlaySound("alert.wav");
               OrdClose(OrderTicket(), OrderLots(), Ask, Slippage, Red);
               Alerts(0, 0, li_40, li_36, Ask, 0, 0, OrderTicket());
               return (0);
            }
            if (gi_380 == 0) {
               SetText(Time[0], High[0] - 0.3 * gi_408 * Point, "CloseSell" + DoubleToStr(Time[0], 0), CharToStr(251), 16711935);
               PlaySound("alert.wav");
               OrdClose(OrderTicket(), OrderLots(), Ask, Slippage, Red);
               Alerts(0, 0, li_40, li_36, Ask, 0, 0, OrderTicket());
               return (0);
            }
         }
      }
   }
   double ld_88 = 0;
   double ld_96 = 0;
   int li_104 = 0;
   if (l_count_24 == 0) {
      if (li_28 == 1 && li_76 == TRUE) {
         if (li_68 == TRUE && gi_380 > 0) {
            SetText(Time[0], High[0] + 1 * gi_408 * Point, "Sell" + DoubleToStr(Time[0], 0), CharToStr(234), 255);
            PlaySound("alert.wav");
            if (Trade1) {
               gs_452 = TradeC1;
               if (TradeTP1 == 0) ld_96 = 0;
               else ld_96 = Bid - TradeTP1 * Point;
               if (TradeSL1 == 0) ld_88 = 0;
               else ld_88 = Bid + TradeSL1 * Point;
               Alerts(li_32, li_28, 0, 0, Bid, ld_88, ld_96, li_104);
               li_104 = OrdSend(Symbol(), 1, TradeLots1, Bid, Slippage, ld_88, ld_96, gs_452, MagicNumber, 0, Red);
            }
            if (Trade2) {
               gs_452 = TradeC2;
               if (TradeTP2 == 0) ld_96 = 0;
               else ld_96 = Bid - TradeTP2 * Point;
               if (TradeSL2 == 0) ld_88 = 0;
               else ld_88 = Bid + TradeSL2 * Point;
               Alerts(li_32, li_28, 0, 0, Bid, ld_88, ld_96, li_104);
               li_104 = OrdSend(Symbol(), 1, TradeLots2, Bid, Slippage, ld_88, ld_96, gs_452, MagicNumber, 0, Red);
            }
            if (Trade3) {
               gs_452 = TradeC3;
               if (TradeTP3 == 0) ld_96 = 0;
               else ld_96 = Bid - TradeTP3 * Point;
               if (TradeSL3 == 0) ld_88 = 0;
               else ld_88 = Bid + TradeSL3 * Point;
               Alerts(li_32, li_28, 0, 0, Bid, ld_88, ld_96, li_104);
               li_104 = OrdSend(Symbol(), 1, TradeLots3, Bid, Slippage, ld_88, ld_96, gs_452, MagicNumber, 0, Red);
            }
            if (Trade4) {
               gs_452 = TradeC4;
               if (TradeTP4 == 0) ld_96 = 0;
               else ld_96 = Bid - TradeTP4 * Point;
               if (TradeSL4 == 0) ld_88 = 0;
               else ld_88 = Bid + TradeSL4 * Point;
               Alerts(li_32, li_28, 0, 0, Bid, ld_88, ld_96, li_104);
               li_104 = OrdSend(Symbol(), 1, TradeLots4, Bid, Slippage, ld_88, ld_96, gs_452, MagicNumber, 0, Red);
            }
            li_28 = 0;
            return (0);
         }
         if (gi_380 == 0) {
            SetText(Time[0], High[0] + 1 * gi_408 * Point, "Sell" + DoubleToStr(Time[0], 0), CharToStr(234), 255);
            PlaySound("alert.wav");
            if (Trade1) {
               gs_452 = TradeC1;
               if (TradeTP1 == 0) ld_96 = 0;
               else ld_96 = Bid - TradeTP1 * Point;
               if (TradeSL1 == 0) ld_88 = 0;
               else ld_88 = Bid + TradeSL1 * Point;
               Alerts(li_32, li_28, 0, 0, Bid, ld_88, ld_96, li_104);
               li_104 = OrdSend(Symbol(), 1, TradeLots1, Bid, Slippage, ld_88, ld_96, gs_452, MagicNumber, 0, Red);
            }
            if (Trade2) {
               gs_452 = TradeC2;
               if (TradeTP2 == 0) ld_96 = 0;
               else ld_96 = Bid - TradeTP2 * Point;
               if (TradeSL2 == 0) ld_88 = 0;
               else ld_88 = Bid + TradeSL2 * Point;
               Alerts(li_32, li_28, 0, 0, Bid, ld_88, ld_96, li_104);
               li_104 = OrdSend(Symbol(), 1, TradeLots2, Bid, Slippage, ld_88, ld_96, gs_452, MagicNumber, 0, Red);
            }
            if (Trade3) {
               gs_452 = TradeC3;
               if (TradeTP3 == 0) ld_96 = 0;
               else ld_96 = Bid - TradeTP3 * Point;
               if (TradeSL3 == 0) ld_88 = 0;
               else ld_88 = Bid + TradeSL3 * Point;
               Alerts(li_32, li_28, 0, 0, Bid, ld_88, ld_96, li_104);
               li_104 = OrdSend(Symbol(), 1, TradeLots3, Bid, Slippage, ld_88, ld_96, gs_452, MagicNumber, 0, Red);
            }
            if (Trade4) {
               gs_452 = TradeC4;
               if (TradeTP4 == 0) ld_96 = 0;
               else ld_96 = Bid - TradeTP4 * Point;
               if (TradeSL4 == 0) ld_88 = 0;
               else ld_88 = Bid + TradeSL4 * Point;
               Alerts(li_32, li_28, 0, 0, Bid, ld_88, ld_96, li_104);
               li_104 = OrdSend(Symbol(), 1, TradeLots4, Bid, Slippage, ld_88, ld_96, gs_452, MagicNumber, 0, Red);
            }
            li_28 = 0;
            return (0);
         }
      }
      if (li_32 == 1 && li_76 == TRUE) {
         if (li_68 == TRUE && gi_380 > 0) {
            SetText(Time[0], Low[0] - 0.3 * gi_408 * Point, "Buy" + DoubleToStr(Time[0], 0), CharToStr(233), 65280);
            PlaySound("alert.wav");
            if (Trade1) {
               gs_452 = TradeC1;
               if (TradeTP1 == 0) ld_96 = 0;
               else ld_96 = Ask + TradeTP1 * Point;
               if (TradeSL1 == 0) ld_88 = 0;
               else ld_88 = Ask - TradeSL1 * Point;
               Alerts(li_32, li_28, 0, 0, Ask, ld_88, ld_96, li_104);
               li_104 = OrdSend(Symbol(), 0, TradeLots1, Ask, Slippage, ld_88, ld_96, gs_452, MagicNumber, 0, Green);
            }
            if (Trade2) {
               gs_452 = TradeC2;
               if (TradeTP2 == 0) ld_96 = 0;
               else ld_96 = Ask + TradeTP2 * Point;
               if (TradeSL2 == 0) ld_88 = 0;
               else ld_88 = Ask - TradeSL2 * Point;
               Alerts(li_32, li_28, 0, 0, Ask, ld_88, ld_96, li_104);
               li_104 = OrdSend(Symbol(), 0, TradeLots2, Ask, Slippage, ld_88, ld_96, gs_452, MagicNumber, 0, Green);
            }
            if (Trade4) {
               gs_452 = TradeC4;
               if (TradeTP3 == 0) ld_96 = 0;
               else ld_96 = Ask + TradeTP3 * Point;
               if (TradeSL3 == 0) ld_88 = 0;
               else ld_88 = Ask - TradeSL3 * Point;
               Alerts(li_32, li_28, 0, 0, Ask, ld_88, ld_96, li_104);
               li_104 = OrdSend(Symbol(), 0, TradeLots3, Ask, Slippage, ld_88, ld_96, gs_452, MagicNumber, 0, Green);
            }
            if (Trade4) {
               gs_452 = TradeC4;
               if (TradeTP3 == 0) ld_96 = 0;
               else ld_96 = Ask + TradeTP4 * Point;
               if (TradeSL3 == 0) ld_88 = 0;
               else ld_88 = Ask - TradeSL4 * Point;
               Alerts(li_32, li_28, 0, 0, Ask, ld_88, ld_96, li_104);
               li_104 = OrdSend(Symbol(), 0, TradeLots4, Ask, Slippage, ld_88, ld_96, gs_452, MagicNumber, 0, Green);
            }
            li_32 = 0;
            return (0);
         }
         if (gi_380 == 0) {
            SetText(Time[0], Low[0] - 0.3 * gi_408 * Point, "Buy" + DoubleToStr(Time[0], 0), CharToStr(233), 65280);
            PlaySound("alert.wav");
            if (Trade1) {
               gs_452 = TradeC1;
               if (TradeTP1 == 0) ld_96 = 0;
               else ld_96 = Ask + TradeTP1 * Point;
               if (TradeSL1 == 0) ld_88 = 0;
               else ld_88 = Ask - TradeSL1 * Point;
               Alerts(li_32, li_28, 0, 0, Ask, ld_88, ld_96, li_104);
               li_104 = OrdSend(Symbol(), 0, TradeLots1, Ask, Slippage, ld_88, ld_96, gs_452, MagicNumber, 0, Red);
            }
            if (Trade3) {
               gs_452 = TradeC3;
               if (TradeTP2 == 0) ld_96 = 0;
               else ld_96 = Ask + TradeTP2 * Point;
               if (TradeSL2 == 0) ld_88 = 0;
               else ld_88 = Ask - TradeSL2 * Point;
               Alerts(li_32, li_28, 0, 0, Ask, ld_88, ld_96, li_104);
               li_104 = OrdSend(Symbol(), 0, TradeLots2, Ask, Slippage, ld_88, ld_96, gs_452, MagicNumber, 0, Red);
            }
            if (Trade3) {
               gs_452 = TradeC3;
               if (TradeTP3 == 0) ld_96 = 0;
               else ld_96 = Ask + TradeTP3 * Point;
               if (TradeSL3 == 0) ld_88 = 0;
               else ld_88 = Ask - TradeSL3 * Point;
               Alerts(li_32, li_28, 0, 0, Ask, ld_88, ld_96, li_104);
               li_104 = OrdSend(Symbol(), 0, TradeLots3, Ask, Slippage, ld_88, ld_96, gs_452, MagicNumber, 0, Red);
            }
            if (Trade4) {
               gs_452 = TradeC4;
               if (TradeTP3 == 0) ld_96 = 0;
               else ld_96 = Ask + TradeTP4 * Point;
               if (TradeSL3 == 0) ld_88 = 0;
               else ld_88 = Ask - TradeSL4 * Point;
               Alerts(li_32, li_28, 0, 0, Ask, ld_88, ld_96, li_104);
               li_104 = OrdSend(Symbol(), 0, TradeLots4, Ask, Slippage, ld_88, ld_96, gs_452, MagicNumber, 0, Red);
            }
            li_32 = 0;
            return (0);
         }
      }
   }
   for (l_pos_0 = 0; l_pos_0 < OrdersTotal(); l_pos_0++) {
      if (OrderSelect(l_pos_0, SELECT_BY_POS, MODE_TRADES)) {
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber || MagicNumber == 0) {
            TrailingPositions();
            TrailingPositions2(TradeC2, TradeSL1);
            TrailingPositions2(TradeC2, TradeSL2);
            TrailingPositions2(TradeC3, TradeSL3);
            TrailingPositions2(TradeC4, TradeSL4);
         }
      }
   }
   Alerts(0, 0, 0, 0, 0, 0, 0, 0);
   return (0);
}

void SetText(int ai_unused_0, double ad_unused_4, string as_unused_12, string as_unused_20, int ai_unused_28) {
}

void Alerts(int ai_0, int ai_4, int ai_8, int ai_12, double ad_16, double ad_24, double ad_32, int ai_40) {
   string ls_44 = "";
   ls_44 = "";
   string l_time2str_52 = "";
   l_time2str_52 = TimeToStr(TimeCurrent(), TIME_DATE|TIME_MINUTES);
   if (UseAlert) {
      if (ai_0 == 1) {
         if (gi_356 == Repeat) g_datetime_360 = 0;
         if (gi_356 > 0 && TimeCurrent() - g_datetime_360 > Periods) {
            if (ai_0 == 1) {
               ls_44 = ls_44 + "Buy @ " + DoubleToStr(ad_16, Digits) + "; SL: " + DoubleToStr(ad_24, Digits) + "; TP: " + DoubleToStr(ad_32, Digits) + " at " + l_time2str_52 + " Order:" + DoubleToStr(ai_40, 0) + ".";
               Alert(Symbol(), " ", Period(), ": ", ls_44);
               if (SendEmail) SendMail(Symbol() + " " + Period() + ": ", Symbol() + " " + Period() + ": " + ls_44);
               gi_356--;
               g_datetime_360 = TimeCurrent();
            }
         }
      }
      if (ai_4 == 1) {
         if (gi_356 == Repeat) g_datetime_360 = 0;
         if (gi_356 > 0 && TimeCurrent() - g_datetime_360 > Periods) {
            if (ai_4 == 1) {
               ls_44 = ls_44 + "Sell @ " + DoubleToStr(ad_16, Digits) + "; SL: " + DoubleToStr(ad_24, Digits) + "; TP: " + DoubleToStr(ad_32, Digits) + " at " + l_time2str_52 + " Order:" + DoubleToStr(ai_40, 0) + ".";
               Alert(Symbol(), " ", Period(), ": ", ls_44);
               if (SendEmail) SendMail(Symbol() + " " + Period() + ": ", Symbol() + " " + Period() + ": " + ls_44);
               gi_356--;
               g_datetime_360 = TimeCurrent();
            }
         }
      }
      if (ai_12 == 1) {
         if (gi_356 == Repeat) g_datetime_360 = 0;
         if (gi_356 > 0 && TimeCurrent() - g_datetime_360 > Periods) {
            if (ai_12 == 1) {
               ls_44 = ls_44 + " Close Sell @ " + DoubleToStr(ad_16, Digits) + " at " + l_time2str_52 + " Order:" + DoubleToStr(ai_40, 0) + ".";
               Alert(Symbol(), " ", Period(), ": ", ls_44);
               if (SendEmail) SendMail(Symbol() + " " + Period() + ": ", Symbol() + " " + Period() + ": " + ls_44);
               gi_356--;
               g_datetime_360 = TimeCurrent();
            }
         }
      }
      if (ai_8 == 1) {
         if (gi_356 == Repeat) g_datetime_360 = 0;
         if (gi_356 > 0 && TimeCurrent() - g_datetime_360 > Periods) {
            if (ai_8 == 1) {
               ls_44 = ls_44 + " Close Buy @ " + DoubleToStr(ad_16, Digits) + " at " + l_time2str_52 + " Order:" + DoubleToStr(ai_40, 0) + ".";
               Alert(Symbol(), " ", Period(), ": ", ls_44);
               if (SendEmail) SendMail(Symbol() + " " + Period() + ": ", Symbol() + " " + Period() + ": " + ls_44);
               gi_356--;
               g_datetime_360 = TimeCurrent();
            }
         }
      }
      if (ai_8 == 0 && ai_12 == 0 && ai_0 == 0 && ai_4 == 0) {
         gi_356 = Repeat;
         g_datetime_360 = 0;
      }
   }
}

void subPrintDetails() {
   string ls_0 = "";
   string ls_8 = "----------------------------------------\n";
   string ls_16 = "\n";
   string ls_unused_24 = "";
   ls_0 = "ForexCrusher v.1.0" + ls_16;
   ls_0 = ls_0 + ls_8;
   ls_0 = ls_0 + "TradeLots1=" + DoubleToStr(TradeLots1, 2) + " | " + "TradeSL1=" + DoubleToStr(TradeSL1, 0) + " | " + "TradeTP1=" + DoubleToStr(TradeTP1, 0) + " | " + "TradeTS1=" + DoubleToStr(TradeTS1, 0) + ls_16;
   ls_0 = ls_0 + "TradeLots2=" + DoubleToStr(TradeLots2, 2) + " | " + "TradeSL2=" + DoubleToStr(TradeSL2, 0) + " | " + "TradeTP2=" + DoubleToStr(TradeTP2, 0) + " | " + "TradeTS2=" + DoubleToStr(TradeTS2, 0) + ls_16;
   ls_0 = ls_0 + "TradeLots3=" + DoubleToStr(TradeLots3, 2) + " | " + "TradeSL3=" + DoubleToStr(TradeSL3, 0) + " | " + "TradeTP3=" + DoubleToStr(TradeTP3, 0) + " | " + "TradeTS3=" + DoubleToStr(TradeTS3, 0) + ls_16;
   ls_0 = ls_0 + "TradeLots4=" + DoubleToStr(TradeLots4, 2) + " | " + "TradeSL4=" + DoubleToStr(TradeSL4, 0) + " | " + "TradeTP4=" + DoubleToStr(TradeTP4, 0) + " | " + "TradeTS4=" + DoubleToStr(TradeTS4, 0) + ls_16;
   ls_0 = ls_0 + "TrailingStop=" + DoubleToStr(TrailingStop, 0) + ls_16;
   ls_0 = ls_0 + ls_8;
   Comment(ls_0);
}

int CntOrd(int a_cmd_0, int a_magic_4) {
   int l_count_8 = 0;
   for (int l_pos_12 = 0; l_pos_12 < OrdersTotal(); l_pos_12++) {
      OrderSelect(l_pos_12, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol())
         if (OrderType() == a_cmd_0 && OrderMagicNumber() == a_magic_4 || a_magic_4 == 0) l_count_8++;
   }
   return (l_count_8);
}

string ErrorDescription(int ai_0) {
   string ls_ret_4;
   switch (ai_0) {
   case 0:
   case 1:
      ls_ret_4 = "no error";
      break;
   case 2:
      ls_ret_4 = "common error";
      break;
   case 3:
      ls_ret_4 = "invalid trade parameters";
      break;
   case 4:
      ls_ret_4 = "trade server is busy";
      break;
   case 5:
      ls_ret_4 = "old version of the client terminal";
      break;
   case 6:
      ls_ret_4 = "no connection with trade server";
      break;
   case 7:
      ls_ret_4 = "not enough rights";
      break;
   case 8:
      ls_ret_4 = "too frequent requests";
      break;
   case 9:
      ls_ret_4 = "malfunctional trade operation";
      break;
   case 64:
      ls_ret_4 = "account disabled";
      break;
   case 65:
      ls_ret_4 = "invalid account";
      break;
   case 128:
      ls_ret_4 = "trade timeout";
      break;
   case 129:
      ls_ret_4 = "invalid price";
      break;
   case 130:
      ls_ret_4 = "invalid stops";
      break;
   case 131:
      ls_ret_4 = "invalid trade volume";
      break;
   case 132:
      ls_ret_4 = "market is closed";
      break;
   case 133:
      ls_ret_4 = "trade is disabled";
      break;
   case 134:
      ls_ret_4 = "not enough money";
      break;
   case 135:
      ls_ret_4 = "price changed";
      break;
   case 136:
      ls_ret_4 = "off quotes";
      break;
   case 137:
      ls_ret_4 = "broker is busy";
      break;
   case 138:
      ls_ret_4 = "requote";
      break;
   case 139:
      ls_ret_4 = "order is locked";
      break;
   case 140:
      ls_ret_4 = "long positions only allowed";
      break;
   case 141:
      ls_ret_4 = "too many requests";
      break;
   case 145:
      ls_ret_4 = "modification denied because order too close to market";
      break;
   case 146:
      ls_ret_4 = "trade context is busy";
      break;
   case 4000:
      ls_ret_4 = "no error";
      break;
   case 4001:
      ls_ret_4 = "wrong function pointer";
      break;
   case 4002:
      ls_ret_4 = "array index is out of range";
      break;
   case 4003:
      ls_ret_4 = "no memory for function call stack";
      break;
   case 4004:
      ls_ret_4 = "recursive stack overflow";
      break;
   case 4005:
      ls_ret_4 = "not enough stack for parameter";
      break;
   case 4006:
      ls_ret_4 = "no memory for parameter string";
      break;
   case 4007:
      ls_ret_4 = "no memory for temp string";
      break;
   case 4008:
      ls_ret_4 = "not initialized string";
      break;
   case 4009:
      ls_ret_4 = "not initialized string in array";
      break;
   case 4010:
      ls_ret_4 = "no memory for array\' string";
      break;
   case 4011:
      ls_ret_4 = "too long string";
      break;
   case 4012:
      ls_ret_4 = "remainder from zero divide";
      break;
   case 4013:
      ls_ret_4 = "zero divide";
      break;
   case 4014:
      ls_ret_4 = "unknown command";
      break;
   case 4015:
      ls_ret_4 = "wrong jump (never generated error)";
      break;
   case 4016:
      ls_ret_4 = "not initialized array";
      break;
   case 4017:
      ls_ret_4 = "dll calls are not allowed";
      break;
   case 4018:
      ls_ret_4 = "cannot load library";
      break;
   case 4019:
      ls_ret_4 = "cannot call function";
      break;
   case 4020:
      ls_ret_4 = "expert function calls are not allowed";
      break;
   case 4021:
      ls_ret_4 = "not enough memory for temp string returned from function";
      break;
   case 4022:
      ls_ret_4 = "system is busy (never generated error)";
      break;
   case 4050:
      ls_ret_4 = "invalid function parameters count";
      break;
   case 4051:
      ls_ret_4 = "invalid function parameter value";
      break;
   case 4052:
      ls_ret_4 = "string function internal error";
      break;
   case 4053:
      ls_ret_4 = "some array error";
      break;
   case 4054:
      ls_ret_4 = "incorrect series array using";
      break;
   case 4055:
      ls_ret_4 = "custom indicator error";
      break;
   case 4056:
      ls_ret_4 = "arrays are incompatible";
      break;
   case 4057:
      ls_ret_4 = "global variables processing error";
      break;
   case 4058:
      ls_ret_4 = "global variable not found";
      break;
   case 4059:
      ls_ret_4 = "function is not allowed in testing mode";
      break;
   case 4060:
      ls_ret_4 = "function is not confirmed";
      break;
   case 4061:
      ls_ret_4 = "send mail error";
      break;
   case 4062:
      ls_ret_4 = "string parameter expected";
      break;
   case 4063:
      ls_ret_4 = "integer parameter expected";
      break;
   case 4064:
      ls_ret_4 = "double parameter expected";
      break;
   case 4065:
      ls_ret_4 = "array as parameter expected";
      break;
   case 4066:
      ls_ret_4 = "requested history data in update state";
      break;
   case 4099:
      ls_ret_4 = "end of file";
      break;
   case 4100:
      ls_ret_4 = "some file error";
      break;
   case 4101:
      ls_ret_4 = "wrong file name";
      break;
   case 4102:
      ls_ret_4 = "too many opened files";
      break;
   case 4103:
      ls_ret_4 = "cannot open file";
      break;
   case 4104:
      ls_ret_4 = "incompatible access to a file";
      break;
   case 4105:
      ls_ret_4 = "no order selected";
      break;
   case 4106:
      ls_ret_4 = "unknown symbol";
      break;
   case 4107:
      ls_ret_4 = "invalid price parameter for trade function";
      break;
   case 4108:
      ls_ret_4 = "invalid ticket";
      break;
   case 4109:
      ls_ret_4 = "trade is not allowed";
      break;
   case 4110:
      ls_ret_4 = "longs are not allowed";
      break;
   case 4111:
      ls_ret_4 = "shorts are not allowed";
      break;
   case 4200:
      ls_ret_4 = "object is already exist";
      break;
   case 4201:
      ls_ret_4 = "unknown object property";
      break;
   case 4202:
      ls_ret_4 = "object is not exist";
      break;
   case 4203:
      ls_ret_4 = "unknown object type";
      break;
   case 4204:
      ls_ret_4 = "no object name";
      break;
   case 4205:
      ls_ret_4 = "object coordinates error";
      break;
   case 4206:
      ls_ret_4 = "no specified subwindow";
      break;
   default:
      ls_ret_4 = "unknown error";
   }
   return (ls_ret_4);
}

int Write(string as_0) {
   int l_file_8 = FileOpen(g_name_444, FILE_CSV|FILE_WRITE|FILE_READ, "/t");
   FileSeek(l_file_8, 0, SEEK_END);
   FileWrite(l_file_8, " Time " + TimeToStr(TimeCurrent(), TIME_DATE|TIME_SECONDS) + ": " + as_0);
   FileClose(l_file_8);
   Print(as_0);
   return (0);
}

int OrdSend(string a_symbol_0, int ai_8, double a_lots_12, double ad_20, int a_slippage_28, double ad_32, double ad_40, string a_comment_48 = "", int a_magic_56 = 0, int a_datetime_60 = 0, color a_color_64 = -1) {
   int l_ticket_80;
   int l_ticket_84;
   int li_68 = MarketInfo(a_symbol_0, MODE_STOPLEVEL);
   double ld_72 = 0;
   int l_count_88 = 0;
   switch (ai_8) {
   case 0:
      if (!IsTradeContextBusy() && IsTradeAllowed()) {
         while (l_count_88 < gi_392) {
            RefreshRates();
            l_ticket_80 = OrderSend(a_symbol_0, OP_BUY, a_lots_12, Ask, a_slippage_28, NormalizeDouble(ad_32, Digits), NormalizeDouble(ad_40, Digits), a_comment_48, a_magic_56, a_datetime_60, a_color_64);
            if (l_ticket_80 <= 0) {
               Write("Error Occured : " + ErrorDescription(GetLastError()));
               Write(Symbol() + " Buy @ " + Ask + " SL @ " + ad_32 + " TP @" + ad_40 + " ticket =" + l_ticket_80);
               l_count_88++;
            } else {
               l_count_88 = gi_392;
               Write("Order opened : " + Symbol() + " Buy @ " + Ask + " SL @ " + ad_32 + " TP @" + ad_40 + " ticket =" + l_ticket_80);
            }
            Sleep(1000 * gi_396);
         }
      }
      l_ticket_84 = l_ticket_80;
      break;
   case 1:
      if (!IsTradeContextBusy() && IsTradeAllowed()) {
         while (l_count_88 < gi_392) {
            RefreshRates();
            l_ticket_80 = OrderSend(a_symbol_0, OP_SELL, a_lots_12, Bid, a_slippage_28, NormalizeDouble(ad_32, Digits), NormalizeDouble(ad_40, Digits), a_comment_48, a_magic_56, a_datetime_60, a_color_64);
            if (l_ticket_80 <= 0) {
               Write("Error Occured : " + ErrorDescription(GetLastError()));
               Write(Symbol() + " Sell @ " + Bid + " SL @ " + ad_32 + " TP @" + ad_40 + " ticket =" + l_ticket_80);
               l_count_88++;
            } else {
               l_count_88 = gi_392;
               Write("Order opened : " + Symbol() + " Sell @ " + Bid + " SL @ " + ad_32 + " TP @" + ad_40 + " ticket =" + l_ticket_80);
            }
            Sleep(1000 * gi_396);
         }
      }
      l_ticket_84 = l_ticket_80;
      break;
   case 4:
      while (l_count_88 < gi_392) {
         RefreshRates();
         if (ad_20 - Ask < li_68 * Point) ld_72 = Ask + li_68 * Point;
         else ld_72 = ad_20;
         l_ticket_80 = OrderSend(a_symbol_0, OP_BUYSTOP, a_lots_12, NormalizeDouble(ld_72, Digits), a_slippage_28, NormalizeDouble(ad_32, Digits), NormalizeDouble(ad_40, Digits), a_comment_48, a_magic_56, a_datetime_60, a_color_64);
         if (l_ticket_80 <= 0) {
            Write("Error Occured : " + ErrorDescription(GetLastError()));
            Write(Symbol() + " Buy Stop @ " + ld_72 + " SL @ " + ad_32 + " TP @" + ad_40 + " ticket =" + l_ticket_80);
            l_count_88++;
         } else {
            l_count_88 = gi_392;
            Write("Order opened : " + Symbol() + " Buy Stop@ " + ld_72 + " SL @ " + ad_32 + " TP @" + ad_40 + " ticket =" + l_ticket_80);
         }
         Sleep(1000 * gi_396);
      }
      l_ticket_84 = l_ticket_80;
      break;
   case 5:
      if (!IsTradeContextBusy() && IsTradeAllowed()) {
         while (l_count_88 < gi_392) {
            RefreshRates();
            if (Bid - ad_20 < li_68 * Point) ld_72 = Bid - li_68 * Point;
            else ld_72 = ad_20;
            l_ticket_80 = OrderSend(a_symbol_0, OP_SELLSTOP, a_lots_12, NormalizeDouble(ld_72, Digits), a_slippage_28, NormalizeDouble(ad_32, Digits), NormalizeDouble(ad_40, Digits), a_comment_48, a_magic_56, a_datetime_60, a_color_64);
            if (l_ticket_80 <= 0) {
               Write("Error Occured : " + ErrorDescription(GetLastError()));
               Write(Symbol() + " Sell Stop @ " + ld_72 + " SL @ " + ad_32 + " TP @" + ad_40 + " ticket =" + l_ticket_80);
               l_count_88++;
            } else {
               l_count_88 = gi_392;
               Write("Order opened : " + Symbol() + " Sell Stop @ " + ld_72 + " SL @ " + ad_32 + " TP @" + ad_40 + " ticket =" + l_ticket_80);
            }
            Sleep(1000 * gi_396);
         }
      }
      l_ticket_84 = l_ticket_80;
      break;
   case 2:
      if (!IsTradeContextBusy() && IsTradeAllowed()) {
         while (l_count_88 < gi_392) {
            RefreshRates();
            if (Ask - ad_20 < li_68 * Point) ld_72 = Ask - li_68 * Point;
            else ld_72 = ad_20;
            l_ticket_80 = OrderSend(a_symbol_0, OP_BUYLIMIT, a_lots_12, NormalizeDouble(ld_72, Digits), a_slippage_28, NormalizeDouble(ad_32, Digits), NormalizeDouble(ad_40, Digits), a_comment_48, a_magic_56, a_datetime_60, a_color_64);
            if (l_ticket_80 <= 0) {
               Write("Error Occured : " + ErrorDescription(GetLastError()));
               Write(Symbol() + " Buy Limit @ " + ld_72 + " SL @ " + ad_32 + " TP @" + ad_40 + " ticket =" + l_ticket_80);
               l_count_88++;
            } else {
               l_count_88 = gi_392;
               Write("Order opened : " + Symbol() + " Buy Limit @ " + ld_72 + " SL @ " + ad_32 + " TP @" + ad_40 + " ticket =" + l_ticket_80);
            }
            Sleep(1000 * gi_396);
         }
      }
      l_ticket_84 = l_ticket_80;
      break;
   case 3:
      if (!IsTradeContextBusy() && IsTradeAllowed()) {
         while (l_count_88 < gi_392) {
            RefreshRates();
            if (ad_20 - Bid < li_68 * Point) ld_72 = Bid + li_68 * Point;
            else ld_72 = ad_20;
            l_ticket_80 = OrderSend(a_symbol_0, OP_BUYLIMIT, a_lots_12, NormalizeDouble(ld_72, Digits), a_slippage_28, NormalizeDouble(ad_32, Digits), NormalizeDouble(ad_40, Digits), a_comment_48, a_magic_56, a_datetime_60, a_color_64);
            if (l_ticket_80 <= 0) {
               Write("Error Occured : " + ErrorDescription(GetLastError()));
               Write(Symbol() + " Sell Limit @ " + ld_72 + " SL @ " + ad_32 + " TP @" + ad_40 + " ticket =" + l_ticket_80);
               l_count_88++;
            } else {
               l_count_88 = gi_392;
               Write("Order opened : " + Symbol() + " Sell Limit @ " + ld_72 + " SL @ " + ad_32 + " TP @" + ad_40 + " ticket =" + l_ticket_80);
            }
            Sleep(1000 * gi_396);
         }
         l_ticket_84 = l_ticket_80;
      }
      break;
   default:
      Write("No valid type of order found");
      l_ticket_84 = -1;
   }
   return (l_ticket_84);
}

int OrdClose(int a_ticket_0, double a_lots_4, double ad_12, int a_slippage_20, color a_color_24 = -1) {
   int l_ord_close_36;
   double ld_unused_28 = 0;
   int l_count_44 = 0;
   if (!IsTradeContextBusy() && IsTradeAllowed()) {
      while (l_count_44 < gi_392) {
         RefreshRates();
         l_ord_close_36 = OrderClose(a_ticket_0, a_lots_4, NormalizeDouble(ad_12, Digits), a_slippage_20, a_color_24);
         if (l_ord_close_36 == 0) {
            Write("Error Occured : " + ErrorDescription(GetLastError()));
            Write(Symbol() + " Close @ " + ad_12 + " ticket =" + a_ticket_0);
            l_count_44++;
         } else {
            l_count_44 = gi_392;
            Write("Order closed : " + Symbol() + " Close @ " + ad_12 + " ticket =" + a_ticket_0);
         }
         Sleep(1000 * gi_396);
      }
   }
   int l_ord_close_40 = l_ord_close_36;
   return (l_ord_close_40);
}

int OrdModify(int a_ticket_0, double ad_4, double ad_12, double ad_20, int a_datetime_28, color a_color_32 = -1) {
   bool l_bool_44;
   double ld_unused_36 = 0;
   int l_count_52 = 0;
   if (!IsTradeContextBusy() && IsTradeAllowed()) {
      while (l_count_52 < gi_392) {
         RefreshRates();
         l_bool_44 = OrderModify(a_ticket_0, NormalizeDouble(ad_4, Digits), NormalizeDouble(ad_12, Digits), NormalizeDouble(ad_20, Digits), a_datetime_28, a_color_32);
         if (l_bool_44 == FALSE) {
            Write("Error Occured : " + ErrorDescription(GetLastError()));
            Write(Symbol() + " Modify @ " + ad_4 + " SL @ " + ad_12 + " TP @" + ad_20 + " ticket =" + a_ticket_0);
            l_count_52++;
         } else {
            l_count_52 = gi_392;
            Write("Order modified : " + Symbol() + " Modify @ " + ad_4 + " SL @ " + ad_12 + " TP @" + ad_20 + " ticket =" + a_ticket_0);
         }
         Sleep(1000 * gi_396);
      }
   }
   int l_bool_48 = l_bool_44;
   return (l_bool_48);
}

void TrailingPositions() {
   double l_bid_0;
   double l_ask_8;
   double l_point_16 = MarketInfo(OrderSymbol(), MODE_POINT);
   if (OrderType() == OP_BUY) {
      l_bid_0 = MarketInfo(OrderSymbol(), MODE_BID);
      if (BreakEven > 0) {
         if (l_bid_0 - OrderOpenPrice() > BreakEven * l_point_16)
            if (OrderStopLoss() - OrderOpenPrice() < 0.0) ModifyStopLoss(OrderOpenPrice() + 0.0 * l_point_16);
      }
      if (TrailingStop > 0) {
         if (l_bid_0 - OrderOpenPrice() > TrailingStop * l_point_16) {
            if (OrderStopLoss() < l_bid_0 - (TrailingStop + TrailingStep - 1) * l_point_16) {
               ModifyStopLoss(l_bid_0 - TrailingStop * l_point_16);
               return;
            }
         }
      }
   }
   if (OrderType() == OP_SELL) {
      l_ask_8 = MarketInfo(OrderSymbol(), MODE_ASK);
      if (BreakEven > 0) {
         if (OrderOpenPrice() - l_ask_8 > BreakEven * l_point_16)
            if (OrderOpenPrice() - OrderStopLoss() < 0.0) ModifyStopLoss(OrderOpenPrice() - 0.0 * l_point_16);
      }
      if (TrailingStop > 0) {
         if (OrderOpenPrice() - l_ask_8 > TrailingStop * l_point_16)
            if (OrderStopLoss() > l_ask_8 + (TrailingStop + TrailingStep - 1) * l_point_16 || OrderStopLoss() == 0.0) ModifyStopLoss(l_ask_8 + TrailingStop * l_point_16);
      }
   }
}

void ModifyStopLoss(double ad_0) {
   PlaySound("alert.wav");
   int li_8 = OrdModify(OrderTicket(), OrderOpenPrice(), ad_0, OrderTakeProfit(), 0, CLR_NONE);
}

void TrailingPositions2(string as_0, int ai_8) {
   double l_bid_12;
   double l_ask_20;
   double l_point_28 = MarketInfo(OrderSymbol(), MODE_POINT);
   if (OrderType() == OP_BUY) {
      l_bid_12 = MarketInfo(OrderSymbol(), MODE_BID);
      if (ai_8 > 0 && OrderComment() == as_0) {
         if (l_bid_12 - OrderOpenPrice() > ai_8 * l_point_28) {
            if (OrderStopLoss() < l_bid_12 - (ai_8 + TrailingStep - 1) * l_point_28) {
               ModifyStopLoss(l_bid_12 - ai_8 * l_point_28);
               return;
            }
         }
      }
   }
   if (OrderType() == OP_SELL) {
      l_ask_20 = MarketInfo(OrderSymbol(), MODE_ASK);
      if (ai_8 > 0 && OrderComment() == as_0) {
         if (OrderOpenPrice() - l_ask_20 > ai_8 * l_point_28)
            if (OrderStopLoss() > l_ask_20 + (ai_8 + TrailingStep - 1) * l_point_28 || OrderStopLoss() == 0.0) ModifyStopLoss(l_ask_20 + ai_8 * l_point_28);
      }
   }
}