
#property copyright "#Copyright © 2008, XrustSolution.#"
#property link      "#http://www.xrust.ucoz.net#"

#include <stdlib.mqh>

extern string a = "Part One = Skalper";
extern int IndSet1 = 3;
extern int IndSet2 = 1;
extern int F_T_treshold = 8;
extern int Stoploss = 0;
extern int Takeprofit = 0;
extern double Risk = 1.0;
extern int TimeFrame = 0;
extern bool OnBar = TRUE;
extern int WlMinute = 0;
extern string b = "Part Two = Locker";
extern bool FullAuto = TRUE;
extern int LockMode = 1;
extern int TotalOrders = 50;
extern bool CloseLock = FALSE;
extern bool Martingale = TRUE;
extern string c = "Part Three Time and Date ";
extern bool TradeOfTime = FALSE;
extern string d = "Hour start of trading";
extern int StartTrade = 19;
extern string e = "Hour end of trading";
extern int EndTrade = 5;
extern string f = "Date end of trading";
extern string EndTradeData = "Enterting Date of End trading in eeee.mm.dd";
extern string h = "Language: if true=engleesh,if false=russia";
extern bool EnglLang = FALSE;
extern bool SetInfotoDispl = TRUE;
extern int TextSize = 12;
extern string m = "Text position to displ";
extern int TextPos = 2;
extern string n = "text color to displ";
extern color TextColor = White;
extern bool UseSound = FALSE;
extern bool TestInfo = TRUE;
extern int MagicFirstOrder = 4642;
extern int Magik = 6559;
double gd_264;
bool gi_272 = TRUE;
string gs_276 = "expert.wav";
int gi_284 = 16711680;
int gi_288 = 255;
bool gi_292 = FALSE;
bool gi_unused_296 = FALSE;
int g_slippage_300 = 3;
int gi_304 = 3;
int gi_unused_308;
double gd_312;
string gs_320;
string gs_328;
string gs_336;
string gs_344;
string gs_352;
string gs_360;
string gs_368;
string gs_376;
string gs_384;
string gs_392;
string gs_400;
string gs_408;
string gs_416;
string gs_424;
string gs_432;
string gs_440;
int gi_448 = 3329330;
int gi_452;
string g_str_concat_456;
string g_str_concat_464;
bool gi_472 = FALSE;
bool gi_unused_476 = TRUE;
int gi_480 = 10;
int gi_484 = 10;
int g_timeframe_488;
int gi_492;
int gi_496;
int g_datetime_500 = 0;
int gi_504 = 0;
int gi_536 = 1;
string gs_540;
double gd_548;
double gd_556;
double gd_564;
double gd_572;
int g_ord_total_580;
double gd_584;
double gd_592;
int g_ord_total_600;
int gi_unused_604 = 0;
int g_time_608 = 0;

void init() {
   gd_312 = AccountBalance();
   gi_452 = TotalOrders;
   gi_unused_308 = MarketInfo(Symbol(), MODE_SPREAD);
   if (EndTradeData == "Enterting Date of End trading in eeee.mm.dd") EndTradeData = "";
   if (TimeFrame == 0) g_timeframe_488 = Period();
   if (TimeFrame == 1) g_timeframe_488 = 30;
   if (TimeFrame == 2) g_timeframe_488 = 60;
   if (TimeFrame == 3) g_timeframe_488 = 240;
   if (TimeFrame == 4) g_timeframe_488 = 1440;
   if (TimeFrame >= 5 && TimeFrame < 15) g_timeframe_488 = 5;
   if (TimeFrame >= 15 && TimeFrame < 30) g_timeframe_488 = 15;
   if (TimeFrame >= 30 && TimeFrame < 60) g_timeframe_488 = 30;
   if (TimeFrame >= 60 && TimeFrame < 240) g_timeframe_488 = 60;
   if (TimeFrame >= 240 && TimeFrame < 1440) g_timeframe_488 = 240;
   if (TimeFrame >= 1440) g_timeframe_488 = 1440;
   if (SetInfotoDispl)
      if (TextPos == 0) TestInfo = FALSE;
   if (Stoploss == 0) gi_492 = gi_484;
   else gi_492 = Stoploss;
   if (Takeprofit == 0) gi_496 = gi_480;
   else gi_496 = Takeprofit;
   if (Digits == 3 || Digits == 5) gi_536 = 10;
   else gi_536 = 1;
   g_slippage_300 *= gi_536;
   Stoploss *= gi_536;
   Takeprofit *= gi_536;
   Average();
   FirstTrade();
}

void deinit() {
   ObjectsDeleteAll(0, OBJ_LABEL);
}

