#property copyright "Copyright © 2012, Rita Lasker"
#property link      "http://www.ritalasker.com"

#include <WinUser32.mqh>
#import "kernel32.dll"
   void GetSystemTime(int& a0[]);
#import

string g_comment_84 = "Forex Pips Hunter GBPUSD";
int gi_92 = 39;
double gd_96 = 2.0;
int gi_104 = 29;
int gi_108 = 1;
int gi_112 = 3;
int gi_116 = 0;
double gd_120 = 10.0;
extern double Lot = 0.1;
extern bool gi_136 = TRUE;
int gi_140 = 4;
int gi_144 = 0;
int gi_148 = 21;
int gi_152 = 8;
int gi_156 = 1;
extern int MAGIC = 1345674;
double gd_164;
int g_slippage_172 = 2;
int g_slippage_176;
double gd_180;
double gd_188;
double g_lotstep_196;
int gi_204;
bool gi_208 = TRUE;
double gd_212;
int gi_236;
int gi_240;
double gd_260;
double gd_268;
double gd_276;
double gd_284;
double gd_292;
double gd_300;
int gi_316;
bool gi_324 = TRUE;
bool gi_328 = TRUE;
double gd_332;

int init() {
   gd_180 = MarketInfo(Symbol(), MODE_MAXLOT);
   gd_188 = MarketInfo(Symbol(), MODE_MINLOT);
   g_lotstep_196 = MarketInfo(Symbol(), MODE_LOTSTEP);
   if (g_lotstep_196 == 0.1) gi_204 = 1;
   if (g_lotstep_196 == 0.01) gi_204 = 2;
   g_slippage_176 = g_slippage_172;
   gd_164 = Point;
   if (Digits == 5 || Digits == 3) {
      gd_164 = 10.0 * gd_164;
      g_slippage_176 = 10 * g_slippage_176;
   }
   return (0);
}

int deinit() {
   return (0);
}

