/*
   Generated by EX4-TO-MQ4 decompiler ONE FILE V4.0.217.1
   EX4 ID : 4D39A832D47852B5354AE2303BBEDE8C
   Website: http://purebeam.biz
   E-mail : purebeam@gmail.com
*/
#property copyright "Copyright © 2007, GREEZLY"
#property link      ""

extern double TakeProfitB = 12.0;
extern double TakeProfitS = 12.0;
extern double StopLossB = 1080.0;
extern double StopLossS = 1080.0;
extern int TimeStopLoss_Minutes = 0;
extern double TakeProfitForLock = 0.0;
extern double StopLossForLock = 134.0;
extern double TrailingStop = 0.0;
extern double StepTrailingStop = 5.0;
extern double BuyStopLevel = 22.0;
extern double BuyLimitLevel = 17.0;
extern double SellStopLevel = 78.0;
extern double SellLimitLevel = 83.0;
extern int UseOneWayRealOrdersB = 1;
extern int UseOneWayRealOrdersS = 1;
extern double MinDistanceRealOrdersB = 390.0;
extern double MinDistanceRealOrdersS = 590.0;
extern int UseAveragingTakeProfitB = 0;
extern int UseAveragingTakeProfitS = 0;
extern int UseAcceleratorIndicator = 0;
extern int iAC_CountBars = 3;
extern int iAC_CountTimeFrames = 3;
extern int iAC_StartTimeFrame = 0;
extern int UseSpeedIndicator = 1;
extern int iSP_CountBars = 2;
extern int iSP_CountTimeFrames = 2;
extern int iSP_StartTimeFrame = 1;
extern int iSP_UseForOpenRealOrders = 0;
extern double iSP_MinValueForOpenRealOrders = 0.0005;
extern double iSP_UseMinTakeProfit = 1.0;
extern double iSP_StopLoss = 35.0;
extern int CountHighLowLimits = 2;
extern double iHL_LimitDistance1 = 150.0;
extern int iHL_Period1 = 4;
extern int iHL_CountBars1 = 24;
extern double iHL_LimitDistance2 = 700.0;
extern int iHL_Period2 = 6;
extern int iHL_CountBars2 = 15;
extern int UseFlatIndicator = 1;
extern double iFL_MinWidthCanal = 20.0;
extern double iFL_MaxWidthCanal = 40.0;
extern double iFL_MinExtremumsCount = 2.0;
extern int iFL_Period = 1;
extern int iFL_CountBars = 12;
extern double iFL_StopLoss = 25.0;
extern double iFL_LotSizeMultiply = 2.0;
extern int UseBuy = 1;
extern int UseSell = 1;
extern int OpenRealOrders = 1;
extern double Risk = 0.05;
extern double BuyDenyLevel = 161.0;
extern double SellDenyLevel = 159.0;
extern int MaxOrdersCount = 0;
extern double MinDistanceDeferFromPrice = 10.0;
extern double MaxDistanceDeferFromPrice = 50.0;
extern double MinDistanceBetweenDefer = 90.0;
extern double MinLotSize = 0.1;
extern double MaxLotSize = 1000.0;
extern double StepLotSize = 0.1;
extern string EndOfTrading = "yyyy.mm.dd hh:mm";
bool gi_452 = FALSE;
int gi_unused_456 = 0;
int gi_460 = 0;
int gi_464 = 1;
int gi_468 = 2;
int gi_472 = 3;
int gi_476 = 4;
int gi_480 = 5;
int gia_484[10] = {1, 5, 15, 30, 60, 240, 1440, 10080, 43200, 0};
double gd_488;
int gia_496[6];
double gda_500[6];
double gda_504[6];
double gda_508[6];
double gd_512;
double gd_520;
double gda_528[2];
double gda_532[2];
double gd_536;
bool gi_544;
double g_ilow_548;
double g_ihigh_556;
double gd_564;

int glGetNormalOrderType(int ai_0) {
   switch (ai_0) {
   case 0:
      return (gi_460);
   case 1:
      return (gi_464);
   case 2:
      return (gi_468);
   case 4:
      return (gi_476);
   case 3:
      return (gi_472);
   case 5:
      return (gi_480);
   }
   return (-1);
}

int init() {
   if (TakeProfitB != 0.0) TakeProfitB = MathMax(MarketInfo(Symbol(), MODE_STOPLEVEL), TakeProfitB);
   if (TakeProfitS != 0.0) TakeProfitS = MathMax(MarketInfo(Symbol(), MODE_STOPLEVEL), TakeProfitS);
   if (StopLossB != 0.0) StopLossB = MathMax(MarketInfo(Symbol(), MODE_STOPLEVEL), StopLossB);
   if (StopLossS != 0.0) StopLossS = MathMax(MarketInfo(Symbol(), MODE_STOPLEVEL), StopLossS);
   if (iSP_StopLoss != 0.0) iSP_StopLoss = MathMax(MarketInfo(Symbol(), MODE_STOPLEVEL), iSP_StopLoss);
   if (iFL_StopLoss != 0.0) iFL_StopLoss = MathMax(MarketInfo(Symbol(), MODE_STOPLEVEL), iFL_StopLoss);
   if (TakeProfitForLock != 0.0) TakeProfitForLock = MathMax(MarketInfo(Symbol(), MODE_STOPLEVEL), TakeProfitForLock);
   if (StopLossForLock != 0.0) StopLossForLock = MathMax(MarketInfo(Symbol(), MODE_STOPLEVEL), StopLossForLock);
   if (UseAcceleratorIndicator == 1) {
      if (iAC_CountBars < 2) iAC_CountBars = 2;
      if (iAC_CountBars > 10) iAC_CountBars = 10;
      if (iAC_CountTimeFrames < 1) iAC_CountTimeFrames = 1;
      if (iAC_CountTimeFrames > 5) iAC_CountTimeFrames = 5;
   }
   if (UseSpeedIndicator == 1) {
      if (iSP_CountBars < 2) iSP_CountBars = 2;
      if (iSP_CountBars > 10) iSP_CountBars = 10;
      if (iSP_CountTimeFrames < 1) iSP_CountTimeFrames = 1;
      if (iSP_CountTimeFrames > 5) iSP_CountTimeFrames = 5;
   }
   if (MaxOrdersCount < 0) MaxOrdersCount = 0;
   MinLotSize = MathMax(MarketInfo(Symbol(), MODE_MINLOT), MinLotSize);
   MaxLotSize = MathMin(MarketInfo(Symbol(), MODE_MAXLOT), MaxLotSize);
   StepLotSize = MathMax(MarketInfo(Symbol(), MODE_LOTSTEP), StepLotSize);
   return (0);
}

