#property copyright "Copyright © 2007, MQL Service"
#property link      "http://www.mqlservice.com"

#include <stdlib.mqh>

extern double PivotPoint = 0.0;
extern int StartUpHour = 100;
extern double LotSize = 0.01;
extern int StopLoss = 200;
extern int CT_TakeProfit = 5;
extern int T_TakeProfit = 10;
extern int TotalOrders = 90;
extern int CT_Interval = 1;
extern int T_Interval = 5;
extern int RangeGap = 10;
extern int Mode = 0;
extern int ProfitTargetFactor = 100;
extern int LossTargetFactor = 10000;
extern int InitialRangeMidline = 0;
extern int NumberOfCycles = 4;
extern int RunTime = 24;
extern bool Eraser = FALSE;
extern int Slippage = 0;
extern int SKIP_LEVEL_ERRORS = 1;
extern bool DoScreenShots = FALSE;
int gi_164 = 0;
int gi_168 = 0;
int gi_172 = 0;
int gi_176 = 20070807;
bool gi_180 = FALSE;
bool gi_unused_184 = TRUE;
int gi_188 = 0;
int gi_192 = 0;
int gi_196 = 0;
int gi_200 = 0;
bool gi_204 = FALSE;
int gi_208 = 0;

int init() {
   string ls_0;
   double l_ihigh_16;
   double l_ilow_24;
   double l_iclose_32;
   int l_datetime_40;
   if (IsExpertEnabled()) Comment("Waiting for the first tick...");
   else Comment("Experts are disabled");
   if (InitialRangeMidline == 1) gi_168 = MathRound(Bid / Point);
   else gi_168 = _pivot(Symbol(), Point, gi_200);
   if (Eraser) Print("Starting " + WindowExpertName() + " in ERASER mode.");
   else {
      switch (Mode) {
      case 0:
         ls_0 = "Auto(";
         break;
      case 2:
         ls_0 = "Trending(";
         break;
      case 1:
         ls_0 = "Counter trend(";
         break;
      default:
         ls_0 = "INVALID MODE #" + Mode + "(";
      }
      switch (InitialRangeMidline) {
      case 0:
         if (PivotPoint > 0.0) ls_0 = ls_0 + "Pivot entered manually)";
         else {
            l_ihigh_16 = iHigh(Symbol(), PERIOD_H1, gi_188);
            l_ilow_24 = iLow(Symbol(), PERIOD_H1, gi_192);
            l_iclose_32 = iClose(Symbol(), PERIOD_H1, gi_196);
            ls_0 = ls_0 + "Pivot [H(" + TimeToStr(iTime(Symbol(), PERIOD_H1, gi_188)) + ")= " + DoubleToStr(l_ihigh_16, Digits) + " + L(" + TimeToStr(iTime(Symbol(), PERIOD_H1, gi_192)) + ")= " + DoubleToStr(l_ilow_24, Digits) + " + C(" + TimeToStr(iTime(Symbol(), PERIOD_H1, gi_196)) + ")= " + DoubleToStr(l_iclose_32, Digits) + "]/3 = " + DoubleToStr((l_ihigh_16 + l_ilow_24 + l_iclose_32) / 3.0, Digits) + ")";
         }
         break;
      case 1:
         ls_0 = ls_0 + "Time of entry)";
         break;
      default:
         ls_0 = ls_0 + "INVALID MIDLINE #" + InitialRangeMidline + ")";
      }
      Print("Starting " + WindowExpertName() + " in " + ls_0 + " mode around " + gi_168);
   }
   gi_172 = gi_168;
   if (RunTime > 0) {
      l_datetime_40 = TimeLocal();
      if (StartUpHour >= 0) {
         for (gi_164 = 86400.0 * MathFloor(l_datetime_40 / 86400) + 3600.0 * MathRound(StartUpHour / 100) + 60.0 * MathMod(StartUpHour, 100); gi_164 < l_datetime_40; gi_164 += 86400) {
         }
      } else gi_164 = l_datetime_40;
   } else gi_164 = 0;
   gi_176 = 200708070;
   gi_180 = FALSE;
   gi_unused_184 = TRUE;
   Print("Gridzilla will start at ", TimeToStr(gi_164), " for ", RunTime, " hours.");
   return (0);
}

int deinit() {
   Comment("");
   return (0);
}