int start() {
   double ld_0;
   double ld_8;
   double ld_16;
   int li_24;
   double ld_28;
   double ld_36;
   double ld_44;
   int li_56;
   int shift_64;
   double ld_68;
   if (!gi_208) return (0);
   
   gd_212 = MarketInfo(Symbol(), MODE_STOPLEVEL) * Point / gd_164;
   int li_52 = f0_8();
   if (gi_92 > 0) {
      li_56 = gi_92;
      if (li_56 < gd_212 + 3.0) li_56 = gd_212 + 3.0;
      for (int pos_60 = 0; pos_60 < OrdersTotal(); pos_60++) {
         if (OrderSelect(pos_60, SELECT_BY_POS, MODE_TRADES)) {
            if (OrderMagicNumber() == MAGIC && Symbol() == OrderSymbol()) {
               if (!IsTesting()) RefreshRates();
               if (OrderType() == OP_BUY) {
                  if (OrderOpenPrice() + li_56 * gd_164 < Bid && OrderStopLoss() < OrderOpenPrice()) {
                     if (OrderModify(OrderTicket(), OrderOpenPrice(), f0_6(OrderOpenPrice() + 3.0 * gd_164), OrderTakeProfit(), 0, Blue))
                        if (!IsOptimization()) Print("Order Modify Error ", GetLastError());
                  }
               }
               if (OrderType() == OP_SELL) {
                  if (OrderOpenPrice() - li_56 * gd_164 > Ask && OrderStopLoss() > OrderOpenPrice() || OrderStopLoss() == 0.0) {
                     if (OrderModify(OrderTicket(), OrderOpenPrice(), f0_6(OrderOpenPrice() - 3.0 * gd_164), OrderTakeProfit(), 0, Red))
                        if (!IsOptimization()) Print("Order Modify Error ", GetLastError());
                  }
               }
            }
         }
      }
   }
   if (gd_96 > 0.0) {
      gd_332 = MathMax(gd_96, gd_212);
      f0_10();
   }
   if (IsTesting()) {
      if (gi_136) {
         MessageBoxA(0, "AutoTimeSettings should be (false) at the test mode. Using ManualGMT_Offset", "Error", 0);
         gi_208 = FALSE;
         return (0);
      }
      gi_236 = gi_140;
   }
   gi_236 = gi_140;
   if (gi_136) gi_236 = f0_11();
   gi_240 = 3600 * gi_236;
   if (li_24 < iTime(Symbol(), PERIOD_D1, 0) + gi_240) {
      if (TimeCurrent() >= iTime(Symbol(), PERIOD_D1, 0) + gi_240) li_24 = iTime(Symbol(), PERIOD_D1, 0) + gi_240;
      else li_24 = iTime(Symbol(), PERIOD_D1, 1) + gi_240;
   }
   if (gi_240 >= 0) {
      if (gi_316 < iTime(Symbol(), PERIOD_D1, 0) + gi_240 && TimeCurrent() >= iTime(Symbol(), PERIOD_D1, 0) + gi_240) {
         gi_316 = iTime(Symbol(), PERIOD_D1, 0) + gi_240;
         f0_3();
         gi_324 = TRUE;
         gi_328 = TRUE;
         shift_64 = iBarShift(Symbol(), 0, li_24);
         ld_0 = High[iHighest(Symbol(), 0, MODE_HIGH, 1440 / Period(), shift_64 + 1)];
         ld_8 = Low[iLowest(Symbol(), 0, MODE_LOW, 1440 / Period(), shift_64 + 1)];
         ld_16 = Close[shift_64 + 1];
         ld_44 = ld_16;
         ld_28 = ld_0;
         ld_36 = ld_8;
         ld_68 = (ld_28 + ld_36 + ld_44) / 3.0;
         gd_260 = f0_6(2.0 * ld_68 - ld_36);
         gd_276 = f0_6((ld_68 + gd_260) / 2.0);
         gd_292 = f0_6(ld_68 + (ld_28 - ld_36));
         gd_268 = f0_6(2.0 * ld_68 - ld_28);
         gd_284 = f0_6((ld_68 + gd_268) / 2.0);
         gd_300 = f0_6(ld_68 - (ld_28 - ld_36));
      }
   } else {
      if (gi_316 < iTime(Symbol(), PERIOD_D1, 1) + 86400 + gi_240 && TimeCurrent() >= iTime(Symbol(), PERIOD_D1, 1) + 86400 + gi_240) {
         gi_316 = iTime(Symbol(), PERIOD_D1, 1) + 86400 + gi_240;
         f0_3();
         gi_324 = TRUE;
         gi_328 = TRUE;
      }
   }
   if (f0_8()) {
      if (Open[1] < gd_276 && Close[1] < gd_276 && Close[0] >= gd_276 && gi_324) {
         gi_324 = FALSE;
         if (f0_4(OP_BUY) == 0) f0_0(OP_BUY);
      }
      if (Open[1] > gd_284 && Close[1] > gd_284 && Close[0] <= gd_284 && gi_328) {
         gi_328 = FALSE;
         if (f0_4(OP_SELL) == 0) f0_0(OP_SELL);
      }
   }
   return (0);
}

