/*
   Generated by EX4-TO-MQ4 decompiler V4.0.224.1 []
   Website: http://purebeam.biz
   E-mail : purebeam@gmail.com
*/
#import "CloveriX-DLL.dll"
   int InitDll();
   double Proc1(double& a0[], double& a1[], double a2, double a3, double a4, double a5, int a6, int a7);
   double Proc2(double& a0[], double& a1[], double a2, double a3, double a4, double a5, int a6, int a7);
   double Proc3(double& a0[], double& a1[], double a2, double a3, double a4, double a5);
   double Proc4(double& a0[], double& a1[], double a2, double a3, double a4, double a5);
   double Proc5(double& a0[], double& a1[], double a2, double a3, double a4, double a5, double a6, double a7);
   double Proc6(double& a0[], double& a1[], double a2, double a3, double a4, double a5, double a6, double a7);
#import

extern string CloveriX_works_on__________ = " ====== EURGBP and EURCHF ===== ";
extern double Risk = 0.25;
extern double MaxTP = 10.0;
extern string Trading_Hours_Settings_______ = "[ GMT_Offset = Market Watch - GMT ]";
extern int GMT_Offset = 0;
extern string ADDITIONAL_SETTINGS______ = "===============================";
extern bool VariableSpreads = FALSE;
extern double VarSpreadFix = 3.0;
extern bool PlaceExternalTP = FALSE;
extern bool CloseOnlyByExtTP = FALSE;
extern bool Spread5PipsAlert = FALSE;
extern bool ErrorAlert = FALSE;
extern bool UseFixedSL = FALSE;
extern double FixedSL = 0.0;
extern string GMT_TRADING_HOURS_SETTINGS_ = "===============================";
extern int Start = 21;
extern int End = 23;
int g_error_176;
bool gi_180;
bool gi_184;
bool gi_188;
int gi_192;
int gi_196;
int g_slippage_200;
int g_slippage_204;
int gi_208 = 4;
double gda_220[3];
double gda_224[3];
double gd_228;
double gd_236;
double gd_244;
double gd_252;
double gd_260;
double gd_268;
double gd_276;
double g_ima_284;
double g_ima_292;
double g_ima_300;
double g_ima_308;
double g_irsi_316;
double g_irsi_324;
double g_iwpr_332;
double g_iwpr_340;
double gd_356;
double gd_364;
double gd_372;
double g_price_380;
double gd_388;
double gd_unused_404;
double gd_unused_444;
double gd_452;
double gd_460;
string gs_468;
bool gi_unused_476 = FALSE;
bool g_bool_480 = FALSE;
int gi_unused_484 = 2;

int init() {
   g_bool_480 = InitDll() == 1;
   if (!g_bool_480) Alert("Invalid DLL file");
   gs_468 = StringSubstr(Symbol(), 0, 6);
   gd_268 = 10000.0 * (Ask - Bid);
   if (gs_468 == "EURCHF" || gs_468 == "EURGBP" && Period() == PERIOD_M15) Coment();
   gi_192 = Start + GMT_Offset;
   gi_196 = End + GMT_Offset;
   gd_unused_444 = Digits;
   if (gi_192 > 23) gi_192 -= 24;
   if (gi_196 > 23) gi_196 -= 24;
   gd_unused_404 = iTime(NULL, PERIOD_M15, 1);
   MaxTP /= 10000.0;
   FixedSL /= 10000.0;
   g_slippage_200 = 0;
   g_slippage_204 = 1;
   if (PlaceExternalTP == TRUE && CloseOnlyByExtTP == TRUE) gi_unused_476 = TRUE;
   return (0);
}

