Navigation:Home > Content >

Collector2.1~.mq4

Time: 2014-07-08 | Download file:Collector2.1~.mq4

#property copyright "Copyright © 2012, Collector"
#property link      "shurgin.ru"

extern int MagicNumber = 1;
extern int Key = 0;
extern string Pair_1 = "EURUSD";
extern string Pair_2 = "GBPUSD";
extern double Lots_1 = 0.1;
extern double Lots_2 = 0.1;
extern double KoeffLots = 1.1;
extern int LossInPoints = 20;
extern int ProfitInPoints = 20;
extern int SumProfitInPoints = 15;
extern int TrailStop = 6;
extern int TrailStep = 3;
extern int Slippage = 3;
extern string OrdersComment = "Shurgin_Collector";
int gi_156;
int g_magic_160;
int g_ticket_164;
int g_count_168;
int gi_172;
int gi_176;
int gi_180;
int g_digits_184;
int g_digits_188;
int g_stoplevel_192;
int g_stoplevel_196;
int gia_200[500];
int gia_204[500];
int gi_208;
int gi_212;
double g_lots_216;
double g_maxlot_240;
double g_minlot_248;
double g_lotstep_256;
double g_maxlot_264;
double g_minlot_272;
double g_lotstep_280;
double g_point_288;
double g_point_296;
double g_ask_304;
double g_ask_312;
double g_bid_320;
double g_bid_328;
string g_comment_336;
string g_comment_344;
string g_text_352;
bool gba_360[500];
bool gba_364[500];
bool gba_368[500];
bool gba_372[500];

int init() {
   if ((!IsExpertEnabled()) && !IsTesting()) Comment("Запуск советников в терминале запрещён! Нажмите кнопку \"Советники\" на панели инструментов.");
   ObjectCreate("Copyright", OBJ_LABEL, 0, 0, 1.0);
   ObjectSet("Copyright", OBJPROP_CORNER, 2);
   ObjectSet("Copyright", OBJPROP_XDISTANCE, 3);
   ObjectSet("Copyright", OBJPROP_YDISTANCE, 1);
   g_text_352 = "Shurgin Aleksandr " + CharToStr(174) + " http://shurgin.ru";
   ObjectSetText("Copyright", g_text_352, 11, "Times New Roman", Tomato);
   ObjectSet("Copyright", OBJPROP_TIMEFRAMES, NULL);
   ObjectSet("Copyright", OBJPROP_BACK, FALSE);
   gi_156 = 1000 * MagicNumber;
   g_maxlot_240 = MarketInfo(Pair_1, MODE_MAXLOT);
   g_minlot_248 = MarketInfo(Pair_1, MODE_MINLOT);
   g_lotstep_256 = MarketInfo(Pair_1, MODE_LOTSTEP);
   if (g_lotstep_256 <= 0.009) gi_172 = 3;
   else {
      if (g_lotstep_256 <= 0.09) gi_172 = 2;
      else {
         if (g_lotstep_256 <= 0.9) gi_172 = 1;
         else gi_172 = 0;
      }
   }
   g_maxlot_264 = MarketInfo(Pair_2, MODE_MAXLOT);
   g_minlot_272 = MarketInfo(Pair_2, MODE_MINLOT);
   g_lotstep_280 = MarketInfo(Pair_2, MODE_LOTSTEP);
   if (g_lotstep_280 <= 0.009) gi_176 = 3;
   else {
      if (g_lotstep_280 <= 0.09) gi_176 = 2;
      else {
         if (g_lotstep_280 <= 0.9) gi_176 = 1;
         else gi_176 = 0;
      }
   }
   g_point_288 = MarketInfo(Pair_1, MODE_POINT);
   g_point_296 = MarketInfo(Pair_2, MODE_POINT);
   g_digits_184 = MarketInfo(Pair_1, MODE_DIGITS);
   g_digits_188 = MarketInfo(Pair_2, MODE_DIGITS);
   g_comment_336 = OrdersComment + " _1";
   g_comment_344 = OrdersComment + " _2";
   gi_180 = 4.0 * (3.0 * (4.0 * (3.0 * AccountNumber() + 127.0) + 8.0));
   return (0);
}