int start() {
   if (Bars < 100) {
      Comment("Waiting for bars...");
      return (0);
   }
   if (Eraser) {
      _remove_pendings(Symbol(), gi_176);
      Comment("Eraser utility active. Not placing any new orders.");
      return (0);
   }
   return (_Gridzilla(Symbol(), Period(), gi_176, LotSize, StopLoss, CT_TakeProfit, T_TakeProfit, Slippage, gi_164, gi_164 + 3600 * RunTime, InitialRangeMidline, Mode, TotalOrders, CT_Interval, T_Interval, ProfitTargetFactor, LossTargetFactor, RangeGap, NumberOfCycles, gi_200));
}

int _Gridzilla(string a_symbol_0, int ai_unused_8, int ai_12, double ad_16, int ai_24, int ai_28, int ai_32, int ai_36 = 0, int ai_40 = 0, int ai_44 = 0, int ai_48 = 0, int ai_52 = 0, int ai_56 = 90, int ai_60 = 1, int ai_64 = 5, int ai_68 = 100, int ai_72 = 10000, int ai_76 = 10, int ai_80 = 4, int ai_84 = -3) {
   int li_144;
   int li_148;
   int li_152;
   int li_156;
   int li_160;
   int li_164;
   int l_digits_88 = MarketInfo(a_symbol_0, MODE_DIGITS);
   if (l_digits_88 == 0) l_digits_88 = 4;
   double l_point_96 = MarketInfo(a_symbol_0, MODE_POINT);
   if (NormalizeDouble(l_point_96, l_digits_88) == 0.0) l_point_96 = Point;
   double l_bid_104 = MarketInfo(a_symbol_0, MODE_BID);
   double l_ask_112 = MarketInfo(a_symbol_0, MODE_ASK);
   int li_120 = MathRound(l_bid_104 / l_point_96);
   int li_unused_124 = MathRound(l_ask_112 / l_point_96);
   string ls_128 = "";
   double ld_136 = _profit(a_symbol_0, ai_12);
   if (ai_68 > 0) {
      if (ld_136 > ai_68) {
         _fa(a_symbol_0, ai_12);
         gi_180 = TRUE;
      }
   }
   if (ai_72 > 0) {
      if (-ld_136 > ai_72) {
         _fa(a_symbol_0, ai_12);
         gi_180 = TRUE;
      }
   }
   if (gi_180) {
      Comment("Profit or Loss TargetFactor reached. To continue restart the EA");
      return (0);
   }
   if (ai_40 > 0) {
      if (TimeLocal() < ai_40) {
         Comment("Gridzilla will start at ", TimeToStr(ai_40), " in ", _secondstodays(ai_40 - TimeLocal()), " seconds");
         gi_204 = TRUE;
         return (0);
      }
   }
   if (gi_204) {
      if (InitialRangeMidline == 1) gi_168 = MathRound(Bid / Point);
      else gi_168 = _pivot(Symbol(), Point, gi_200);
      Print("Starting " + WindowExpertName() + " around " + gi_168, " (", InitialRangeMidline, ")");
      gi_172 = gi_168;
   }
   gi_204 = FALSE;
   if (ai_44 > 0) {
      if (ai_44 < TimeLocal()) {
         Comment(WindowExpertName() + " halted at ", TimeToStr(ai_44));
         _remove_pendings(a_symbol_0, ai_12);
         return (0);
      }
   }
   ls_128 = WindowExpertName() + " started at " + TimeToStr(gi_164) + " for " + RunTime + " hours\n";
   ls_128 = ls_128 + "Time to run is " + _secondstodays(ai_44 - TimeLocal()) + "s; current profit is " + DoubleToStr(ld_136, 2) + "; target is " + ai_68 
   + "\n";
   ls_128 = ls_128 + "Midline: " + gi_172;
   if (ai_52 == 0) {
      if (ai_48 == 0) {
         li_144 = _pivot(a_symbol_0, l_point_96, ai_84);
         if (li_144 != gi_168) {
            Print("Pivot point change from ", gi_168, " to ", li_144);
            _remove_pendings(a_symbol_0, ai_12);
            gi_172 = li_144;
         }
         gi_168 = li_144;
      }
      li_148 = gi_172 + ai_56 * ai_60 / 2 + ai_76;
      li_152 = gi_172 - ai_56 * ai_60 / 2 - ai_76;
      li_156 = gi_172 + ai_56 * ai_60 + ai_76;
      li_160 = gi_172 - ai_56 * ai_60 - ai_76;
      ls_128 = ls_128 + "; range max: " + li_148 + "; range min: " + li_152 + "; midline up: " + li_156 + "; midline down: " + li_160;
      if (li_120 < li_148 && li_120 > li_152) {
         ls_128 = ls_128 
         + "\nCT mode around midpoint: " + DoubleToStr(gi_172 * l_point_96, l_digits_88);
         _ct(a_symbol_0, ai_12, ai_56, ai_60, ad_16, ai_36, gi_172, ai_24, ai_28, l_point_96, l_digits_88, ai_80, l_bid_104);
      } else {
         if (li_120 >= li_156) {
            ls_128 = ls_128 
            + "\nCT in upper range";
            gi_172 = li_156;
            _ct(a_symbol_0, ai_12, ai_56, ai_60, ad_16, ai_36, gi_172, ai_24, ai_28, l_point_96, l_digits_88, ai_80, l_bid_104);
         } else {
            if (li_120 <= li_160) {
               ls_128 = ls_128 
               + "\nCT in lower range";
               gi_172 = li_160;
               _ct(a_symbol_0, ai_12, ai_56, ai_60, ad_16, ai_36, gi_172, ai_24, ai_28, l_point_96, l_digits_88, ai_80, l_bid_104);
            } else {
               if (li_120 >= li_148) {
                  ls_128 = ls_128 
                  + "\nT mode up";
                  li_164 = (li_156 - li_148) / ai_64;
                  _t(a_symbol_0, ai_12, li_164, ai_64, ad_16, ai_36, li_148, ai_24, ai_32, l_point_96, l_digits_88, l_bid_104, 4);
               } else {
                  if (li_120 <= li_152) {
                     ls_128 = ls_128 
                     + "\nT mode dn";
                     li_164 = (li_152 - li_160) / ai_64;
                     _t(a_symbol_0, ai_12, li_164, ai_64, ad_16, ai_36, li_152, ai_24, ai_32, l_point_96, l_digits_88, l_bid_104, 5);
                  }
               }
            }
         }
      }
   }
   if (ai_52 == 1) {
      ls_128 = ls_128 
      + "\nCT mode";
      _ct(a_symbol_0, ai_12, ai_56, ai_60, ad_16, ai_36, gi_168, ai_24, ai_28, l_point_96, l_digits_88, ai_80, l_bid_104);
   }
   if (ai_52 == 2) {
      ls_128 = ls_128 
      + "\nT mode";
      _t(a_symbol_0, ai_12, ai_56, ai_64, ad_16, ai_36, gi_168, ai_24, ai_32, l_point_96, l_digits_88, l_bid_104, -1);
   }
   Comment(ls_128);
   return (0);
}