int deinit() {
   return (0);
}

int start() {
   int l_cmd_0;
   int l_magic_4;
   double ld_12;
   double l_lots_20;
   double l_ord_open_price_28;
   double l_ord_stoploss_44;
   double l_price_52;
   double ld_60;
   double ld_68;
   double ld_76;
   double ld_84;
   double ld_92;
   double l_ilow_100;
   double l_ihigh_108;
   int l_ticket_116;
   int li_120;
   int li_124;
   bool li_144;
   bool li_148;
   string ls_152;
   int l_str2time_160;
   double lda_164[10][10];
   int lia_168[10];
   double lda_172[10][10];
   int lia_176[10];
   double ld_180;
   double ld_188;
   int li_196;
   int li_200;
   int l_count_208;
   int l_count_212;
   int li_216;
   gd_488 = 1.0 / MathPow(10, Digits);
   if (EndOfTrading != "yyyy.mm.dd hh:mm" && StringTrimLeft(EndOfTrading) != "") {
      l_str2time_160 = StrToTime(StringTrimLeft(EndOfTrading));
      if (l_str2time_160 > D'01.01.2007 02:00') {
         if (Time[0] > l_str2time_160) {
            if (gi_452) Print("Запрещена торговля после ", EndOfTrading);
            return (0);
         }
      }
   }
   int l_ord_total_140 = OrdersTotal();
   for (int l_pos_128 = 0; l_pos_128 < l_ord_total_140; l_pos_128++) {
      if (!OrderSelect(l_pos_128, SELECT_BY_POS, MODE_TRADES)) return (0);
      if (OrderSymbol() != Symbol()) {
      }
   }
   gd_520 = 0;
   if (UseAcceleratorIndicator == 1) {
      for (l_pos_128 = 0; l_pos_128 < iAC_CountTimeFrames; l_pos_128++) {
         for (int li_132 = 0; li_132 < iAC_CountBars; li_132++) lda_164[l_pos_128][li_132] = iAC(NULL, gia_484[iAC_StartTimeFrame + l_pos_128], li_132);
         if (lda_164[l_pos_128][iAC_CountBars - 2] > lda_164[l_pos_128][iAC_CountBars - 1]) lia_168[l_pos_128] = 1;
         else {
            if (lda_164[l_pos_128][iAC_CountBars - 2] < lda_164[l_pos_128][iAC_CountBars - 1]) lia_168[l_pos_128] = -1;
            else {
               lia_168[l_pos_128] = 0;
               break;
            }
         }
         for (li_132 = iAC_CountBars - 3; li_132 >= 0; li_132--) {
            if (lia_168[l_pos_128] == 1 && lda_164[l_pos_128][li_132] <= lda_164[l_pos_128][li_132 + 1]) {
               lia_168[l_pos_128] = 0;
               break;
            }
            if (lia_168[l_pos_128] == -1 && lda_164[l_pos_128][li_132] >= lda_164[l_pos_128][li_132 + 1]) {
               lia_168[l_pos_128] = 0;
               break;
            }
         }
      }
      gd_520 = lia_168[0];
      for (l_pos_128 = 1; l_pos_128 < iAC_CountTimeFrames; l_pos_128++) {
         if (lia_168[l_pos_128] != gd_520) {
            gd_520 = 0;
            break;
         }
      }
      if (gd_520 != 0.0) gd_520 = lda_164[0][0] - lda_164[0][1];
   }
   gd_512 = 0;
   if (UseSpeedIndicator == 1) {
      for (l_pos_128 = 0; l_pos_128 < iSP_CountTimeFrames; l_pos_128++) {
         for (li_132 = 0; li_132 < iSP_CountBars; li_132++) {
            ld_180 = (iHigh(NULL, gia_484[iSP_StartTimeFrame + l_pos_128], li_132 + 2) + iLow(NULL, gia_484[iSP_StartTimeFrame + l_pos_128], li_132 + 2) + 2.0 * iClose(NULL, gia_484[iSP_StartTimeFrame +
               l_pos_128], li_132 + 2)) / 4.0;
            ld_188 = (iHigh(NULL, gia_484[iSP_StartTimeFrame + l_pos_128], li_132 + 1) + iLow(NULL, gia_484[iSP_StartTimeFrame + l_pos_128], li_132 + 1) + 2.0 * iClose(NULL, gia_484[iSP_StartTimeFrame +
               l_pos_128], li_132 + 1)) / 4.0;
            if (ld_180 > 0.0) lda_172[l_pos_128][li_132] = (ld_188 - ld_180) / ld_180;
            else lda_172[l_pos_128][li_132] = 0;
         }
         if (l_pos_128 == 0 && iSP_StartTimeFrame == 0) {
            if (Bid > iOpen(NULL, gia_484[0], 0)) lia_176[0] = 1;
            else {
               if (Bid < iOpen(NULL, gia_484[0], 0)) lia_176[0] = -1;
               else {
                  if (lda_172[l_pos_128][0] > 0.0) lia_176[l_pos_128] = 1;
                  else {
                     if (lda_172[l_pos_128][0] < 0.0) lia_176[l_pos_128] = -1;
                     else {
                        lia_176[l_pos_128] = 0;
                        break;
                     }
                  }
               }
            }
         } else {
            if (iOpen(NULL, gia_484[iSP_StartTimeFrame + l_pos_128], 0) > iClose(NULL, gia_484[iSP_StartTimeFrame + l_pos_128], 1)) lia_176[l_pos_128] = 1;
            else {
               if (iOpen(NULL, gia_484[iSP_StartTimeFrame + l_pos_128], 0) < iClose(NULL, gia_484[iSP_StartTimeFrame + l_pos_128], 1)) lia_176[l_pos_128] = -1;
               else {
                  if (lda_172[l_pos_128][0] > 0.0) lia_176[l_pos_128] = 1;
                  else {
                     if (lda_172[l_pos_128][0] < 0.0) lia_176[l_pos_128] = -1;
                     else {
                        lia_176[l_pos_128] = 0;
                        break;
                     }
                  }
               }
            }
         }
         if ((lda_172[l_pos_128][iSP_CountBars - 1]) * lia_176[l_pos_128] <= 0.0) {
            lia_176[l_pos_128] = 0;
            break;
         }
         for (li_132 = iSP_CountBars - 2; li_132 >= 0; li_132--) {
            if (lia_176[l_pos_128] == 1 && lda_172[l_pos_128][li_132] <= lda_172[l_pos_128][li_132 + 1]) {
               lia_176[l_pos_128] = 0;
               break;
            }
            if (lia_176[l_pos_128] == -1 && lda_172[l_pos_128][li_132] >= lda_172[l_pos_128][li_132 + 1]) {
               lia_176[l_pos_128] = 0;
               break;
            }
         }
      }
      gd_512 = lia_176[0];
      for (l_pos_128 = 1; l_pos_128 < iSP_CountTimeFrames; l_pos_128++) {
         if (lia_176[l_pos_128] != gd_512) {
            gd_512 = 0;
            break;
         }
      }
      if (gd_512 != 0.0) {
         ld_180 = iOpen(NULL, gia_484[0], 0);
         ld_188 = Bid;
         if (iSP_StartTimeFrame == 0 && ld_180 != ld_188 && ld_180 > 0.0) gd_512 = (ld_188 - ld_180) / ld_180;
         else gd_512 = lda_172[0][0];
      }
   }
   if (CountHighLowLimits > 0) {
      for (int l_index_136 = 0; l_index_136 < CountHighLowLimits; l_index_136++) {
         gda_528[l_index_136] = 0;
         gda_532[l_index_136] = 0;
         switch (l_index_136) {
         case 0:
            li_200 = iHL_Period1;
            li_196 = iHL_CountBars1;
            break;
         case 1:
            li_200 = iHL_Period2;
            li_196 = iHL_CountBars2;
         }
         for (l_pos_128 = 0; l_pos_128 < li_196; l_pos_128++) {
            l_ilow_100 = iLow(NULL, gia_484[li_200], l_pos_128);
            l_ihigh_108 = iHigh(NULL, gia_484[li_200], l_pos_128);
            if (l_ilow_100 > 0.0)
               if (gda_528[l_index_136] == 0.0 || l_ilow_100 < gda_528[l_index_136]) gda_528[l_index_136] = l_ilow_100;
            if (l_ihigh_108 > 0.0)
               if (gda_532[l_index_136] == 0.0 || l_ihigh_108 > gda_532[l_index_136]) gda_532[l_index_136] = l_ihigh_108;
         }
      default:
      }
   }
   if (UseFlatIndicator == 1) {
      gi_544 = FALSE;
      g_ilow_548 = 0;
      g_ihigh_556 = 0;
      gd_564 = 0;
      for (l_pos_128 = 0; l_pos_128 < iFL_CountBars; l_pos_128++) {
         l_ilow_100 = iLow(NULL, gia_484[iFL_Period], l_pos_128);
         l_ihigh_108 = iHigh(NULL, gia_484[iFL_Period], l_pos_128);
         if (l_ilow_100 > 0.0)
            if (g_ilow_548 == 0.0 || l_ilow_100 < g_ilow_548) g_ilow_548 = l_ilow_100;
         if (l_ihigh_108 > 0.0)
            if (g_ihigh_556 == 0.0 || l_ihigh_108 > g_ihigh_556) g_ihigh_556 = l_ihigh_108;
      }
      gd_564 = g_ihigh_556 - g_ilow_548;
      if (g_ihigh_556 > 0.0 && g_ilow_548 > 0.0 && gd_564 >= iFL_MinWidthCanal * gd_488 && gd_564 <= iFL_MaxWidthCanal * gd_488) {
         l_count_208 = 0;
         l_count_212 = 0;
         li_216 = 0;
         for (l_pos_128 = 0; l_pos_128 < iFL_CountBars; l_pos_128++) {
            l_ilow_100 = iLow(NULL, gia_484[iFL_Period], l_pos_128);
            l_ihigh_108 = iHigh(NULL, gia_484[iFL_Period], l_pos_128);
            if (l_ilow_100 > 0.0 && l_ilow_100 < g_ilow_548 + gd_564 / 4.0 && li_216 >= 0) {
               li_216 = -1;
               l_count_208++;
            }
            if (l_ihigh_108 > 0.0 && l_ihigh_108 > g_ihigh_556 - gd_564 / 4.0 && li_216 <= 0) {
               li_216 = 1;
               l_count_212++;
            }
         }
         if (l_count_208 >= iFL_MinExtremumsCount && l_count_212 >= iFL_MinExtremumsCount) gi_544 = TRUE;
      }
   }
   if (!glAnalizCurrentOrdersState()) return (0);
   if (TimeStopLoss_Minutes > 0) {
      l_ord_total_140 = OrdersTotal();
      for (l_pos_128 = l_ord_total_140 - 1; l_pos_128 >= 0; l_pos_128--) {
         OrderSelect(l_pos_128, SELECT_BY_POS, MODE_TRADES);
         if (OrderSymbol() == Symbol()) {
            if (OrderType() == OP_BUY || OrderType() == OP_SELL) {
               if (TimeCurrent() - OrderOpenTime() > 60 * TimeStopLoss_Minutes) {
                  if (gi_452) Print("Удаляем открытый ордер по временному стоп-лоссу.");
                  glOrderClose();
               }
            }
         }
      }
   }
   if (gia_496[gi_460] != 0 && gia_496[gi_468] != 0) glDeleteAllDeferOrders(gi_468, Symbol());
   if (gia_496[gi_464] != 0 && gia_496[gi_472] != 0) glDeleteAllDeferOrders(gi_472, Symbol());
   if (gia_496[gi_460] != 0 && gia_496[gi_476] != 0) glDeleteAllDeferOrders(gi_476, Symbol(), gda_500[gi_460], 0);
   if (gia_496[gi_464] != 0 && gia_496[gi_480] != 0) glDeleteAllDeferOrders(gi_480, Symbol(), 0, gda_504[gi_464]);
   if (gia_496[gi_460] != 0 && gia_496[gi_464] != 0) {
      if ((TakeProfitForLock != 0.0 && TakeProfitForLock != TakeProfitB || TakeProfitForLock != TakeProfitS) || (StopLossForLock != 0.0 && StopLossForLock != StopLossB ||
         StopLossForLock != StopLossS)) {
         ld_60 = (Ask + Bid) / 2.0;
         l_ord_total_140 = OrdersTotal();
         for (l_pos_128 = 0; l_pos_128 < l_ord_total_140; l_pos_128++) {
            if (!OrderSelect(l_pos_128, SELECT_BY_POS, MODE_TRADES)) {
               Print("Ошибка получения текущего ордера OrderSelect(): ", GetLastError());
               return (0);
            }
            if (OrderSymbol() == Symbol()) {
               l_cmd_0 = OrderType();
               l_ord_open_price_28 = OrderOpenPrice();
               l_price_52 = OrderTakeProfit();
               l_ord_stoploss_44 = OrderStopLoss();
               l_magic_4 = OrderMagicNumber();
               if (l_magic_4 != 7770100) {
                  if (l_cmd_0 == OP_BUY && l_ord_open_price_28 == gda_500[gi_460]) {
                     if (l_price_52 < l_ord_open_price_28 + TakeProfitForLock * gd_488)
                        if (OrderModify(OrderTicket(), l_ord_open_price_28, l_ord_stoploss_44, l_ord_open_price_28 + TakeProfitForLock * gd_488, 0)) l_price_52 = l_ord_open_price_28 + TakeProfitForLock * gd_488;
                     if (StopLossForLock != 0.0 && l_ord_open_price_28 > gda_504[gi_464] && l_ord_open_price_28 - gda_504[gi_464] > 2.2 * StopLossForLock * gd_488 && MathAbs(l_ord_open_price_28 - ld_60) < MathAbs(gda_504[gi_464] - ld_60) &&
                        l_ord_stoploss_44 < l_ord_open_price_28 - StopLossForLock * gd_488)
                        if (OrderModify(OrderTicket(), l_ord_open_price_28, l_ord_open_price_28 - StopLossForLock * gd_488, l_price_52, 0)) l_ord_stoploss_44 = l_ord_open_price_28 - StopLossForLock * gd_488;
                  } else {
                     if (l_cmd_0 == OP_SELL && l_ord_open_price_28 == gda_504[gi_464]) {
                        if (l_price_52 > l_ord_open_price_28 - TakeProfitForLock * gd_488)
                           if (OrderModify(OrderTicket(), l_ord_open_price_28, l_ord_stoploss_44, l_ord_open_price_28 - TakeProfitForLock * gd_488, 0)) l_price_52 = l_ord_open_price_28 - TakeProfitForLock * gd_488;
                        if (StopLossForLock != 0.0 && l_ord_open_price_28 < gda_500[gi_460] && gda_500[gi_460] - l_ord_open_price_28 > 2.2 * StopLossForLock * gd_488 && MathAbs(l_ord_open_price_28 - ld_60) < MathAbs(gda_500[gi_460] - ld_60) &&
                           l_ord_stoploss_44 > l_ord_open_price_28 + StopLossForLock * gd_488)
                           if (OrderModify(OrderTicket(), l_ord_open_price_28, l_ord_open_price_28 + StopLossForLock * gd_488, l_price_52, 0)) l_ord_stoploss_44 = l_ord_open_price_28 + StopLossForLock * gd_488;
                     }
                  }
               }
            }
         }
      }
   }
   if (UseAcceleratorIndicator == 1 && gd_520 != 0.0) {
      if (gd_520 > 0.0) glDeleteAllDeferOrders(gi_472, Symbol());
      if (gd_520 < 0.0) glDeleteAllDeferOrders(gi_468, Symbol());
   }
   if (UseSpeedIndicator == 1 && gd_512 != 0.0) {
      if (gd_512 > 0.0 && gia_496[gi_472] > 0 && gda_500[gi_472] < Bid + 20.0 * gd_488) glDeleteAllDeferOrders(gi_472, Symbol());
      if (gd_512 < 0.0 && gia_496[gi_468] > 0 && gda_504[gi_468] > Ask - 20.0 * gd_488) glDeleteAllDeferOrders(gi_468, Symbol());
   }
   if (CountHighLowLimits > 0) {
      for (l_index_136 = 0; l_index_136 < CountHighLowLimits; l_index_136++) {
         switch (l_index_136) {
         case 0:
            gd_536 = iHL_LimitDistance1;
            break;
         case 1:
            gd_536 = iHL_LimitDistance2;
         }
         if (gia_496[gi_476] != 0 && gda_528[l_index_136] > 0.0 && gda_504[gi_476] >= gda_528[l_index_136] + gd_536 * gd_488) glDeleteAllDeferOrders(gi_476, Symbol(), gda_528[l_index_136] + gd_536 * gd_488, 0);
         if (!(gia_496[gi_480] != 0 && gda_532[l_index_136] > 0.0 && gda_500[gi_480] <= gda_532[l_index_136] - gd_536 * gd_488)) continue;
         glDeleteAllDeferOrders(gi_480, Symbol(), 0, gda_532[l_index_136] - gd_536 * gd_488);
      default:
      }
   }
   l_ord_total_140 = OrdersTotal();
   if (TrailingStop > 0.0) {
      for (l_pos_128 = 0; l_pos_128 < l_ord_total_140; l_pos_128++) {
         OrderSelect(l_pos_128, SELECT_BY_POS, MODE_TRADES);
         if (OrderSymbol() == Symbol()) {
            if (OrderType() == OP_BUY) {
               if (Bid > OrderOpenPrice() + gd_488 * TrailingStop)
                  if (OrderStopLoss() == 0.0 || OrderStopLoss() < Bid - gd_488 * (TrailingStop + StepTrailingStop)) OrderModify(OrderTicket(), OrderOpenPrice(), Bid - gd_488 * TrailingStop, OrderTakeProfit(), 0);
            } else {
               if (OrderType() == OP_SELL) {
                  if (Ask < OrderOpenPrice() - gd_488 * TrailingStop)
                     if (OrderStopLoss() == 0.0 || OrderStopLoss() > Ask + gd_488 * (TrailingStop + StepTrailingStop)) OrderModify(OrderTicket(), OrderOpenPrice(), Ask + gd_488 * TrailingStop, OrderTakeProfit(), 0);
               }
            }
         }
      }
   }
   if (!glAnalizCurrentOrdersState()) return (0);
   if (Risk > 0.0) ld_12 = glDoubleRound(AccountFreeMargin() / 1000.0 * Risk, StepLotSize);
   else ld_12 = MinLotSize;
   if (ld_12 < MinLotSize) ld_12 = MinLotSize;
   if (ld_12 > MaxLotSize) ld_12 = MaxLotSize;
   if (AccountFreeMargin() < 1000.0 * ld_12)
      if (gia_496[gi_460] + gia_496[gi_464] == 0) return (0);
   for (l_pos_128 = 0; l_pos_128 < 2; l_pos_128++) {
      if (MaxOrdersCount != 0)
         if (gia_496[gi_460] + gia_496[gi_464] >= MaxOrdersCount) break;
      if (l_pos_128 == 0) {
         if (UseBuy == 0) continue;
         li_120 = gi_460;
         ls_152 = "BUY";
         ld_60 = Ask;
      } else {
         if (UseSell == 0) continue;
         li_120 = gi_464;
         ls_152 = "SELL";
         ld_60 = Bid;
      }
      ld_68 = 100.0 * MathFloor(ld_60 / gd_488 / 100.0) * gd_488;
      if (OpenRealOrders == 1) {
         li_148 = FALSE;
         if (UseFlatIndicator == 1 && gi_544) {
            if (li_120 == gi_460 && ld_60 > g_ilow_548 && ld_60 <= g_ilow_548 + gd_564 / 4.0) {
               if (iOpen(NULL, gia_484[0], 0) >= iClose(NULL, gia_484[0], 1) && iClose(NULL, gia_484[0], 1) > iClose(NULL, gia_484[0], 2) && iClose(NULL, gia_484[0], 2) > iClose(NULL, gia_484[0], 3)) {
                  li_148 = TRUE;
                  l_cmd_0 = 0;
                  l_ord_open_price_28 = ld_60;
                  l_price_52 = l_ord_open_price_28 + NormalizeDouble(gd_564 / 2.0, Digits);
                  if (TakeProfitB > 0.0)
                     if (l_price_52 > l_ord_open_price_28 + TakeProfitB * gd_488) l_price_52 = l_ord_open_price_28 + TakeProfitB * gd_488;
                  if (iFL_StopLoss > 0.0) l_ord_stoploss_44 = l_ord_open_price_28 - iFL_StopLoss * gd_488;
                  else {
                     if (StopLossB > 0.0) l_ord_stoploss_44 = l_ord_open_price_28 - StopLossB * gd_488;
                     else l_ord_stoploss_44 = 0;
                  }
               }
            }
            if (li_120 == gi_464 && ld_60 < g_ihigh_556 && ld_60 >= g_ihigh_556 - gd_564 / 4.0) {
               if (iOpen(NULL, gia_484[0], 0) <= iClose(NULL, gia_484[0], 1) && iClose(NULL, gia_484[0], 1) < iClose(NULL, gia_484[0], 2) && iClose(NULL, gia_484[0], 2) < iClose(NULL, gia_484[0], 3)) {
                  li_148 = TRUE;
                  l_cmd_0 = 1;
                  l_ord_open_price_28 = ld_60;
                  l_price_52 = l_ord_open_price_28 - NormalizeDouble(gd_564 / 2.0, Digits);
                  if (TakeProfitS > 0.0)
                     if (l_price_52 < l_ord_open_price_28 - TakeProfitS * gd_488) l_price_52 = l_ord_open_price_28 - TakeProfitS * gd_488;
                  if (iFL_StopLoss > 0.0) l_ord_stoploss_44 = l_ord_open_price_28 + iFL_StopLoss * gd_488;
                  else {
                     if (StopLossS > 0.0) l_ord_stoploss_44 = l_ord_open_price_28 + StopLossS * gd_488;
                     else l_ord_stoploss_44 = 0;
                  }
               }
            }
            if (li_148) l_magic_4 = 7770200;
         }
         if (!li_148 && UseSpeedIndicator == 1 && iSP_UseForOpenRealOrders == 1 && MathAbs(gd_512) > iSP_MinValueForOpenRealOrders) {
            if (li_120 == gi_460 && gd_512 > 0.0) {
               li_148 = TRUE;
               l_cmd_0 = 0;
               l_ord_open_price_28 = ld_60;
               if (iSP_UseMinTakeProfit == 1.0) l_price_52 = l_ord_open_price_28 + MarketInfo(Symbol(), MODE_STOPLEVEL) * gd_488;
               else l_price_52 = l_ord_open_price_28 + TakeProfitB * gd_488;
               if (iSP_StopLoss > 0.0) l_ord_stoploss_44 = l_ord_open_price_28 - iSP_StopLoss * gd_488;
               else {
                  if (StopLossB > 0.0) l_ord_stoploss_44 = l_ord_open_price_28 - StopLossB * gd_488;
                  else l_ord_stoploss_44 = 0;
               }
            }
            if (li_120 == gi_464 && gd_512 < 0.0) {
               li_148 = TRUE;
               l_cmd_0 = 1;
               l_ord_open_price_28 = ld_60;
               if (iSP_UseMinTakeProfit == 1.0) l_price_52 = l_ord_open_price_28 - MarketInfo(Symbol(), MODE_STOPLEVEL) * gd_488;
               else l_price_52 = l_ord_open_price_28 - TakeProfitS * gd_488;
               if (iSP_StopLoss > 0.0) l_ord_stoploss_44 = l_ord_open_price_28 + iSP_StopLoss * gd_488;
               else {
                  if (StopLossS > 0.0) l_ord_stoploss_44 = l_ord_open_price_28 + StopLossS * gd_488;
                  else l_ord_stoploss_44 = 0;
               }
            }
            if (li_148) l_magic_4 = 7770300;
         }
         if (li_148)
            if (!glCheckOrderBeforeAdding(l_ord_open_price_28, li_120, l_cmd_0, ld_60)) li_148 = FALSE;
         if (li_148)
            if (!glCheckDistanceFromOneWayReal(l_ord_open_price_28, li_120, ls_152, ld_76)) li_148 = FALSE;
         if (li_148)
            if (!glCheckHighLowLimit(l_ord_open_price_28, li_120, ls_152)) li_148 = FALSE;
         if (li_148)
            if (!glCheckOpenOrdersBeforeAdding(l_ord_open_price_28, l_cmd_0, ld_60)) li_148 = FALSE;
         if (li_148) {
            l_lots_20 = ld_12;
            if (l_magic_4 == 7770200) {
               if (iFL_LotSizeMultiply > 0.0 && iFL_LotSizeMultiply != 1.0) {
                  l_lots_20 = glDoubleRound(ld_12 * iFL_LotSizeMultiply, StepLotSize);
                  if (l_lots_20 > MaxLotSize) l_lots_20 = MaxLotSize;
               }
            } else {
               if (l_magic_4 == 7770300) {
               }
            }
            l_ticket_116 = OrderSend(Symbol(), l_cmd_0, l_lots_20, l_ord_open_price_28, 0, l_ord_stoploss_44, l_price_52, "zdima", l_magic_4, 0, Green);
            if (l_ticket_116 > 0) {
               if (OrderSelect(l_ticket_116, SELECT_BY_TICKET, MODE_TRADES)) {
                  if (!(!glAnalizCurrentOrdersState())) continue;
                  return (0);
               }
            } else Print("Ошибка открытия ", ls_152, " ордера (Lots = ", ld_12, "): ", GetLastError());
         }
      }
      for (li_132 = 0; li_132 < 2; li_132++) {
         l_magic_4 = 7770000;
         if (li_132 == 0) {
            if (li_120 == gi_460) {
               if (BuyStopLevel < 0.0) continue;
               l_cmd_0 = 4;
               li_124 = gi_476;
               ls_152 = "BUY STOP";
               if (ld_60 < ld_68 + BuyStopLevel * gd_488) l_ord_open_price_28 = ld_68 + BuyStopLevel * gd_488;
               else l_ord_open_price_28 = ld_68 + (BuyStopLevel + 100.0) * gd_488;
            } else {
               if (SellStopLevel < 0.0) continue;
               l_cmd_0 = 5;
               li_124 = gi_480;
               ls_152 = "SELL STOP";
               if (ld_60 < ld_68 + SellStopLevel * gd_488) l_ord_open_price_28 = ld_68 - (100 - SellStopLevel) * gd_488;
               else l_ord_open_price_28 = ld_68 + SellStopLevel * gd_488;
            }
         } else {
            if (li_120 == gi_460) {
               if (BuyLimitLevel < 0.0) continue;
               l_cmd_0 = 2;
               li_124 = gi_468;
               ls_152 = "BUY LIMIT";
               if (ld_60 < ld_68 + BuyLimitLevel * gd_488) l_ord_open_price_28 = ld_68 - (100 - BuyLimitLevel) * gd_488;
               else l_ord_open_price_28 = ld_68 + BuyLimitLevel * gd_488;
            } else {
               if (SellLimitLevel < 0.0) continue;
               l_cmd_0 = 3;
               li_124 = gi_472;
               ls_152 = "SELL LIMIT";
               if (ld_60 < ld_68 + SellLimitLevel * gd_488) l_ord_open_price_28 = ld_68 + SellLimitLevel * gd_488;
               else l_ord_open_price_28 = ld_68 + (SellLimitLevel + 100.0) * gd_488;
            }
         }
         if (li_120 == gi_460) {
            l_price_52 = l_ord_open_price_28 + TakeProfitB * gd_488;
            if (StopLossB > 0.0) l_ord_stoploss_44 = l_ord_open_price_28 - StopLossB * gd_488;
            else l_ord_stoploss_44 = 0;
         } else {
            l_price_52 = l_ord_open_price_28 - TakeProfitS * gd_488;
            if (StopLossS > 0.0) l_ord_stoploss_44 = l_ord_open_price_28 + StopLossS * gd_488;
            else l_ord_stoploss_44 = 0;
         }
         li_144 = TRUE;
         if (li_144)
            if (!glCheckOrderBeforeAdding(l_ord_open_price_28, li_120, l_cmd_0, ld_60)) li_144 = FALSE;
         if (li_144) {
            if (MinDistanceDeferFromPrice != 0.0)
               if (MathAbs(l_ord_open_price_28 - ld_60) < MinDistanceDeferFromPrice * gd_488) li_144 = FALSE;
            if (MaxDistanceDeferFromPrice != 0.0)
               if (MathAbs(l_ord_open_price_28 - ld_60) > MaxDistanceDeferFromPrice * gd_488) li_144 = FALSE;
         }
         if (li_144)
            if (gia_496[li_120] > 0 && li_132 == 1) li_144 = FALSE;
         if (li_144) {
            if (gia_496[li_124] > 0) {
               if (li_124 == gi_476 || li_124 == gi_472) ld_84 = gda_500[li_124];
               else ld_84 = gda_504[li_124];
               ld_92 = MinDistanceBetweenDefer * gd_488;
               if (li_124 == gi_476 || li_124 == gi_472) {
                  if (l_ord_open_price_28 > ld_84 - ld_92) li_144 = FALSE;
               } else
                  if (l_ord_open_price_28 < ld_84 + ld_92) li_144 = FALSE;
            }
         }
         if (li_144 && gia_496[li_120] > 0) {
            if (glCheckDistanceFromOneWayReal(l_ord_open_price_28, li_120, ls_152, ld_76)) {
               if ((li_120 == gi_460 && UseAveragingTakeProfitB == 1) || (li_120 == gi_464 && UseAveragingTakeProfitS == 1)) {
                  l_price_52 = (l_ord_open_price_28 + ld_76) / 2.0;
                  l_magic_4 = 7770100;
               }
            } else li_144 = FALSE;
         }
         if (li_144 && UseAcceleratorIndicator == 1) {
            if (li_132 == 1) {
               if (li_120 == gi_460 && gd_520 < 0.0) li_144 = FALSE;
               if (li_120 == gi_464 && gd_520 > 0.0) li_144 = FALSE;
            }
         }
         if (li_144 && UseSpeedIndicator == 1) {
            if (li_132 == 1) {
               if (li_120 == gi_460 && gd_512 < 0.0) li_144 = FALSE;
               if (li_120 == gi_464 && gd_512 > 0.0) li_144 = FALSE;
            }
         }
         if (li_144 && CountHighLowLimits > 0)
            if (!glCheckHighLowLimit(l_ord_open_price_28, li_120, ls_152)) li_144 = FALSE;
         if (li_144)
            if (!glCheckOpenOrdersBeforeAdding(l_ord_open_price_28, l_cmd_0, ld_60)) li_144 = FALSE;
         if (li_144) {
            l_lots_20 = ld_12;
            l_ticket_116 = OrderSend(Symbol(), l_cmd_0, l_lots_20, l_ord_open_price_28, 0, l_ord_stoploss_44, l_price_52, "zdima", l_magic_4, 0, Green);
            if (l_ticket_116 > 0) {
               if (!(OrderSelect(l_ticket_116, SELECT_BY_TICKET, MODE_TRADES))) continue;
               if (!(!glAnalizCurrentOrdersState())) continue;
               return (0);
            }
            Print("Ошибка открытия ", ls_152, " ордера (Lots = ", ld_12, "): ", GetLastError());
         }
      }
   }
   return (0);
}

