//+------------------------------------------------------------------+
//|                                                      RN V1;1.mq4 |
//|                                                              GGG |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
/*
   Generated by EX4-TO-MQ4 decompiler V4.0.224.1 []
   Website: http://purebeam.biz
   E-mail : purebeam@gmail.com
*/
#property copyright "Traders Secret Library"
#property link      "www.rovernorthforexsystem.com"

#include <WinUser32.mqh>

extern string note = " ======= Authentication SETTINGS ======";
extern string note0 = " ======= GENERAL SETTINGS ======";
extern int HourStartTrade = 8;
extern int MinuteStartTrade = 0;
extern int HourStopTrade = 23;
extern int MinuteStopTrade = 0;

extern int per_MA_big = 144;
extern int per_MA_small = 10;

extern int Reverse_pip_range = 5;
extern int MagicNumber = 34456;
extern bool AllowTradeLive = TRUE;
extern bool RequestConfirm = FALSE;
extern bool EnableAlerts = FALSE;
extern bool EnableEmail = FALSE;
extern color ArrowUP = Yellow;
extern color ArrowDOWN = Red;
extern string note1 = " ===== Money Management ======";
extern double LotSize = 0.1;
extern int StopLoss = 50;
extern int TakeProfit = 30;
extern int Slippage = 3;
extern string note2 = " ===== Advanced Money Management ======";
extern bool UseTrailing = TRUE;
extern int TrailingStop = 15;
extern int TrailingStep = 10;
extern bool UseProfitGrid = FALSE;
extern int GridLevel = 15;
extern int LockProfitPips = 3;
extern double MultiplyFactor = 1.5;
int gi_216 = 4;
int meth = MODE_EMA;
int prix = PRICE_CLOSE;
int gi_228 = 0;
double point;
double slip;
double TS;
double gd_256;
double gd_264;
int g_ticket_272 = 0;
int digit_lot = 0;
int gi_280 = 1;
int g_mb_code_284 = -3;
bool gi_288 = FALSE;
bool gi_292 = FALSE;
bool gi_unused_296 = TRUE;
bool gi_unused_300 = TRUE;
double g_price_304 = 0.0;
double gd_unused_312 = 0.0;
string gs_320 = "";
string gs_328 = "";
int gi_336 = 0;
bool gi_340 = FALSE;
bool gi_344 = TRUE;
int gia_348[64] = {65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47};
bool gi_unused_352 = FALSE;
string gs_dummy_356;
int gi_unused_364 = 0;
int gi_unused_368 = 1;
string gs_dummy_372;
int gi_unused_380 = 0;
int gi_unused_384 = 0;
int gi_unused_388 = 0;
string gs_dummy_392;
int gia_unused_400[1];
string gs_unused_404 = "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
int g_time_412 = 0;

int init() {
   if (Point == 0.00001) point = 0.0001;
   else {
      if (Point == 0.001) point = 0.01;
      else point = Point;
   }
   gi_280 = point / Point;
   gi_216 = MathRound(TakeProfit / GridLevel) - 1.0;
   slip = Slippage * point / Point;
   TS = point * TrailingStop;
        if (MarketInfo(Symbol(), MODE_MINLOT) == 0.01) digit_lot = 2;
   else if (MarketInfo(Symbol(), MODE_MINLOT) == 0.1) digit_lot = 1;
   gs_320 = "\n Initialising ...";
   Comment(gs_320);
   gs_328 = "";
   double l_lots_0 = LotSize;
   for (int l_count_8 = 0; l_count_8 < gi_216; l_count_8++) {
      gs_328 = gs_328 + DoubleToStr(l_lots_0, 2) + "  ";
      l_lots_0 = NormalizeDouble(l_lots_0 * MultiplyFactor, digit_lot);
   }
   g_price_304 = -1;
   int l_ord_total_12 = OrdersTotal();
   for (int l_pos_16 = 0; l_pos_16 < l_ord_total_12; l_pos_16++) {
      OrderSelect(l_pos_16, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol() && OrderCloseTime() == 0 && OrderMagicNumber() == MagicNumber) {
         if (OrderType() == OP_BUY) {
            if (OrderOpenPrice() > g_price_304) {
               g_price_304 = OrderOpenPrice();
               g_ticket_272 = OrderTicket();
            }
         } else {
            if (OrderType() == OP_SELL) {
               if (g_price_304 < 0.0 || OrderOpenPrice() < g_price_304) {
                  g_price_304 = OrderOpenPrice();
                  g_ticket_272 = OrderTicket();
               }
            }
         }
      }
   }
   gi_336 = FALSE;
   gi_340 = FALSE;
   gi_344 = TRUE;
   return (0);
}

