/*
   Generated by EX4-TO-MQ4 decompiler V4.0.224.1 []
   Website: http://purebeam.biz
   E-mail : purebeam@gmail.com
*/
#property copyright "11"
#property link      "11"

int gi_76 = 0;
double g_open_80 = 0.0;
extern double lot = 0.1;
extern double koef = 1.0;
extern int SL = 20;
extern int TP = 10;
double gd_unused_112;
double gd_120;
double gd_128;
int g_magic_144 = 777;
int g_pos_148;
int gi_152;
int g_ticket_156;
int g_count_160;
double gd_164 = 1.0;
bool gi_172;

void init() {
   if (Digits == 3 || Digits == 5) {
      gd_164 = 10;
      return;
   }
   gd_164 = 1;
}

void deinit() {
   lot = 0.1;
}

int start() {
   int l_digits_0 = MarketInfo(OrderSymbol(), MODE_DIGITS);
   gd_120 = NormalizeDouble(Ask, l_digits_0);
   gd_128 = NormalizeDouble(Bid, l_digits_0);
   gi_172 = FALSE;
   g_count_160 = 0;
   int li_4 = 357000;

   if (rust() == 1) lot = 0.1;
   if (rust() == 0) lot = 0.2;
   for (g_pos_148 = 0; g_pos_148 < gi_152; g_pos_148++) {
      OrderSelect(g_pos_148, SELECT_BY_POS, MODE_TRADES);
      if (OrderMagicNumber() == g_magic_144) {
         gi_172 = TRUE;
         g_count_160++;
      }
   }
   g_open_80 = Open[0];
   Sleep(10000);
   if (gi_172 == FALSE && gi_76 == 1 && g_count_160 <= 1) gi_76 = 0;
   if (gi_172 == FALSE && gi_76 == 2 && g_count_160 <= 1) gi_76 = 0;
   if (gi_172 == FALSE && Close[0] > g_open_80 && Close[1] > Open[1]) {
      gd_unused_112 = Close[0];
      Print("want to sell");
      RefreshRates();
      g_ticket_156 = OrderSend(Symbol(), OP_SELL, lot, nd(Bid), 3, nd(Bid - SL * Point * gd_164), nd(Ask - TP * Point * gd_164), "", g_magic_144, 0, Red);
      Print(g_ticket_156);
      Print(Bid);
      if (g_ticket_156 < 0) Print("Error opening sell order : ", GetLastError() + g_ticket_156);
      gi_76 = 1;
   }
   if (gi_172 == FALSE && Close[0] < g_open_80 && Close[1] < Open[1]) {
      gd_unused_112 = Close[0];
      Print("want to buy");
      Print(Ask);
      RefreshRates();
      g_ticket_156 = OrderSend(Symbol(), OP_BUY, lot, nd(Ask), 3, nd(Ask - SL * Point), nd(Bid + TP * Point), "", g_magic_144, 0, Green);
      Print(g_ticket_156);
      if (g_ticket_156 < 0) Print("Error opening BUY order : ", GetLastError() + g_ticket_156);
      gi_76 = 2;
   }
   return (0);
}

double nd(double ad_0) {
   return (NormalizeDouble(ad_0, Digits));
}

int rust() {
   int l_datetime_0 = 0;
   double l_ord_profit_4 = 0;
   for (int l_hist_total_12 = OrdersHistoryTotal(); l_hist_total_12 >= 0; l_hist_total_12--) {
      if (OrderSelect(l_hist_total_12, SELECT_BY_POS, MODE_HISTORY)) {
         if (OrderSymbol() == Symbol()) {
            if (l_datetime_0 < OrderCloseTime()) {
               l_datetime_0 = OrderCloseTime();
               l_ord_profit_4 = OrderProfit();
            }
         }
      }
   }
   if (l_ord_profit_4 == 0.0 && l_datetime_0 == 0) {
   }
   if (l_ord_profit_4 >= 0.0) return (1);
   if (l_ord_profit_4 < 0.0) return (0);
   return (0);
}