bool glCheckOrderBeforeAdding(double ad_0, int ai_8, int ai_12, double ad_unused_16) {
   if (ai_8 == gi_460) {
      if (UseBuy == 0) return (FALSE);
      if (ai_12 == 0 && OpenRealOrders == 0) return (FALSE);
      if (BuyDenyLevel > 0.0 && ad_0 >= BuyDenyLevel) return (FALSE);
   }
   if (ai_8 == gi_464) {
      if (UseSell == 0) return (FALSE);
      if (ai_12 == 1 && OpenRealOrders == 0) return (FALSE);
      if (SellDenyLevel > 0.0 && ad_0 <= SellDenyLevel) return (FALSE);
   }
   return (TRUE);
}

bool glCheckOpenOrdersBeforeAdding(double ad_0, int ai_8, double ad_12) {
   int l_ord_total_32;
   int l_cmd_36;
   int l_ticket_40;
   double ld_44;
   double ld_52;
   if (ai_8 == 0 || ai_8 == 2)
      if (!glDeleteAllDeferOrders(OP_BUYLIMIT, Symbol())) return (FALSE);
   if (ai_8 == 1 || ai_8 == 3)
      if (!glDeleteAllDeferOrders(OP_SELLLIMIT, Symbol())) return (FALSE);
   if (MaxOrdersCount != 0) {
      l_ord_total_32 = OrdersTotal();
      while (l_ord_total_32 >= MaxOrdersCount) {
         ld_52 = 0;
         for (int l_pos_20 = 0; l_pos_20 < l_ord_total_32; l_pos_20++) {
            if (!OrderSelect(l_pos_20, SELECT_BY_POS, MODE_TRADES)) Print("Ошибка получения текущего ордера OrderSelect(): ", GetLastError());
            else {
               if (OrderSymbol() == Symbol()) {
                  l_cmd_36 = OrderType();
                  if (l_cmd_36 == OP_BUYLIMIT || l_cmd_36 == OP_BUYSTOP) ld_44 = MathAbs(Ask - OrderOpenPrice());
                  else {
                     if (!(l_cmd_36 == OP_SELLLIMIT || l_cmd_36 == OP_SELLSTOP)) continue;
                     ld_44 = MathAbs(Bid - OrderOpenPrice());
                  }
                  if (ld_44 > ld_52) {
                     ld_52 = ld_44;
                     l_ticket_40 = OrderTicket();
                  }
               }
            }
         }
         if (ld_52 > MathAbs(ad_0 - ad_12)) {
            if (OrderDelete(l_ticket_40)) {
               l_ord_total_32 = OrdersTotal();
               continue;
            }
            return (FALSE);
         }
         return (FALSE);
      }
   }
   return (TRUE);
}

