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

extern int MaxTrades = 1;
extern int stoploss = 250;
extern int takeprofit = 400;
extern double TrailingStop = 75.0;
extern bool UseHourTrade = FALSE;
extern int FromHourTrade = 7;
extern int ToHourTrade = 17;
extern string MM_Parameters = "---------- Money Management";
extern double Lots = 1.0;
extern bool MM = FALSE;
extern bool AccountIsMicro = FALSE;
extern int Risk = 10;
double g_point_136;
extern int ID = 22200;

void deinit() {
   Comment("");
}

int start() {
   double l_price_116;
   double l_price_124;
   if (Point == 0.00001) g_point_136 = 0.0001;
   else {
      if (Point == 0.001) g_point_136 = 0.01;
      else g_point_136 = Point;
   }
   string ls_0 = "2020.05.05";
   int l_str2time_8 = StrToTime(ls_0);
   if (TimeCurrent() >= l_str2time_8) {
      Alert("Please Update your EA .. This version has been expired!");
      return (0);
   }
   if (UseHourTrade) {
      if (!(Hour() >= FromHourTrade && Hour() <= ToHourTrade)) {
         Comment("Non-Trading Hours!");
         return (0);
      }
   }
   double l_ihigh_12 = iHigh(NULL, PERIOD_H4, 1);
   double l_ihigh_20 = iHigh(NULL, PERIOD_H4, 2);
   double l_ihigh_28 = iHigh(NULL, PERIOD_H4, 3);
   double l_ihigh_36 = iHigh(NULL, PERIOD_H4, 4);
   double l_ilow_44 = iLow(NULL, PERIOD_H4, 1);
   double l_ilow_52 = iLow(NULL, PERIOD_H4, 2);
   double l_ilow_60 = iLow(NULL, PERIOD_H4, 3);
   double l_ilow_68 = iLow(NULL, PERIOD_H4, 4);
   double l_iadx_76 = iADX(NULL, 0, 29, PRICE_CLOSE, MODE_MAIN, 0);
   double l_iadx_84 = iADX(NULL, 0, 29, PRICE_CLOSE, MODE_MAIN, 0);
   double l_imacd_92 = iMACD(NULL, 0, 90, 99, 88, PRICE_TYPICAL, MODE_MAIN, 0);
   double l_imomentum_100 = iMomentum(NULL, 0, 200, PRICE_OPEN, 0);
   double l_irsi_108 = iRSI(NULL, 0, 21, PRICE_CLOSE, 0);
   if (MM) Lots = subLotSize();
   if (iMACD(NULL, 0, 14, 26, 9, PRICE_CLOSE, MODE_MAIN, 0) > iMACD(NULL, 0, 14, 26, 9, PRICE_CLOSE, MODE_MAIN, 1) || (l_ilow_44 > l_ilow_52 && l_ilow_68 > l_ilow_52 &&
      l_ihigh_28 > l_ihigh_20) && l_imomentum_100 > 100.0 && l_irsi_108 > 70.0 && l_imacd_92 > 0.0) {
      closesell(ID);
      if (orderscnt() < MaxTrades) {
         if (stoploss == 0) l_price_116 = 0;
         else l_price_116 = Ask - stoploss * g_point_136;
         if (takeprofit == 0) l_price_124 = 0;
         else l_price_124 = Ask + takeprofit * g_point_136;
         OrderSend(Symbol(), OP_BUY, Lots, Ask, 2, l_price_116, l_price_124, "My Own Expert", ID, 0, Blue);
         PlaySound("Alert.wav");
      }
   }
   if (iMACD(NULL, 0, 14, 26, 9, PRICE_CLOSE, MODE_MAIN, 0) < iMACD(NULL, 0, 14, 26, 9, PRICE_CLOSE, MODE_MAIN, 1) || (l_ihigh_20 > l_ihigh_12 && l_ihigh_20 > l_ihigh_36 &&
      l_ilow_52 > l_ilow_60) && l_imomentum_100 < 100.0 && l_irsi_108 < 30.0 && l_imacd_92 < 0.0) {
      closebuy(ID);
      if (orderscnt() < MaxTrades) {
         if (stoploss == 0) l_price_116 = 0;
         else l_price_116 = Bid + stoploss * g_point_136;
         if (takeprofit == 0) l_price_124 = 0;
         else l_price_124 = Bid - takeprofit * g_point_136;
         OrderSend(Symbol(), OP_SELL, Lots, Bid, 2, l_price_116, l_price_124, "My Own Expert", ID, 0, Red);
         PlaySound("Alert.wav");
      }
   }
   double l_ord_open_price_132 = 0;
   double l_price_140 = 0;
   double l_ord_takeprofit_148 = 0;
   double l_ord_lots_156 = 0;
   int l_ticket_164 = 0;
   int l_cmd_168 = 0;
   for (int l_pos_172 = 0; l_pos_172 < OrdersTotal(); l_pos_172++) {
      if (OrderSelect(l_pos_172, SELECT_BY_POS, MODE_TRADES) != FALSE) {
         if (OrderSymbol() == Symbol()) {
            if (OrderMagicNumber() == ID) {
               if (OrderType() <= OP_SELL) {
                  l_ticket_164 = OrderTicket();
                  l_cmd_168 = OrderType();
                  l_ord_open_price_132 = OrderOpenPrice();
                  l_price_140 = OrderStopLoss();
                  l_ord_takeprofit_148 = OrderTakeProfit();
                  l_ord_lots_156 = OrderLots();
                  if (l_cmd_168 == OP_BUY) {
                     if (TrailingStop > 0.0 && Bid - l_ord_open_price_132 > TrailingStop * g_point_136 && l_price_140 < Bid - TrailingStop * g_point_136 || l_price_140 == 0.0) {
                        l_price_140 = Bid - TrailingStop * g_point_136;
                        OrderModify(l_ticket_164, l_ord_open_price_132, l_price_140, l_ord_takeprofit_148, 0, Red);
                     }
                  } else {
                     if (TrailingStop > 0.0 && l_ord_open_price_132 - Ask > TrailingStop * g_point_136 && l_price_140 > Ask + TrailingStop * g_point_136 || l_price_140 == 0.0) {
                        l_price_140 = Ask + TrailingStop * g_point_136;
                        OrderModify(l_ticket_164, l_ord_open_price_132, l_price_140, l_ord_takeprofit_148, 0, Red);
                     }
                  }
               }
            }
         }
      }
   }
   return (0);
}

