/*
   G e n e r a t e d  by ex4-to-mq4 decompiler FREEWARE 4.0.509.5
   Website:  Ht t p:// W wW.meTa q u Ot e s .Ne t
   E-mail : s uP P ORT @M eta Qu oTEs.nE t
*/
#property copyright "Copyright © 2011, www.lifesdream.org"
#property link      "http://www.lifesdream.org"

//#include <stdlib.mqh>
#import "stdlib.ex4"
   string ErrorDescription(int a0); // DA69CBAFF4D38B87377667EEC549DE5A
#import

extern int magic = 18330;
extern string www.lifesdream.org = "Momentum Grid v1.5";
extern string CommonSettings = "---------------------------------------------";
extern int user_slippage = 2;
extern int grid_size = 35;
extern double profit_lock = 0.6;
extern int profit_mode = 1;
extern string MoneyManagementSettings = "---------------------------------------------";
extern double min_lots = 0.1;
extern double min_lots_increment = 0.1;
extern double account_risk = 100.0;
extern string IndicatorSettings = "---------------------------------------------";
extern int mom_period = 20;
extern int mom_level = 100;
extern int shift = 0;
string Gs_168 = "Momentum Grid v1.5";
int Gia_176[50];
int Gia_180[50];
double Gda_184[50];
double Gda_188[50];
double Gda_192[50];
double Gda_196[50];
double Gda_200[50];
double Gda_204[50];
double G_imomentum_208 = 0.0;
double G_imomentum_216 = 0.0;
int Gi_224 = 0;
int Gi_228 = 0;
double Gd_232 = 0.0;
double Gd_240 = 0.0;
double Gd_248 = 0.0;
double Gd_256 = 0.0;
double Gd_264 = 0.0;
double Gd_272 = 0.0;
double Gd_280 = 0.0;
double Gd_288 = 0.0;
int Gi_296 = 0;
int Gi_300 = 10;
double Gd_304 = 4.0;
double Gd_312 = 25.0;
string Gs_320 = "OrderReliable fname unset";
int G_error_328 = 0/* NO_ERROR */;
string Gs_332 = "V1_1_1";

// EA2B2676C28C0DB26D39331A336C6B92
int start() {
   double point_0 = MarketInfo(Symbol(), MODE_POINT);
   double Ld_unused_8 = 0;
   if (MarketInfo(Symbol(), MODE_DIGITS) == 4.0 || MarketInfo(Symbol(), MODE_DIGITS) == 2.0) Gi_296 = user_slippage;
   else
      if (MarketInfo(Symbol(), MODE_DIGITS) == 5.0 || MarketInfo(Symbol(), MODE_DIGITS) == 3.0) Gi_296 = 10 * user_slippage;
   if (IsTradeAllowed() == FALSE) {
      Comment("Copyright © 2011, www.lifesdream.org\nTrade not allowed.");
      return;
   }
   f0_10();
   f0_17();
   f0_11();
   f0_16();
   f0_21();
   f0_7();
   return (0);
}

// 72AB758D11C2D92CDF036A18DDE842B5
void f0_10() {
   Gi_224 = 0;
   Gi_228 = 0;
   for (int index_0 = 0; index_0 < 50; index_0++) {
      Gia_176[index_0] = 0;
      Gda_184[index_0] = 0;
      Gda_192[index_0] = 0;
      Gda_200[index_0] = 0;
      Gia_180[index_0] = 0;
      Gda_188[index_0] = 0;
      Gda_196[index_0] = 0;
      Gda_204[index_0] = 0;
   }
}

// DBFEE3EB37F90B3D848B0740DD3CE5E0
void f0_18() {
   Gd_264 = 0;
   Gd_272 = 0;
   ObjectDelete("line_buy");
   ObjectDelete("line_buy_ts");
}

// 23A8A411C8CC96B481AA426CA9EF7C65
void f0_2() {
   Gd_280 = 0;
   Gd_288 = 0;
   ObjectDelete("line_sell");
   ObjectDelete("line_sell_ts");
}