void _ct(string as_0, int ai_8, int ai_12, int ai_16, double ad_20, int ai_28, int ai_32, int ai_36, int ai_40, double ad_44, int ai_52, int ai_56, double ad_60) {
   int li_72;
   double ld_76;
   double ld_84;
   for (int l_count_68 = 0; l_count_68 < ai_12; l_count_68++) {
      li_72 = ai_16 * (l_count_68 - ai_12 / 2);
      if (li_72 >= 0) li_72 += ai_16;
      if (li_72 > 0) {
         ld_76 = (ai_32 + li_72) * ad_44;
         if (_no_order(as_0, ai_8, NormalizeDouble(ld_76, ai_52), ai_16, ai_56)) {
            if (ld_76 > ad_60) _Gridzilla_OrderSend(as_0, OP_SELLLIMIT, _nv(as_0, ad_20), ld_76, ai_28, _sl(3, as_0, ld_76, ai_36), _tp(3, as_0, ld_76, ai_40), WindowExpertName(), _order_magic(as_0, ai_8), 0, Red);
            else _Gridzilla_OrderSend(as_0, OP_SELLSTOP, _nv(as_0, ad_20), ld_76, ai_28, _sl(5, as_0, ld_76, ai_36), _tp(5, as_0, ld_76, ai_40), WindowExpertName(), _order_magic(as_0, ai_8), 0, Red);
         }
      } else {
         ld_84 = Ask - Bid;
         ld_76 = (ai_32 + li_72) * ad_44 + ld_84;
         if (_no_order(as_0, ai_8, NormalizeDouble(ld_76, ai_52), ai_16, ai_56)) {
            if (ld_76 < ad_60 + ld_84) _Gridzilla_OrderSend(as_0, OP_BUYLIMIT, _nv(as_0, ad_20), ld_76, ai_28, _sl(2, as_0, ld_76, ai_36), _tp(2, as_0, ld_76, ai_40), WindowExpertName(), _order_magic(as_0, ai_8), 0, Blue);
            else _Gridzilla_OrderSend(as_0, OP_BUYSTOP, _nv(as_0, ad_20), ld_76, ai_28, _sl(4, as_0, ld_76, ai_36), _tp(4, as_0, ld_76, ai_40), WindowExpertName(), _order_magic(as_0, ai_8), 0, Blue);
         }
      }
   }
}