int start() {
   int li_4;
   if (!IsTesting()) {
      if (!IsExpertEnabled()) {
         Comment("В торговом терминале запрещён запуск экспертов.");
         return (0);
      }
      if (!IsTradeAllowed()) {
         Comment("Торговля запрещена или торговый поток занят.");
         return (0);
      }
      Comment("");
   }
   if ((!IsTesting()) && !IsOptimization() && (!IsDemo())) {
      if (gi_180 != Key) {
         Comment("Для торговли на реальном счете нужен лицензионный ключ.");
         return (0);
      }
   }
   g_stoplevel_192 = MarketInfo(Pair_1, MODE_STOPLEVEL);
   g_stoplevel_196 = MarketInfo(Pair_2, MODE_STOPLEVEL);
   g_ask_304 = MarketInfo(Pair_1, MODE_ASK);
   g_ask_312 = MarketInfo(Pair_2, MODE_ASK);
   g_bid_320 = MarketInfo(Pair_1, MODE_BID);
   g_bid_328 = MarketInfo(Pair_2, MODE_BID);
   for (int l_pos_0 = 0; l_pos_0 < 500; l_pos_0++) {
      gia_200[l_pos_0] = 0;
      gia_204[l_pos_0] = 0;
      gba_360[l_pos_0] = 0;
      gba_364[l_pos_0] = 0;
      gba_368[l_pos_0] = 0;
      gba_372[l_pos_0] = 0;
   }
   gi_208 = 0;
   gi_212 = 0;
   int li_8 = -1;
   int li_12 = -1;
   for (l_pos_0 = OrdersTotal() - 1; l_pos_0 >= 0; l_pos_0--) {
      if (OrderSelect(l_pos_0, SELECT_BY_POS)) {
         if ((OrderSymbol() != Pair_1 && OrderSymbol() != Pair_2) || OrderMagicNumber() < gi_156 || OrderMagicNumber() >= gi_156 + 1000) continue;
         if (OrderStopLoss() > 0.0) {
            if (OrderType() == OP_SELL && OrderSymbol() == Pair_1) {
               if (OrderOpenPrice() - g_ask_304 > TrailStop * g_point_288 && OrderStopLoss() > g_ask_304 + (TrailStop + TrailStep) * g_point_288 && OrderStopLoss() <= OrderOpenPrice()) OrderModify(OrderTicket(), OrderOpenPrice(), NRd1(g_ask_304 + TrailStop * g_point_288), OrderTakeProfit(), 0);
            } else {
               if (OrderType() == OP_BUY && OrderSymbol() == Pair_1) {
                  if (g_bid_320 - OrderOpenPrice() > TrailStop * g_point_288 && OrderStopLoss() < g_bid_320 - (TrailStop + TrailStep) * g_point_288 && OrderStopLoss() >= OrderOpenPrice()) OrderModify(OrderTicket(), OrderOpenPrice(), NRu1(g_bid_320 - TrailStop * g_point_288), OrderTakeProfit(), 0);
               } else {
                  if (OrderType() == OP_SELL && OrderSymbol() == Pair_2) {
                     if (OrderOpenPrice() - g_ask_312 > TrailStop * g_point_296 && OrderStopLoss() > g_ask_312 + (TrailStop + TrailStep) * g_point_296 && OrderStopLoss() <= OrderOpenPrice()) OrderModify(OrderTicket(), OrderOpenPrice(), NRd2(g_ask_312 + TrailStop * g_point_296), OrderTakeProfit(), 0);
                  } else {
                     if (OrderType() == OP_BUY && OrderSymbol() == Pair_2)
                        if (g_bid_328 - OrderOpenPrice() > TrailStop * g_point_296 && OrderStopLoss() < g_bid_328 - (TrailStop + TrailStep) * g_point_296 && OrderStopLoss() >= OrderOpenPrice()) OrderModify(OrderTicket(), OrderOpenPrice(), NRu2(g_bid_328 - TrailStop * g_point_296), OrderTakeProfit(), 0);
                  }
               }
            }
         } else {
            if (OrderType() == OP_SELL) {
               if (OrderMagicNumber() < gi_156 + 500) {
                  li_4 = OrderMagicNumber() - gi_156;
                  gba_364[li_4] = 1;
                  if (li_8 < li_4) li_8 = li_4;
                  if (OrderSymbol() == Pair_1) {
                     gi_208 += (OrderOpenPrice() - MarketInfo(Pair_1, MODE_ASK)) / g_point_288;
                     gia_200[li_4] += (OrderOpenPrice() - MarketInfo(Pair_1, MODE_ASK)) / g_point_288;
                  } else {
                     gi_208 += (OrderOpenPrice() - MarketInfo(Pair_2, MODE_ASK)) / g_point_296;
                     gia_200[li_4] += (OrderOpenPrice() - MarketInfo(Pair_2, MODE_ASK)) / g_point_296;
                  }
               } else {
                  li_4 = OrderMagicNumber() - gi_156 - 500;
                  gba_372[li_4] = 1;
                  if (li_12 < li_4) li_12 = li_4;
                  if (OrderSymbol() == Pair_1) {
                     gi_212 += (OrderOpenPrice() - MarketInfo(Pair_1, MODE_ASK)) / g_point_288;
                     gia_204[li_4] += (OrderOpenPrice() - MarketInfo(Pair_1, MODE_ASK)) / g_point_288;
                  } else {
                     gi_212 += (OrderOpenPrice() - MarketInfo(Pair_2, MODE_ASK)) / g_point_296;
                     gia_204[li_4] += (OrderOpenPrice() - MarketInfo(Pair_2, MODE_ASK)) / g_point_296;
                  }
               }
            } else {
               if (OrderType() == OP_BUY) {
                  if (OrderMagicNumber() < gi_156 + 500) {
                     li_4 = OrderMagicNumber() - gi_156;
                     gba_360[li_4] = 1;
                     if (li_8 < li_4) li_8 = li_4;
                     if (OrderSymbol() == Pair_1) {
                        gi_208 += (MarketInfo(Pair_1, MODE_BID) - OrderOpenPrice()) / g_point_288;
                        gia_200[li_4] += (MarketInfo(Pair_1, MODE_BID) - OrderOpenPrice()) / g_point_288;
                     } else {
                        gi_208 += (MarketInfo(Pair_2, MODE_BID) - OrderOpenPrice()) / g_point_296;
                        gia_200[li_4] += (MarketInfo(Pair_2, MODE_BID) - OrderOpenPrice()) / g_point_296;
                     }
                  } else {
                     li_4 = OrderMagicNumber() - gi_156 - 500;
                     gba_368[li_4] = 1;
                     if (li_12 < li_4) li_12 = li_4;
                     if (OrderSymbol() == Pair_1) {
                        gi_212 += (MarketInfo(Pair_1, MODE_BID) - OrderOpenPrice()) / g_point_288;
                        gia_204[li_4] += (MarketInfo(Pair_1, MODE_BID) - OrderOpenPrice()) / g_point_288;
                     } else {
                        gi_212 += (MarketInfo(Pair_2, MODE_BID) - OrderOpenPrice()) / g_point_296;
                        gia_204[li_4] += (MarketInfo(Pair_2, MODE_BID) - OrderOpenPrice()) / g_point_296;
                     }
                  }
               }
            }
         }
      }
   }
   if (li_8 >= 2 && gi_208 >= SumProfitInPoints) {
      Print("*** Закрытие всех первых ордеров по (SumProfitInPoints) профиту ", gi_208);
      CloseAll(1, -1);
      return (0);
   }
   if (li_12 >= 2 && gi_212 >= SumProfitInPoints) {
      Print("*** Закрытие всех вторых ордеров по (SumProfitInPoints) профиту ", gi_212);
      CloseAll(2, -1);
      return (0);
   }
   if (gia_200[li_8] >= ProfitInPoints) {
      Print("*** Закрытие ", li_8 + 1, " пары первых ордеров по (ProfitInPoints) профиту ", gia_200[li_8]);
      CloseAll(1, li_8);
      return (0);
   }
   if (gia_204[li_12] >= ProfitInPoints) {
      Print("*** Закрытие ", li_12 + 1, " пары вторых ордеров по (ProfitInPoints) профиту ", gia_204[li_12]);
      CloseAll(2, li_12);
      return (0);
   }
   RefreshRates();
   if (li_8 < 0) {
      Print("*** Открытие первого ордера первой пары");
      g_lots_216 = NRL1(Lots_1);
      g_magic_160 = gi_156;
   } else {
      if (li_8 >= 0 && (!gba_360[li_8]) && gba_364[li_8]) {
         Print("*** Открытие первого ордера первой пары");
         g_lots_216 = NRL1(Lots_1 * MathPow(KoeffLots, li_8));
         g_magic_160 = gi_156 + li_8;
      } else {
         if (li_8 >= 0 && gba_360[li_8] && gba_364[li_8] && gia_200[li_8] <= (-LossInPoints) * MathPow(KoeffLots, li_8)) {
            Print("*** Открытие ", li_8 + 1, " ордера первой пары ордеров. Убыток предыдущего уровня ", gia_200[li_8]);
            g_lots_216 = NRL1(Lots_1 * MathPow(KoeffLots, li_8 + 1));
            g_magic_160 = gi_156 + li_8 + 1;
         } else g_lots_216 = 0;
      }
   }
   if (g_lots_216 > 0.0) {
      g_ticket_164 = OrderSend(Pair_1, OP_BUY, g_lots_216, MarketInfo(Pair_1, MODE_ASK), Slippage, 0, 0, g_comment_336, g_magic_160, 0, Blue);
      if (g_ticket_164 > 0) {
         g_count_168 = 0;
         while (g_count_168 < 90 && !OrderSelect(g_ticket_164, SELECT_BY_TICKET)) {
            g_count_168++;
            Sleep(2000);
         }
      }
   }
   if (li_8 < 0) {
      Print("*** Открытие первого ордера первой пары");
      g_lots_216 = NRL2(Lots_2);
      g_magic_160 = gi_156;
   } else {
      if (li_8 >= 0 && (!gba_364[li_8]) && gba_360[li_8]) {
         Print("*** Открытие первого ордера первой пары");
         g_lots_216 = NRL2(Lots_2 * MathPow(KoeffLots, li_8));
         g_magic_160 = gi_156 + li_8;
      } else {
         if (li_8 >= 0 && gba_360[li_8] && gba_364[li_8] && gia_200[li_8] <= (-LossInPoints) * MathPow(KoeffLots, li_8)) {
            Print("*** Открытие ", li_8 + 1, " ордера первой пары ордеров. Убыток предыдущего уровня ", gia_200[li_8]);
            g_lots_216 = NRL2(Lots_2 * MathPow(KoeffLots, li_8 + 1));
            g_magic_160 = gi_156 + li_8 + 1;
         } else g_lots_216 = 0;
      }
   }
   if (g_lots_216 > 0.0) {
      g_ticket_164 = OrderSend(Pair_2, OP_SELL, g_lots_216, MarketInfo(Pair_2, MODE_BID), Slippage, 0, 0, g_comment_336, g_magic_160, 0, Red);
      if (g_ticket_164 > 0) {
         g_count_168 = 0;
         while (g_count_168 < 90 && !OrderSelect(g_ticket_164, SELECT_BY_TICKET)) {
            g_count_168++;
            Sleep(2000);
         }
      }
   }
   if (li_12 < 0) {
      Print("*** Открытие первого ордера второй пары");
      g_lots_216 = NRL1(Lots_1);
      g_magic_160 = gi_156 + 500;
   } else {
      if (li_12 >= 0 && (!gba_368[li_12]) && gba_372[li_12]) {
         Print("*** Открытие первого ордера второй пары");
         g_lots_216 = NRL1(Lots_1 * MathPow(KoeffLots, li_12));
         g_magic_160 = gi_156 + li_12 + 500;
      } else {
         if (li_12 >= 0 && gba_368[li_12] && gba_372[li_12] && gia_204[li_12] <= (-LossInPoints) * MathPow(KoeffLots, li_12)) {
            Print("*** Открытие ", li_12 + 1, " ордера второй пары ордеров. Убыток предыдущего уровня ", gia_204[li_12]);
            g_lots_216 = NRL1(Lots_1 * MathPow(KoeffLots, li_12 + 1));
            g_magic_160 = gi_156 + li_12 + 501;
         } else g_lots_216 = 0;
      }
   }
   if (g_lots_216 > 0.0) {
      g_ticket_164 = OrderSend(Pair_2, OP_BUY, g_lots_216, MarketInfo(Pair_2, MODE_ASK), Slippage, 0, 0, g_comment_344, g_magic_160, 0, Blue);
      if (g_ticket_164 > 0) {
         g_count_168 = 0;
         while (g_count_168 < 90 && !OrderSelect(g_ticket_164, SELECT_BY_TICKET)) {
            g_count_168++;
            Sleep(2000);
         }
      }
   }
   if (li_12 < 0) {
      Print("*** Открытие первого ордера второй пары");
      g_lots_216 = NRL2(Lots_2);
      g_magic_160 = gi_156 + 500;
   } else {
      if (li_12 >= 0 && (!gba_372[li_12]) && gba_368[li_12]) {
         Print("*** Открытие первого ордера второй пары");
         g_lots_216 = NRL2(Lots_2 * MathPow(KoeffLots, li_12));
         g_magic_160 = gi_156 + li_12 + 500;
      } else {
         if (li_12 >= 0 && gba_368[li_12] && gba_372[li_12] && gia_204[li_12] <= (-LossInPoints) * MathPow(KoeffLots, li_12)) {
            Print("*** Открытие ", li_12 + 1, " ордера второй пары ордеров. Убыток предыдущего уровня ", gia_204[li_12]);
            g_lots_216 = NRL2(Lots_2 * MathPow(KoeffLots, li_12 + 1));
            g_magic_160 = gi_156 + li_12 + 501;
         } else g_lots_216 = 0;
      }
   }
   if (g_lots_216 > 0.0) {
      g_ticket_164 = OrderSend(Pair_1, OP_SELL, g_lots_216, MarketInfo(Pair_1, MODE_BID), Slippage, 0, 0, g_comment_344, g_magic_160, 0, Red);
      if (g_ticket_164 > 0) {
         g_count_168 = 0;
         while (g_count_168 < 90 && !OrderSelect(g_ticket_164, SELECT_BY_TICKET)) {
            g_count_168++;
            Sleep(2000);
         }
      }
   }
   return (0);
}