void start() {
   gi_504 = 0;
   Info();
   WL(WlMinute);
   if (FullAuto) {
      if (AccountBalance() + GrossProfit() < 0.75 * AccountBalance()) CloseLock = TRUE;
      else CloseLock = FALSE;
   }
   if (Stoploss == 0) {
      Average();
      gi_492 = gi_484;
   } else gi_492 = Stoploss;
   if (Takeprofit == 0) {
      Average();
      gi_496 = gi_480;
   } else gi_496 = Takeprofit;
   if (LockMode == 0) gs_540 = " FullAuto";
   if (LockMode == 1) gs_540 = " FlatMode";
   if (LockMode == 2) gs_540 = " TrandMode";
   if (LockMode == 3) gs_540 = " ContrTrand";
   string l_str_concat_0 = StringConcatenate("Sl=", gi_492, " Tp=", gi_496, "  X", PriseChanelWide(), "   ", gs_540, "  ", gd_556, " ", gd_564, " ", gd_572);
   if (SetInfotoDispl) {
      if (!EnglLang) SetInfoRus(TextSize, TextPos, TextColor);
      else SetInfoEngl(TextSize, TextPos, TextColor);
   }
   if (g_ord_total_580 < OrdersTotal()) g_ord_total_580 = OrdersTotal();
   gd_264 = CalcLotsAuto();
   if (CountOpOrd("0") > 0)
      if (GrossProfit() >= gi_496 * FirstOrderLots() * MarketInfo(Symbol(), MODE_LOTSIZE) / 10000.0 / CountOpOrd("0") / gi_536) CloseAll();
   if (CountOpOrd("0") < 1) gi_452 = TotalOrders;
   if (gi_452 > AccountBalance() / (MarketInfo(Symbol(), MODE_MARGINREQUIRED) * CalcLotsAuto())) gi_452 = AccountBalance() / (MarketInfo(Symbol(), MODE_MARGINREQUIRED) * CalcLotsAuto());
   if (LockMode == 1) {
      if (CloseLock) {
         CloseBy();
         DelBigestLoss();
      }
      if (CountOpOrd("0") > 1 && CountOpOrd("0") < gi_452) {
         if (Martingale) gd_264 = CalcLotsAuto() * PriseChanelWide();
         if (OutOfPrise()) {
            if (PriseDist(gi_492 * PriseChanelWide()))
               if (NearOrdDistanse(gi_492 * PriseChanelWide()) > 0) OpenPosition("", OP_BUY, gd_264, 0, 0, Magik);
            if (PriseDist(gi_492 * PriseChanelWide()))
               if (NearOrdDistanse(gi_492 * PriseChanelWide()) < 0) OpenPosition("", OP_SELL, gd_264, 0, 0, Magik);
         } else {
            if (PriseDist(gi_492 / 3))
               if (MidleOrders(gi_492 / 3) == 0) OpenPosition("", OP_BUY, gd_264, 0, 0, Magik);
            if (PriseDist(gi_492 / 3))
               if (MidleOrders(gi_492 / 3) == 1) OpenPosition("", OP_SELL, gd_264, 0, 0, Magik);
         }
      }
      if (CountOpOrd("0", MagicFirstOrder) == 1 && CountOpOrd("0", Magik) < 1) {
         if (IfOrdstoploss() < 0)
            if (PriseDist(gi_492)) OpenPosition("", OP_BUY, OrdLot(), 0, 0, Magik, l_str_concat_0);
         if (IfOrdstoploss() > 0)
            if (PriseDist(gi_492)) OpenPosition("", OP_SELL, OrdLot(), 0, 0, Magik, l_str_concat_0);
      }
      if (TradeTime()) {
         if (EndTradeForDateTime()) return;
         if (CountOpOrd("", MagicFirstOrder, -1) < 1) {
            FirstTrade();
            if (gi_504 < 0) {
               OpenPosition("", OP_SELL, gd_264, 0, 0, MagicFirstOrder, l_str_concat_0);
               return;
            }
            if (gi_504 > 0) {
               OpenPosition("", OP_BUY, gd_264, 0, 0, MagicFirstOrder, l_str_concat_0);
               return;
            }
         }
      }
   }
   if (LockMode == 2) {
      if (CountOpOrd("0") > 1 && CountOpOrd("0") < gi_452) {
         if (CloseLock) {
            CloseBy();
            DelBigestLoss();
         }
         if (NevBar())
            if (gi_472) SetArrow("NullProfitDot", 4, 1, 0, NullDot(), White);
         if (Martingale) gd_264 = CalcLotsAuto() * PriseChanelWide();
         if (OutOfPrise()) {
            if (OutOfPrise()) {
               if (NearOrdDistanse(gi_492 / PriseChanelWide()) < 0)
                  if (PriseDist(gi_492 / PriseChanelWide())) OpenPosition("", OP_BUY, gd_264, 0, 0, Magik);
            }
            if (OutOfPrise()) {
               if (NearOrdDistanse(gi_492 / PriseChanelWide()) > 0)
                  if (PriseDist(gi_492 / PriseChanelWide())) OpenPosition("", OP_SELL, gd_264, 0, 0, Magik);
            }
         } else {
            if (PriseDist(gi_492 / PriseChanelWide() / 2))
               if (MidleOrders(gi_492 / PriseChanelWide() / 2) == 0) OpenPosition("", OP_BUY, gd_264, 0, 0, Magik);
            if (PriseDist(gi_492 / PriseChanelWide() / 2))
               if (MidleOrders(gi_492 / PriseChanelWide() / 2) == 1) OpenPosition("", OP_SELL, gd_264, 0, 0, Magik);
         }
      }
      if (CountOpOrd("0", MagicFirstOrder) == 1 && CountOpOrd("0", Magik) < 1) {
         if (IfOrdstoploss() > 0)
            if (PriseDist(gi_492)) OpenPosition("", OP_BUY, OrdLot(), 0, 0, Magik, l_str_concat_0);
         if (IfOrdstoploss() < 0)
            if (PriseDist(gi_492)) OpenPosition("", OP_SELL, OrdLot(), 0, 0, Magik, l_str_concat_0);
      }
      if (TradeTime()) {
         if (EndTradeForDateTime()) return;
         if (CountOpOrd("0", MagicFirstOrder) < 1) {
            FirstTrade();
            if (gi_504 < 0) {
               OpenPosition("", OP_SELL, gd_264, 0, 0, MagicFirstOrder, l_str_concat_0);
               return;
            }
            if (gi_504 > 0) {
               OpenPosition("", OP_BUY, gd_264, 0, 0, MagicFirstOrder, l_str_concat_0);
               return;
            }
         }
      }
   }
   if (LockMode == 3) {
      if (CountOpOrd("0") > 1 && CountOpOrd("0") < gi_452) {
         if (CloseLock) {
            CloseBy();
            DelBigestLoss();
         }
         if (NevBar())
            if (gi_472) SetArrow("NullProfitDot", 4, 1, 0, NullDot(), White);
         if (Martingale) gd_264 = CalcLotsAuto() * PriseChanelWide();
         if (OutOfPrise()) {
            if (OutOfPrise()) {
               if (NearOrdDistanse(gi_492 * PriseChanelWide()) > 0)
                  if (PriseDist(gi_492 * PriseChanelWide())) OpenPosition("", OP_BUY, gd_264, 0, 0, Magik);
            }
            if (OutOfPrise()) {
               if (NearOrdDistanse(gi_492 * PriseChanelWide()) < 0)
                  if (PriseDist(gi_492 * PriseChanelWide())) OpenPosition("", OP_SELL, gd_264, 0, 0, Magik);
            }
         } else {
            if (PriseDist(gi_492 / PriseChanelWide() / 4))
               if (MidleOrders(gi_492 / PriseChanelWide() / 4) == 0) OpenPosition("", OP_BUY, gd_264, 0, 0, Magik);
            if (PriseDist(gi_492 / PriseChanelWide() / 4))
               if (MidleOrders(gi_492 / PriseChanelWide() / 4) == 1) OpenPosition("", OP_SELL, gd_264, 0, 0, Magik);
         }
      }
      if (CountOpOrd("0", MagicFirstOrder) == 1 && CountOpOrd("0", Magik) < 1) {
         if (IfOrdstoploss() < 0)
            if (PriseDist(gi_492)) OpenPosition("", OP_BUY, OrdLot(), 0, 0, Magik, l_str_concat_0);
         if (IfOrdstoploss() > 0)
            if (PriseDist(gi_492)) OpenPosition("", OP_SELL, OrdLot(), 0, 0, Magik, l_str_concat_0);
      }
      if (TradeTime()) {
         if (EndTradeForDateTime()) return;
         if (CountOpOrd("0", MagicFirstOrder) < 1) {
            FirstTrade();
            if (gi_504 < 0) {
               OpenPosition("", OP_SELL, gd_264, 0, 0, MagicFirstOrder, l_str_concat_0);
               return;
            }
            if (gi_504 > 0) {
               OpenPosition("", OP_BUY, gd_264, 0, 0, MagicFirstOrder, l_str_concat_0);
               return;
            }
         }
      }
   }
   if (TestInfo) {
      Testinfo();
      if (IsTesting()) {
         SetArrow("Ama1_", 4, 1, 0, AMA(IndSet1, 30, 9, 25, 150, g_timeframe_488, 1), White);
         SetArrow("Ama2_", 4, 1, 0, AMA(IndSet1 + IndSet2, 30, 9, 25, 150, g_timeframe_488, 1), Red);
      }
   }
   if (OnBar) g_datetime_500 = iTime(NULL, g_timeframe_488, 0);
}

double FirstOrderLots() {
   double l_ord_lots_0;
   for (int l_pos_8 = 0; l_pos_8 < OrdersTotal(); l_pos_8++) {
      if (OrderSelect(l_pos_8, SELECT_BY_POS, MODE_TRADES)) {
         if (OrderSymbol() == Symbol())
            if (OrderMagicNumber() == MagicFirstOrder) l_ord_lots_0 = OrderLots();
      }
   }
   if (l_ord_lots_0 == 0.0) l_ord_lots_0 = CalcLotsAuto();
   return (l_ord_lots_0);
}

void Testinfo() {
   string ls_16;
   string ls_24;
   string ls_32;
   if (GrossProfit() < gd_584) gd_584 = GrossProfit();
   if (GrossProfit() > gd_592) gd_592 = GrossProfit();
   if (OrdersTotal() > g_ord_total_600) g_ord_total_600 = OrdersTotal();
   string ls_8 = "";
   if (FullAuto) ls_16 = "TRUE";
   else ls_16 = "FALSE";
   if (LockMode == 1) ls_8 = "FlatMode";
   if (LockMode == 2) ls_8 = "TrandMode";
   if (LockMode == 3) ls_8 = "ContrTrand";
   if (OnBar) ls_32 = "TRUE";
   else ls_32 = "FALSE";
   if (CloseLock) ls_24 = "TRUE";
   else ls_24 = "FALSE";
   Comment(Secondnull("EXPERT SETTING", 18), Secondnull("EXPERT DATA", 33), " \n", Secondnull("IndSet 1 ="), leadnull(IndSet1), Secondnull("Balanse =", 29), leadnull(AccountBalance()), 
      "\n", Secondnull("IndSet 2 ="), leadnull(IndSet2), Secondnull("Equty =", 29), leadnull(AccountBalance() + GrossProfit()), 
      "\n", Secondnull("FullAuto =", 14), Secondnull(ls_16, 7), Secondnull("CurrDrdn =", 29), leadnull(GrossProfit(-1)), 
      "\n", Secondnull("TakeProfit =", 14), leadnull(gi_496 / gi_536), Secondnull("MaxDrdn =", 28), leadnull(gd_584), 
      "\n", Secondnull("StopLos ="), leadnull(gi_492 / gi_536), Secondnull("CurrProfit =", 29), leadnull(GrossProfit(1)), 
      "\n", Secondnull("Risc =", 17), leadnull(Risk), Secondnull("MaxProfit =", 29), leadnull(gd_592), 
      "\n", Secondnull("TimeFrame =", 12), leadnull(g_timeframe_488), Secondnull("OpenOrders =", 26), leadnull(OrdersTotal(), 6), 
      "\n", Secondnull("TotalOrders =", 14), leadnull(gi_452), Secondnull("MaxOpenOrd =", 23), leadnull(g_ord_total_600, 6), 
      "\n", Secondnull("OnBar ="), Secondnull(ls_32, 7), Secondnull("TotalOrders =", 25), leadnull(OrdersTotal() + OrdersHistoryTotal(), 6), 
      "\n", Secondnull("LockMode =", 13), Secondnull(ls_8, 10), Secondnull("PriseChanelWide =X", 20), leadnull(PriseChanelWide(), 4), 
      "\n", Secondnull("CloseLock =", 14), Secondnull(ls_24, 7), Secondnull("RelProfit =", 28), leadnull(histcount(), 5), 
   "\n", Secondnull("FTtreshold =", 14), Secondnull(F_T_treshold, 7), Secondnull("Porog =", 32), leadnull(gd_548, 5));
}