// C95E7A31339E23521630B0A2F23DD826
void f0_17() {
   int index_0 = 0;
   int index_4 = 0;
   double Ld_8 = 0;
   double Ld_16 = 0;
   double Ld_24 = 0;
   double Ld_32 = 0;
   for (int pos_40 = 0; pos_40 < OrdersTotal(); pos_40++) {
      if (OrderSelect(pos_40, SELECT_BY_POS, MODE_TRADES) == TRUE) {
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == magic && OrderType() == OP_BUY) {
            Gia_176[index_0] = OrderTicket();
            Gda_184[index_0] = OrderLots();
            Gda_192[index_0] = OrderProfit() + OrderCommission() + OrderSwap();
            Gda_200[index_0] = OrderOpenPrice();
            Ld_8 += Gda_192[index_0];
            Ld_24 += OrderLots();
            index_0++;
         }
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == magic && OrderType() == OP_SELL) {
            Gia_180[index_4] = OrderTicket();
            Gda_188[index_4] = OrderLots();
            Gda_196[index_4] = OrderProfit() + OrderCommission() + OrderSwap();
            Gda_204[index_4] = OrderOpenPrice();
            Ld_16 += Gda_196[index_4];
            Ld_32 += OrderLots();
            index_4++;
         }
      }
   }
   Gi_224 = index_0;
   Gi_228 = index_4;
   Gd_232 = Ld_8;
   Gd_240 = Ld_16;
   Gd_248 = Ld_24;
   Gd_256 = Ld_32;
}

// 85A868D4042C3C6F081B66174F7835FA
void f0_11() {
   int Li_0;
   double Ld_4;
   double Ld_12;
   double Ld_20;
   for (int index_28 = 0; index_28 < Gi_224 - 1; index_28++) {
      for (int Li_32 = index_28 + 1; Li_32 < Gi_224; Li_32++) {
         if (Gda_184[index_28] > 0.0 && Gda_184[Li_32] > 0.0) {
            if (Gda_184[Li_32] < Gda_184[index_28]) {
               Ld_4 = Gda_184[index_28];
               Gda_184[index_28] = Gda_184[Li_32];
               Gda_184[Li_32] = Ld_4;
               Li_0 = Gia_176[index_28];
               Gia_176[index_28] = Gia_176[Li_32];
               Gia_176[Li_32] = Li_0;
               Ld_12 = Gda_192[index_28];
               Gda_192[index_28] = Gda_192[Li_32];
               Gda_192[Li_32] = Ld_12;
               Ld_20 = Gda_200[index_28];
               Gda_200[index_28] = Gda_200[Li_32];
               Gda_200[Li_32] = Ld_20;
            }
         }
      }
   }
   for (index_28 = 0; index_28 < Gi_228 - 1; index_28++) {
      for (Li_32 = index_28 + 1; Li_32 < Gi_228; Li_32++) {
         if (Gda_188[index_28] > 0.0 && Gda_188[Li_32] > 0.0) {
            if (Gda_188[Li_32] < Gda_188[index_28]) {
               Ld_4 = Gda_188[index_28];
               Gda_188[index_28] = Gda_188[Li_32];
               Gda_188[Li_32] = Ld_4;
               Li_0 = Gia_180[index_28];
               Gia_180[index_28] = Gia_180[Li_32];
               Gia_180[Li_32] = Li_0;
               Ld_12 = Gda_196[index_28];
               Gda_196[index_28] = Gda_196[Li_32];
               Gda_196[Li_32] = Ld_12;
               Ld_20 = Gda_204[index_28];
               Gda_204[index_28] = Gda_204[Li_32];
               Gda_204[Li_32] = Ld_20;
            }
         }
      }
   }
}