bool glCheckDistanceFromOneWayReal(double ad_0, int ai_8, string as_unused_12, double &ad_20) {
   double ld_28;
   if (gia_496[ai_8] == 0) return (TRUE);
   if (ai_8 == gi_460 && UseOneWayRealOrdersB == 0) return (FALSE);
   if (ai_8 == gi_464 && UseOneWayRealOrdersS == 0) return (FALSE);
   if (ai_8 == gi_460) {
      ad_20 = gda_500[ai_8];
      ld_28 = gia_496[ai_8] * MinDistanceRealOrdersB * gd_488;
   } else {
      ad_20 = gda_504[ai_8];
      ld_28 = gia_496[ai_8] * MinDistanceRealOrdersS * gd_488;
   }
   if (ai_8 == gi_460) {
      if (ad_0 > ad_20 - ld_28) return (FALSE);
   } else
      if (ad_0 < ad_20 + ld_28) return (FALSE);
   return (TRUE);
}

bool glCheckHighLowLimit(double ad_0, int ai_8, string as_unused_12) {
   for (int l_index_28 = 0; l_index_28 < CountHighLowLimits; l_index_28++) {
      switch (l_index_28) {
      case 0:
         gd_536 = iHL_LimitDistance1;
         break;
      case 1:
         gd_536 = iHL_LimitDistance2;
      }
      if (ai_8 == gi_460 && gda_528[l_index_28] > 0.0 && ad_0 >= gda_528[l_index_28] + gd_536 * gd_488) return (FALSE);
      if (!(ai_8 == gi_464 && gda_532[l_index_28] > 0.0 && ad_0 <= gda_532[l_index_28] - gd_536 * gd_488)) continue;
      return (FALSE);
   default:
   }
   return (TRUE);
}