string leadnull(double ad_0, int ai_8 = 8) {
   string ls_unused_16;
   string l_str_concat_24;
   string l_dbl2str_32 = DoubleToStr(ad_0, 1);
   int l_str_len_12 = StringLen(l_dbl2str_32);
   if (l_str_len_12 < ai_8) for (int l_count_40 = 0; l_count_40 <= ai_8 - l_str_len_12; l_count_40++) l_str_concat_24 = StringConcatenate(l_str_concat_24, " ");
   return (StringConcatenate(l_str_concat_24, l_dbl2str_32));
}

string Secondnull(string as_0, int ai_8 = 15) {
   string ls_unused_16;
   string l_str_concat_24;
   int l_str_len_12 = StringLen(as_0);
   if (l_str_len_12 < ai_8) for (int l_count_32 = 0; l_count_32 <= ai_8 - l_str_len_12; l_count_32++) l_str_concat_24 = StringConcatenate(l_str_concat_24, " ");
   return (StringConcatenate(l_str_concat_24, as_0));
}

double histcount() {
   double ld_0 = 0;
   double ld_8 = 0;
   for (int l_pos_16 = 0; l_pos_16 < OrdersHistoryTotal(); l_pos_16++) {
      if (OrderSelect(l_pos_16, SELECT_BY_POS, MODE_HISTORY)) {
         if (OrderSymbol() == Symbol()) {
            ld_8++;
            if (OrderMagicNumber() == MagicFirstOrder) {
               ld_0++;
               ld_8--;
            }
         }
      }
   }
   if (ld_0 == 0.0) ld_0 = 1;
   if (ld_8 == 0.0) ld_8 = 1;
   double ld_ret_20 = ld_0 / ld_8;
   return (ld_ret_20);
}

void Average() {
   gi_484 = (MathAbs(iHigh(Symbol(), PERIOD_H1, 1) - iLow(Symbol(), PERIOD_H1, 1)) / Point + MathAbs(iHigh(Symbol(), PERIOD_H1, 24) - iLow(Symbol(), PERIOD_H1, 24)) / Point +
      MathAbs(iHigh(Symbol(), PERIOD_H1, 48) - iLow(Symbol(), PERIOD_H1, 48)) / Point + MathAbs(iHigh(Symbol(), PERIOD_H1, 72) - iLow(Symbol(), PERIOD_H1, 72)) / Point + MathAbs(iHigh(Symbol(), PERIOD_H1, 96) - iLow(Symbol(), PERIOD_H1, 96)) / Point) / 5.0;
   if (LockMode == 2) gi_484 /= 2;
   gi_480 = (MathAbs(iOpen(Symbol(), PERIOD_H1, 1) - iClose(Symbol(), PERIOD_H1, 1)) / Point + MathAbs(iOpen(Symbol(), PERIOD_H1, 24) - iClose(Symbol(), PERIOD_H1, 24)) / Point +
      MathAbs(iOpen(Symbol(), PERIOD_H1, 48) - iClose(Symbol(), PERIOD_H1, 48)) / Point + MathAbs(iOpen(Symbol(), PERIOD_H1, 72) - iClose(Symbol(), PERIOD_H1, 72)) / Point + MathAbs(iOpen(Symbol(), PERIOD_H1, 96) - iClose(Symbol(), PERIOD_H1, 96)) / Point) / 5.0 / 2.0;
}

void WL(int ai_0 = 0) {
   double ld_4;
   if (ai_0 != 0) {
      if (OrdersTotal() >= 1) {
         if (OrdersTotal() <= 1) {
            ld_4 = MarketInfo(Symbol(), MODE_STOPLEVEL) + MarketInfo(Symbol(), MODE_SPREAD);
            for (int l_pos_12 = 0; l_pos_12 < OrdersTotal(); l_pos_12++) {
               if (OrderSelect(l_pos_12, SELECT_BY_POS, MODE_TRADES)) {
                  if (OrderSymbol() == Symbol()) {
                     if (OrderMagicNumber() == MagicFirstOrder) {
                        if (TimeCurrent() - OrderOpenTime() > 60 * ai_0) {
                           if (OrderProfit() > 0.0) {
                              if (OrderStopLoss() != OrderOpenPrice()) {
                                 if (MathAbs(OrderOpenPrice() - Bid) / Point > ld_4) {
                                    OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), OrderTakeProfit(), 0, White);
                                    return;
                                 }
                              }
                           }
                        }
                     }
                  }
               }
            }
         }
      }
   }
}

void FirstTrade() {
   double lda_20[3];
   double lda_24[3];
   int lia_28[3];
   int li_0 = (gi_492 + gi_496) / 2;
   if (OnBar)
      if (g_datetime_500 == iTime(NULL, g_timeframe_488, 0)) return;
   if (OnBar) g_datetime_500 = iTime(NULL, g_timeframe_488, 0);
   gi_504 = 0;
   for (int li_12 = 1; li_12 <= 3; li_12++) {
      lda_20[li_12 - 1] = AMA(IndSet1, 30, 9, 25, 150, g_timeframe_488, li_12);
      lda_24[li_12 - 1] = AMA(IndSet1 + IndSet2, 30, 9, 25, 150, g_timeframe_488, li_12);
   }
   lia_28[0] = (lda_20[0] - lda_24[0]) / Point / gi_536;
   lia_28[1] = (lda_20[1] - lda_24[1]) / Point / gi_536;
   lia_28[2] = (lda_20[2] - lda_24[2]) / Point / gi_536;
   gd_548 = lia_28[0] - lia_28[1];
   gd_556 = lia_28[0];
   gd_564 = lia_28[1];
   gd_572 = lia_28[2];
   if (FullAuto) {
      if (lda_20[0] == lda_20[1] && lda_20[1] == lda_20[2]) {
         if (lia_28[0] == lia_28[1] && lia_28[1] == lia_28[2]) LockMode = 1;
      } else {
         if ((lia_28[0] != lia_28[1] && lia_28[1] != lia_28[2]) || (lia_28[0] != lia_28[1] && lia_28[1] == lia_28[2]) || (lia_28[0] == lia_28[1] && lia_28[1] != lia_28[2])) LockMode = 3;
         if (lia_28[0] == lia_28[1] && lia_28[1] == lia_28[2]) LockMode = 2;
      }
   }
   if (LockMode == 3) {
      if (lda_20[0] > lda_20[1] && lda_20[1] > lda_20[2]) {
         if (lia_28[0] > 0 && lia_28[1] > 0 && lia_28[2] > 0) {
            if (lia_28[0] >= lia_28[1] && lia_28[1] >= lia_28[2]) {
               if (iLow(Symbol(), g_timeframe_488, 1) > lda_20[0]) {
                  gi_504 = 1;
                  return;
               }
            }
         }
      }
      if (lda_20[0] < lda_20[1] && lda_20[1] < lda_20[2]) {
         if (lia_28[0] < 0 && lia_28[1] < 0 && lia_28[2] < 0) {
            if (lia_28[0] <= lia_28[1] && lia_28[1] <= lia_28[2]) {
               if (iHigh(Symbol(), g_timeframe_488, 1) < lda_20[0]) {
                  gi_504 = -1;
                  return;
               }
            }
         }
      }
      gi_504 = 0;
      return;
   }
   if (LockMode == 2) {
      if (lda_20[0] > lda_20[1] && lda_20[1] > lda_20[2]) {
         if (lia_28[0] > 0 && lia_28[1] > 0 && lia_28[2] > 0) {
            if (lia_28[0] >= lia_28[1] && lia_28[1] >= lia_28[2]) {
               if (iLow(Symbol(), g_timeframe_488, 1) > lda_20[0]) {
                  gi_504 = 1;
                  return;
               }
            }
         }
      }
      if (lda_20[0] < lda_20[1] && lda_20[1] < lda_20[2]) {
         if (lia_28[0] < 0 && lia_28[1] < 0 && lia_28[2] < 0) {
            if (lia_28[0] <= lia_28[1] && lia_28[1] <= lia_28[2]) {
               if (iHigh(Symbol(), g_timeframe_488, 1) < lda_20[0]) {
                  gi_504 = -1;
                  return;
               }
            }
         }
      }
      gi_504 = 0;
      return;
   }
   if (LockMode == 1) {
      if (lda_20[0] == lda_20[1] && lda_20[1] == lda_20[2]) {
         if (lia_28[0] == lia_28[1] && lia_28[1] == lia_28[2]) {
            if (lia_28[0] >= 0 && lia_28[1] >= 0 && lia_28[2] >= 0) {
               if (lia_28[0] <= F_T_treshold) {
                  if (iClose(Symbol(), g_timeframe_488, 1) < lda_24[0] && iClose(Symbol(), g_timeframe_488, 1) > lda_20[0]) {
                     gi_504 = 1;
                     return;
                  }
               }
            }
         }
         if (lia_28[0] == lia_28[1] && lia_28[1] == lia_28[2]) {
            if (lia_28[0] <= 0 && lia_28[1] <= 0 && lia_28[2] <= 0) {
               if (lia_28[0] >= -1 * F_T_treshold) {
                  if (iClose(Symbol(), g_timeframe_488, 1) > lda_20[0] && iClose(Symbol(), g_timeframe_488, 1) < lda_24[0]) {
                     gi_504 = -1;
                     return;
                  }
               }
            }
         }
      }
   }
   gi_504 = 0;
}