void f0_0(int a_cmd_0) {
   color color_12;
   double price_20;
   string ls_28;
   int ticket_36;
   double price_40;
   double price_48;
   double ld_4 = f0_1();
   if (AccountFreeMarginCheck(Symbol(), a_cmd_0, ld_4) <= 0.0) {
      if (!IsOptimization()) Alert("We have no money!");
      gi_208 = FALSE;
      return;
   }
   if (a_cmd_0 == OP_BUY) color_12 = Blue;
   if (a_cmd_0 == OP_SELL) color_12 = Red;
   for (int count_16 = 0; count_16 <= gi_112; count_16++) {
      if (!IsTesting()) {
         while (!IsTradeAllowed()) Sleep(5000);
         RefreshRates();
      }
      if (a_cmd_0 == OP_BUY) {
         price_20 = f0_6(Ask);
         ls_28 = "Buy";
      }
      if (a_cmd_0 == OP_SELL) {
         price_20 = f0_6(Bid);
         ls_28 = "Sell";
      }
      ticket_36 = OrderSend(Symbol(), a_cmd_0, ld_4, price_20, g_slippage_176, 0, 0, g_comment_84, MAGIC, 0, color_12);
      if (ticket_36 > 0) {
         if (!(OrderSelect(ticket_36, SELECT_BY_TICKET, MODE_TRADES))) break;
         if (!IsOptimization()) Print("Market Order ", ls_28, " ¹ ", ticket_36, " is opening!");
         if (a_cmd_0 == OP_BUY) price_40 = f0_7(0, OrderOpenPrice());
         if (a_cmd_0 == OP_SELL) price_40 = f0_7(1, OrderOpenPrice());
         if (!OrderModify(ticket_36, OrderOpenPrice(), price_40, 0, 0, color_12))
            if (!IsOptimization()) Print("StartModify StopLoss Error", GetLastError());
         if (a_cmd_0 == OP_BUY) price_48 = f0_5(0, OrderOpenPrice());
         if (a_cmd_0 == OP_SELL) price_48 = f0_5(1, OrderOpenPrice());
         if (!(!OrderModify(ticket_36, OrderOpenPrice(), price_40, price_48, 0, color_12))) break;
         if (!(!IsOptimization())) break;
         Print("StartModify TakeProfit Error ", GetLastError());
         return;
      }
      if (f0_9(GetLastError(), a_cmd_0) != 1) break;
   }
}

double f0_1() {
   double ld_ret_0;
   double marginrequired_8;
   double ld_16;
   if (gi_116 == 0) ld_ret_0 = Lot;
   if (gi_116 == 1) ld_ret_0 = NormalizeDouble(AccountEquity() / 100000.0 * gd_120, gi_204);
   if (gi_116 == 2) ld_ret_0 = NormalizeDouble(AccountFreeMargin() / 100000.0 * gd_120, gi_204);
   if (gi_116 == 3) ld_ret_0 = NormalizeDouble(AccountBalance() / 100000.0 * gd_120, gi_204);
   if (gi_116 == 4) {
      marginrequired_8 = MarketInfo(Symbol(), MODE_MARGINREQUIRED);
      ld_16 = AccountEquity() * gd_120 / 100.0;
      ld_ret_0 = NormalizeDouble(ld_16 / marginrequired_8, gi_204);
   }
   if (ld_ret_0 > gd_180) ld_ret_0 = gd_180;
   if (ld_ret_0 < gd_188) ld_ret_0 = gd_188;
   return (ld_ret_0);
}

double f0_5(int ai_0, double ad_unused_4) {
   double ld_ret_12;
   if (ai_0 == 0) {
      ld_ret_12 = f0_6(gd_292);
      if (!IsTesting()) RefreshRates();
      if (ld_ret_12 < f0_6(Bid + gd_212 * gd_164) && ld_ret_12 > 0.0) ld_ret_12 = f0_6(Bid + gd_212 * gd_164);
   }
   if (ai_0 == 1) {
      ld_ret_12 = f0_6(gd_300);
      if (!IsTesting()) RefreshRates();
      if (ld_ret_12 > f0_6(Ask - gd_212 * gd_164)) ld_ret_12 = f0_6(Ask - gd_212 * gd_164);
   }
   return (ld_ret_12);
}

double f0_7(int ai_0, double ad_unused_4) {
   double ld_ret_12;
   if (ai_0 == 0) {
      ld_ret_12 = f0_6(gd_284);
      if (!IsTesting()) RefreshRates();
      if (ld_ret_12 > f0_6(Bid - gd_212 * gd_164)) ld_ret_12 = f0_6(Bid - gd_212 * gd_164);
   }
   if (ai_0 == 1) {
      ld_ret_12 = f0_6(gd_276);
      if (!IsTesting()) RefreshRates();
      if (ld_ret_12 < f0_6(Ask + gd_212 * gd_164) && ld_ret_12 > 0.0) ld_ret_12 = f0_6(Ask + gd_212 * gd_164);
   }
   return (ld_ret_12);
}