void _t(string as_0, int ai_8, int ai_12, int ai_16, double ad_20, int ai_28, int ai_32, int ai_36, int ai_40, double ad_44, int ai_52, double ad_56, int ai_64) {
   bool l_bool_72;
   bool l_bool_76;
   int li_80;
   double ld_84;
   double ld_92;
   for (int l_count_68 = 0; l_count_68 < ai_12; l_count_68++) {
      l_bool_72 = ai_64 == 4;
      l_bool_76 = ai_64 == 5;
      if (ai_64 < 0) {
         l_bool_72 = TRUE;
         l_bool_76 = TRUE;
      }
      li_80 = ai_16 * (l_count_68 - ai_12 / 2);
      if (li_80 >= 0) li_80 += ai_16;
      if (li_80 > 0) {
         ld_84 = Ask - Bid;
         ld_92 = (ai_32 + li_80) * ad_44 + ld_84;
         if (l_bool_72) {
            if (_no_order(as_0, ai_8, NormalizeDouble(ld_92, ai_52), ai_16, -1)) {
               if (ld_92 > ad_56 + ld_84) _Gridzilla_OrderSend(as_0, OP_BUYSTOP, _nv(as_0, ad_20), ld_92, ai_28, _sl(4, as_0, ld_92, ai_36), _tp(4, as_0, ld_92, ai_40), WindowExpertName(), _order_magic(as_0, ai_8), 0, Blue);
               else _Gridzilla_OrderSend(as_0, OP_BUYLIMIT, _nv(as_0, ad_20), ld_92, ai_28, _sl(2, as_0, ld_92, ai_36), _tp(2, as_0, ld_92, ai_40), WindowExpertName(), _order_magic(as_0, ai_8), 0, Blue);
            }
         }
      } else {
         ld_92 = (ai_32 + li_80) * ad_44;
         if (l_bool_76) {
            if (_no_order(as_0, ai_8, NormalizeDouble(ld_92, ai_52), ai_16, -1)) {
               if (ld_92 < ad_56) _Gridzilla_OrderSend(as_0, OP_SELLSTOP, _nv(as_0, ad_20), ld_92, ai_28, _sl(5, as_0, ld_92, ai_36), _tp(5, as_0, ld_92, ai_40), WindowExpertName(), _order_magic(as_0, ai_8), 0, Red);
               else _Gridzilla_OrderSend(as_0, OP_SELLLIMIT, _nv(as_0, ad_20), ld_92, ai_28, _sl(3, as_0, ld_92, ai_36), _tp(3, as_0, ld_92, ai_40), WindowExpertName(), _order_magic(as_0, ai_8), 0, Red);
            }
         }
      }
   }
}

int _pivot(string a_symbol_0, double ad_8, int ai_16 = 0) {
   if (PivotPoint > 0.0) return (MathRound(PivotPoint / ad_8));
   int l_datetime_20 = TimeCurrent();
   for (int li_24 = 86400.0 * MathFloor(l_datetime_20 / 86400) + 3600.0 * MathRound((ai_16 + 24) % 24); li_24 > l_datetime_20; li_24 -= 86400) {
   }
   int li_28 = 0;
   li_24 += 3600 * (li_28 - 1);
   int l_shift_32 = iBarShift(a_symbol_0, PERIOD_H1, li_24);
   int li_36 = l_shift_32 + 20;
   int li_40 = (TimeHour(iTime(a_symbol_0, PERIOD_H1, l_shift_32)) + 1) % 24;
   while (TimeHour(iTime(a_symbol_0, PERIOD_H1, li_36)) != li_40 && li_36 < 50) li_36++;
   for (int li_44 = li_36 - l_shift_32 + 1; TimeDayOfWeek(iTime(a_symbol_0, PERIOD_H1, l_shift_32)) == 0; li_44--) l_shift_32++;
   gi_188 = iHighest(a_symbol_0, PERIOD_H1, MODE_HIGH, li_44, l_shift_32);
   gi_192 = iLowest(a_symbol_0, PERIOD_H1, MODE_LOW, li_44, l_shift_32);
   gi_196 = l_shift_32;
   double l_ihigh_48 = iHigh(a_symbol_0, PERIOD_H1, gi_188);
   double l_ilow_56 = iLow(a_symbol_0, PERIOD_H1, gi_192);
   double l_iclose_64 = iClose(a_symbol_0, PERIOD_H1, l_shift_32);
   double ld_72 = (l_ihigh_48 + l_ilow_56 + l_iclose_64) / 3.0;
   int li_ret_80 = MathRound(ld_72 / ad_8);
   return (li_ret_80);
}