double AMA(int ai_0 = 2, int ai_4 = 30, int ai_8 = 9, int ai_12 = 25, int ai_16 = 300, int a_timeframe_20 = 0, int ai_24 = 1) {
   double lda_28[];
   double lda_32[];
   double ld_60;
   double ld_68;
   double ld_76;
   double ld_92;
   double ld_100;
   ArrayResize(lda_28, ai_16 + 1);
   ArrayResize(lda_32, ai_16 + 1);
   ArrayInitialize(lda_28, 0);
   ArrayInitialize(lda_32, 0);
   ArrayIsSeries(lda_28);
   ArrayIsSeries(lda_32);
   double ld_44 = 2.0 / (ai_4 + 1);
   double ld_52 = 2.0 / (ai_0 + 1) - ld_44;
   double ld_84 = iClose(Symbol(), a_timeframe_20, ai_16 - ai_8);
   lda_32[ai_16 - ai_8] = iClose(Symbol(), a_timeframe_20, ai_16 - ai_8 + 1);
   for (int li_36 = ai_16; li_36 >= 0; li_36--) {
      ld_60 = 0;
      for (int li_40 = li_36; li_40 <= li_36 + ai_8 - 1; li_40++) ld_60 += MathAbs(iClose(Symbol(), a_timeframe_20, li_40) - iClose(Symbol(), a_timeframe_20, li_40 + 1));
      if (ld_60 != 0.0) ld_68 = MathAbs(iClose(Symbol(), a_timeframe_20, li_36) - iClose(Symbol(), a_timeframe_20, li_36 + ai_8)) / ld_60;
      else ld_68 = 0;
      ld_76 = ld_68 * ld_52 + ld_44;
      ld_84 += NormalizeDouble(ld_76 * ld_76 * (iClose(Symbol(), a_timeframe_20, li_36) - ld_84), 4);
      lda_32[li_36] = ld_84;
      if (ai_12 < 1) {
         lda_28[li_36] = lda_32[li_36];
         if (li_36 != ai_24) continue;
         return (lda_28[li_36]);
      }
      for (li_40 = li_36; li_40 <= li_36 + ai_4 - 1; li_40++) ld_92 += MathAbs(lda_32[li_40] - (lda_32[li_40 + 1]));
      ld_100 = lda_32[li_36] - (lda_32[li_36 + 1]);
      if (ld_100 >= 0.0) {
         if (ld_100 < NormalizeDouble(ai_12 * ld_92 / (100 * ai_4), 4) && iHigh(Symbol(), a_timeframe_20, li_36) <= iHigh(Symbol(), a_timeframe_20, iHighest(NULL, 0, MODE_HIGH, 4, li_36)) +
            10.0 * Point) {
            lda_28[li_36] = lda_28[li_36 + 1];
            if (li_36 == ai_24) return (lda_28[li_36]);
         }
         lda_28[li_36] = lda_32[li_36];
         if (li_36 == ai_24) return (lda_28[li_36]);
      } else {
         if (MathAbs(ld_100) < NormalizeDouble(ai_12 * ld_92 / (100 * ai_4), 4) && iLow(Symbol(), a_timeframe_20, li_36) > iLow(Symbol(), a_timeframe_20, iLowest(NULL, 0, MODE_LOW, 4, li_36)) - 10.0 * Point) {
            lda_28[li_36] = lda_28[li_36 + 1];
            if (li_36 == ai_24) return (lda_28[li_36]);
         }
         lda_28[li_36] = lda_32[li_36];
         if (li_36 == ai_24) return (lda_28[li_36]);
      }
      ld_92 = 0.0;
   }
   return (lda_28[ai_24]);
}

void SetInfoRus(int ai_0 = 9, int ai_4 = 0, int ai_8 = 32768) {
   int li_12 = 0;
   int li_52 = ai_0 / 2;
   int li_16 = li_12 + li_52 + ai_0;
   int li_20 = li_16 + li_52 + ai_0;
   int li_24 = li_20 + li_52 + ai_0;
   int li_28 = li_24 + li_52 + ai_0;
   int li_32 = li_28 + li_52 + ai_0;
   int li_36 = li_32 + li_52 + ai_0;
   int li_40 = li_36 + li_52 + ai_0;
   int li_44 = li_40 + li_52 + ai_0;
   int li_48 = li_44 + li_52 + ai_0;
   SetLabel("LabRu0", "Lock&&ScalpingXR_v_2009_Rus ©", Yellow, 5, li_12, ai_4, ai_0);
   SetLabel("LabRu1", gs_336, ai_8, 5, li_16, ai_4, ai_0);
   SetLabel("LabRu2", gs_344, ai_8, 5, li_20, ai_4, ai_0);
   SetLabel("LabRu3", gs_352, ai_8, 5, li_24, ai_4, ai_0);
   SetLabel("LabRu4", gs_360, ai_8, 5, li_28, ai_4, ai_0);
   SetLabel("LabRu5", gs_368, ai_8, 5, li_32, ai_4, ai_0);
   SetLabel("LabRu6", gs_376, ai_8, 5, li_36, ai_4, ai_0);
   SetLabel("LabRu7", gs_384, ai_8, 5, li_40, ai_4, ai_0);
   SetLabel("LabRu8", gs_320, gi_448, 5, li_44, ai_4, ai_0);
   SetLabel("LabRu9", g_str_concat_456, gi_448, 5, li_48, ai_4, ai_0);
}

void SetInfoEngl(int ai_0 = 9, int ai_4 = 0, int ai_8 = 32768) {
   int li_12 = 0;
   int li_52 = ai_0 / 2;
   int li_16 = li_12 + li_52 + ai_0;
   int li_20 = li_16 + li_52 + ai_0;
   int li_24 = li_20 + li_52 + ai_0;
   int li_28 = li_24 + li_52 + ai_0;
   int li_32 = li_28 + li_52 + ai_0;
   int li_36 = li_32 + li_52 + ai_0;
   int li_40 = li_36 + li_52 + ai_0;
   int li_44 = li_40 + li_52 + ai_0;
   int li_48 = li_44 + li_52 + ai_0;
   SetLabel("LabRu0", "Lock&&ScalpingXR_v_2009_Rus ©", Yellow, 5, li_12, ai_4, ai_0);
   SetLabel("LabRu1", gs_392, ai_8, 5, li_16, ai_4, ai_0);
   SetLabel("LabRu2", gs_400, ai_8, 5, li_20, ai_4, ai_0);
   SetLabel("LabRu3", gs_408, ai_8, 5, li_24, ai_4, ai_0);
   SetLabel("LabRu4", gs_416, ai_8, 5, li_28, ai_4, ai_0);
   SetLabel("LabRu5", gs_424, ai_8, 5, li_32, ai_4, ai_0);
   SetLabel("LabRu6", gs_432, ai_8, 5, li_36, ai_4, ai_0);
   SetLabel("LabRu7", gs_440, ai_8, 5, li_40, ai_4, ai_0);
   SetLabel("LabRu8", gs_328, gi_448, 5, li_44, ai_4, ai_0);
   SetLabel("LabRu9", g_str_concat_464, gi_448, 5, li_48, ai_4, ai_0);
}

void Info() {
   gs_336 = "Всего средств                          =" + DoubleToStr(AccountBalance() + GrossProfit(), 1);
   gs_344 = "Свободных средсв                  =" + DoubleToStr(AccountFreeMargin(), 1);
   gs_352 = "Текущая прибыль                     =" + DoubleToStr(GrossProfit(), 1);
   gs_360 = "Прибыль с начала торговли   =" + DoubleToStr(AccountBalance() - gd_312, 1);
   gs_368 = "Максимально ордеров            =" + DoubleToStr(gi_452, 0) + " Шт";
   gs_376 = "Открытых ордеров                  =" + CountOpOrd("0") + "Шт.";
   gs_384 = "Риск                                            =" + DoubleToStr(Risk, 1) + " %";
   gs_392 = "Account balanse                               =" + DoubleToStr(AccountBalance() + GrossProfit(), 1);
   gs_400 = "Account free margine                       =" + DoubleToStr(AccountFreeMargin(), 1);
   gs_408 = "current profit                                       =" + DoubleToStr(GrossProfit(), 1);
   gs_416 = "profit from the start of trading           =" + DoubleToStr(AccountBalance() - gd_312, 1);
   gs_424 = "Orders for open maximum               =" + DoubleToStr(gi_452, 0) + " Ps";
   gs_432 = "Count open orders                            =" + CountOpOrd("0") + "Ps.";
   gs_440 = "Risk                                                     = " + DoubleToStr(Risk, 1) + " %";
}