double NRL1(double ad_0) {
   if (ad_0 < g_minlot_248) return (g_minlot_248);
   if (ad_0 > g_maxlot_240) return (g_maxlot_240);
   return (NormalizeDouble(ad_0, gi_172));
}

double NRL2(double ad_0) {
   if (ad_0 < g_minlot_272) return (g_minlot_272);
   if (ad_0 > g_maxlot_264) return (g_maxlot_264);
   return (NormalizeDouble(ad_0, gi_176));
}

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

void CloseAll(int ai_0, int ai_4) {
   bool li_12 = TRUE;
   if (ai_0 == 1) {
      while (li_12) {
         li_12 = FALSE;
         for (int l_pos_8 = OrdersTotal() - 1; l_pos_8 >= 0; l_pos_8--) {
            if (OrderSelect(l_pos_8, SELECT_BY_POS)) {
               if (OrderStopLoss() <= 0.0) {
                  if (ai_4 >= 0 && OrderMagicNumber() != gi_156 + ai_4) continue;
                  if (ai_4 < 0 && OrderMagicNumber() < gi_156 || OrderMagicNumber() >= gi_156 + 500) continue;
                  RefreshRates();
                  if (OrderType() == OP_SELL && OrderSymbol() == Pair_2) {
                     li_12 = TRUE;
                     if (OrderProfit() + OrderSwap() + OrderCommission() > 0.0 && TrailStop >= g_stoplevel_196 && NRu2(MarketInfo(Pair_2, MODE_ASK) + TrailStop * g_point_296) <= OrderOpenPrice()) OrderModify(OrderTicket(), OrderOpenPrice(), NRu2(MarketInfo(Pair_2, MODE_ASK) + TrailStop * g_point_296), OrderTakeProfit(), 0);
                     else OrderClose(OrderTicket(), OrderLots(), MarketInfo(Pair_2, MODE_ASK), Slippage);
                  } else {
                     if (OrderType() == OP_BUY && OrderSymbol() == Pair_1) {
                        li_12 = TRUE;
                        if (OrderProfit() + OrderSwap() + OrderCommission() > 0.0 && TrailStop >= g_stoplevel_192 && NRd1(MarketInfo(Pair_1, MODE_BID) - TrailStop * g_point_288) >= OrderOpenPrice()) OrderModify(OrderTicket(), OrderOpenPrice(), NRd1(MarketInfo(Pair_1, MODE_BID) - TrailStop * g_point_288), OrderTakeProfit(), 0);
                        else OrderClose(OrderTicket(), OrderLots(), MarketInfo(Pair_1, MODE_BID), Slippage);
                     }
                  }
               }
            }
         }
         if (li_12) Sleep(2000);
      }
   } else {
      while (li_12) {
         li_12 = FALSE;
         for (l_pos_8 = OrdersTotal() - 1; l_pos_8 >= 0; l_pos_8--) {
            if (OrderSelect(l_pos_8, SELECT_BY_POS)) {
               if (OrderStopLoss() <= 0.0) {
                  if (ai_4 >= 0 && OrderMagicNumber() != gi_156 + ai_4 + 500) continue;
                  if (ai_4 < 0 && OrderMagicNumber() < gi_156 + 500 || OrderMagicNumber() >= gi_156 + 1000) continue;
                  RefreshRates();
                  if (OrderType() == OP_SELL && OrderSymbol() == Pair_1) {
                     li_12 = TRUE;
                     if (OrderProfit() + OrderSwap() + OrderCommission() > 0.0 && TrailStop >= g_stoplevel_192 && NRu1(MarketInfo(Pair_1, MODE_ASK) + TrailStop * g_point_288) <= OrderOpenPrice()) OrderModify(OrderTicket(), OrderOpenPrice(), NRu1(MarketInfo(Pair_1, MODE_ASK) + TrailStop * g_point_288), OrderTakeProfit(), 0);
                     else OrderClose(OrderTicket(), OrderLots(), MarketInfo(Pair_1, MODE_ASK), Slippage);
                  } else {
                     if (OrderType() == OP_BUY && OrderSymbol() == Pair_2) {
                        li_12 = TRUE;
                        if (OrderProfit() + OrderSwap() + OrderCommission() > 0.0 && TrailStop >= g_stoplevel_196 && NRd2(MarketInfo(Pair_2, MODE_BID) - TrailStop * g_point_296) >= OrderOpenPrice()) OrderModify(OrderTicket(), OrderOpenPrice(), NRd2(MarketInfo(Pair_2, MODE_BID) - TrailStop * g_point_296), OrderTakeProfit(), 0);
                        else OrderClose(OrderTicket(), OrderLots(), MarketInfo(Pair_2, MODE_BID), Slippage);
                     }
                  }
               }
            }
         }
         if (li_12) Sleep(2000);
      }
   }
}

double NRu1(double ad_0) {
   return (NormalizeDouble(MathCeil(ad_0 / g_point_288) * g_point_288, g_digits_184));
}

double NRd1(double ad_0) {
   return (NormalizeDouble(MathFloor(ad_0 / g_point_288) * g_point_288, g_digits_184));
}

double NRu2(double ad_0) {
   return (NormalizeDouble(MathCeil(ad_0 / g_point_296) * g_point_296, g_digits_188));
}

double NRd2(double ad_0) {
   return (NormalizeDouble(MathFloor(ad_0 / g_point_296) * g_point_296, g_digits_188));
}

Recommend