int start() {
   if (!IsDllsAllowed()) {
      Alert("You have not allowed DLL calls in settings");
      return;
   }
   if (!g_bool_480) {
      Alert("Invalid DLL file");
      return;
   }
   gs_468 = StringSubstr(Symbol(), 0, 6);
   if ((gs_468 != "EURCHF" && gs_468 != "EURGBP") || Period() != PERIOD_M15) {
      Comment("------------------", 
      "\n", "WRONG TRADING PAIR OR TIMEFRAME");
      return;
   }
   Coment();
   HideTestIndicators(TRUE);
   gi_180 = FALSE;
   gi_184 = FALSE;
   gi_188 = FALSE;
   g_error_176 = GetLastError();
   if (FixedSL < gd_260 + gd_276 && UseFixedSL) {
      Alert("Incorrect FixedSL. Check parameters.");
      return;
   }
   gd_260 = MathMax(MarketInfo(Symbol(), MODE_STOPLEVEL), MarketInfo(Symbol(), MODE_FREEZELEVEL));
   gda_220[0] = High[1];
   gda_220[1] = High[2];
   gda_220[2] = High[3];
   gda_220[3] = High[4];
   gda_224[0] = Low[1];
   gda_224[1] = Low[2];
   gda_224[2] = Low[3];
   gda_224[3] = Low[4];
   gd_228 = NormalizeDouble(Proc1(gda_220, gda_224, gd_228, gd_236, gd_268, gd_276, gi_184, gi_188), 5);
   gd_236 = NormalizeDouble(Proc2(gda_220, gda_224, gd_228, gd_236, gd_268, gd_276, gi_184, gi_188), 5);
   gd_244 = NormalizeDouble(Proc3(gda_220, gda_224, gd_228, gd_236, gd_268, gd_276), 5);
   gd_252 = NormalizeDouble(Proc4(gda_220, gda_224, gd_228, gd_236, gd_268, gd_276), 5);
   gd_268 = 10000.0 * (Ask - Bid);
   gd_276 = Ask - Bid;
   g_ima_284 = iMA(NULL, 0, 9, 0, MODE_SMA, PRICE_MEDIAN, 1);
   g_ima_292 = iMA(NULL, 0, 9, 0, MODE_SMA, PRICE_MEDIAN, 2);
   g_ima_300 = iMA(NULL, 0, 9, 0, MODE_SMA, PRICE_MEDIAN, 3);
   g_ima_308 = iMA(NULL, 0, 18, 0, MODE_SMA, PRICE_MEDIAN, 0);
   g_irsi_316 = iRSI(NULL, 0, 7, PRICE_OPEN, 0);
   g_irsi_324 = iRSI(NULL, 0, 7, PRICE_OPEN, 1);
   g_iwpr_332 = iWPR(NULL, 0, 14, 0);
   g_iwpr_340 = iWPR(NULL, 0, 14, 1);
   gd_356 = MathMin(gd_252, gd_236);
   gd_372 = MathMax(gd_244, gd_228);
   gd_364 = NormalizeDouble(Proc5(gda_220, gda_224, gd_228, gd_236, gd_268, gd_276, gd_356, gd_372), 5);
   g_price_380 = NormalizeDouble(Proc6(gda_220, gda_224, gd_228, gd_236, gd_268, gd_276, gd_356, gd_372), 5);
   gd_452 = gd_260 / 10000.0;
   if (VariableSpreads == TRUE) {
      gd_388 = VarSpreadFix / 10000.0;
      gi_208 = 6;
   } else gd_388 = Ask - Bid;
   CHF();
   gi_180 = FALSE;
   gi_184 = FALSE;
   gi_188 = FALSE;
   GBP();
   if (g_error_176 >= 2/* COMMON_ERROR */ && g_error_176 != 130/* INVALID_STOPS */ && g_error_176 <= 150 && ErrorAlert == TRUE) Alert("error # ", g_error_176, " Find a definition here http://docs.mql4.com/constants/errors");
   return (0);
}

double Lot() {
   double ld_0 = NormalizeDouble(AccountBalance() / 1000.0 * Risk, 2);
   ld_0 = MarketInfo(Symbol(), MODE_LOTSTEP) * NormalizeDouble(ld_0 / MarketInfo(Symbol(), MODE_LOTSTEP), 0);
   ld_0 = MathMin(MarketInfo(Symbol(), MODE_MAXLOT), ld_0);
   ld_0 = MathMax(MarketInfo(Symbol(), MODE_MINLOT), ld_0);
   return (ld_0);
}