bool EndTradeForDateTime() {
   if (EndTradeData == "") {
      gs_320 = "Дата конца торговли не определена ";
      gs_328 = "Date of the end of trading is not defined ";
      gi_448 = 3329330;
      return (FALSE);
   }
   int l_str2time_0 = StrToTime(TimeToStr(TimeCurrent(), TIME_DATE));
   int l_str2time_4 = StrToTime(EndTradeData);
   if (l_str2time_0 >= l_str2time_4) {
      gs_320 = "Торговля остановленна " + EndTradeData;
      gs_328 = "Trading stopped " + EndTradeData;
      gi_448 = 255;
      return (TRUE);
   }
   gs_320 = "Торговля будет остановленна " + EndTradeData;
   gs_328 = "Trading will be stopped " + EndTradeData;
   gi_448 = 3329330;
   return (FALSE);
}

double NullDot() {
   double ld_ret_0 = 0;
   int li_8 = 0;
   int li_12 = 0;
   if (OrdersTotal() < 1) return (Bid);
   double l_minlot_16 = MarketInfo(Symbol(), MODE_MINLOT);
   for (int l_pos_24 = 0; l_pos_24 < OrdersTotal(); l_pos_24++) {
      if (OrderSelect(l_pos_24, SELECT_BY_POS, MODE_TRADES)) {
         if (OrderSymbol() == Symbol()) {
            li_12 = OrderLots() / l_minlot_16;
            li_8 += li_12;
            ld_ret_0 += OrderOpenPrice() * li_12;
         }
      }
   }
   ld_ret_0 /= li_8;
   return (ld_ret_0);
}

void SetArrow(string as_0, int ai_8, int a_width_12, int ai_16, double ad_20, color a_color_28) {
   string l_str_concat_32 = StringConcatenate(as_0, TimeToStr(TimeLocal(), TIME_DATE|TIME_SECONDS));
   ObjectCreate(l_str_concat_32, OBJ_ARROW, 0, 0, 0, 0, 0, 0, 0);
   ObjectSet(l_str_concat_32, OBJPROP_ARROWCODE, ai_8);
   ObjectSet(l_str_concat_32, OBJPROP_STYLE, STYLE_DASHDOT);
   ObjectSet(l_str_concat_32, OBJPROP_TIME1, Time[ai_16]);
   ObjectSet(l_str_concat_32, OBJPROP_PRICE1, ad_20);
   ObjectSet(l_str_concat_32, OBJPROP_WIDTH, a_width_12);
   ObjectSet(l_str_concat_32, OBJPROP_COLOR, a_color_28);
}

bool TradeTime() {
   int li_0 = StartTrade;
   int li_4 = EndTrade;
   if (!TradeOfTime) {
      if (DayOfWeek() == 6 || DayOfWeek() == 0) {
         g_str_concat_464 = "Start trade in  01:00 am";
         g_str_concat_456 = "Начало торговли в понедельник в 01:00";
         return (FALSE);
      }
      return (TRUE);
   }
   if (DayOfWeek() == 6 || DayOfWeek() == 0) {
      g_str_concat_464 = "Start trade in  01:00 am";
      g_str_concat_456 = "Начало торговли в понедельник в 01:00";
      return (FALSE);
   }
   if (li_0 < li_4) {
      if (TimeHour(TimeCurrent()) >= li_0 && TimeHour(TimeCurrent()) < li_4 || CountOpOrd("0") > 0) {
         g_str_concat_464 = StringConcatenate("By the end of trading session :", DoubleToStr(li_4 - TimeHour(TimeCurrent()), 0), " hours");
         g_str_concat_456 = StringConcatenate("До конца торговой сессии :", DoubleToStr(li_4 - TimeHour(TimeCurrent()), 0), " часов");
         return (TRUE);
      }
   }
   if (li_0 > li_4) {
      if (TimeHour(TimeCurrent()) >= li_0 || TimeHour(TimeCurrent()) < li_4 || CountOpOrd("0") > 0) {
         g_str_concat_464 = StringConcatenate("By the end of trading session :", DoubleToStr(li_4 - TimeHour(TimeCurrent()), 0), " hours");
         g_str_concat_456 = StringConcatenate("До конца торговой сессии :", DoubleToStr(li_4 - TimeHour(TimeCurrent()), 0), " часов");
         return (TRUE);
      }
   }
   g_str_concat_464 = StringConcatenate("resting to : ", li_0, ": 00");
   g_str_concat_456 = StringConcatenate("отдыхаем до : ", li_0, ": 00");
   return (FALSE);
}

int PriseChanelWide() {
   double l_ord_open_price_0 = 100000;
   double l_ord_open_price_8 = 0;
   for (int l_pos_16 = 0; l_pos_16 < OrdersTotal(); l_pos_16++) {
      if (OrderSelect(l_pos_16, SELECT_BY_POS, MODE_TRADES)) {
         if (OrderSymbol() == Symbol()) {
            if (OrderMagicNumber() == MagicFirstOrder || OrderMagicNumber() == Magik) {
               if (l_ord_open_price_0 > OrderOpenPrice()) l_ord_open_price_0 = OrderOpenPrice();
               if (l_ord_open_price_8 < OrderOpenPrice()) l_ord_open_price_8 = OrderOpenPrice();
            }
         }
      }
   }
   int li_ret_20 = MathRound((l_ord_open_price_8 - l_ord_open_price_0) / Point / gi_492);
   if (li_ret_20 < 1) li_ret_20 = 1;
   return (li_ret_20);
}

int MidleOrders(int ai_0 = 10) {
   int l_cmd_20;
   int l_cmd_24;
   int l_magic_28;
   int l_magic_32;
   if (OrdersTotal() < 2) return (-1);
   double ld_4 = 100000;
   double ld_12 = 1000000;
   for (int l_pos_36 = 0; l_pos_36 < OrdersTotal(); l_pos_36++) {
      if (OrderSelect(l_pos_36, SELECT_BY_POS, MODE_TRADES)) {
         if (OrderSymbol() == Symbol()) {
            if (OrderOpenPrice() - Bid > 0.0) {
               if (OrderOpenPrice() - Bid < ld_4) {
                  ld_4 = OrderOpenPrice() - Bid;
                  l_cmd_20 = OrderType();
                  l_magic_28 = OrderMagicNumber();
               }
            }
         }
      }
   }
   for (l_pos_36 = 0; l_pos_36 < OrdersTotal(); l_pos_36++) {
      if (OrderSelect(l_pos_36, SELECT_BY_POS, MODE_TRADES)) {
         if (OrderSymbol() == Symbol()) {
            if (Bid - OrderOpenPrice() > 0.0) {
               if (Bid - OrderOpenPrice() < ld_12) {
                  ld_12 = Bid - OrderOpenPrice();
                  l_cmd_24 = OrderType();
                  l_magic_32 = OrderMagicNumber();
               }
            }
         }
      }
   }
   ld_4 = NormalizeDouble(ld_4 / Point, Digits);
   ld_12 = NormalizeDouble(ld_12 / Point, Digits);
   if (MathAbs(ld_4 + ld_12) < ai_0) return (-1);
   if (l_cmd_20 == l_cmd_24) {
      if (MathAbs(ld_4 - ld_12) < 3.0) {
         if (l_magic_28 == MagicFirstOrder && l_magic_32 == MagicFirstOrder) return (-1);
         if (l_cmd_20 == OP_BUY) return (0);
         if (l_cmd_20 == OP_SELL) return (1);
      }
   } else {
      if (LockMode == 1) {
      }
   }
   return (-1);
}

double GrossProfit(int ai_0 = 0) {
   double ld_ret_4 = 0;
   if (OrdersTotal() < 1) return (-1);
   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 (IsOrder("", MagicFirstOrder) || IsOrder("", Magik)) {
            if (ai_0 == 0) ld_ret_4 = ld_ret_4 + OrderProfit() + OrderSwap() + OrderCommission();
            if (ai_0 > 0)
               if (OrderProfit() > 0.0) ld_ret_4 = ld_ret_4 + OrderProfit() + OrderSwap() + OrderCommission();
            if (ai_0 < 0)
               if (OrderProfit() < 0.0) ld_ret_4 = ld_ret_4 + OrderProfit() + OrderSwap() + OrderCommission();
         }
      }
   }
   return (ld_ret_4);
}