// F126818C511C5C61572C35AC4CAEA0A2
void f0_21() {
   double Lda_84[50];
   double Lda_88[50];
   double Ld_0 = 0;
   double Ld_8 = 0;
   int Li_unused_16 = 1;
   double Ld_20 = 0;
   double Ld_28 = 0;
   double Ld_36 = 0;
   double Ld_44 = 0;
   double Ld_52 = 0;
   double Ld_60 = 0;
   double Ld_68 = 0;
   double Ld_76 = 0;
   double Ld_92 = MarketInfo(Symbol(), MODE_POINT);
   if (Gi_296 > user_slippage) Ld_92 = 10.0 * Ld_92;
   if (Gi_224 >= 1) {
      if (profit_mode == 1) Ld_0 = f0_9(Gda_184[0]);
      if (profit_mode == 2) Ld_0 = f0_9(Gd_248) / Gi_224;
   }
   if (Gi_228 >= 1) {
      if (profit_mode == 1) Ld_8 = f0_9(Gda_188[0]);
      if (profit_mode == 2) Ld_8 = f0_9(Gd_256) / Gi_228;
   }
   if (Gi_224 >= 1) {
      Ld_68 = f0_14(Gda_184[0]);
      for (int index_100 = 0; index_100 < 50; index_100++) Lda_84[index_100] = 0;
      for (index_100 = 0; index_100 < Gi_224; index_100++) Lda_84[index_100] = MathRound(Gda_184[index_100] / Gda_184[0]);
      for (index_100 = 0; index_100 < Gi_224; index_100++) {
         Ld_36 += Lda_84[index_100];
         Ld_52 += Gda_200[index_100] * Lda_84[index_100];
      }
      Ld_20 = Ld_0 / (Ld_68 / Ld_92);
      Ld_20 += Ld_52;
      Ld_20 /= Ld_36;
      f0_20(Ld_20, "line_buy", DodgerBlue, STYLE_SOLID, 2);
      if (Gd_272 > 0.0) {
         Ld_20 = Gd_272 / (Ld_68 / Ld_92);
         Ld_20 += Ld_52;
         Ld_20 /= Ld_36;
         f0_20(Ld_20, "line_buy_ts", DodgerBlue, STYLE_DASH, 1);
      }
   }
   if (Gi_228 >= 1) {
      Ld_76 = f0_14(Gda_188[0]);
      for (index_100 = 0; index_100 < 50; index_100++) Lda_88[index_100] = 0;
      for (index_100 = 0; index_100 < Gi_228; index_100++) Lda_88[index_100] = MathRound(Gda_188[index_100] / Gda_188[0]);
      for (index_100 = 0; index_100 < Gi_228; index_100++) {
         Ld_44 += Lda_88[index_100];
         Ld_60 += Gda_204[index_100] * Lda_88[index_100];
      }
      Ld_28 = -1.0 * (Ld_8 / (Ld_76 / Ld_92));
      Ld_28 += Ld_60;
      Ld_28 /= Ld_44;
      f0_20(Ld_28, "line_sell", Tomato, STYLE_SOLID, 2);
      if (Gd_288 > 0.0) {
         Ld_28 = -1.0 * (Gd_288 / (Ld_76 / Ld_92));
         Ld_28 += Ld_60;
         Ld_28 /= Ld_44;
         f0_20(Ld_28, "line_sell_ts", Tomato, STYLE_DASH, 1);
      }
   }
}

// BD787A2F340C4AC21BA083035209F751
void f0_16() {
   double Ld_8 = 0;
   double Ld_16 = 0;
   if (Gi_224 >= 1) {
      if (profit_mode == 1) Ld_8 = f0_9(Gda_184[0]);
      if (profit_mode == 2) Ld_8 = f0_9(Gd_248) / Gi_224;
   }
   if (Gi_228 >= 1) {
      if (profit_mode == 1) Ld_16 = f0_9(Gda_188[0]);
      if (profit_mode == 2) Ld_16 = f0_9(Gd_256) / Gi_228;
   }
   string Ls_0 = "\nCopyright © 2011, www.lifesdream.org" 
      + "\nMomentum Grid v1.5 is running." 
      + "\n" 
      + "\nSETTINGS: " 
      + "\nGrid size: " + grid_size 
      + "\nProfit locked: " + DoubleToStr(100.0 * profit_lock, 2) + "%" 
      + "\nMinimum lots: " + DoubleToStr(min_lots, 2) 
      + "\nAccount risk: " + DoubleToStr(account_risk, 0) + "%" 
      + "\nMomentum period: " + DoubleToStr(mom_period, 0) 
      + "\nMomentum level: " + DoubleToStr(mom_level, 0) 
      + "\nMomentum shift: " + DoubleToStr(shift, 0) 
      + "\n" 
      + "\nBUY ORDERS" 
      + "\nNumber of orders: " + Gi_224 
      + "\nTotal lots: " + DoubleToStr(Gd_248, 2) 
      + "\nCurrent profit: " + DoubleToStr(Gd_232, 2) 
      + "\nProfit goal: $" + DoubleToStr(Ld_8, 2) 
      + "\nMaximum profit reached: $" + DoubleToStr(Gd_264, 2) 
      + "\nProfit locked: $" + DoubleToStr(Gd_272, 2) 
      + "\n" 
      + "\nSELL ORDERS" 
      + "\nNumber of orders: " + Gi_228 
      + "\nTotal lots: " + DoubleToStr(Gd_256, 2) 
      + "\nCurrent profit: " + DoubleToStr(Gd_240, 2) 
      + "\nProfit goal: $" + DoubleToStr(Ld_16, 2) 
      + "\nMaximum profit reached: $" + DoubleToStr(Gd_280, 2) 
   + "\nProfit locked: $" + DoubleToStr(Gd_288, 2);
   Comment(Ls_0);
}

