/*
   Generated by EX4-TO-MQ4 decompiler V4.0.224.1 []
   Website: 
   E-mail : 
*/  
#property copyright "© "
#property link      "v"

#include <WinUser32.mqh>

extern int Key = 0;
extern double Lots = 1.0;
double gd_88;
double gd_96 = 0.0;
double gd_104 = 24.0;
double gd_112 = 0.0;
double gd_120 = 24.0;
double g_pips_128 = 35.0;
extern string tx1="Stoploss";
extern double g_pips_136  = 1000.0;
bool gi_144 = FALSE;
bool gi_148 = FALSE;
double gd_152 = 10.0;
bool gi_160 = TRUE;
bool gi_164 = TRUE;
bool gi_168 = TRUE;
bool gi_172 = TRUE;
bool gi_176 = TRUE;
bool gi_180 = TRUE;
bool gi_184 = TRUE;
int g_magic_188 = 1841;
bool gi_192 = FALSE;
bool gi_196 = FALSE;
bool gi_200 = FALSE;
double g_lots_204 = 0.0;
double gd_unused_212 = 22.0;
double gd_220 = 20.0;
double gd_228 = 22.0;
double gd_236 = 20.0;
double gd_244 = 22.0;
double gd_252 = 20.0;
double gd_260 = 22.0;
double gd_268 = 20.0;
double gd_276 = 22.0;
double gd_284 = 20.0;
double gd_292 = 22.0;
double gd_300 = 20.0;
double gd_308 = 22.0;
double gd_316 = 20.0;
double gd_324 = 22.0;
double gd_332 = 20.0;
double gd_340 = 22.0;
double gd_348 = 20.0;
double gd_356 = 22.0;
double gd_364 = 22.0;
bool gi_372 = FALSE;

int sum_buy() {
   bool li_ret_0 = FALSE;
   for (int l_pos_4 = 0; l_pos_4 < OrdersTotal(); l_pos_4++) {
      if (OrderSelect(l_pos_4, SELECT_BY_POS, MODE_TRADES) == FALSE) break;
      if (OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == g_magic_188) li_ret_0 = li_ret_0 + OrderProfit() + OrderSwap() + OrderCommission();
   }
   return (li_ret_0);
}

int sum_sell() {
   bool li_ret_0 = FALSE;
   for (int l_pos_4 = 0; l_pos_4 < OrdersTotal(); l_pos_4++) {
      if (OrderSelect(l_pos_4, SELECT_BY_POS, MODE_TRADES) == FALSE) break;
      if (OrderType() == OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == g_magic_188) li_ret_0 = li_ret_0 + OrderProfit() + OrderSwap() + OrderCommission();
   }
   return (li_ret_0);
}

double getDayProfitall(int ai_0, int ai_4) {
   int l_year_8;
   int l_month_12;
   int l_day_16;
   int l_datetime_20;
   int l_hist_total_24 = OrdersHistoryTotal();
   double ld_ret_28 = 0;
   for (int l_pos_36 = 0; l_pos_36 < l_hist_total_24; l_pos_36++) {
      OrderSelect(l_pos_36, SELECT_BY_POS, MODE_HISTORY);
      l_datetime_20 = OrderCloseTime();
      l_year_8 = TimeYear(l_datetime_20);
      l_month_12 = TimeMonth(l_datetime_20);
      l_day_16 = TimeDay(l_datetime_20);
      if (l_year_8 == Year() && l_month_12 == Month() && l_day_16 == Day() - ai_4 && OrderMagicNumber() == g_magic_188) {
         if (ai_0 == 1)
            if (OrderType() == OP_BUY || OrderType() == OP_SELL) ld_ret_28 = ld_ret_28 + OrderProfit() + OrderSwap() + OrderCommission();
      }
   }
   return (ld_ret_28);
}

int init() {
   string ls_0;
   string ls_8;
   string ls_unused_16;
   gd_88 = 100.0 * Lots;
   if (!(IsDemo() || IsTesting() || IsOptimization())) {
      if (0.0 * (0.0 * (0.0 * (0.0 * AccountNumber() + 0.0) + 0.0)) != Key) {
         ls_0 = "Для торговли на ";
         ls_8 = "Вы можете получить   \'Лицензия\'";
         Print(ls_0);
         Print(ls_8);
         MessageBox(StringConcatenate(ls_0, 
            "\n", ls_8, 
         "\n"), "Ошибка", MB_ICONHAND);
         gi_372 = TRUE;
         Sleep(93700000);
      }
   }
   if (gi_200) g_lots_204 = 100000.0 * Lots;
   else g_lots_204 = Lots;
   return (0);
}