int NearOrdDistanse(int ai_0 = 20) {
   double l_ord_open_price_4 = 1000000;
   if (CountOpOrd("0") < 2) return (0);
   for (int l_pos_12 = 0; l_pos_12 < OrdersTotal(); l_pos_12++) {
      if (OrderSelect(l_pos_12, SELECT_BY_POS, MODE_TRADES)) {
         if (OrderSymbol() == Symbol()) {
            if (OrderMagicNumber() == MagicFirstOrder || OrderMagicNumber() == Magik)
               if (MathAbs(Bid - OrderOpenPrice()) < l_ord_open_price_4) l_ord_open_price_4 = OrderOpenPrice();
         }
      }
   }
   if (MathAbs(Bid - l_ord_open_price_4) > ai_0 * Point) {
      if (Bid - l_ord_open_price_4 > 0.0) return (-1);
      if (Bid - l_ord_open_price_4 < 0.0) return (1);
   }
   return (0);
}

bool PriseDist(int ai_0 = 20, int ai_4 = 10) {
   double ld_unused_8 = 100000;
   for (int l_pos_16 = 0; l_pos_16 < OrdersTotal(); l_pos_16++) {
      if (OrderSelect(l_pos_16, SELECT_BY_POS, MODE_TRADES)) {
         if (OrderSymbol() == Symbol()) {
            if (OrderMagicNumber() == MagicFirstOrder || OrderMagicNumber() == Magik) {
               if (MathAbs(Bid - OrderOpenPrice()) < ai_0 * Point)
                  if (OrderOpenTime() > iTime(Symbol(), g_timeframe_488, ai_4) * OrdersTotal()) return (FALSE);
            }
         }
      }
   }
   return (TRUE);
}

bool OutOfPrise() {
   double l_ord_open_price_0 = 100000;
   double l_ord_open_price_8 = 0;
   if (OrdersTotal() < 2) return (TRUE);
   for (int l_pos_16 = 0; l_pos_16 < OrdersTotal(); l_pos_16++) {
      if (OrderSelect(l_pos_16, SELECT_BY_POS, MODE_TRADES)) {
         if (OrderSymbol() == Symbol()) {
            if (OrderMagicNumber() == MagicFirstOrder || OrderMagicNumber() == Magik) {
               if (l_ord_open_price_0 > OrderOpenPrice()) l_ord_open_price_0 = OrderOpenPrice();
               if (l_ord_open_price_8 < OrderOpenPrice()) l_ord_open_price_8 = OrderOpenPrice();
            }
         }
      }
   }
   if (Bid > l_ord_open_price_0 && Bid < l_ord_open_price_8) return (FALSE);
   return (TRUE);
}

double OrdLot() {
   return (OrderLots());
}

int IfOrdstoploss() {
   if (OrderType() == OP_BUY)
      if (OrderOpenPrice() - Bid >= gi_492 * Point) return (-1);
   if (OrderType() == OP_SELL)
      if (Ask - OrderOpenPrice() >= gi_492 * Point) return (1);
   return (0);
}

int CountOpOrd(string as_0 = "", int ai_8 = -1, int ai_12 = -1) {
   int l_count_16 = 0;
   for (int l_pos_20 = 0; l_pos_20 < OrdersTotal(); l_pos_20++) {
      if (OrderSelect(l_pos_20, SELECT_BY_POS, MODE_TRADES))
         if (IsOrder(as_0, ai_8, ai_12)) l_count_16++;
   }
   return (l_count_16);
}

bool IsOrder(string as_0 = "", int a_magic_8 = -1, int a_cmd_12 = -1) {
   if (as_0 == "0" || as_0 == "") as_0 = Symbol();
   if (OrderSymbol() == as_0) {
      if (a_magic_8 == -1 || OrderMagicNumber() == a_magic_8) {
         if (a_cmd_12 == -1 || OrderType() == a_cmd_12)
            if (OrderCloseTime() == 0) return (TRUE);
      }
   }
   return (FALSE);
}

double CalcLotsAuto() {
   int li_100;
   if (IsTesting() || IsOptimization())
      if (TestInfo) return (0.1);
   RefreshRates();
   double ld_24 = GlobalVariableGet("multi") - GlobalVariableGet("disable");
   double ld_32 = GlobalVariableGet("sredstva");
   double ld_40 = MarketInfo(Symbol(), MODE_MARGINREQUIRED) / 1000.0;
   double l_free_magrin_48 = AccountFreeMargin();
   double ld_56 = AccountBalance();
   double l_minlot_64 = MarketInfo(Symbol(), MODE_MINLOT);
   double l_maxlot_72 = MarketInfo(Symbol(), MODE_MAXLOT);
   double l_lotstep_80 = MarketInfo(Symbol(), MODE_LOTSTEP);
   double ld_unused_88 = AccountStopoutLevel();
   int li_unused_96 = 1000.0 * l_minlot_64;
   if (ld_32 == 0.0) {
      ld_32 = ld_56;
      GlobalVariableSet("sredstva", ld_56);
   }
   if (GlobalVariableGet("multi") > 1.0) Risk = GlobalVariableGet("Risc");
   if (Risk < 0.0) Risk = 0;
   if (Risk > 100.0) Risk = 100;
   if (l_lotstep_80 == 0.01) li_100 = 2;
   else li_100 = 1;
   if (ld_24 < 1.0) ld_24 = 1;
   double ld_16 = ld_56 * (Risk / 100.0) / ld_24;
   double ld_ret_0 = NormalizeDouble(ld_16 / 1000.0, li_100);
   if (ld_ret_0 > l_maxlot_72) ld_ret_0 = l_maxlot_72;
   if (ld_ret_0 < l_minlot_64) ld_ret_0 = l_minlot_64;
   return (ld_ret_0);
}

void CloseBy() {
   double ld_0;
   double ld_8;
   double l_ord_open_price_16;
   double l_ord_open_price_24;
   double l_ord_profit_32;
   double l_ord_profit_40;
   double l_ord_open_price_48;
   double l_ord_open_price_56;
   int l_ticket_68;
   int l_ticket_72;
   int l_cmd_76;
   int l_cmd_80;
   if (CountOpOrd("0") >= 3) {
      if (OutOfPrise() == 0) {
         ld_0 = 100000;
         ld_8 = 100000;
         l_ord_open_price_16 = 1000000;
         l_ord_open_price_24 = 0;
         for (int l_pos_64 = 0; l_pos_64 < OrdersTotal(); l_pos_64++) {
            if (OrderSelect(l_pos_64, SELECT_BY_POS, MODE_TRADES)) {
               if (IsOrder("", MagicFirstOrder) || IsOrder("", Magik)) {
                  if (OrderOpenPrice() - Bid > 0.0 && OrderOpenPrice() - Bid < ld_0) {
                     ld_0 = OrderOpenPrice() - Bid;
                     l_ticket_68 = OrderTicket();
                     l_cmd_76 = OrderType();
                     l_ord_profit_32 = OrderProfit();
                     l_ord_open_price_48 = OrderOpenPrice();
                  }
                  if (Bid - OrderOpenPrice() > 0.0 && Bid - OrderOpenPrice() < ld_8) {
                     ld_8 = Bid - OrderOpenPrice();
                     l_ticket_72 = OrderTicket();
                     l_cmd_80 = OrderType();
                     l_ord_profit_40 = OrderProfit();
                     l_ord_open_price_56 = OrderOpenPrice();
                  }
               }
            }
         }
         for (l_pos_64 = 0; l_pos_64 < OrdersTotal(); l_pos_64++) {
            if (OrderSelect(l_pos_64, SELECT_BY_POS, MODE_TRADES)) {
               if (OrderSymbol() == Symbol()) {
                  if (OrderMagicNumber() == MagicFirstOrder || OrderMagicNumber() == Magik) {
                     if (l_ord_open_price_16 > OrderOpenPrice()) l_ord_open_price_16 = OrderOpenPrice();
                     if (l_ord_open_price_24 < OrderOpenPrice()) l_ord_open_price_24 = OrderOpenPrice();
                  }
               }
            }
         }
         l_ord_open_price_48 = NormalizeDouble(l_ord_open_price_48, Digits);
         l_ord_open_price_56 = NormalizeDouble(l_ord_open_price_56, Digits);
         l_ord_open_price_16 = NormalizeDouble(l_ord_open_price_16, Digits);
         l_ord_open_price_24 = NormalizeDouble(l_ord_open_price_24, Digits);
         l_ord_profit_32 = NormalizeDouble(l_ord_profit_32, 2);
         l_ord_profit_40 = NormalizeDouble(l_ord_profit_40, 2);
         if (l_cmd_76 == l_cmd_80) {
            if (l_ord_profit_32 - l_ord_profit_40 == 0.0) {
               del(l_ticket_68);
               del(l_ticket_72);
            }
         }
      }
   }
}