// ED4F1D667416C1A8B3393107C004A3DC
void f0_20(double A_price_0, string A_name_8, color A_color_16, int A_style_20, int A_width_24) {
   if (ObjectFind(A_name_8) == -1) {
      ObjectCreate(A_name_8, OBJ_HLINE, 0, Time[0], A_price_0);
      ObjectSet(A_name_8, OBJPROP_STYLE, A_style_20);
      ObjectSet(A_name_8, OBJPROP_COLOR, A_color_16);
      ObjectSet(A_name_8, OBJPROP_WIDTH, A_width_24);
      return;
   }
   ObjectSet(A_name_8, OBJPROP_PRICE1, A_price_0);
   ObjectSet(A_name_8, OBJPROP_STYLE, A_style_20);
   ObjectSet(A_name_8, OBJPROP_COLOR, A_color_16);
   ObjectSet(A_name_8, OBJPROP_WIDTH, A_width_24);
}

// 8E8C42B33CBEDA01A1F9BE92C9C8A47F
double f0_13() {
   double Ld_ret_0 = min_lots;
   if (Ld_ret_0 > MarketInfo(Symbol(), MODE_MAXLOT)) Ld_ret_0 = MarketInfo(Symbol(), MODE_MAXLOT);
   if (Ld_ret_0 < MarketInfo(Symbol(), MODE_MINLOT)) Ld_ret_0 = MarketInfo(Symbol(), MODE_MINLOT);
   return (Ld_ret_0);
}

// B4F212E511D2E70332D9FCA974C4DA8C
double f0_15(double Ad_0) {
   int count_16;
   if (min_lots_increment >= 1.0) count_16 = 0;
   else {
      count_16 = 0;
      for (double Ld_8 = Ad_0; Ld_8 < 1.0; Ld_8 = 10.0 * Ld_8) count_16++;
   }
   return (count_16);
}

// E8AA14254F7633D8F03F032FC36FBC23
double f0_19(double Ad_0) {
   double Ld_16 = f0_9(min_lots);
   double Ld_24 = MathFloor(MathAbs(Ad_0 / Ld_16));
   double Ld_ret_8 = NormalizeDouble(Ld_24 * min_lots_increment, f0_15(min_lots_increment));
   if (Ld_ret_8 < min_lots) Ld_ret_8 = min_lots;
   if (Ld_ret_8 > MarketInfo(Symbol(), MODE_MAXLOT)) Ld_ret_8 = MarketInfo(Symbol(), MODE_MAXLOT);
   if (Ld_ret_8 < MarketInfo(Symbol(), MODE_MINLOT)) Ld_ret_8 = MarketInfo(Symbol(), MODE_MINLOT);
   return (Ld_ret_8);
}

// A8C4252F1711418AF22CC90416295B51
double f0_14(double Ad_0) {
   double Ld_36;
   double Ld_ret_8 = 0;
   double Ld_16 = MarketInfo(Symbol(), MODE_TICKVALUE);
   double ticksize_24 = MarketInfo(Symbol(), MODE_TICKSIZE);
   int digits_32 = MarketInfo(Symbol(), MODE_DIGITS);
   if (Ad_0 != 0.0) {
      Ld_36 = 1 / Ad_0;
      if (digits_32 == 5 || digits_32 == 3) Ld_ret_8 = 10.0 * Ld_16;
      else
         if (digits_32 == 4 || digits_32 == 2) Ld_ret_8 = Ld_16;
      Ld_ret_8 /= Ld_36;
   }
   return (Ld_ret_8);
}

// 64DAE4B26B1ADE6B63355255DDF8ADAD
double f0_9(double Ad_0) {
   int Li_ret_8 = grid_size * f0_14(Ad_0);
   return (Li_ret_8);
}

// 299F62AF68464BA1BBCB6FEEFD4DB011
double f0_3(double Ad_0) {
   int Li_ret_8 = (-1 * grid_size) * f0_14(Ad_0);
   return (Li_ret_8);
}

