/*
   Generated by EX4-TO-MQ4 decompiler V4.0.224.1 []
   Website: http://purebeam.biz
   E-mail : purebeam@gmail.com
*/
#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net/"

extern int tp = 9;
extern int sh = 4;
extern double lot = 0.1;
extern double dec = 2.0;
extern double tpa = 0.0;
extern double balance = 0.5;
int g_datetime_116;
int g_slippage_120;
int g_pos_124;
int g_ticket_128;
int g_ticket_132;
int g_count_140;
int g_error_144;
double g_lots_148 = 0.0;
bool g_bool_156 = FALSE;
bool g_bool_160 = FALSE;
bool gi_164 = FALSE;
bool gi_168 = FALSE;
int gi_172;
int gi_176;
double gda_180[11][8];
int gi_184;
int gi_188;
double gda_192[11][8];
double gd_196;
double gd_204;
double gd_212;
double gd_220;
int g_count_228;
int g_count_232;

int kol_buy() {
   int l_count_0 = 0;
   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) l_count_0++;
   }
   return (l_count_0);
}

int kol_sell() {
   int l_count_0 = 0;
   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) l_count_0++;
   }
   return (l_count_0);
}

void buy_array_reset() {
   ArrayInitialize(gda_180, 0.0);
   int l_index_0 = 0;
   for (g_pos_124 = 0; g_pos_124 < OrdersTotal(); g_pos_124++) {
      if (OrderSelect(g_pos_124, SELECT_BY_POS, MODE_TRADES) == FALSE) break;
      if (OrderSymbol() == Symbol() && OrderType() == OP_BUY) {
         l_index_0++;
         gda_180[l_index_0][0] = OrderTicket();
         gda_180[l_index_0][1] = OrderOpenPrice();
         gda_180[l_index_0][2] = OrderLots();
         gda_180[l_index_0][3] = OrderType();
         gda_180[l_index_0][4] = OrderMagicNumber();
         gda_180[l_index_0][5] = OrderStopLoss();
         gda_180[l_index_0][6] = OrderTakeProfit();
         gda_180[l_index_0][7] = OrderProfit();
      }
   }
   gi_176 = kol_buy();
   gda_180[0][0] = gi_172;
   gd_196 = gda_180[kol_buy()][2];
   gd_212 = gda_180[kol_buy()][1];
}

void sell_array_reset() {
   ArrayInitialize(gda_192, 0.0);
   int l_index_0 = 0;
   for (g_pos_124 = 0; g_pos_124 < OrdersTotal(); g_pos_124++) {
      if (OrderSelect(g_pos_124, SELECT_BY_POS, MODE_TRADES) == FALSE) break;
      if (OrderSymbol() == Symbol() && OrderType() == OP_SELL) {
         l_index_0++;
         gda_192[l_index_0][0] = OrderTicket();
         gda_192[l_index_0][1] = OrderOpenPrice();
         gda_192[l_index_0][2] = OrderLots();
         gda_192[l_index_0][3] = OrderType();
         gda_192[l_index_0][4] = OrderMagicNumber();
         gda_192[l_index_0][5] = OrderStopLoss();
         gda_192[l_index_0][6] = OrderTakeProfit();
         gda_192[l_index_0][7] = OrderProfit();
      }
   }
   gi_188 = kol_sell();
   gda_192[0][0] = gi_184;
   gd_204 = gda_192[kol_sell()][2];
   gd_220 = gda_192[kol_sell()][1];
}

void buy_open() {
   g_count_140 = 0;
   g_ticket_132 = -1;
   if (gd_196 == 0.0) g_lots_148 = lot;
   else g_lots_148 = dec * gd_196;
   while (g_ticket_132 == -1 && g_count_140 < 3) {
      g_ticket_132 = OrderSend(Symbol(), OP_BUY, g_lots_148, Ask, g_slippage_120, 0, Ask + (tp + tpa) * Point, " ", 20050611, 0, Yellow);
      Print("tic_buy=", g_ticket_132);
      if (g_ticket_132 == -1) {
         g_error_144 = GetLastError();
         g_count_140++;
         Print("Îøèáêà ¹", g_error_144, " ïðè buy ", g_count_140);
         Sleep(6000);
         RefreshRates();
      }
   }
   g_datetime_116 = TimeCurrent();
   buy_array_reset();
   g_count_228++;
}