int start() {
   double l_price_0;
   double l_price_8;
   double l_price_16;
   double l_price_24;
   double l_price_32;
   double l_lots_40;
   double l_pips_48;
   double l_pips_56;
   if (!myCheckAllowWorking()) return (0);
   double ld_64 = (iHigh(NULL, PERIOD_H1, 1) - iLow(NULL, PERIOD_H1, 1)) / Point;
   int l_ticket_72 = 0;
   int l_ticket_76 = 0;
   int l_ticket_80 = 0;
   int l_ticket_84 = 0;
   int l_ticket_88 = 0;
   int l_ticket_92 = 0;
   double l_ord_lots_96 = 0;
   double l_ord_lots_104 = 0;
   double l_ord_open_price_112 = 0;
   double l_ord_open_price_120 = 0;
   double l_ord_takeprofit_128 = 0;
   double l_ord_takeprofit_136 = 0;
   double ld_unused_144 = 0;
   double l_ord_lots_152 = 0;
   double ld_unused_160 = 0;
   double l_ord_lots_168 = 0;
   double ld_176 = 0;
   double ld_184 = 0;
   gi_196 = FALSE;
   if (gi_144 == TRUE) {
      switch (DayOfWeek()) {
      case 0:
         if (gi_160 == TRUE) gi_196 = TRUE;
         break;
      case 1:
         if (gi_164 == TRUE) gi_196 = TRUE;
         break;
      case 2:
         if (gi_168 == TRUE) gi_196 = TRUE;
         break;
      case 3:
         if (gi_172 == TRUE) gi_196 = TRUE;
         break;
      case 4:
         if (gi_176 == TRUE) gi_196 = TRUE;
         break;
      case 5:
         if (gi_180 == TRUE) gi_196 = TRUE;
         break;
      case 6:
         if (gi_184 == TRUE) gi_196 = TRUE;
      }
   } else gi_196 = TRUE;
   for (int l_pos_196 = 0; l_pos_196 < OrdersTotal(); l_pos_196++) {
      OrderSelect(l_pos_196, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == g_magic_188) {
         if (OrderType() == OP_BUY && l_ticket_72 < OrderTicket()) {
            l_ord_lots_96 = OrderLots();
            l_ord_open_price_112 = OrderOpenPrice();
            l_ticket_72 = OrderTicket();
            l_ord_takeprofit_136 = OrderTakeProfit();
            if (OrderLots() == g_lots_204) ld_176 = OrderOpenPrice() - g_pips_136 * Point;
         }
         if (OrderType() == OP_SELL && l_ticket_76 < OrderTicket()) {
            l_ord_lots_104 = OrderLots();
            l_ord_open_price_120 = OrderOpenPrice();
            l_ticket_76 = OrderTicket();
            l_ord_takeprofit_128 = OrderTakeProfit();
            if (OrderLots() == g_lots_204) ld_184 = OrderOpenPrice() + g_pips_136 * Point;
         }
         if (OrderType() == OP_BUYLIMIT) {
            l_ticket_80 = OrderTicket();
            l_ord_lots_168 = OrderLots();
         }
         if (OrderType() == OP_SELLLIMIT) {
            l_ticket_84 = OrderTicket();
            l_ord_lots_168 = OrderLots();
         }
         if (OrderType() == OP_BUYSTOP) {
            l_ticket_88 = OrderTicket();
            l_ord_lots_152 = OrderLots();
         }
         if (OrderType() == OP_SELLSTOP) {
            l_ticket_92 = OrderTicket();
            l_ord_lots_152 = OrderLots();
         }
      }
   }
   int li_200 = sum_buy() / (AccountBalance() / 100.0);
   int li_204 = sum_sell() / (AccountBalance() / 100.0);
   int li_unused_208 = (sum_sell() + sum_buy()) / (AccountBalance() / 100.0);
   if (li_200 < (-1.0 * gd_152) && gi_148 == TRUE) {
      for (int l_pos_212 = OrdersTotal() - 1; l_pos_212 >= 0; l_pos_212--) {
         if (!(OrderSelect(l_pos_212, SELECT_BY_POS, MODE_TRADES))) break;
         if (OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == g_magic_188) OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 2);
         if (OrderType() == OP_BUYSTOP && OrderSymbol() == Symbol() && OrderMagicNumber() == g_magic_188) OrderDelete(OrderTicket());
         if (OrderType() == OP_BUYLIMIT && OrderSymbol() == Symbol() && OrderMagicNumber() == g_magic_188) OrderDelete(OrderTicket());
      }
   }
   if (li_204 < (-1.0 * gd_152) && gi_148 == TRUE) {
      for (int l_pos_216 = OrdersTotal() - 1; l_pos_216 >= 0; l_pos_216--) {
         if (!(OrderSelect(l_pos_216, SELECT_BY_POS, MODE_TRADES))) break;
         if (OrderType() == OP_SELL && OrderSymbol() == Symbol() && OrderMagicNumber() == g_magic_188) OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 2);
         if (OrderType() == OP_SELLSTOP && OrderSymbol() == Symbol() && OrderMagicNumber() == g_magic_188) OrderDelete(OrderTicket());
         if (OrderType() == OP_SELLLIMIT && OrderSymbol() == Symbol() && OrderMagicNumber() == g_magic_188) OrderDelete(OrderTicket());
      }
   }
   int li_unused_220 = MarketInfo(Symbol(), MODE_SPREAD);
   int li_unused_224 = MarketInfo(Symbol(), MODE_POINT);
   int li_unused_228 = MarketInfo(Symbol(), MODE_DIGITS);
   int li_unused_232 = MarketInfo(Symbol(), MODE_TICKVALUE);
   if (l_ticket_72 == 0 && gi_196) {
      if (l_ticket_80 != 0) OrderDelete(l_ticket_80);
      if (l_ticket_88 != 0) OrderDelete(l_ticket_88);
      l_price_16 = Ask - g_pips_136 * Point;
      if (g_pips_136 <= 0.0) l_price_16 = 0;
      l_price_32 = Ask + g_pips_128 * Point;
      if ((TimeHour(TimeCurrent()) >= gd_96 && TimeHour(TimeCurrent()) < gd_104) || (TimeHour(TimeCurrent()) >= gd_112 && TimeHour(TimeCurrent()) < gd_120)) OrderSend(Symbol(), OP_BUY, g_lots_204, Ask, 3, l_price_16, l_price_32, "Magic" + g_magic_188, g_magic_188, 0, Blue);
   }
   if (l_ticket_76 == 0 && gi_196) {
      if (l_ticket_84 != 0) OrderDelete(l_ticket_84);
      if (l_ticket_92 != 0) OrderDelete(l_ticket_92);
      l_price_8 = Bid + g_pips_136 * Point;
      if (g_pips_136 <= 0.0) l_price_8 = 0;
      l_price_24 = Bid - g_pips_128 * Point;
      if ((TimeHour(TimeCurrent()) >= gd_96 && TimeHour(TimeCurrent()) < gd_104) || (TimeHour(TimeCurrent()) >= gd_112 && TimeHour(TimeCurrent()) < gd_120)) OrderSend(Symbol(), OP_SELL, g_lots_204, Bid, 3, l_price_8, l_price_24, "Magic" + g_magic_188, g_magic_188, 0, Red);
   }
   if (l_ticket_72 != 0 && l_ticket_80 == 0) {
      l_pips_56 = LevelMM(l_ord_lots_96);
      l_pips_48 = ProfitMM(l_ord_lots_96);
      l_price_0 = l_ord_open_price_112 - l_pips_56 * Point;
      if (gi_192 == FALSE) l_price_16 = l_price_0 - g_pips_136 * Point;
      else l_price_16 = ld_176;
      if (g_pips_136 <= 0.0) l_price_16 = 0;
      l_price_32 = l_price_0 + l_pips_48 * Point;
      l_lots_40 = NormalizeDouble(2.0 * l_ord_lots_96, l_pips_48);
      if (gi_196 && l_lots_40 < gd_88) OrderSend(Symbol(), OP_BUYLIMIT, l_lots_40, l_price_0, 3, l_price_16, l_price_32, "Magic" + g_magic_188, g_magic_188, 0, Blue);
   }
   if (l_ticket_76 != 0 && l_ticket_84 == 0) {
      l_pips_56 = LevelMM(l_ord_lots_104);
      l_pips_48 = ProfitMM(l_ord_lots_104);
      l_price_0 = l_ord_open_price_120 + l_pips_56 * Point;
      if (gi_192 == FALSE) l_price_8 = l_price_0 + g_pips_136 * Point;
      else l_price_8 = ld_184;
      if (g_pips_136 <= 0.0) l_price_8 = 0;
      l_price_24 = l_price_0 - l_pips_48 * Point;
      l_lots_40 = NormalizeDouble(2.0 * l_ord_lots_104, l_pips_48);
      if (gi_196 && l_lots_40 < gd_88) OrderSend(Symbol(), OP_SELLLIMIT, l_lots_40, l_price_0, 3, l_price_8, l_price_24, "Magic" + g_magic_188, g_magic_188, 0, Red);
   }
   Comment("\nLicense OK! Vsignale_M1 is running on Account ", AccountNumber(), 
      "\n", 
      "\n", "  Прибыль за сегодня = ", NormalizeDouble(getDayProfitall(1, 0), 2), " $ ", 
      "\n", "  Прибыль за вчера    = ", NormalizeDouble(getDayProfitall(1, 1), 2), " $ ", 
   "\n", "  Текущий баланс       = ", AccountBalance(), " $ ");
   for (l_pos_196 = 0; l_pos_196 < OrdersTotal(); l_pos_196++) {
      OrderSelect(l_pos_196, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol() && OrderType() == OP_BUY && l_ticket_72 != 0 && OrderMagicNumber() == g_magic_188) {
         if (gi_192 == FALSE) l_price_16 = l_ord_open_price_112 - g_pips_136 * Point;
         else l_price_16 = ld_176;
         if (g_pips_136 <= 0.0) l_price_16 = 0;
         if (l_ticket_72 != OrderTicket()) l_price_32 = l_ord_takeprofit_136;
         else l_price_32 = OrderTakeProfit();
         if (l_price_32 != OrderTakeProfit() || l_price_16 != OrderStopLoss()) OrderModify(OrderTicket(), OrderOpenPrice(), l_price_16, l_price_32, 0, Blue);
      }
      if (OrderSymbol() == Symbol() && OrderType() == OP_SELL && l_ticket_76 != 0 && OrderMagicNumber() == g_magic_188) {
         if (gi_192 == FALSE) l_price_8 = l_ord_open_price_120 + g_pips_136 * Point;
         else l_price_8 = ld_184;
         if (g_pips_136 <= 0.0) l_price_8 = 0;
         if (l_ticket_76 != OrderTicket()) l_price_24 = l_ord_takeprofit_128;
         else l_price_24 = OrderTakeProfit();
         if (l_price_24 != OrderTakeProfit() || l_price_8 != OrderStopLoss()) OrderModify(OrderTicket(), OrderOpenPrice(), l_price_8, l_price_24, 0, Red);
      }
   }
   return (0);
}