// 5D1ECA158C00250D9C4C32D947B7C433
void f0_7() {
   int Li_0 = -1;
   bool Li_8 = FALSE;
   G_imomentum_208 = iMomentum(Symbol(), 0, mom_period, PRICE_TYPICAL, shift);
   G_imomentum_216 = iMomentum(Symbol(), 0, mom_period, PRICE_TYPICAL, shift + 1);
   if ((100 - account_risk) / 100.0 * AccountBalance() > AccountEquity()) {
      for (int index_4 = 0; index_4 <= Gi_224 - 1; index_4++) Li_8 = f0_1(Gia_176[index_4], Gda_184[index_4], MarketInfo(Symbol(), MODE_BID), Gi_296, Blue);
      for (index_4 = 0; index_4 <= Gi_228 - 1; index_4++) Li_8 = f0_1(Gia_180[index_4], Gda_188[index_4], MarketInfo(Symbol(), MODE_ASK), Gi_296, Red);
      f0_18();
      f0_2();
   }
   if (Gi_224 == 0)
      if (G_imomentum_208 > mom_level && G_imomentum_216 < mom_level) Li_0 = f0_8(Symbol(), OP_BUY, f0_13(), MarketInfo(Symbol(), MODE_ASK), Gi_296, 0, 0, Gs_168, magic, 0, Blue);
   if (Gi_224 >= 1) {
      if (Gd_232 < f0_3(Gd_248))
         if (Gi_224 < 50 && G_imomentum_208 > mom_level && G_imomentum_216 < mom_level) Li_0 = f0_8(Symbol(), OP_BUY, f0_19(Gd_232), MarketInfo(Symbol(), MODE_ASK), Gi_296, 0, 0, Gs_168, magic, 0, Blue);
      if (Gd_264 == 0.0 && profit_mode == 1 && Gd_232 > f0_9(Gda_184[0])) {
         Gd_264 = Gd_232;
         Gd_272 = profit_lock * Gd_264;
      }
      if (Gd_264 == 0.0 && profit_mode == 2 && Gd_232 > f0_9(Gd_248) / Gi_224) {
         Gd_264 = Gd_232;
         Gd_272 = profit_lock * Gd_264;
      }
      if (Gd_264 > 0.0) {
         if (Gd_232 > Gd_264) {
            Gd_264 = Gd_232;
            Gd_272 = profit_lock * Gd_232;
         }
      }
      if (Gd_264 > 0.0 && Gd_272 > 0.0 && Gd_264 > Gd_272 && Gd_232 < Gd_272) {
         for (index_4 = 0; index_4 <= Gi_224 - 1; index_4++) Li_8 = f0_1(Gia_176[index_4], Gda_184[index_4], MarketInfo(Symbol(), MODE_BID), Gi_296, Blue);
         f0_18();
      }
   }
   if (Gi_228 == 0)
      if (G_imomentum_208 < mom_level && G_imomentum_216 > mom_level) Li_0 = f0_8(Symbol(), OP_SELL, f0_13(), MarketInfo(Symbol(), MODE_BID), Gi_296, 0, 0, Gs_168, magic, 0, Red);
   if (Gi_228 >= 1) {
      if (Gd_240 < f0_3(Gd_256))
         if (Gi_228 < 50 && G_imomentum_208 < mom_level && G_imomentum_216 > mom_level) Li_0 = f0_8(Symbol(), OP_SELL, f0_19(Gd_240), MarketInfo(Symbol(), MODE_BID), Gi_296, 0, 0, Gs_168, magic, 0, Red);
      if (Gd_280 == 0.0 && profit_mode == 1 && Gd_240 > f0_9(Gda_188[0])) {
         Gd_280 = Gd_240;
         Gd_288 = profit_lock * Gd_280;
      }
      if (Gd_280 == 0.0 && profit_mode == 2 && Gd_240 > f0_9(Gd_256) / Gi_228) {
         Gd_280 = Gd_240;
         Gd_288 = profit_lock * Gd_280;
      }
      if (Gd_280 > 0.0) {
         if (Gd_240 > Gd_280) {
            Gd_280 = Gd_240;
            Gd_288 = profit_lock * Gd_280;
         }
      }
      if (Gd_280 > 0.0 && Gd_288 > 0.0 && Gd_280 > Gd_288 && Gd_240 < Gd_288) {
         for (index_4 = 0; index_4 <= Gi_228 - 1; index_4++) Li_8 = f0_1(Gia_180[index_4], Gda_188[index_4], MarketInfo(Symbol(), MODE_ASK), Gi_296, Red);
         f0_2();
      }
   }
}