bool _no_order(string a_symbol_0, int ai_8, double ad_12, int ai_20, int ai_24) {
   int l_datetime_28 = 0;
   bool li_32 = TRUE;
   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() != a_symbol_0) continue;
         if (OrderMagicNumber() / 10 != ai_8 / 10) continue;
         li_32 = FALSE;
         if (OrderMagicNumber() % 10 > 0) l_datetime_28 = OrderOpenTime();
         if (MathRound(MathAbs(OrderOpenPrice() - ad_12) / MarketInfo(a_symbol_0, MODE_POINT)) >= ai_20) continue;
         return (FALSE);
      }
      Print("OrderSelect() error - ", ErrorDescription(GetLastError()));
   }
   if (li_32) return (TRUE);
   if (ai_24 < 0) return (TRUE);
   int l_count_40 = 0;
   for (l_pos_36 = OrdersHistoryTotal() - 1; l_pos_36 >= 0; l_pos_36--) {
      if (OrderSelect(l_pos_36, SELECT_BY_POS, MODE_HISTORY)) {
         if (OrderSymbol() != a_symbol_0) continue;
         if (OrderMagicNumber() / 10 != ai_8 / 10) continue;
         if (MathRound(MathAbs(OrderOpenPrice() - ad_12) / MarketInfo(a_symbol_0, MODE_POINT)) >= ai_20) continue;
         l_count_40++;
         if (l_count_40 >= ai_24) return (FALSE);
         if (OrderOpenTime() < l_datetime_28) return (l_count_40 < ai_24);
         if (OrderMagicNumber() % 10 <= 0) continue;
         return (l_count_40 < ai_24);
      }
      Print("OrderSelect() error - ", ErrorDescription(GetLastError()));
   }
   return (TRUE);
}

int _order_magic(string a_symbol_0, int ai_8) {
   for (int l_pos_12 = OrdersTotal() - 1; l_pos_12 >= 0; l_pos_12--) {
      if (OrderSelect(l_pos_12, SELECT_BY_POS, MODE_TRADES)) {
         if (OrderSymbol() != a_symbol_0) continue;
         if (OrderMagicNumber() / 10 != ai_8 / 10) continue;
         return (ai_8);
      }
      Print("OrderSelect() error - ", ErrorDescription(GetLastError()));
   }
   return (ai_8 + 1);
}

void _fa(string a_symbol_0, int ai_8, bool ai_12 = FALSE) {
   for (int l_pos_16 = OrdersTotal() - 1; l_pos_16 >= 0; l_pos_16--) {
      if (OrderSelect(l_pos_16, SELECT_BY_POS, MODE_TRADES)) {
         if (OrderSymbol() == a_symbol_0) {
            if (OrderMagicNumber() / 10 == ai_8 / 10 || ai_12) {
               if (OrderType() <= OP_SELL) _OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), Slippage, Gray);
               else _OrderDelete(OrderTicket());
            }
         }
      } else Print("OrderSelect() error - ", ErrorDescription(GetLastError()));
   }
}

void _remove_pendings(string a_symbol_0, int ai_8, bool ai_12 = FALSE) {
   for (int l_pos_16 = OrdersTotal() - 1; l_pos_16 >= 0; l_pos_16--) {
      if (OrderSelect(l_pos_16, SELECT_BY_POS, MODE_TRADES)) {
         if (OrderSymbol() == a_symbol_0) {
            if (OrderMagicNumber() / 10 == ai_8 / 10 || ai_12)
               if (OrderType() > OP_SELL) _OrderDelete(OrderTicket());
         }
      } else Print("OrderSelect() error - ", ErrorDescription(GetLastError()));
   }
}