int f0_9(int ai_0, int ai_4) {
   switch (ai_0) {
   case 4:
      Alert(Symbol(), ": Trade server is busy. Try repeat...");
      Sleep(3000);
      return (1);
   case 6:
      Alert(Symbol(), ": No connection with trade server. Try repeat...");
      Sleep(5000);
      return (1);
   case 128:
      Alert(Symbol(), ": Trade timeout. Try repeat...");
      Sleep(66000);
      if (ai_4 > 1) return (1);
      if (f0_4(ai_4) > 0) return (0);
      return (1);
   case 130:
      Alert(Symbol(), ": Invalid stops. Try repeat...");
      gd_212 += 0.5;
      return (1);
   case 142:
      Alert(Symbol(), ": Trade timeout. Try repeat...");
      Sleep(66000);
      if (ai_4 > 1) return (1);
      if (f0_4(ai_4) > 0) return (0);
      return (1);
   case 143:
      Alert(Symbol(), ": Trade timeout. Try repeat...");
      Sleep(66000);
      if (ai_4 > 1) return (1);
      if (f0_4(ai_4) > 0) return (0);
      return (1);
   case 129:
      Alert(Symbol(), ": Invalid price. Try repeat...");
      Sleep(3000);
      return (1);
   case 135:
      Alert(Symbol(), ": Price changed. Try repeat...");
      RefreshRates();
      return (1);
   case 136:
      Alert(Symbol(), ": Off quotes. Wait new tick...");
      while (!RefreshRates()) Sleep(1);
      return (1);
   case 137:
      Alert(Symbol(), ": Broker is busy. Try repeat...");
      Sleep(3000);
      return (1);
   case 138:
      Alert(Symbol(), ": Requote. Try repeat...");
      Sleep(5000);
      return (1);
   case 146:
      Alert(Symbol(), ": Trade context is busy. Try repeat...");
      Sleep(500);
      return (1);
   case 2:
      Alert("Common error.");
      return (0);
   case 5:
      Alert("Old version of the client terminal.");
      gi_208 = FALSE;
      return (0);
   case 64:
      Alert("Account disabled.");
      gi_208 = FALSE;
      return (0);
   case 133:
      Alert("Trade is disabled.");
      return (0);
   case 134:
      Alert(Symbol(), ": Not enough money.");
      return (0);
   }
   Alert(Symbol(), ": Is other error ", ai_0);
   return (0);
}

double f0_6(double ad_0) {
   return (NormalizeDouble(ad_0, Digits));
}

int f0_8() {
   if (gi_144 == 0) return (1);
   int li_0 = gi_148 + 1 + gi_236;
   if (li_0 > 23) li_0 -= 24;
   int li_4 = gi_152 + gi_236;
   if (li_4 > 23) li_4 -= 24;
   bool li_ret_8 = FALSE;
   if (li_0 < li_4) {
      if (li_0 <= Hour() && Hour() < li_4) li_ret_8 = TRUE;
   } else
      if (li_0 <= Hour() || Hour() < li_4) li_ret_8 = TRUE;
   return (li_ret_8);
}

int f0_4(int a_cmd_0 = -1) {
   int count_4 = 0;
   int order_total_8 = OrdersTotal();
   for (int pos_12 = 0; pos_12 < order_total_8; pos_12++) {
      if (OrderSelect(pos_12, SELECT_BY_POS, MODE_TRADES)) {
         if (OrderSymbol() == Symbol() && OrderMagicNumber() == MAGIC && OrderType() <= OP_SELL)
            if (OrderType() == a_cmd_0 || a_cmd_0 == -1) count_4++;
      }
   }
   return (count_4);
}