// 63F809ED5E900872F8FA2BC6E67A5CF3
int f0_8(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 = 0, color A_color_64 = -1) {
   double Ld_96;
   Gs_320 = "OrderSendReliable";
   f0_4(" attempted " + f0_6(A_cmd_8) + " " + A_lots_12 + " lots @" + A_price_20 + " sl:" + A_price_32 + " tp:" + A_price_40);
   if (IsStopped()) {
      f0_4("error: IsStopped() == true");
      G_error_328 = 2;
      return (-1);
   }
   for (int count_68 = 0; !IsTradeAllowed() && count_68 < Gi_300; count_68++) f0_0(Gd_304, Gd_312);
   if (!IsTradeAllowed()) {
      f0_4("error: no operation possible because IsTradeAllowed()==false, even after retries.");
      G_error_328 = 146;
      return (-1);
   }
   int digits_72 = MarketInfo(A_symbol_0, MODE_DIGITS);
   if (digits_72 > 0) {
      A_price_20 = NormalizeDouble(A_price_20, digits_72);
      A_price_32 = NormalizeDouble(A_price_32, digits_72);
      A_price_40 = NormalizeDouble(A_price_40, digits_72);
   }
   if (A_price_32 != 0.0) f0_12(A_symbol_0, A_price_20, A_price_32);
   int error_76 = GetLastError();
   error_76 = 0;
   G_error_328 = 0;
   bool Li_80 = FALSE;
   bool Li_84 = FALSE;
   int ticket_88 = -1;
   if (A_cmd_8 == OP_BUYSTOP || A_cmd_8 == OP_SELLSTOP || A_cmd_8 == OP_BUYLIMIT || A_cmd_8 == OP_SELLLIMIT) {
      count_68 = 0;
      while (!Li_80) {
         if (IsTradeAllowed()) {
            ticket_88 = 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);
            error_76 = GetLastError();
            G_error_328 = error_76;
         } else count_68++;
         switch (error_76) {
         case 0/* NO_ERROR */:
            Li_80 = TRUE;
            break;
         case 4/* SERVER_BUSY */:
         case 6/* NO_CONNECTION */:
         case 129/* INVALID_PRICE */:
         case 136/* OFF_QUOTES */:
         case 137/* BROKER_BUSY */:
         case 146/* TRADE_CONTEXT_BUSY */:
            count_68++;
            break;
         case 135/* PRICE_CHANGED */:
         case 138/* REQUOTE */:
            RefreshRates();
            continue;
            break;
         case 130/* INVALID_STOPS */:
            Ld_96 = MarketInfo(A_symbol_0, MODE_STOPLEVEL) * MarketInfo(A_symbol_0, MODE_POINT);
            if (A_cmd_8 == OP_BUYSTOP) {
               if (MathAbs(MarketInfo(A_symbol_0, MODE_ASK) - A_price_20) <= Ld_96) Li_84 = TRUE;
            } else {
               if (A_cmd_8 == OP_SELLSTOP)
                  if (MathAbs(MarketInfo(A_symbol_0, MODE_BID) - A_price_20) <= Ld_96) Li_84 = TRUE;
            }
            Li_80 = TRUE;
            break;
         default:
            Li_80 = TRUE;
            if (count_68 > Gi_300) Li_80 = TRUE;
            if (Li_80) {
               if (error_76 != 0/* NO_ERROR */) f0_4("non-retryable error: " + f0_5(error_76));
               if (count_68 > Gi_300) f0_4("retry attempts maxed at " + Gi_300);
            }
            if (!(!Li_80)) continue;
            f0_4("retryable error (" + count_68 + "/" + Gi_300 + "): " + f0_5(error_76));
            f0_0(Gd_304, Gd_312);
            RefreshRates();
         }
      }
      if (error_76 == 0/* NO_ERROR */) {
         f0_4("apparently successful OP_BUYSTOP or OP_SELLSTOP order placed, details follow.");
         OrderSelect(ticket_88, SELECT_BY_TICKET, MODE_TRADES);
         OrderPrint();
         return (ticket_88);
      }
      if (!Li_84) {
         f0_4("failed to execute stop or limit order after " + count_68 + " retries");
         f0_4("failed trade: " + f0_6(A_cmd_8) + " " + A_symbol_0 + "@" + A_price_20 + " tp@" + A_price_40 + " sl@" + A_price_32);
         f0_4("last error: " + f0_5(error_76));
         return (-1);
      }
   }
   if (Li_84) {
      f0_4("going from limit order to market order because market is too close.");
      if (A_cmd_8 == OP_BUYSTOP || A_cmd_8 == OP_BUYLIMIT) {
         A_cmd_8 = 0;
         A_price_20 = MarketInfo(A_symbol_0, MODE_ASK);
      } else {
         if (A_cmd_8 == OP_SELLSTOP || A_cmd_8 == OP_SELLLIMIT) {
            A_cmd_8 = 1;
            A_price_20 = MarketInfo(A_symbol_0, MODE_BID);
         }
      }
   }
   error_76 = GetLastError();
   error_76 = 0;
   G_error_328 = 0;
   ticket_88 = -1;
   if (A_cmd_8 == OP_BUY || A_cmd_8 == OP_SELL) {
      count_68 = 0;
      while (!Li_80) {
         if (IsTradeAllowed()) {
            ticket_88 = 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);
            error_76 = GetLastError();
            G_error_328 = error_76;
         } else count_68++;
         switch (error_76) {
         case 0/* NO_ERROR */:
            Li_80 = TRUE;
            break;
         case 4/* SERVER_BUSY */:
         case 6/* NO_CONNECTION */:
         case 129/* INVALID_PRICE */:
         case 136/* OFF_QUOTES */:
         case 137/* BROKER_BUSY */:
         case 146/* TRADE_CONTEXT_BUSY */:
            count_68++;
            break;
         case 135/* PRICE_CHANGED */:
         case 138/* REQUOTE */:
            RefreshRates();
            continue;
            break;
         default:
            Li_80 = TRUE;
            if (count_68 > Gi_300) Li_80 = TRUE;
            if (!Li_80) {
               f0_4("retryable error (" + count_68 + "/" + Gi_300 + "): " + f0_5(error_76));
               f0_0(Gd_304, Gd_312);
               RefreshRates();
            }
            if (!(Li_80)) continue;
            if (error_76 != 0/* NO_ERROR */) f0_4("non-retryable error: " + f0_5(error_76));
            if (count_68 <= Gi_300) continue;
            f0_4("retry attempts maxed at " + Gi_300);
         }
      }
      if (error_76 == 0/* NO_ERROR */) {
         f0_4("apparently successful OP_BUY or OP_SELL order placed, details follow.");
         OrderSelect(ticket_88, SELECT_BY_TICKET, MODE_TRADES);
         OrderPrint();
         return (ticket_88);
      }
      f0_4("failed to execute OP_BUY/OP_SELL, after " + count_68 + " retries");
      f0_4("failed trade: " + f0_6(A_cmd_8) + " " + A_symbol_0 + "@" + A_price_20 + " tp@" + A_price_40 + " sl@" + A_price_32);
      f0_4("last error: " + f0_5(error_76));
      return (-1);
   }
   return (0);
}