void DelBigestLoss() {
   double ld_0;
   int li_8;
   double lda_12[50][2];
   double lda_16[50][2];
   double lda_20[50][2];
   int li_32;
   int li_36;
   int li_44;
   int li_48;
   double ld_52;
   bool li_60;
   int li_68;
   if (OrdersTotal() >= 2) {
      li_32 = 0;
      li_36 = 0;
      for (int l_pos_40 = OrdersTotal() + 1; l_pos_40 >= 0; l_pos_40--) {
         if (OrderSelect(l_pos_40, SELECT_BY_POS, MODE_TRADES)) {
            if (IsOrder("", MagicFirstOrder) || IsOrder("", Magik)) {
               if (OrderProfit() < 0.0) {
                  lda_16[li_36][0] = OrderProfit();
                  lda_16[li_36][1] = OrderTicket();
                  li_36++;
               }
               if (OrderProfit() > 0.0) {
                  lda_12[li_32][0] = OrderProfit();
                  lda_12[li_32][1] = OrderTicket();
                  li_32++;
               }
            }
         }
      }
      li_32--;
      li_36--;
      if (li_32 >= 1) {
         if (li_36 >= 1) {
            ArrayResize(lda_12, li_32);
            ArrayResize(lda_20, li_32);
            for (l_pos_40 = 0; l_pos_40 < li_36; l_pos_40++) {
               lda_20[l_pos_40][0] = 0;
               lda_20[l_pos_40][1] = 0;
            }
            for (l_pos_40 = 0; l_pos_40 < li_32; l_pos_40++) {
               li_44 = ArrayMaximum(lda_12);
               lda_20[l_pos_40][0] = lda_12[li_44][0];
               lda_20[l_pos_40][1] = lda_12[li_44][1];
               lda_12[li_44][0] = 0;
               lda_12[li_44][1] = 0;
            }
            for (l_pos_40 = 0; l_pos_40 < li_36; l_pos_40++) {
               lda_12[l_pos_40][0] = 0;
               lda_12[l_pos_40][1] = 0;
            }
            for (l_pos_40 = 0; l_pos_40 < li_32; l_pos_40++) {
               lda_12[l_pos_40][0] = lda_20[l_pos_40][0];
               lda_12[l_pos_40][1] = lda_20[l_pos_40][1];
            }
            li_44 = 0;
            ArrayResize(lda_20, li_36);
            ArrayResize(lda_16, li_36);
            for (l_pos_40 = 0; l_pos_40 < li_36; l_pos_40++) {
               lda_20[l_pos_40][0] = 0;
               lda_20[l_pos_40][1] = 0;
            }
            for (l_pos_40 = 0; l_pos_40 < li_36; l_pos_40++) {
               li_44 = ArrayMinimum(lda_16);
               lda_20[l_pos_40][0] = lda_16[li_44][0];
               lda_20[l_pos_40][1] = lda_16[li_44][1];
               lda_16[li_44][0] = 0;
               lda_16[li_44][1] = 0;
            }
            for (l_pos_40 = 0; l_pos_40 < li_36; l_pos_40++) {
               lda_16[l_pos_40][0] = 0;
               lda_16[l_pos_40][1] = 0;
            }
            for (l_pos_40 = 0; l_pos_40 < li_36; l_pos_40++) {
               lda_16[l_pos_40][0] = lda_20[l_pos_40][0];
               lda_16[l_pos_40][1] = lda_20[l_pos_40][1];
            }
            li_60 = FALSE;
            for (int l_index_64 = 0; l_index_64 < li_36 / 2; l_index_64++) {
               if (lda_16[l_index_64][0] < 0.0) {
                  ld_0 = lda_16[l_index_64][0];
                  li_8 = lda_16[l_index_64][1];
               }
               l_pos_40 = 0;
               if (l_pos_40 < li_32) {
                  ld_52 += lda_12[l_pos_40][0];
                  li_48++;
                  if (NormalizeDouble(ld_52, 2) + NormalizeDouble(ld_0, 2) >= 0.0 && NormalizeDouble(ld_52, 2) + NormalizeDouble(ld_0, 2) < 1.5) li_60 = TRUE;
               }
            }
            if (li_60) {
               del(li_8);
               for (l_pos_40 = 0; l_pos_40 < li_48; l_pos_40++) {
                  li_68 = lda_12[l_pos_40][1];
                  del(li_68);
               }
            }
         }
      }
   }
}

bool NevBar() {
   if (g_time_608 == Time[0]) return (FALSE);
   g_time_608 = Time[0];
   return (TRUE);
}

void CloseAll() {
   while (OrdersTotal() > 0) {
      for (int l_pos_0 = OrdersTotal() + 1; l_pos_0 >= 0; l_pos_0--)
         if (OrderSelect(l_pos_0, SELECT_BY_POS, MODE_TRADES)) del(OrderTicket());
   }
}

void del(int a_ticket_0) {
   int l_error_4;
   string l_symbol_12;
   double l_bid_20;
   for (int l_count_8 = 0; l_count_8 < 1; l_count_8++) {
      GetLastError();
      OrderSelect(a_ticket_0, SELECT_BY_TICKET, MODE_TRADES);
      l_symbol_12 = OrderSymbol();
      if (OrderType() == OP_BUY) {
         RefreshRates();
         l_bid_20 = MarketInfo(l_symbol_12, MODE_BID);
         if (!OrderClose(a_ticket_0, OrderLots(), l_bid_20, 3, Green)) l_error_4 = GetLastError();
      }
      if (OrderType() == OP_SELL) {
         RefreshRates();
         l_bid_20 = MarketInfo(l_symbol_12, MODE_ASK);
         if (!OrderClose(a_ticket_0, OrderLots(), l_bid_20, 3, Green)) l_error_4 = GetLastError();
      }
      if (l_error_4 == 0/* NO_ERROR */) {
         PlaySound("expert.wav");
         return;
      }
      if (l_error_4 != 0/* NO_ERROR */) {
         PlaySound("timeout.wav");
         Print("Error for Close Funtion =", l_error_4);
      }
      while (!IsTradeAllowed()) Sleep(5000);
      if (l_error_4 == 146/* TRADE_CONTEXT_BUSY */) while (IsTradeContextBusy()) Sleep(11000);
   }
}

void SetLabel(string a_name_0, string a_text_8, color a_color_16, int a_x_20, int a_y_24, int a_corner_28 = 0, int a_fontsize_32 = 9) {
   if (ObjectFind(a_name_0) < 0) ObjectCreate(a_name_0, OBJ_LABEL, 0, 0, 0);
   ObjectSetText(a_name_0, a_text_8, a_fontsize_32);
   ObjectSet(a_name_0, OBJPROP_COLOR, a_color_16);
   ObjectSet(a_name_0, OBJPROP_XDISTANCE, a_x_20);
   ObjectSet(a_name_0, OBJPROP_YDISTANCE, a_y_24);
   ObjectSet(a_name_0, OBJPROP_CORNER, a_corner_28);
   ObjectSet(a_name_0, OBJPROP_FONTSIZE, a_fontsize_32);
}

void Message(string as_0) {
   if (StringLen(as_0) > 0) Print(as_0);
}