bool glAnalizCurrentOrdersState() {
   int l_cmd_16;
   int li_20;
   double l_ord_open_price_24;
   double l_ord_lots_32;
   int l_ord_total_12 = OrdersTotal();
   for (int l_pos_0 = 0; l_pos_0 < 6; l_pos_0++) {
      gda_500[l_pos_0] = -1;
      gda_504[l_pos_0] = -1;
      gda_508[l_pos_0] = 0;
      gia_496[l_pos_0] = 0;
   }
   for (l_pos_0 = 0; l_pos_0 < l_ord_total_12; l_pos_0++) {
      if (!OrderSelect(l_pos_0, SELECT_BY_POS, MODE_TRADES)) {
         Print("Ошибка получения текущего ордера OrderSelect(): ", GetLastError());
         return (FALSE);
      }
      if (OrderSymbol() == Symbol()) {
         l_cmd_16 = OrderType();
         l_ord_open_price_24 = OrderOpenPrice();
         l_ord_lots_32 = OrderLots();
         li_20 = glGetNormalOrderType(l_cmd_16);
         if (li_20 == -1) {
            Print("Ошибка определения типа текущего ордера: OrderType() = ", l_cmd_16);
            return (FALSE);
         }
         gia_496[li_20]++;
         if (gda_500[li_20] == -1.0 || l_ord_open_price_24 < gda_500[li_20]) gda_500[li_20] = l_ord_open_price_24;
         if (gda_504[li_20] == -1.0 || l_ord_open_price_24 > gda_504[li_20]) gda_504[li_20] = l_ord_open_price_24;
         gda_508[li_20] += l_ord_lots_32;
      }
   }
   return (TRUE);
}