int Coment() {
   Comment("------------------", 
      "\n", "CloveriX is Enabled", 
      "\n", "------------------", 
      "\n", "======= SERVER TIME =======", 
      "\n", "Trading Start Hour: ", gi_192, ":00", 
      "\n", "Trading End Hour: ", gi_196, ":00", 
      "\n", "------------------", 
      "\n", "Risk = ", 100.0 * Risk, "%", 
      "\n", "TradingLot = ", Lot(), 
      "\n", "Current Spread = ", gd_268, 
   "\n", "Max Available Spread = ", gi_208);
   return (0);
}

int OC(double a_price_0) {
   OrderClose(OrderTicket(), OrderLots(), a_price_0, g_slippage_204, Red);
   return (0);
}

int OrderModif() {
   for (int l_pos_0 = OrdersTotal() - 1; l_pos_0 >= 0; l_pos_0--) {
      if (OrderSelect(l_pos_0, SELECT_BY_POS) == TRUE && OrderMagicNumber() == 318200 || OrderMagicNumber() == 318201 && StringSubstr(OrderSymbol(), 0, 6) == gs_468) {
         gi_180 = TRUE;
         if (OrderType() == OP_BUY) {
            if ((gda_220[0] + 0.0003 <= g_ima_284 && Bid >= gda_220[0] - 0.0001) || (gda_220[0] <= g_ima_284 && gda_220[1] <= g_ima_292 && gda_220[2] <= g_ima_300 && Bid >= gda_220[0])) {
               OC(Bid);
               gd_460 = 1;
            }
            if (Bid > g_price_380 - 0.00001 && CloseOnlyByExtTP == FALSE) OC(Bid);
            if (Bid >= gda_220[0] && gda_220[0] <= gda_220[1] && gda_220[1] <= gda_220[2] && gda_220[2] <= gda_220[3] && g_ima_284 <= g_ima_300 && gs_468 == "EURGBP") {
               OC(Bid);
               gd_460 = 1;
            }
            if (g_price_380 - OrderOpenPrice() > MaxTP && g_price_380 - Bid >= gd_452 && PlaceExternalTP == TRUE)
               if ((Ask - OrderStopLoss() >= gd_260 && FixedSL) || !FixedSL) OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), OrderOpenPrice() + MaxTP, 0, CLR_NONE);
            if (g_price_380 - OrderOpenPrice() <= MaxTP && g_price_380 != OrderTakeProfit() && g_price_380 - Bid >= gd_452 && PlaceExternalTP == TRUE)
               if ((Ask - OrderStopLoss() >= gd_260 && FixedSL) || !FixedSL) OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), g_price_380, 0, CLR_NONE);
            if (Bid - OrderOpenPrice() > MaxTP - 0.00001 && CloseOnlyByExtTP == FALSE) OC(Bid);
            if (OrderOpenPrice() - Low[0] >= 0.001 && OrderOpenPrice() - Bid <= (OrderOpenPrice() - Low[0]) / 2.0) {
               OC(Bid);
               gd_460 = 1;
            }
         }
         if (OrderType() == OP_SELL) {
            if ((gda_224[0] - 0.0003 >= g_ima_284 && Bid <= gda_224[0] + 0.0001) || (gda_224[0] >= g_ima_284 && gda_224[1] >= g_ima_292 && gda_224[2] >= g_ima_300 && Bid <= gda_224[0])) {
               OC(Ask);
               gd_460 = 2;
            }
            if (Ask - gd_388 < gd_364 + 0.00001 && CloseOnlyByExtTP == FALSE) OC(Ask);
            if (Bid <= gda_224[0] && gda_224[0] >= gda_224[1] && gda_224[1] >= gda_224[2] && gda_224[2] >= gda_224[3] && g_ima_284 >= g_ima_300 && gs_468 == "EURGBP") {
               OC(Ask);
               gd_460 = 2;
            }
            if (OrderOpenPrice() - gd_364 - gd_388 > MaxTP && Ask - gd_364 - gd_388 >= gd_452 && PlaceExternalTP == TRUE)
               if ((Ask - OrderStopLoss() >= gd_260 && FixedSL) || !FixedSL) OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), OrderOpenPrice() - MaxTP, 0, CLR_NONE);
            if (OrderOpenPrice() - gd_364 - gd_388 <= MaxTP && gd_364 + gd_388 != OrderTakeProfit() && Ask - gd_364 - gd_388 >= gd_452 && PlaceExternalTP == TRUE)
               if ((Ask - OrderStopLoss() >= gd_260 && FixedSL) || !FixedSL) OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), gd_364 + gd_388, 0, CLR_NONE);
            if (OrderOpenPrice() - Ask > MaxTP - 0.00001 && CloseOnlyByExtTP == FALSE) OC(Ask);
            if (High[0] - OrderOpenPrice() >= 0.001 && Ask - OrderOpenPrice() <= (High[0] - OrderOpenPrice()) / 2.0) {
               OC(Ask);
               gd_460 = 2;
            }
         }
      }
   }
   return (0);
}