double _sl(int ai_0, string a_symbol_4, double ad_12, int ai_20, int ai_24 = 0) {
   if (ai_0 == 0 || ai_0 == 4 || ai_0 == 2) {
      if (ai_20 > 0) return (ad_12 - (ai_20 + ai_24) * MarketInfo(a_symbol_4, MODE_POINT));
      return (0.0);
   }
   if (ai_0 == 1 || ai_0 == 5 || ai_0 == 3) {
      if (ai_20 > 0) return (ad_12 + (ai_20 + ai_24) * MarketInfo(a_symbol_4, MODE_POINT));
      return (0.0);
   }
   return (0.0);
}

double _tp(int ai_0, string a_symbol_4, double ad_12, int ai_20, int ai_24 = 0) {
   if (ai_0 == 0 || ai_0 == 4 || ai_0 == 2) {
      if (ai_20 > 0) return (ad_12 + (ai_20 + ai_24) * MarketInfo(a_symbol_4, MODE_POINT));
      return (0.0);
   }
   if (ai_0 == 1 || ai_0 == 5 || ai_0 == 3) {
      if (ai_20 > 0) return (ad_12 - (ai_20 + ai_24) * MarketInfo(a_symbol_4, MODE_POINT));
      return (0.0);
   }
   return (0.0);
}

double _nv(string a_symbol_0, double ad_8, bool ai_16 = FALSE) {
   double l_lotstep_20 = MarketInfo(a_symbol_0, MODE_LOTSTEP);
   double l_minlot_28 = MarketInfo(a_symbol_0, MODE_MINLOT);
   double l_maxlot_36 = MarketInfo(a_symbol_0, MODE_MAXLOT);
   if (l_lotstep_20 > 0.0) {
      if (l_maxlot_36 > 0.0) {
         if (ai_16) return (MathMin(MathRound(ad_8 / l_lotstep_20) * l_lotstep_20, l_maxlot_36));
         if (l_minlot_28 > 0.0) return (MathMax(MathMin(MathRound(ad_8 / l_lotstep_20) * l_lotstep_20, l_maxlot_36), l_minlot_28));
      }
   }
   return (ad_8);
}

int _Gridzilla_OrderSend(string a_symbol_0, int a_cmd_8, double a_lots_12, double a_price_20, int a_slippage_28, double a_price_32, double a_price_40, string a_comment_48, int a_magic_56, int a_datetime_60, color a_color_64) {
   int l_stoplevel_68;
   double l_point_72;
   int li_80;
   int li_84;
   int li_88;
   int li_92;
   int li_96;
   int l_error_112;
   if (SKIP_LEVEL_ERRORS == 1) {
      l_stoplevel_68 = MarketInfo(a_symbol_0, MODE_STOPLEVEL);
      l_point_72 = MarketInfo(a_symbol_0, MODE_POINT);
      li_80 = MathRound(a_price_20 / l_point_72);
      li_84 = MathRound(MarketInfo(a_symbol_0, MODE_BID) / l_point_72);
      li_88 = MathRound(MarketInfo(a_symbol_0, MODE_ASK) / l_point_72);
      li_92 = MathRound(a_price_40 / l_point_72);
      li_96 = MathRound(a_price_32 / l_point_72);
      if (a_cmd_8 == OP_SELLLIMIT) {
         if (li_80 - li_84 < l_stoplevel_68) return (-1);
         if (li_80 - li_92 < l_stoplevel_68) return (-1);
         if (li_96 > 0)
            if (li_96 - li_80 < l_stoplevel_68) return (-1);
      }
      if (a_cmd_8 == OP_BUYLIMIT) {
         if (li_88 - li_80 < l_stoplevel_68) return (-1);
         if (li_92 > 0)
            if (li_92 - li_80 < l_stoplevel_68) return (-1);
         if (li_80 - li_96 < l_stoplevel_68) return (-1);
      }
      if (a_cmd_8 == OP_SELLSTOP) {
         if (li_84 - li_80 < l_stoplevel_68) return (-1);
         if (li_80 - li_92 < l_stoplevel_68) return (-1);
         if (li_96 > 0)
            if (li_96 - li_80 < l_stoplevel_68) return (-1);
      }
      if (a_cmd_8 == OP_BUYSTOP) {
         if (li_80 - li_88 < l_stoplevel_68) return (-1);
         if (li_92 > 0)
            if (li_92 - li_80 < l_stoplevel_68) return (-1);
         if (li_80 - li_96 < l_stoplevel_68) return (-1);
      }
   }
   string ls_100 = _strcmd(a_cmd_8, a_symbol_0, a_price_20, a_price_32, a_price_40);
   int l_ticket_108 = OrderSend(a_symbol_0, a_cmd_8, a_lots_12, a_price_20, a_slippage_28, a_price_32, a_price_40, a_comment_48, a_magic_56, a_datetime_60, a_color_64);
   if (l_ticket_108 < 0) {
      l_error_112 = GetLastError();
      if (l_error_112 == 4109/* TRADE_NOT_ALLOWED */ || l_error_112 == 4110/* LONGS__NOT_ALLOWED */ || l_error_112 == 4111/* SHORTS_NOT_ALLOWED */) return (-1);
      Print("ERROR OrderSend #", l_error_112, ": ", ErrorDescription(l_error_112), ls_100);
      if (DoScreenShots) {
         Sleep(1000);
         _MakeScreenShot(a_symbol_0, "OrderSend ERROR" + ErrorDescription(l_error_112) + ls_100);
      }
   } else {
      if (DoScreenShots) {
         Sleep(1000);
         _MakeScreenShot(a_symbol_0, "OrderSend" + ls_100);
      }
   }
   return (l_ticket_108);
}