bool glDeleteAllDeferOrders(int a_cmd_0 = -1, string a_symbol_4 = "", double ad_12 = 0.0, double ad_20 = 0.0) {
   int l_cmd_44;
   double l_ord_open_price_48;
   bool li_ret_56 = TRUE;
   int l_ord_total_40 = OrdersTotal();
   for (int l_pos_28 = l_ord_total_40 - 1; l_pos_28 >= 0; l_pos_28--) {
      if (OrderSelect(l_pos_28, SELECT_BY_POS, MODE_TRADES)) {
         l_cmd_44 = OrderType();
         l_ord_open_price_48 = OrderOpenPrice();
         if (a_cmd_0 == -1 || l_cmd_44 == a_cmd_0 && a_symbol_4 == "" || OrderSymbol() == a_symbol_4) {
            if (a_cmd_0 == -1)
               if (l_cmd_44 == OP_BUY || l_cmd_44 == OP_SELL) continue;
            if (ad_12 != 0.0 && l_ord_open_price_48 < ad_12) continue;
            if (ad_20 != 0.0 && l_ord_open_price_48 > ad_20) continue;
            if (!OrderDelete(OrderTicket())) li_ret_56 = FALSE;
         }
      } else li_ret_56 = FALSE;
   }
   return (li_ret_56);
}