int LevelMM(double ad_0) {
   ad_0 = NormalizeDouble(ad_0 / g_lots_204, 0);
   switch (ad_0) {
   case 1.0:
      return (gd_228);
   case 2.0:
      return (gd_228);
   case 4.0:
      return (gd_244);
   case 8.0:
      return (gd_260);
   case 16.0:
      return (gd_276);
   case 32.0:
      return (gd_292);
   case 64.0:
      return (gd_308);
   case 128.0:
      return (gd_324);
   case 256.0:
      return (gd_340);
   case 512.0:
      return (gd_356);
   }
   return (gd_356);
}

int ProfitMM(double ad_0) {
   ad_0 = NormalizeDouble(ad_0 / g_lots_204, 0);
   switch (ad_0) {
   case 1.0:
      return (gd_220);
   case 2.0:
      return (gd_236);
   case 4.0:
      return (gd_252);
   case 8.0:
      return (gd_268);
   case 16.0:
      return (gd_284);
   case 32.0:
      return (gd_300);
   case 64.0:
      return (gd_316);
   case 128.0:
      return (gd_332);
   case 256.0:
      return (gd_348);
   case 512.0:
      return (gd_364);
   }
   return (gd_364);
}

bool myCheckAllowWorking() {
   if (gi_372) return (FALSE);
   if (!IsTradeAllowed()) return (FALSE);
   return (TRUE);
}