string _strcmd(int ai_0, string a_symbol_4, double ad_12, double ad_20, double ad_28) {
   int li_36 = 9;
   int l_digits_40 = MarketInfo(a_symbol_4, MODE_DIGITS);
   string ls_ret_44 = "";
   switch (ai_0) {
   case 0:
      li_36 = 10;
      ls_ret_44 = "; BUY @" + DoubleToStr(ad_12, l_digits_40) + " SL" + DoubleToStr(ad_20, l_digits_40) + " TP" + DoubleToStr(ad_28, l_digits_40);
      break;
   case 1:
      li_36 = 9;
      ls_ret_44 = "; BUY @" + DoubleToStr(ad_12, l_digits_40) + " SL" + DoubleToStr(ad_20, l_digits_40) + " TP" + DoubleToStr(ad_28, l_digits_40);
      break;
   case 4:
      li_36 = 10;
      ls_ret_44 = "; BUYSTOP @" + DoubleToStr(ad_12, l_digits_40) + " SL" + DoubleToStr(ad_20, l_digits_40) + " TP" + DoubleToStr(ad_28, l_digits_40);
      break;
   case 2:
      li_36 = 10;
      ls_ret_44 = "; BUYLIMIT @" + DoubleToStr(ad_12, l_digits_40) + " SL" + DoubleToStr(ad_20, l_digits_40) + " TP" + DoubleToStr(ad_28, l_digits_40);
      break;
   case 5:
      li_36 = 9;
      ls_ret_44 = "; SELLSTOP @" + DoubleToStr(ad_12, l_digits_40) + " SL" + DoubleToStr(ad_20, l_digits_40) + " TP" + DoubleToStr(ad_28, l_digits_40);
      break;
   case 3:
      li_36 = 9;
      ls_ret_44 = "; SELLLIMIT @" + DoubleToStr(ad_12, l_digits_40) + " SL" + DoubleToStr(ad_20, l_digits_40) + " TP" + DoubleToStr(ad_28, l_digits_40);
      break;
   default:
      ls_ret_44 = "; UNKNOWN command " + ai_0;
   }
   ls_ret_44 = ls_ret_44 + "; market @" + DoubleToStr(MarketInfo(a_symbol_4, li_36), l_digits_40) + " Stop Level: " + DoubleToStr(MarketInfo(a_symbol_4, MODE_STOPLEVEL), 0);
   return (ls_ret_44);
}

int _OrderClose(int a_ticket_0, double a_lots_4, double a_price_12, int a_slippage_20, color a_color_24 = -1) {
   int l_error_52;
   OrderSelect(a_ticket_0, SELECT_BY_TICKET);
   string l_symbol_28 = OrderSymbol();
   int l_digits_36 = MarketInfo(l_symbol_28, MODE_DIGITS);
   string ls_40 = "; @" + DoubleToStr(a_price_12, l_digits_36);
   bool l_ord_close_48 = OrderClose(a_ticket_0, a_lots_4, a_price_12, a_slippage_20, a_color_24);
   if (!l_ord_close_48) {
      l_error_52 = GetLastError();
      Print("ERROR OrderClose #", l_error_52, ": ", ErrorDescription(l_error_52), ls_40);
      if (DoScreenShots) {
         Sleep(1000);
         _MakeScreenShot(l_symbol_28, "OrderClose ERROR" + ErrorDescription(l_error_52) + ls_40);
      }
   } else {
      if (DoScreenShots) {
         Sleep(1000);
         _MakeScreenShot(l_symbol_28, "OrderClose" + ls_40);
      }
   }
   return (l_ord_close_48);
}