// 207CA77B652B96112F2962B17C7FC706
int f0_1(int A_ticket_0, double A_lots_4, double A_price_12, int A_slippage_20, color A_color_24 = -1) {
   Gs_320 = "OrderCloseReliable";
   f0_4(" attempted close of #" + A_ticket_0 + " price:" + A_price_12 + " lots:" + A_lots_4 + " slippage:" + A_slippage_20);
   if (!OrderSelect(A_ticket_0, SELECT_BY_TICKET)) {
      G_error_328 = GetLastError();
      f0_4("error: " + ErrorDescription(G_error_328));
      return (0);
   }
   int cmd_28 = OrderType();
   string symbol_32 = OrderSymbol();
   if (cmd_28 != OP_BUY && cmd_28 != OP_SELL) {
      G_error_328 = 4108;
      f0_4("error: trying to close ticket #" + A_ticket_0 + ", which is " + f0_6(cmd_28) + ", not OP_BUY or OP_SELL");
      return (0);
   }
   if (IsStopped()) {
      f0_4("error: IsStopped() == true");
      return (0);
   }
   int count_40 = 0;
   int error_44 = GetLastError();
   error_44 = 0;
   G_error_328 = 0;
   bool Li_48 = FALSE;
   count_40 = 0;
   bool is_closed_52 = FALSE;
   while (!Li_48) {
      if (IsTradeAllowed()) {
         is_closed_52 = OrderClose(A_ticket_0, A_lots_4, A_price_12, A_slippage_20, A_color_24);
         error_44 = GetLastError();
         G_error_328 = error_44;
      } else count_40++;
      if (is_closed_52 == 1) Li_48 = TRUE;
      switch (error_44) {
      case 135/* PRICE_CHANGED */: continue;
      case 138/* REQUOTE */: continue;
      case 0/* NO_ERROR */:
         Li_48 = TRUE;
         break;
      case 4/* SERVER_BUSY */:
      case 6/* NO_CONNECTION */:
      case 129/* INVALID_PRICE */:
      case 136/* OFF_QUOTES */:
      case 137/* BROKER_BUSY */:
      case 146/* TRADE_CONTEXT_BUSY */:
      case 128/* TRADE_TIMEOUT */:
         count_40++;
         break;
      default:
         Li_48 = TRUE;
      }
      if (count_40 > Gi_300) Li_48 = TRUE;
      if (!Li_48) {
         f0_4("retryable error (" + count_40 + "/" + Gi_300 + "): " + f0_5(error_44));
         f0_0(Gd_304, Gd_312);
         if (cmd_28 == OP_BUY) A_price_12 = NormalizeDouble(MarketInfo(symbol_32, MODE_BID), MarketInfo(symbol_32, MODE_DIGITS));
         if (cmd_28 == OP_SELL) A_price_12 = NormalizeDouble(MarketInfo(symbol_32, MODE_ASK), MarketInfo(symbol_32, MODE_DIGITS));
      }
      if (Li_48) {
         if (error_44 != 0/* NO_ERROR */ && error_44 != 1/* NO_RESULT */) f0_4("non-retryable error: " + f0_5(error_44));
         if (count_40 > Gi_300) f0_4("retry attempts maxed at " + Gi_300);
      }
   }
   if (is_closed_52 == 1 || error_44 == 0/* NO_ERROR */) {
      f0_4("apparently successful close order, updated trade details follow.");
      OrderSelect(A_ticket_0, SELECT_BY_TICKET, MODE_TRADES);
      OrderPrint();
      return (1);
   }
   f0_4("failed to execute close after " + count_40 + " retries");
   f0_4("failed close: Ticket #" + A_ticket_0 + ", Price: " + A_price_12 + ", Slippage: " + A_slippage_20);
   f0_4("last error: " + f0_5(error_44));
   return (0);
}