double glDoubleRound(double ad_0, double ad_8) {
   int li_20 = 1;
   if (ad_8 == 0.0) return (ad_0);
   while (ad_8 * li_20 < 1.0) li_20 = 10 * li_20;
   int li_16 = ad_8 * li_20;
   double ld_24 = MathFloor(ad_0 * li_20);
   if (MathAbs(ad_0 * li_20 - ld_24) > 0.5) ld_24++;
   double ld_32 = ld_24 / li_16;
   if (ld_32 != MathRound(ld_32)) {
      if (MathAbs(ld_32 - MathFloor(ld_32)) > 0.5) ld_32 = MathFloor(ld_32) + 1.0;
      else ld_32 = MathFloor(ld_32);
      ld_24 = ld_32 * li_16;
   }
   return (ld_24 / li_20);
}

int glOrderClose(double a_price_0 = -1.0, double a_slippage_8 = 0.0) {
   int l_error_32;
   bool li_ret_36 = FALSE;
   if (a_price_0 == -1.0) a_price_0 = glGetCurrentClosePrice();
   for (int l_count_16 = 0; l_count_16 < 5; l_count_16++) {
      if (OrderClose(OrderTicket(), OrderLots(), a_price_0, a_slippage_8)) {
         li_ret_36 = TRUE;
         break;
      }
      l_error_32 = GetLastError();
      Print("OrderClose failed, Error: ", l_error_32);
      if (l_error_32 > 4000/* NO_MQLERROR */) break;
      Sleep(1000);
      RefreshRates();
      a_price_0 = glGetCurrentClosePrice();
   }
   return (li_ret_36);
}

double glGetCurrentClosePrice() {
   double l_price_0;
   if (OrderSymbol() == Symbol()) {
      if (OrderType() == OP_BUY) l_price_0 = Bid;
      else l_price_0 = Ask;
   } else {
      if (OrderType() == OP_BUY) l_price_0 = MarketInfo(OrderSymbol(), MODE_BID);
      else l_price_0 = MarketInfo(OrderSymbol(), MODE_ASK);
   }
   return (l_price_0);
}