void ModifyOrder(double a_ord_open_price_0 = -1.0, double a_ord_stoploss_8 = 0.0, double a_ord_takeprofit_16 = 0.0, int a_datetime_24 = 0) {
   bool l_bool_28;
   double l_ask_44;
   double l_bid_52;
   int l_error_80;
   color l_color_32 = White;
   int li_76 = MarketInfo(OrderSymbol(), MODE_DIGITS);
   if (a_ord_open_price_0 <= 0.0) a_ord_open_price_0 = OrderOpenPrice();
   if (a_ord_stoploss_8 < 0.0) a_ord_stoploss_8 = OrderStopLoss();
   if (a_ord_takeprofit_16 < 0.0) a_ord_takeprofit_16 = OrderTakeProfit();
   a_ord_open_price_0 = NormalizeDouble(a_ord_open_price_0, li_76);
   a_ord_stoploss_8 = NormalizeDouble(a_ord_stoploss_8, li_76);
   a_ord_takeprofit_16 = NormalizeDouble(a_ord_takeprofit_16, li_76);
   double ld_36 = NormalizeDouble(OrderOpenPrice(), li_76);
   double ld_60 = NormalizeDouble(OrderStopLoss(), li_76);
   double ld_68 = NormalizeDouble(OrderTakeProfit(), li_76);
   if (a_ord_open_price_0 == ld_36 && a_ord_stoploss_8 == ld_60 && a_ord_takeprofit_16 == ld_68) return;
   if (a_ord_stoploss_8 == ld_60 && ld_60 != 0.0 || a_ord_stoploss_8 != 0.0 && a_ord_takeprofit_16 == ld_68 && ld_68 != 0.0 || a_ord_takeprofit_16 != 0.0) return;
   for (int li_84 = 1; li_84 <= gi_304; li_84++) {
      while (!IsTradeAllowed()) Sleep(5000);
      RefreshRates();
      l_bool_28 = OrderModify(OrderTicket(), a_ord_open_price_0, a_ord_stoploss_8, a_ord_takeprofit_16, a_datetime_24, l_color_32);
      if (l_bool_28) {
         if (!(UseSound)) break;
         PlaySound(gs_276);
         return;
      }
      l_error_80 = GetLastError();
      l_ask_44 = MarketInfo(OrderSymbol(), MODE_ASK);
      l_bid_52 = MarketInfo(OrderSymbol(), MODE_BID);
      if (gi_272) {
         Print("Error(", l_error_80, ") modifying order: ", ErrorDescription(l_error_80), ", try ", li_84);
         Print("Ask=", l_ask_44, "  Bid=", l_bid_52, "  sy=", OrderSymbol(), "  op=" + GetNameOP(OrderType()), "  pp=", a_ord_open_price_0, "  sl=", a_ord_stoploss_8, "  tp=", a_ord_takeprofit_16);
      }
      Sleep(10000);
   }
}

bool ExistPositions(string as_0 = "", int a_cmd_8 = -1, int a_magic_12 = -1, int ai_16 = 0) {
   int l_ord_total_24 = OrdersTotal();
   if (as_0 == "0") as_0 = Symbol();
   for (int l_pos_20 = 0; l_pos_20 < l_ord_total_24; l_pos_20++) {
      if (OrderSelect(l_pos_20, SELECT_BY_POS, MODE_TRADES)) {
         if (OrderSymbol() == as_0 || as_0 == "") {
            if (OrderType() == OP_BUY || OrderType() == OP_SELL) {
               if (a_cmd_8 < OP_BUY || OrderType() == a_cmd_8) {
                  if (a_magic_12 < 0 || OrderMagicNumber() == a_magic_12)
                     if (ai_16 <= OrderOpenTime()) return (TRUE);
               }
            }
         }
      }
   }
   return (FALSE);
}

string GetNameOP(int ai_0) {
   switch (ai_0) {
   case 0:
      return ("Buy");
   case 1:
      return ("Sell");
   case 2:
      return ("BuyLimit");
   case 3:
      return ("SellLimit");
   case 4:
      return ("BuyStop");
   case 5:
      return ("SellStop");
   }
   return ("Unknown Operation");
}

void OpenPosition(string a_symbol_0, int a_cmd_8, double a_lots_12, double a_pips_20 = 0.0, double ad_28 = 0.0, int a_magic_36 = 0, string a_comment_40 = "") {
   color l_color_48;
   int l_datetime_52;
   double l_price_56;
   double l_ask_64;
   double l_bid_72;
   double l_price_80;
   double l_price_88;
   int l_digits_96;
   int l_error_100;
   int l_ticket_108 = 0;
   string ls_unused_112 = "";
   if (a_symbol_0 == "" || a_symbol_0 == "0") a_symbol_0 = Symbol();
   int l_stoplevel_120 = MarketInfo(a_symbol_0, MODE_STOPLEVEL);
   int l_spread_124 = MarketInfo(a_symbol_0, MODE_SPREAD);
   if (ad_28 != 0.0 && ad_28 < l_stoplevel_120 + l_spread_124) ad_28 = l_stoplevel_120;
   if (a_cmd_8 < OP_SELL) {
      if (a_pips_20 > 0.0) l_price_88 = MarketInfo(Symbol(), MODE_ASK) - a_pips_20 * Point;
      else l_price_88 = 0;
      if (ad_28 > 0.0) l_price_80 = MarketInfo(Symbol(), MODE_ASK) + ad_28 * Point;
      else l_price_80 = 0;
   } else {
      if (a_pips_20 > 0.0) l_price_88 = MarketInfo(Symbol(), MODE_BID) + a_pips_20 * Point;
      else l_price_88 = 0;
      if (ad_28 > 0.0) l_price_80 = MarketInfo(Symbol(), MODE_BID) - ad_28 * Point;
      else l_price_80 = 0;
   }
   if (a_cmd_8 == OP_BUY) l_color_48 = gi_284;
   else l_color_48 = gi_288;
   for (int li_104 = 1; li_104 <= gi_304; li_104++) {
      if (IsStopped()) {
         Print("OpenPosition(): Остановка работы функции");
         break;
      }
      while (!IsTradeAllowed()) Sleep(5000);
      RefreshRates();
      l_digits_96 = MarketInfo(a_symbol_0, MODE_DIGITS);
      l_ask_64 = MarketInfo(a_symbol_0, MODE_ASK);
      l_bid_72 = MarketInfo(a_symbol_0, MODE_BID);
      if (a_cmd_8 == OP_BUY) l_price_56 = l_ask_64;
      else l_price_56 = l_bid_72;
      l_price_56 = NormalizeDouble(l_price_56, l_digits_96);
      l_datetime_52 = TimeCurrent();
      if (gi_292) l_ticket_108 = OrderSend(a_symbol_0, a_cmd_8, a_lots_12, l_price_56, g_slippage_300, 0, 0, a_comment_40, a_magic_36, 0, l_color_48);
      else l_ticket_108 = OrderSend(a_symbol_0, a_cmd_8, a_lots_12, l_price_56, g_slippage_300, l_price_88, l_price_80, a_comment_40, a_magic_36, 0, l_color_48);
      if (l_ticket_108 > 0) {
         if (!(UseSound)) break;
         PlaySound(gs_276);
         break;
      }
      l_error_100 = GetLastError();
      if (l_error_100 == 148) {
         gi_452 = OrdersTotal() - 1;
         return;
      }
      if (l_error_100 == 134/* NOT_ENOUGH_MONEY */) {
         gi_452 = OrdersTotal() - 1;
         return;
      }
      if (l_ask_64 == 0.0 && l_bid_72 == 0.0) Message("Проверьте в Обзоре рынка наличие символа " + a_symbol_0);
      if (gi_272) {
         Print("Error(", l_error_100, ") opening position: ", ErrorDescription(l_error_100), ", try ", li_104);
         Print("Ask=", l_ask_64, " Bid=", l_bid_72, " sy=", a_symbol_0, " ll=", a_lots_12, " op=", GetNameOP(a_cmd_8), " pp=", l_price_56, " sl=", l_price_88, " tp=", l_price_80, " mn=", a_magic_36);
      }
      if (l_error_100 == 2/* COMMON_ERROR */ || l_error_100 == 64/* ACCOUNT_DISABLED */ || l_error_100 == 65/* INVALID_ACCOUNT */ || l_error_100 == 133/* TRADE_DISABLED */) {
         gi_unused_296 = TRUE;
         break;
      }
      if (l_error_100 == 4/* SERVER_BUSY */ || l_error_100 == 131/* INVALID_TRADE_VOLUME */ || l_error_100 == 132/* MARKET_CLOSED */) {
         Sleep(300000);
         break;
      }
      if (l_error_100 == 128/* TRADE_TIMEOUT */ || l_error_100 == 142 || l_error_100 == 143) {
         Sleep(66666.0);
         if (ExistPositions(a_symbol_0, a_cmd_8, a_magic_36, l_datetime_52)) {
            if (!(UseSound)) break;
            PlaySound(gs_276);
            break;
         }
      }
      if (l_error_100 == 140/* LONG_POSITIONS_ONLY_ALLOWED */ || l_error_100 == 148 || l_error_100 == 4110/* LONGS__NOT_ALLOWED */ || l_error_100 == 4111/* SHORTS_NOT_ALLOWED */) break;
      if (l_error_100 == 141/* TOO_MANY_REQUESTS */) Sleep(100000);
      if (l_error_100 == 145/* TRADE_MODIFY_DENIED */) Sleep(17000);
      if (l_error_100 == 146/* TRADE_CONTEXT_BUSY */) while (IsTradeContextBusy()) Sleep(11000);
      if (l_error_100 != 135/* PRICE_CHANGED */) Sleep(7700.0);
   }
   if (gi_292 && l_ticket_108 > 0 && l_price_88 > 0.0 || l_price_80 > 0.0)
      if (OrderSelect(l_ticket_108, SELECT_BY_TICKET)) ModifyOrder(-1, l_price_88, l_price_80);
}
