extern double Lot = 0.1;
extern bool SpreadControl = TRUE;
extern string OPT_KEY = "235271440";
bool activation_flag = FALSE;
bool gi_100;

int init() {
   if (Period() != PERIOD_H1 || StringSubstr(Symbol(), 0, 6) != "EURUSD") {
      if (IsTesting() == FALSE) return (MessageBox("You have to select symbol: EURUSD, timeframe: H1"));
      if (IsTesting() == TRUE) {
         Print("You have to select symbol: EURUSD, timeframe: H1");
         return;
      }
   }
   if (!IsDllsAllowed()) {
      if (IsTesting() == FALSE) return (MessageBox("You have not allowed DLL imports in settings"));
      if (IsTesting() == TRUE) {
         Print("You have not allowed DLL imports in settings");
         return;
      }
   }
   if ((!IsExpertEnabled()) && !IsTesting()) MessageBox("You have not enabled Expert Advisor for running");
   if (IsTesting() == TRUE) {
      GlobalVariableSet(Symbol() + "Dbuy", 50);
      GlobalVariableSet(Symbol() + "Dsell", 50);
   }
   if (IsTesting() == FALSE) {
      GlobalVariableSet(Symbol() + "Dbuy", Day());
      GlobalVariableSet(Symbol() + "Dsell", Day());
   }
   Comment("------------------------------", 
   "\n", "Connecting to server...");
   Sleep(2000);
   gi_100 = FALSE;
   int li_0 = InitDll(WindowHandle(Symbol(), Period()));
   if (li_0 == -1) {
      gi_100 = TRUE;
      Comment("------------------------------", 
      "\n", "Unable to connect to server. Next attempt in 5 seconds.");
      Sleep(5000);
   }
   if (li_0 == 0) {
      Comment("------------------------------", 
      "\n", "Account #", AccountNumber(), " is not activated");
   }
   if (li_0 == 1) {
      Comment("------------------------------", 
      "\n", "Connected. Account is verified.");
   }
   activation_flag = li_0 == 1;
   return (0);
}