int CheckParam() {
   if (gd_268 >= gi_208 + 1 && Spread5PipsAlert == TRUE) Alert("Spread is higher than ", gi_208, " pips - NO TRADE");
   if ((DayOfWeek() == 5 && Hour() > 12) || (DayOfWeek() == 1 && Hour() < 12)) gi_184 = TRUE;
   if (gi_192 > gi_196 && Hour() >= gi_192 || Hour() < gi_196) gi_188 = TRUE;
   if (gi_192 < gi_196 && (Hour() >= gi_192 && Hour() < gi_196)) gi_188 = TRUE;
   return (0);
}

string GBP() {
   double l_price_0;
   if (gs_468 != "EURGBP" || DayOfWeek() == 0) return;
   Coment();
   CheckParam();
   OrderModif();
   if (FixedSL != 0.0 && UseFixedSL == TRUE) l_price_0 = Ask - FixedSL;
   if (Bid <= g_ima_308 && gd_460 != 1.0 && g_ima_284 > g_ima_300 && gi_184 != TRUE && gd_268 <= gi_208 && Ask - gd_388 < gd_356 + 0.00001 && gi_180 != TRUE && gi_188 == TRUE) {
      OrderSend(Symbol(), OP_BUY, Lot(), Ask, g_slippage_200, l_price_0, 0, 0, 318200, 0, Blue);
      gd_460 = 0;
   }
   if (FixedSL != 0.0 && UseFixedSL == TRUE) l_price_0 = Bid + FixedSL;
   if (Bid >= g_ima_308 && gd_460 != 2.0 && g_ima_284 < g_ima_300 && gi_184 != TRUE && gd_268 <= gi_208 && Period() == PERIOD_M15 && Bid > gd_372 - 0.00001 && gi_180 != TRUE &&
      gi_188 == TRUE) {
      OrderSend(Symbol(), OP_SELL, Lot(), Bid, g_slippage_200, l_price_0, 0, 0, 318201, 0, Blue);
      gd_460 = 0;
   }
   return ("");
}

void CHF() {
   double l_price_0;
   if (gs_468 != "EURCHF" || DayOfWeek() == 0) return;
   Coment();
   CheckParam();
   OrderModif();
   if (FixedSL != 0.0 && UseFixedSL == TRUE) l_price_0 = Ask - FixedSL;
   if (gd_460 != 1.0 && g_ima_284 > g_ima_300 && gi_184 != TRUE && gd_268 <= gi_208 && Ask - gd_388 < gd_356 + 0.00001 && gi_180 != TRUE && gi_188 == TRUE) {
      OrderSend(Symbol(), OP_BUY, Lot(), Ask, g_slippage_200, l_price_0, 0, 0, 318200, 0, Blue);
      gd_460 = 0;
   }
   if (FixedSL != 0.0 && UseFixedSL == TRUE) l_price_0 = Bid + FixedSL;
   if (gd_460 != 2.0 && g_ima_284 < g_ima_300 && gi_184 != TRUE && gd_268 <= gi_208 && Bid > gd_372 - 0.00001 && gi_180 != TRUE && gi_188 == TRUE) {
      OrderSend(Symbol(), OP_SELL, Lot(), Bid, g_slippage_200, l_price_0, 0, 0, 318201, 0, Blue);
      gd_460 = 0;
   }
}