int deinit() {
  // ObjectsDeleteAll();
   Comment("");
   return (0);
}

int start() {
   double ma_small__0;
   double ma_big__0;
   if (gi_344 == TRUE && gi_336 == FALSE)
      {
      gs_320 = "\nRNTA v1\n authenticating...";
      Comment(gs_320);
      gi_336 = TRUE;
      if (gi_336 == TRUE) gi_340 = TRUE;
      gi_344 = FALSE;
     }
   if (gi_340 == TRUE)
     {
      gs_320 = get_message();
      if (Trading_Hours() && gi_228 <= 0) {
         if (g_ticket_272 == 0 && !gi_288 || !gi_292) check_for_open();
         if (gi_288) {
            gs_320 = gs_320 
               + "\n SELL Retracement Present" 
               + "\n Waiting for Sell Signal";
            ma_small__0 = iMA(Symbol(), PERIOD_M15, per_MA_small, 0, meth, prix, 0);
            ma_big__0   = iMA(Symbol(), PERIOD_M15, per_MA_big, 0, meth, prix, 0);
            if (ma_small__0 - Bid < point * Reverse_pip_range && ma_small__0 < ma_big__0 && Bid < ma_small__0)
              {
               if (EnableAlerts) Alert(Symbol() + " Sell Order Signal");
               if (EnableEmail) SendMail("Rover North Sell Alert!", "Sell alert on " + Symbol() + "[" + Period() + "m]");
               if (AllowTradeLive) 
                  {
                  if (RequestConfirm)
                     {
                     PlaySound("alert.wav");
                     g_mb_code_284 = MessageBox(Symbol() + " Sell Order Signal \n Enter the Trade?", "RNTA Signal", MB_YESNO|MB_ICONQUESTION);
                     if (g_mb_code_284 == IDYES) OpenTrade(OP_SELL);
                     else gi_228 = 4;
                  }
                   else OpenTrade(OP_SELL);
               } 
               else gi_228 = 4;
               ObjectCreate("RNTA_Buy" + Time[0], OBJ_ARROW, 0, TimeCurrent(), Bid);
               ObjectSet("RNTA_Buy" + Time[0], OBJPROP_ARROWCODE, 230);
               ObjectSet("RNTA_Buy" + Time[0], OBJPROP_COLOR, ArrowDOWN);
            }
         } else {
            if (gi_292) {
               gs_320 = gs_320 
                  + "\n BUY Retracement Present" 
               + "\n Waiting for Buy Signal";
               ma_small__0 = iMA(Symbol(), PERIOD_M15, per_MA_small, 0, meth, prix, 0);
               ma_big__0 = iMA(Symbol(), PERIOD_M15, per_MA_big, 0, meth, prix, 0);
               if (Bid - ma_small__0 < point * Reverse_pip_range && ma_small__0 > ma_big__0 && Bid > ma_small__0) {
                  if (EnableAlerts) Alert(Symbol() + " Buy Order Signal");
                  if (EnableEmail) SendMail("Rover North Buy Alert!", "Buy alert on " + Symbol() + "[" + Period() + "m]");
                  if (AllowTradeLive) {
                     if (RequestConfirm) {
                        PlaySound("alert.wav");
                        g_mb_code_284 = MessageBox(Symbol() + " Buy Order Signal \n Enter the Trade?", "RNTA Signal", MB_YESNO|MB_ICONQUESTION);
                        if (g_mb_code_284 == IDYES) OpenTrade(OP_BUY);
                        else gi_228 = 4;
                     } else OpenTrade(OP_BUY);
                  } else gi_228 = 4;
                  ObjectCreate("RNTA_Sell" + Time[0], OBJ_ARROW, 0, TimeCurrent(), Bid);
                  ObjectSet("RNTA_Sell" + Time[0], OBJPROP_ARROWCODE, 228);
                  ObjectSet("RNTA_Sell" + Time[0], OBJPROP_COLOR, ArrowUP);
               }
            }
         }
      } else
         if (NewBar()) gi_228--;
      if (g_ticket_272 != 0) {
         gs_320 = gs_320 
            + "\n>>>>>>>>>>>>>>>>>>>>>>>>>>" 
            + "\n We are in the trade" 
         + "\n>>>>>>>>>>>>>>>>>>>>>>>>>>";
              if (UseTrailing) TrailStop();
         else if (UseProfitGrid) update_trades();
         check_Ticket();
      }
      Comment(gs_320);
   }
   return (0);
}
//===============================================================================
string get_message() {
   string ls_ret_0 = "";
   if (LotSize < MarketInfo(Symbol(), MODE_MINLOT)) {
      ls_ret_0 = "\n === Rover North Trade Assistant ===\n\n ERROR: Wrong Minimum Lot Size.\n\n";
      ls_ret_0 = ls_ret_0 + " Your broker allows a minimum lot size of " + DoubleToStr(MarketInfo(Symbol(), MODE_MINLOT), 2) + "lots,\n";
      ls_ret_0 = ls_ret_0 + " while you set " + DoubleToStr(LotSize, 2) + " lots.\n";
      ls_ret_0 = ls_ret_0 + " Please check!\n";
      gd_unused_312 = 1;
      return (ls_ret_0);
   }
   ls_ret_0 = ls_ret_0 
      + "\n === Rover North Trade Assistant ===" 
      + "\n Server Time: " + TimeToStr(TimeCurrent(), TIME_MINUTES) 
      + "\n =========================" 
   + "\n Status: RUNNING ...";
   gd_unused_312 = 0;
   ls_ret_0 = ls_ret_0 
      + "\n Authentication: OK\n" 
      + "\n =========================" 
      + "\n Lot Size: " + DoubleToStr(LotSize, 2) + " lots\n" 
      + "\n Slippage: " + Slippage 
   + "\n Spread: " + DoubleToStr(MarketInfo(Symbol(), MODE_SPREAD) / gi_280, 1);
   if (UseTrailing) {
      ls_ret_0 = ls_ret_0 
         + "\n =========================" 
         + "\n Trailing ENABLED" 
         + "\n Trailing STOP: " + TrailingStop 
      + "\n Trailing Step: " + TrailingStep;
   } else {
      if (UseProfitGrid) {
         ls_ret_0 = ls_ret_0 
            + "\n =========================" 
            + "\n Grid Profit ENABLED" 
            + "\n Grid Level: " + GridLevel 
            + "\n Lock Pips for Profit: " + LockProfitPips 
            + "\n Multiply Factor: " + DoubleToStr(MultiplyFactor, 2) 
         + "\n Lots to open: " + gi_216 + " ( " + gs_328 + ")";
      }
   }
   return (ls_ret_0);
}