int _OrderDelete(int a_ticket_0) {
   int l_error_16;
   OrderSelect(a_ticket_0, SELECT_BY_TICKET);
   string l_symbol_4 = OrderSymbol();
   bool l_ord_delete_12 = OrderDelete(a_ticket_0);
   if (!l_ord_delete_12) {
      l_error_16 = GetLastError();
      Print("ERROR OrderDelete #", l_error_16, ": ", ErrorDescription(l_error_16));
      if (DoScreenShots) {
         Sleep(1000);
         _MakeScreenShot(l_symbol_4, "OrderDelete ERROR" + ErrorDescription(l_error_16));
      }
   } else {
      if (DoScreenShots) {
         Sleep(1000);
         _MakeScreenShot(l_symbol_4, "OrderDelete");
      }
   }
   return (l_ord_delete_12);
}

double _profit(string a_symbol_0, int ai_8) {
   double ld_ret_12 = 0.0;
   bool li_20 = TRUE;
   int l_datetime_24 = 0;
   for (int l_pos_28 = OrdersTotal() - 1; l_pos_28 >= 0; l_pos_28--) {
      if (OrderSelect(l_pos_28, SELECT_BY_POS, MODE_TRADES)) {
         if (OrderSymbol() == a_symbol_0) {
            if (OrderMagicNumber() / 10 == ai_8 / 10) {
               ld_ret_12 += OrderProfit() + OrderSwap();
               li_20 = FALSE;
               if (OrderMagicNumber() % 10 > 0) l_datetime_24 = OrderOpenTime();
            }
         }
      } else Print("OrderSelect_profit_t() error - ", ErrorDescription(GetLastError()));
   }
   if (li_20) return (ld_ret_12);
   for (l_pos_28 = OrdersHistoryTotal() - 1; l_pos_28 >= 0; l_pos_28--) {
      if (OrderSelect(l_pos_28, SELECT_BY_POS, MODE_HISTORY)) {
         if (OrderSymbol() != a_symbol_0) continue;
         if (OrderMagicNumber() / 10 != ai_8 / 10) continue;
         if (OrderOpenTime() < l_datetime_24) return (ld_ret_12);
         ld_ret_12 += OrderProfit() + OrderSwap();
         if (OrderMagicNumber() % 10 <= 0) continue;
         return (ld_ret_12);
      }
      Print("OrderSelect_profit_h() error - ", ErrorDescription(GetLastError()));
   }
   return (ld_ret_12);
}

void _MakeScreenShot(string as_0, string as_8 = "") {
   string ls_16;
   if (IsTesting() == 0) {
      gi_208++;
      ls_16 = WindowExpertName() + as_0 + "\\" + Year() + "-" + _al0(Month(), 2) + "-" + _al0(Day(), 2) + " " + _al0(Hour(), 2) + "_" + _al0(Minute(), 2) + "_" + _al0(Seconds(), 2) + " " + gi_208 + as_8 + ".gif";
      if (!WindowScreenShot(ls_16, 640, 480)) Print("ScreenShot error: ", ErrorDescription(GetLastError()));
   }
}

string _al0(int ai_0, int ai_4) {
   for (string l_dbl2str_8 = DoubleToStr(ai_0, 0); StringLen(l_dbl2str_8) < ai_4; l_dbl2str_8 = "0" + l_dbl2str_8) {
   }
   return (l_dbl2str_8);
}

string _secondstodays(int ai_0) {
   int li_ret_16 = MathMod(ai_0, 60);
   ai_0 /= 60;
   int li_12 = MathMod(ai_0, 60);
   ai_0 /= 60;
   int li_8 = MathMod(ai_0, 24);
   ai_0 /= 24;
   if (ai_0 > 0) return (ai_0 + "d " + li_8 + "h " + li_12 + "m " + li_ret_16);
   if (li_8 > 0) return (li_8 + "h " + li_12 + "m " + li_ret_16);
   if (li_12 > 0) return (li_12 + "m " + li_ret_16);
   return (li_ret_16);
}