int start() {
   int str_len_0;
   int li_4;
   double ld_8;
   double ld_16;
   double str2dbl_24;
   double PeriodsNum;
   double str2dbl_40;
   double workTF;
   double mult;
   bool li_68;
   bool OpenPositions;
   int order_total_76;
   double spread;
   double highest_88;
   double lowest_96;
   double ihigh_104;
   double ilow_112;
   int order_total_120;
   if (gi_100) {
      init();
      return (0);
   }
   if (!activation_flag) return (0);
   if (Period() != PERIOD_H1 || StringSubstr(Symbol(), 0, 6) != "EURUSD") return;
   if (IsDllsAllowed()) {
      Comment("------------------------------", 
         "\n", "Connected. Account is verified.", 
         "\n", "------------------------------", 
      "\n", "EuroKeyEA is running");
      str_len_0 = StringLen(OPT_KEY);
      if (str_len_0 == 9) li_4 = 4;
      if (str_len_0 == 8) li_4 = 3;
      ld_8 = 10.0 * StrToDouble(StringSubstr(OPT_KEY, 0, 1));
      ld_16 = 10.0 * StrToDouble(StringSubstr(OPT_KEY, 1, 1));
      str2dbl_24 = StrToDouble(StringSubstr(OPT_KEY, 2, 1));
      PeriodsNum = StrToDouble(StringSubstr(OPT_KEY, 3, 1));
      str2dbl_40 = StrToDouble(StringSubstr(OPT_KEY, 4, 1));
      workTF = StrToDouble(StringSubstr(OPT_KEY, 5, li_4));
      if (Digits == 3 || Digits == 2) mult = 0.01;
      if (Digits == 4 || Digits == 5) mult = 0.0001;
      li_68 = FALSE;
      OpenPositions = FALSE;
      order_total_76 = OrdersTotal();
      spread = Ask - Bid;
      highest_88 = iHighest(NULL, workTF, MODE_HIGH, PeriodsNum, 1);
      lowest_96 = iLowest(NULL, workTF, MODE_LOW, PeriodsNum, 1);
      ihigh_104 = iHigh(NULL, workTF, highest_88);
      ilow_112 = iLow(NULL, workTF, lowest_96);
      if (DayOfWeek() == 1 && Bid >= ihigh_104 && Hour() == 0) GlobalVariableSet(Symbol() + "Dbuy", Day());
      if (DayOfWeek() == 1 && Bid <= ilow_112 && Hour() == 0) GlobalVariableSet(Symbol() + "Dsell", Day());
      order_total_120 = OrdersTotal();
      if (IsTesting() == TRUE) order_total_120 = 0;
      for (int pos_64 = 0; pos_64 <= order_total_120; pos_64++)
         if (OrderSelect(pos_64, SELECT_BY_POS) == TRUE && OrderSymbol() == Symbol() && OrderMagicNumber() == 1231230) OpenPositions = TRUE;
      if (OpenPositions == FALSE && GlobalVariableGet(Symbol() + "H") != 0.0) GlobalVariableSet(Symbol() + "H", 0);
      if (OpenPositions == FALSE && GlobalVariableGet(Symbol() + "L") != 0.0) GlobalVariableSet(Symbol() + "L", 0);
      if (SpreadControl == TRUE) {
         if (Proc1(spread, ihigh_104, ilow_112, order_total_120, OpenPositions, order_total_76, mult, Day(), GlobalVariableGet(Symbol() + "Dbuy"), GlobalVariableGet(Symbol() + "Dsell"),
            Bid, Ask) == 1) {
            GlobalVariableSet(Symbol() + "Dbuy", Day());
            Alert("Spread is higher then 3 pips - no trade");
         }
         if (Proc2(spread, ihigh_104, ilow_112, order_total_120, OpenPositions, order_total_76, mult, Day(), GlobalVariableGet(Symbol() + "Dbuy"), GlobalVariableGet(Symbol() + "Dsell"),
            Bid, Ask) == 1) {
            GlobalVariableSet(Symbol() + "Dsell", Day());
            Alert("Spread is higher then 3 pips - no trade");
         }
      }
      if (Proc3(spread, ihigh_104, ilow_112, order_total_120, OpenPositions, order_total_76, mult, Day(), GlobalVariableGet(Symbol() + "Dbuy"), GlobalVariableGet(Symbol() + "Dsell"),
         Bid, Ask) == 1) {
         GlobalVariableSet(Symbol() + "Dbuy", Day());
         GlobalVariableSet(Symbol() + "H", ihigh_104);
         while (li_68 == FALSE || IsTradeContextBusy() == TRUE) {
            for (pos_64 = 0; pos_64 <= order_total_120; pos_64++)
               if (OrderSelect(pos_64, SELECT_BY_POS) == TRUE && OrderSymbol() == Symbol() && OrderMagicNumber() == 1231230) li_68 = TRUE;
            if (li_68 != TRUE) OrderSend(Symbol(), OP_BUY, Lot, Ask, 50, 0, 0, 0, 1231230, 0, Blue);
         }
      }
      if (Proc4(spread, ihigh_104, ilow_112, order_total_120, OpenPositions, order_total_76, mult, Day(), GlobalVariableGet(Symbol() + "Dbuy"), GlobalVariableGet(Symbol() + "Dsell"),
         Bid, Ask) == 1) {
         GlobalVariableSet(Symbol() + "Dsell", Day());
         GlobalVariableSet(Symbol() + "L", ilow_112);
         while (li_68 == FALSE || IsTradeContextBusy() == TRUE) {
            for (pos_64 = 0; pos_64 <= order_total_120; pos_64++)
               if (OrderSelect(pos_64, SELECT_BY_POS) == TRUE && OrderSymbol() == Symbol() && OrderMagicNumber() == 1231230) li_68 = TRUE;
            if (li_68 != TRUE) OrderSend(Symbol(), OP_SELL, Lot, Bid, 50, 0, 0, 0, 1231230, 0, Blue);
         }
      }
      for (pos_64 = 0; pos_64 <= order_total_120; pos_64++) {
         if (OrderSelect(pos_64, SELECT_BY_POS) == TRUE && OrderSymbol() == Symbol() && OrderMagicNumber() == 1231230 && OrderStopLoss() == 0.0 && OrderTakeProfit() == 0.0) {
            if (OrderType() == OP_BUY) OrderModify(OrderTicket(), OrderOpenPrice(), ihigh_104 - ld_16 * mult + spread, ihigh_104 + ld_8 * mult + spread, 0, CLR_NONE);
            if (OrderType() == OP_SELL) OrderModify(OrderTicket(), OrderOpenPrice(), ilow_112 + ld_16 * mult, ilow_112 - ld_8 * mult, 0, CLR_NONE);
         }
      }
      for (pos_64 = 0; pos_64 <= order_total_120; pos_64++) {
         if (OrderSelect(pos_64, SELECT_BY_POS) == TRUE && OrderSymbol() == Symbol() && OrderMagicNumber() == 1231230 && OrderStopLoss() != 0.0 && OrderTakeProfit() != 0.0) {
            if (GlobalVariableGet(Symbol() + "H") != 0.0 && OrderType() == OP_BUY && Bid - GlobalVariableGet(Symbol() + "H") >= str2dbl_40 * mult && OrderStopLoss() < OrderOpenPrice()) OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), OrderTakeProfit(), 0, CLR_NONE);
            if (GlobalVariableGet(Symbol() + "L") != 0.0 && OrderType() == OP_SELL && GlobalVariableGet(Symbol() + "L") - Ask >= str2dbl_40 * mult && OrderStopLoss() > OrderOpenPrice()) OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), OrderTakeProfit(), 0, CLR_NONE);
            if (OrderType() == OP_BUY && OrderStopLoss() >= OrderOpenPrice() && Bid - OrderStopLoss() >= str2dbl_24 * (2.0 * mult)) OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss() + str2dbl_24 * mult, OrderTakeProfit(), 0, CLR_NONE);
            if (OrderType() == OP_SELL && OrderStopLoss() <= OrderOpenPrice() && OrderOpenPrice() - Ask >= str2dbl_24 * (2.0 * mult)) OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss() - str2dbl_24 * mult, OrderTakeProfit(), 0, CLR_NONE);
         }
      }
   }
   return (0);
}

int Proc1(double spr, double h, double l, int ordertotal, int openpos, int ordertotal2, double multiplier, int d, int dbuy, int dsell, double bid_, double ask_)
{
   if ((h <= bid_) && (3*multiplier <= spr)) return (1);
    else return(0);
}

int Proc2(double a0, double a1, double a2, int a3, int a4, int a5, double a6, int a7, int a8, int a9, double a10, double a11)
{
   if ((a2 >= a10) && (3 * a6 <= a0)) return (1);
    else return(0);
}

int Proc3(double a0, double a1, double a2, int a3, int a4, int a5, double a6, int a7, int a8, int a9, double a10, double a11)
{
   if ((a4!=1) && (a1<=a10) && (a7!=a8)) return(1);
    else return(0);
}

int Proc4(double a0, double a1, double a2, int a3, int a4, int a5, double a6, int a7, int a8, int a9, double a10, double a11)
{
   if ((a4!=1) && (a2>=a10) && (a7!=a9)) return(1);
    else return(0);
}

int InitDll(int a0)
{
   return(1);
}