void sell_open() {
   g_ticket_132 = -1;
   g_count_140 = 0;
   if (gd_204 == 0.0) g_lots_148 = lot;
   else g_lots_148 = dec * gd_204;
   while (g_ticket_132 == -1 && g_count_140 < 3) {
      g_ticket_132 = OrderSend(Symbol(), OP_SELL, g_lots_148, Bid, g_slippage_120, 0, Bid - (tp + tpa) * Point, " ", 20050611, 0, Red);
      Print("tic_sell=", g_ticket_132);
      if (g_ticket_132 == -1) {
         g_error_144 = GetLastError();
         g_count_140++;
         Print("Îøèáêà ¹", g_error_144, " ïðè sell ", g_count_140);
         Sleep(6000);
         RefreshRates();
      }
   }
   g_datetime_116 = TimeCurrent();
   sell_array_reset();
   g_count_232++;
}

void buy_close() {
   g_count_140 = 0;
   g_ticket_128 = 0;
   while (kol_buy() > 0 && g_count_140 < 3) {
      for (g_pos_124 = 1; g_pos_124 <= gi_172; g_pos_124++) {
         g_ticket_128 = gda_180[g_pos_124][0];
         if (!OrderClose(g_ticket_128, gda_180[g_pos_124][2], Bid, g_slippage_120, White)) {
            g_error_144 = GetLastError();
            g_count_140++;
            Print("Îøèáêà ¹", g_error_144, " ïðè close buy ", g_count_140);
            Sleep(6000);
            RefreshRates();
         }
      }
      g_count_140++;
   }
   buy_array_reset();
   g_count_228 = 0;
}

void sell_close() {
   g_count_140 = 0;
   g_ticket_128 = 0;
   while (kol_sell() > 0 && g_count_140 < 3) {
      for (g_pos_124 = 1; g_pos_124 <= gi_184; g_pos_124++) {
         g_ticket_128 = gda_192[g_pos_124][0];
         if (!OrderClose(g_ticket_128, gda_192[g_pos_124][2], Ask, g_slippage_120, White)) {
            g_error_144 = GetLastError();
            g_count_140++;
            Print("Îøèáêà ¹", g_error_144, " ïðè close sell ", g_count_140);
            Sleep(6000);
            RefreshRates();
         }
      }
      g_count_140++;
   }
   sell_array_reset();
   g_count_232 = 0;
}

int init() {
   g_count_228 = 0;
   g_count_232 = 0;
   gd_196 = 0;
   gd_204 = 0;
   gi_172 = kol_buy() + 1;
   gi_184 = kol_sell() + 1;
   buy_array_reset();
   sell_array_reset();
   return (0);
}

int start() {
   if (IsTradeAllowed() == FALSE) return (0);
   if (kol_buy() != g_count_228) {
      buy_array_reset();
      g_count_228 = kol_buy();
   }
   if (kol_sell() != g_count_232) {
      sell_array_reset();
      g_count_232 = kol_sell();
   }
   g_bool_156 = kol_buy() < 1 || gd_212 - sh * Point > Ask && AccountFreeMargin() > AccountBalance() * balance;
   g_bool_160 = kol_sell() < 1 || gd_220 + sh * Point < Bid && AccountFreeMargin() > AccountBalance() * balance;
   if (gda_180[gi_176][2] > 0.0) gi_164 = gda_180[gi_176][7] / (10.0 * gda_180[gi_176][2]) > tp;
   if (gda_192[gi_188][2] > 0.0) gi_168 = gda_192[gi_188][7] / (10.0 * gda_192[gi_188][2]) > tp;
   if (gi_164) buy_close();
   if (gi_168) sell_close();
   if (g_bool_156) buy_open();
   if (g_bool_160) sell_open();
   return (0);
}