int check_for_open() 
  {
          if (check_bar(2) == 1 && check_bar(1) == 1) gi_288 = TRUE;
   else   if (check_bar(2) == 0 && check_bar(1) == 0) gi_292 = TRUE;
   return (-1);
 }

int check_bar(int ai_0) {
   double big_60_0 = iMA(Symbol(), PERIOD_H1, per_MA_big, 0, meth, prix, 0);
   double big_30_0 = iMA(Symbol(), PERIOD_M30, per_MA_big, 0, meth, prix, 0);
   double big_15_0 = iMA(Symbol(), PERIOD_M15, per_MA_big, 0, meth, prix, ai_0);
   double small_60_0 = iMA(Symbol(), PERIOD_H1, per_MA_small, 0, meth, prix, 0);
   double small_30_0 = iMA(Symbol(), PERIOD_M30, per_MA_small, 0, meth, prix, 0);
   double small_15_0 = iMA(Symbol(), PERIOD_M15, per_MA_small, 0, meth, prix, ai_0);
   double ld_52 = (High[ai_0] + Low[ai_0]) / 2.0;
   if (High[ai_0] < small_15_0 && small_15_0 < big_15_0 && small_30_0 < big_30_0 && small_60_0 < big_60_0 && High[ai_0] < small_30_0 && High[ai_0] < small_60_0) return (1);
   if (Low[ai_0] > small_15_0 && small_15_0 > big_15_0 && small_30_0 > big_30_0 && small_60_0 > big_60_0 && Low[ai_0] > small_30_0 && Low[ai_0] > small_60_0) return (0);
   return (-1);
}