double subLotSize() {
   double ld_ret_0 = MathCeil(AccountFreeMargin() * Risk / 1000.0) / 100.0;
   if (AccountIsMicro == FALSE) {
      if (ld_ret_0 < 0.1) ld_ret_0 = Lots;
      if (ld_ret_0 > 0.5 && ld_ret_0 < 1.0) ld_ret_0 = 0.5;
      if (ld_ret_0 > 1.0) ld_ret_0 = MathCeil(ld_ret_0);
      if (ld_ret_0 > 100.0) ld_ret_0 = 100;
   } else {
      if (ld_ret_0 < 0.01) ld_ret_0 = Lots;
      if (ld_ret_0 > 1.0) ld_ret_0 = MathCeil(ld_ret_0);
      if (ld_ret_0 > 100.0) ld_ret_0 = 100;
   }
   return (ld_ret_0);
}

void closebuy(int a_magic_0) {
   for (int l_pos_4 = 0; l_pos_4 < OrdersTotal(); l_pos_4++) {
      OrderSelect(l_pos_4, SELECT_BY_POS);
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == a_magic_0 && OrderType() == OP_BUY) OrderClose(OrderTicket(), OrderLots(), Bid, 9, Yellow);
   }
}

void closesell(int a_magic_0) {
   for (int l_pos_4 = 0; l_pos_4 < OrdersTotal(); l_pos_4++) {
      OrderSelect(l_pos_4, SELECT_BY_POS);
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == a_magic_0 && OrderType() == OP_SELL) OrderClose(OrderTicket(), OrderLots(), Ask, 9, Yellow);
   }
}

int orderscnt() {
   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))
         if (OrderSymbol() == Symbol() && ID == OrderMagicNumber()) l_count_0++;
   }
   return (l_count_0);
}