void f0_3(int a_cmd_0 = -1) {
   color color_8;
   double price_12;
   string ls_20;
   bool is_closed_28;
   for (int pos_4 = OrdersTotal() - 1; pos_4 >= 0; pos_4--) {
      OrderSelect(pos_4, SELECT_BY_POS, MODE_TRADES);
      if (OrderType() <= OP_SELL && OrderType() == a_cmd_0 || a_cmd_0 == -1 && OrderSymbol() == Symbol() && OrderMagicNumber() == MAGIC) {
         while (true) {
            if (!IsTesting()) {
               while (!IsTradeAllowed()) Sleep(5000);
               RefreshRates();
            }
            if (OrderType() == OP_BUY) {
               color_8 = Blue;
               price_12 = f0_6(Bid);
               ls_20 = "Buy";
               if (price_12 >= OrderTakeProfit() && OrderTakeProfit() != 0.0 || OrderTicket() == 0) return;
            }
            if (OrderType() == OP_SELL) {
               color_8 = Red;
               price_12 = f0_6(Ask);
               ls_20 = "Sell";
               if (price_12 <= OrderTakeProfit() || OrderTicket() == 0) return;
            }
            is_closed_28 = OrderClose(OrderTicket(), OrderLots(), price_12, g_slippage_176, color_8);
            if (is_closed_28) {
               if (!(!IsOptimization())) break;
               Print("Market Order ", ls_20, " ¹ ", OrderTicket(), " is closing.");
               break;
            }
            if (f0_9(GetLastError(), OrderType()) == 1) continue;
            break;
         }
      }
   }
}

int f0_2() {
   int lia_0[4];
   GetSystemTime(lia_0);
   string ls_4 = (lia_0[0] & 65535) + "." + (lia_0[0] >> 16) + "." + (lia_0[1] >> 16) + " " + (lia_0[2] & 65535) + ":" + (lia_0[2] >> 16) + ":" + (lia_0[3] & 65535);
   return (StrToTime(ls_4));
}

int f0_11() {
   int li_0 = f0_2();
   double ld_4 = TimeCurrent() - li_0;
   ld_4 /= 3600.0;
   ld_4 = MathRound(ld_4);
   int li_ret_12 = ld_4;
   return (li_ret_12);
}

void f0_10() {
   double price_4;
   for (int pos_0 = 0; pos_0 < OrdersTotal(); pos_0++) {
      OrderSelect(pos_0, SELECT_BY_POS, MODE_TRADES);
      if (OrderMagicNumber() == MAGIC) {
         if (OrderType() <= OP_SELL && OrderSymbol() == Symbol()) {
            if (OrderType() == OP_BUY) {
               if (!IsTesting()) RefreshRates();
               if (Bid - OrderOpenPrice() <= gd_164 * MathMax(gd_332, gi_104)) continue;
               price_4 = f0_6(Bid - gd_164 * gd_332);
               if (f0_6(OrderStopLoss()) >= f0_6(price_4 - gd_164 * gi_108)) continue;
               if (!(!OrderModify(OrderTicket(), OrderOpenPrice(), price_4, OrderTakeProfit(), 0, Blue))) continue;
               if (!(!IsOptimization())) continue;
               Print("Buy TrailingStop OrderModifyError ", GetLastError());
               continue;
            }
            if (!IsTesting()) RefreshRates();
            if (OrderOpenPrice() - Ask > gd_164 * MathMax(gd_332, gi_104)) {
               price_4 = f0_6(Ask + gd_164 * gd_332);
               if (f0_6(OrderStopLoss()) > price_4 + gd_164 * gi_108 || OrderStopLoss() == 0.0) {
                  if (!OrderModify(OrderTicket(), OrderOpenPrice(), price_4, OrderTakeProfit(), 0, Red))
                     if (!IsOptimization()) Print("Sell TrailingStop OrderModifyError ", GetLastError());
               }
            }
         }
      }
   }
}