int OpenTrade(int a_cmd_0) {
   double l_price_4;
   double l_price_12;
   double l_price_20;
   int li_28 = 1;
   RefreshRates();
   if (a_cmd_0 == OP_BUY) l_price_4 = Ask;
   else {
      l_price_4 = Bid;
      li_28 = -1;
   }
   g_ticket_272 = OrderSend(Symbol(), a_cmd_0, LotSize, l_price_4, slip, 0, 0, 0, MagicNumber);
   if (g_ticket_272 < 0) {
      Print("Error opening Order " + GetLastError());
      return (0);
   }
   if (StopLoss != 0 || TakeProfit != 0) {
      if (OrderSelect(g_ticket_272, SELECT_BY_TICKET) != FALSE) {
         l_price_12 = 0;
         l_price_20 = 0;
         if (StopLoss != 0) l_price_12 = OrderOpenPrice() - li_28 * point * StopLoss;
         if (TakeProfit != 0) l_price_20 = OrderOpenPrice() + li_28 * point * TakeProfit;
         OrderModify(g_ticket_272, OrderOpenPrice(), l_price_12, l_price_20, OrderExpiration());
      }
   }
   if (a_cmd_0 == OP_BUY) {
      gd_256 = OrderOpenPrice();
      if (UseProfitGrid && !UseTrailing) OpenPending(OP_BUYSTOP, 1);
   } else {
      gd_264 = OrderOpenPrice();
      if (UseProfitGrid && !UseTrailing) OpenPending(OP_SELLSTOP, -1);
   }
   gi_288 = FALSE;
   gi_292 = FALSE;
   g_price_304 = OrderOpenPrice();
   return (1);
}

int OpenPending(int a_cmd_0, int ai_4) {
   double ld_8;
   double l_lots_16 = LotSize;
   int l_ticket_24 = 0;
   int l_count_28 = 0;
   int l_count_32 = 0;
   bool li_ret_36 = FALSE;
   if (ai_4 == 1) ld_8 = gd_256;
   else ld_8 = gd_264;
   while (l_count_32 < gi_216) {
      l_count_28++;
      for (int li_40 = l_count_32; li_40 < gi_216; li_40++) {
         l_ticket_24 = OrderSend(Symbol(), a_cmd_0, l_lots_16, ld_8 + ai_4 * point * GridLevel, slip, 0, 0, 0, MagicNumber);
         if (l_ticket_24 > 0) {
            l_lots_16 = NormalizeDouble(l_lots_16 * MultiplyFactor, digit_lot);
            ld_8 += ai_4 * point * GridLevel;
            l_count_32++;
            li_ret_36 = TRUE;
         }
      }
      if (l_count_28 == 3) break;
   }
   if (ai_4 == 1) gd_256 = ld_8;
   else gd_264 = ld_8;
   return (li_ret_36);
}

void update_trades() {
   int l_ord_total_0 = OrdersTotal();
   double ld_4 = check_for_update();
   if (ld_4 > 0.0) {
      OrderSelect(g_ticket_272, SELECT_BY_TICKET);
      if (OrderType() == OP_BUY) g_price_304 += LockProfitPips * point;
      else g_price_304 -= LockProfitPips * point;
      for (int l_pos_12 = 0; l_pos_12 < l_ord_total_0; l_pos_12++) {
         OrderSelect(l_pos_12, SELECT_BY_POS, MODE_TRADES);
         if (OrderSymbol() == Symbol() && OrderCloseTime() == 0 && OrderMagicNumber() == MagicNumber && OrderType() < OP_BUYLIMIT) {
            if (!OrderModify(OrderTicket(), OrderOpenPrice(), g_price_304, OrderTakeProfit(), 0)) {
               Sleep(500);
               if (!OrderModify(OrderTicket(), OrderOpenPrice(), g_price_304, OrderTakeProfit(), 0)) {
                  Sleep(500);
                  OrderModify(OrderTicket(), OrderOpenPrice(), g_price_304, OrderTakeProfit(), 0);
               }
            }
         }
      }
      g_price_304 = ld_4;
   }
}