// 5149AAE98DBC653E2BE4FD3425E2DF9F
string f0_5(int Ai_0) {
   return ("" + Ai_0 + ":" + ErrorDescription(Ai_0));
}

// 31D7DFA8442FDF5E8A38123047829292
void f0_4(string As_0) {
   if (!IsTesting() || IsOptimization()) Print(Gs_320 + " " + Gs_332 + ":" + As_0);
}

// 550DB68CC3F51F6BFA0BAE3A70286DC3
string f0_6(int Ai_0) {
   if (Ai_0 == 0) return ("OP_BUY");
   if (Ai_0 == 1) return ("OP_SELL");
   if (Ai_0 == 4) return ("OP_BUYSTOP");
   if (Ai_0 == 5) return ("OP_SELLSTOP");
   if (Ai_0 == 2) return ("OP_BUYLIMIT");
   if (Ai_0 == 3) return ("OP_SELLLIMIT");
   return ("(CMD==" + Ai_0 + ")");
}

// 865F439BFDE48E26BCAABDADB2D94DE5
void f0_12(string A_symbol_0, double Ad_8, double &Ad_16) {
   double Ld_24;
   if (Ad_16 != 0.0) {
      Ld_24 = MarketInfo(A_symbol_0, MODE_STOPLEVEL) * MarketInfo(A_symbol_0, MODE_POINT);
      if (MathAbs(Ad_8 - Ad_16) <= Ld_24) {
         if (Ad_8 > Ad_16) Ad_16 = Ad_8 - Ld_24;
         else {
            if (Ad_8 < Ad_16) Ad_16 = Ad_8 + Ld_24;
            else f0_4("EnsureValidStop: error, passed in price == sl, cannot adjust");
         }
         Ad_16 = NormalizeDouble(Ad_16, MarketInfo(A_symbol_0, MODE_DIGITS));
      }
   }
}

// 081406D171CBF6523BDAC6124BC069E0
void f0_0(double Ad_0, double Ad_8) {
   double Ld_16;
   int Li_24;
   double Ld_28;
   if (IsTesting() == FALSE) {
      Ld_16 = MathCeil(Ad_0 / 0.1);
      if (Ld_16 > 0.0) {
         Li_24 = MathRound(Ad_8 / 0.1);
         Ld_28 = 1.0 - 1.0 / Ld_16;
         Sleep(100);
         for (int count_36 = 0; count_36 < Li_24; count_36++) {
            if (MathRand() > 32768.0 * Ld_28) break;
            Sleep(100);
         }
      }
   }
}