double check_for_update() {
   int l_ord_total_0 = OrdersTotal();
   for (int l_pos_4 = 0; l_pos_4 < l_ord_total_0; l_pos_4++) {
      OrderSelect(l_pos_4, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol() && OrderCloseTime() == 0 && OrderMagicNumber() == MagicNumber && OrderType() < OP_BUYLIMIT)
         if (OrderTakeProfit() == 0.0 && OrderStopLoss() == 0.0) return (OrderOpenPrice());
   }
   return (-1);
}

void CloseALL(int a_cmd_0) {
   int l_cmd_4;
   int l_ord_total_8 = 0;
   int l_pos_12 = 0;
   double oop = 0;
   bool li_24 = TRUE;
        if (a_cmd_0 == OP_BUY)  l_cmd_4 = 4;
   else if (a_cmd_0 == OP_SELL) l_cmd_4 = 5;
   while (li_24) {
      li_24 = FALSE;
      l_ord_total_8 = OrdersTotal();
      for (l_pos_12 = 0; l_pos_12 < l_ord_total_8; l_pos_12++) {
         OrderSelect(l_pos_12, SELECT_BY_POS, MODE_TRADES);
         if (OrderSymbol() == Symbol() && OrderCloseTime() == 0 && OrderMagicNumber() == MagicNumber && OrderType() == a_cmd_0 || OrderType() == l_cmd_4) {
            if (OrderType() > OP_SELL) OrderDelete(OrderTicket());
            else {
               RefreshRates();
               if (OrderType() == OP_BUY) oop = Bid;
               else                       oop = Ask;
               OrderClose(OrderTicket(), OrderLots(), oop, slip);
               Print(" Market Delete Order " + OrderTicket());
            }
            li_24 = TRUE;
         }
      }
   }
}

void check_Ticket() {
   if (OrderSelect(g_ticket_272, SELECT_BY_TICKET) != FALSE) {
      if (OrderCloseTime() != 0) {
         g_ticket_272 = 0;
         CloseALL(OrderType());
         return;
      }
   }
}

int Trading_Hours() {
   if (Hour() < HourStartTrade) return (0);
   if (Hour() == HourStartTrade && Minute() < MinuteStartTrade) return (0);
   if (Hour() > HourStopTrade) return (0);
   if (Hour() == HourStopTrade && Minute() > MinuteStopTrade) return (0);
   return (1);
}

void TrailStop() {
   double ld_0;
   int li_8 = 1;
   double l_price_12 = 0;
   if (OrderSelect(g_ticket_272, SELECT_BY_TICKET) != FALSE) {
      if (OrderCloseTime() == 0) {
         if (OrderType() == OP_BUY) {
            li_8 = 1;
            l_price_12 = Bid;
         } else {
            if (OrderType() == OP_SELL) {
               li_8 = -1;
               l_price_12 = Ask;
            }
         }
         if (OrderType() != OP_BUY && OrderType() != OP_SELL) return;
         if (li_8 * (l_price_12 - OrderStopLoss()) > TS && li_8 * (l_price_12 - OrderOpenPrice()) / point > TrailingStop) {
            if (li_8 * OrderStopLoss() < li_8 * OrderOpenPrice() || OrderStopLoss() == 0.0) ld_0 = OrderOpenPrice();
            else ld_0 = OrderStopLoss();
            OrderModify(OrderTicket(), OrderOpenPrice(), ld_0 + li_8 * point * TrailingStep, OrderTakeProfit(), 0);
         }
      }
   }
}

bool NewBar() {
   if (g_time_412 == 0) g_time_412 = Time[0];
   if (g_time_412 != Time[0]) {
      g_time_412 = Time[0];
      return (TRUE);
   }
   return (FALSE);
}
