/*
   Generated by EX4-TO-MQ4 decompiler V4.0.224.1 []
   Website: http://purebeam.biz
   E-mail : purebeam@gmail.com
*/
#property copyright "Copyright © 2009, Forex-Solutions-Team"
#property link      "www.sRs_Trend_Rider.com"

#include <WinUser32.mqh>
#include <stdlib.mqh>

extern bool Get_Full_Take_Profit = TRUE;
extern bool Alert_With_Sound = TRUE;
extern bool Get_Protected_Stop_Loss = TRUE;
extern color Lines_Color = Lime;
double gd_unused_92 = 1.0;
string gs_100 = "";
string gsa_108[9];

int init() {
   if (Digits == 5 || Digits == 3) gd_unused_92 = 10;
   HideTestIndicators(TRUE);
   CheckForOrder();
   Comments();
   return (Digits);
}

int deinit() {
   deleteObject("line_FirstTakeProfit");
   deleteObject("arrow_FirstTakeProfit");
   deleteObject("label_FirstTakeProfit");
   deleteObject("sRs_down");
   deleteObject("sRs_up");
   return (0);
}

int start() {
   Comments();
   CheckForOrder();
   return (0);
}

void Comments() {
   string ls_0 = "\n";
   string ls_8 = ""
      + "\n" 
      + "sRs Trade Management Robot 1.3" 
      + "\n" 
      + "------------------------------------------------" 
      + "\n" 
      + "Account Leverage:  " + DoubleToStr(AccountLeverage(), 0)
      + "\n" 
      + "Account Currency:  " + AccountCurrency() 
      + "\n" 
      + "Account Balance:    " + DoubleToStr(AccountBalance(), 2)
      + "\n"
      + "Account Equity:      " + DoubleToStr(AccountEquity(), 2)
      + "\n"
      + "------------------------------------------------" 
      + "\n" 
      + "Trade Information:" 
      + "\n" 
      + "Trade Open Price:   " + gsa_108[4]
      + "\n" 
      + "Trade Stop Loss:     " + gsa_108[2] 
      + "\n" 
      + "First Take Profit:     " + gsa_108[8] 
      + "\n" 
   + "------------------------------------------------" + ls_0;
   for (int l_count_16 = 0; !IsStopped() && !IsConnected(); l_count_16++) {
      Comment("Waiting for connection");
      Sleep(150);
   }
   if (gsa_108[0] == "") {
      deleteObject("line_FirstTakeProfit");
      deleteObject("arrow_FirstTakeProfit");
      deleteObject("label_FirstTakeProfit");
      Comment(Symbol() + ": Did not find any open order! Exiting...");
      return;
   }
   Comment(ls_8);
}

void createObject(string as_0, string as_8, double a_price_16, color a_color_24) {
   if (ObjectFind("arrow_" + as_0) == -1) {
      ObjectCreate("arrow_" + as_0, OBJ_HLINE, 0, Time[80], a_price_16);
      ObjectSet("arrow_" + as_0, OBJPROP_STYLE, STYLE_DASHDOT);
      ObjectSet("arrow_" + as_0, OBJPROP_COLOR, a_color_24);
      ObjectSetText("arrow_" + as_0, as_8 + DoubleToStr(a_price_16, Digits), 12, "Tahoma", a_color_24);
   }
   if (ObjectFind("line_" + as_0) == -1) {
      ObjectCreate("line_" + as_0, OBJ_HLINE, 0, TimeCurrent(), a_price_16);
      ObjectSet("line_" + as_0, OBJPROP_COLOR, a_color_24);
      ObjectSet("line_" + as_0, OBJPROP_STYLE, STYLE_DASHDOT);
   }
   if (ObjectFind("label_" + as_0) != 0) {
      ObjectCreate("label_" + as_0, OBJ_TEXT, 0, Time[100], a_price_16);
      ObjectSetText("label_" + as_0, as_8 + DoubleToStr(a_price_16, Digits), 12, "Tahoma", a_color_24);
   }
}

void deleteObject(string a_name_0) {
   if (ObjectFind(a_name_0) != -1) ObjectDelete(a_name_0);
}

void SetOrderProperties() {
   int l_file_0 = FileOpen("SRS" + gsa_108[1] + gsa_108[4] + gsa_108[5] + ".dat", FILE_BIN|FILE_WRITE);
   if (l_file_0 > 0) {
      FileWriteArray(l_file_0, gsa_108, 0, 9);
      FileClose(l_file_0);
      return;
   }
   int l_error_4 = GetLastError();
   if (l_error_4 > 0/* NO_ERROR */) Print("SRS" + gsa_108[1] + gsa_108[4] + gsa_108[5] + ".dat" + "Error File error=", l_error_4, ": ", ErrorDescription(l_error_4));
}

void GetOrderProperties(string as_0) {
   int l_file_8 = FileOpen("SRS" + as_0 + ".dat", FILE_BIN|FILE_READ);
   if (l_file_8 > 0) {
      FileReadArray(l_file_8, gsa_108, 0, 9);
      FileClose(l_file_8);
      return;
   }
   int l_error_12 = GetLastError();
   if (l_error_12 > 0/* NO_ERROR */) Print("SRS" + as_0 + ".dat" + "Error File error=", l_error_12, ": ", ErrorDescription(l_error_12));
}

bool FileExists(string a_name_0) {
   int l_file_8 = FileOpen(a_name_0, FILE_READ);
   if (l_file_8 < 1) return (FALSE);
   FileClose(l_file_8);
   return (TRUE);
}

void CheckForOrder() {
   int l_error_4;
   bool l_ord_close_8;
   int l_count_0 = 0;
   for (int l_pos_12 = OrdersTotal() - 1; l_pos_12 >= 0; l_pos_12--) {
      if (OrderSelect(l_pos_12, SELECT_BY_POS, MODE_TRADES) == FALSE) break;
      if (OrderSymbol() == Symbol()) {
         RefreshRates();
         l_count_0++;
         if (OrderStopLoss() <= 0.0) {
            DoAlert(Symbol() + ": You must set your StopLoss! ");
            break;
         }
         if (FileExists("SRS" + Symbol() + DoubleToStr(OrderOpenPrice(), Digits) + OrderOpenTime() + ".dat")) {
            GetOrderProperties(Symbol() + DoubleToStr(OrderOpenPrice(), Digits) + OrderOpenTime());
            if (gsa_108[3] == "1" && gsa_108[2] != DoubleToStr(OrderStopLoss(), Digits)) {
               gsa_108[2] = DoubleToStr(OrderStopLoss(), Digits);
               switch (OrderType()) {
               case OP_SELL:
                  gsa_108[8] = DoubleToStr(StrToDouble(gsa_108[4]) - (StrToDouble(gsa_108[2]) - StrToDouble(gsa_108[4])) * 2.0, Digits);
                  break;
               case OP_BUY:
                  gsa_108[8] = DoubleToStr(StrToDouble(gsa_108[4]) + (StrToDouble(gsa_108[4]) - StrToDouble(gsa_108[2])) * 2.0, Digits);
               }
               SetOrderProperties();
            }
         } else {
            gsa_108[0] = OrderTicket();
            gsa_108[1] = OrderSymbol();
            gsa_108[2] = DoubleToStr(OrderStopLoss(), Digits);
            gsa_108[3] = "1";
            gsa_108[4] = DoubleToStr(OrderOpenPrice(), Digits);
            gsa_108[5] = OrderOpenTime();
            gsa_108[6] = DoubleToStr(OrderLots(), 2);
            gsa_108[7] = OrderType();
            switch (OrderType()) {
            case OP_SELL:
               gsa_108[8] = DoubleToStr(StrToDouble(gsa_108[4]) - (StrToDouble(gsa_108[2]) - StrToDouble(gsa_108[4])) * 2.0, Digits);
               break;
            case OP_BUY:
               gsa_108[8] = DoubleToStr(StrToDouble(gsa_108[4]) + (StrToDouble(gsa_108[4]) - StrToDouble(gsa_108[2])) * 2.0, Digits);
            }
            SetOrderProperties();
         }
         switch (OrderType()) {
         case OP_SELL:
            if (gsa_108[3] == "1") {
               deleteObject("line_FirstTakeProfit");
               deleteObject("arrow_FirstTakeProfit");
               deleteObject("label_FirstTakeProfit");
               createObject("FirstTakeProfit", "First Take Profit: ", StrToDouble(gsa_108[8]), Lines_Color);
               if (StrToDouble(gsa_108[2]) > 0.0 && Ask <= StrToDouble(gsa_108[8])) {
                  l_ord_close_8 = OrderClose(OrderTicket(), 0.8 * OrderLots(), Ask, 5, Lines_Color);
                  if (l_ord_close_8) {
                     if (Alert_With_Sound) Alert(Symbol(), " closed ", " @ ", NormalizeDouble(OrderClosePrice(), Digits), " TP1 ");
                     if (!Alert_With_Sound) MessageBox(Symbol() + " closed @ " + DoubleToStr(OrderClosePrice(), Digits) + " TP1 ", "sRs Information", MB_OKCANCEL);
                     if (Get_Full_Take_Profit) {
                        gsa_108[3] = "2";
                        SetOrderProperties();
                     } else CleanParams();
                  } else {
                     l_error_4 = GetLastError();
                     if (l_error_4 > 0/* NO_ERROR */) {
                        Print("OrderClose failed, error=", l_error_4, ": ", ErrorDescription(l_error_4));
                        if (l_error_4 == 138/* REQUOTE */) Alert("sRs tried to close a trade @" + DoubleToStr(Ask, Digits) + " but broker rejected order.");
                     }
                  }
               }
            }
            if (gsa_108[3] == "2" && Get_Full_Take_Profit) {
               if (iMA(OrderSymbol(), 0, 4, 0, MODE_EMA, PRICE_CLOSE, 1) > iMA(OrderSymbol(), 0, 6, 0, MODE_EMA, PRICE_CLOSE, 1) && iMA(OrderSymbol(), 0, 6, 0, MODE_EMA, PRICE_CLOSE, 1) > iMA(OrderSymbol(), 0, 14, 0, MODE_EMA, PRICE_CLOSE, 1)) {
                  l_ord_close_8 = OrderClose(OrderTicket(), OrderLots(), Ask, 5, Lines_Color);
                  if (l_ord_close_8) {
                     if (Alert_With_Sound) Alert(Symbol(), " closed ", " @ ", NormalizeDouble(OrderClosePrice(), Digits), " TP2 ");
                     if (!Alert_With_Sound) MessageBox(Symbol() + " closed @ " + DoubleToStr(OrderClosePrice(), Digits) + " TP2 ", "sRs Information", MB_OKCANCEL);
                     CleanParams();
                  } else {
                     l_error_4 = GetLastError();
                     if (l_error_4 > 0/* NO_ERROR */) {
                        Print("OrderClose failed, error=", l_error_4, ": ", ErrorDescription(l_error_4));
                        if (l_error_4 == 138/* REQUOTE */) Alert("sRs tried to close a trade @" + DoubleToStr(Ask, Digits) + " but broker rejected order.");
                     }
                  }
               }
            }
            if (gsa_108[3] == "1" && Get_Protected_Stop_Loss && TimeCurrent() - OrderOpenTime() > 60 * Period()) {
               if (iMA(NULL, 0, 4, 0, MODE_EMA, PRICE_CLOSE, 1) > iMA(NULL, 0, 6, 0, MODE_EMA, PRICE_CLOSE, 1) && iMA(NULL, 0, 6, 0, MODE_EMA, PRICE_CLOSE, 1) > iMA(NULL, 0, 14, 0, MODE_EMA, PRICE_CLOSE, 1)) {
                  l_ord_close_8 = OrderClose(OrderTicket(), OrderLots(), Ask, 5, Lines_Color);
                  if (l_ord_close_8) {
                     if (Alert_With_Sound) Alert(Symbol(), " closed ", " @ ", NormalizeDouble(OrderClosePrice(), Digits), " PSL ");
                     if (!Alert_With_Sound) MessageBox(Symbol() + " closed @ " + DoubleToStr(OrderClosePrice(), Digits) + " PSL ", "sRs Information", MB_OKCANCEL);
                     CleanParams();
                  } else {
                     l_error_4 = GetLastError();
                     if (l_error_4 > 0/* NO_ERROR */) {
                        Print("OrderClose failed, error=", l_error_4, ": ", ErrorDescription(l_error_4));
                        if (l_error_4 == 138/* REQUOTE */) Alert("sRs tried to close a trade @" + DoubleToStr(Ask, Digits) + " but broker rejected order.");
                     }
                  }
               }
            }
            break;
         case OP_BUY:
            if (gsa_108[3] == "1") {
               deleteObject("line_FirstTakeProfit");
               deleteObject("arrow_FirstTakeProfit");
               deleteObject("label_FirstTakeProfit");
               createObject("FirstTakeProfit", "First Take Profit: ", StrToDouble(gsa_108[8]), Lines_Color);
               if (StrToDouble(gsa_108[2]) > 0.0 && Bid >= StrToDouble(gsa_108[8])) {
                  l_ord_close_8 = OrderClose(OrderTicket(), 0.8 * OrderLots(), Bid, 5, Lines_Color);
                  if (l_ord_close_8) {
                     if (Alert_With_Sound) Alert(Symbol(), " closed ", " @ ", NormalizeDouble(OrderClosePrice(), Digits), " TP1 ");
                     if (!Alert_With_Sound) MessageBox(Symbol() + " closed @ " + DoubleToStr(OrderClosePrice(), Digits) + " TP1 ", "sRs Information", MB_OKCANCEL);
                     if (Get_Full_Take_Profit) {
                        gsa_108[3] = "2";
                        SetOrderProperties();
                     } else CleanParams();
                  } else {
                     l_error_4 = GetLastError();
                     if (l_error_4 > 0/* NO_ERROR */) {
                        if (l_error_4 == 138/* REQUOTE */) Alert("sRs tried to close a trade @" + DoubleToStr(Bid, Digits) + " but broker rejected order.");
                        Print("OrderClose failed, error=", l_error_4, ": ", ErrorDescription(l_error_4));
                     }
                  }
               }
            }
            if (gsa_108[3] == "2" && Get_Full_Take_Profit) {
               if (iMA(OrderSymbol(), 0, 4, 0, MODE_EMA, PRICE_CLOSE, 1) < iMA(OrderSymbol(), 0, 6, 0, MODE_EMA, PRICE_CLOSE, 1) && iMA(OrderSymbol(), 0, 6, 0, MODE_EMA, PRICE_CLOSE, 1) < iMA(OrderSymbol(), 0, 14, 0, MODE_EMA, PRICE_CLOSE, 1)) {
                  l_ord_close_8 = OrderClose(OrderTicket(), OrderLots(), Bid, 5, Lines_Color);
                  if (l_ord_close_8) {
                     if (Alert_With_Sound) Alert(Symbol(), " closed ", " @ ", NormalizeDouble(OrderClosePrice(), Digits), " TP2 ");
                     if (!Alert_With_Sound) MessageBox(Symbol() + " closed @ " + DoubleToStr(OrderClosePrice(), Digits) + " TP2 ", "sRs Information", MB_OKCANCEL);
                     CleanParams();
                  } else {
                     l_error_4 = GetLastError();
                     if (l_error_4 > 0/* NO_ERROR */) {
                        if (l_error_4 == 138/* REQUOTE */) Alert("sRs tried to close a trade @" + DoubleToStr(Bid, Digits) + " but broker rejected order.");
                        Print("OrderClose failed, error=", l_error_4, ": ", ErrorDescription(l_error_4));
                     }
                  }
               }
            }
            if (gsa_108[3] == "1" && Get_Protected_Stop_Loss && TimeCurrent() - OrderOpenTime() > 60 * Period()) {
               if (iMA(NULL, 0, 4, 0, MODE_EMA, PRICE_CLOSE, 1) < iMA(NULL, 0, 6, 0, MODE_EMA, PRICE_CLOSE, 1) && iMA(NULL, 0, 6, 0, MODE_EMA, PRICE_CLOSE, 1) < iMA(NULL, 0, 14, 0, MODE_EMA, PRICE_CLOSE, 1)) {
                  l_ord_close_8 = OrderClose(OrderTicket(), OrderLots(), Bid, 5, Lines_Color);
                  if (l_ord_close_8) {
                     if (Alert_With_Sound) Alert(Symbol(), " closed ", " @ ", NormalizeDouble(OrderClosePrice(), Digits), " PSL ");
                     if (!Alert_With_Sound) MessageBox(Symbol() + " closed @ " + DoubleToStr(OrderClosePrice(), Digits) + " PSL ", "sRs Information", MB_OKCANCEL);
                     CleanParams();
                  } else {
                     l_error_4 = GetLastError();
                     if (l_error_4 > 0/* NO_ERROR */) {
                        if (l_error_4 == 138/* REQUOTE */) Alert("sRs tried to close a trade @" + DoubleToStr(Bid, Digits) + " but broker rejected order.");
                        Print("OrderClose failed, error=", l_error_4, ": ", ErrorDescription(l_error_4));
                     }
                  }
               }
            }
         }
      }
   }
   if (l_count_0 == 0) {
      deleteObject("line_FirstTakeProfit");
      deleteObject("arrow_FirstTakeProfit");
      deleteObject("label_FirstTakeProfit");
      Comment(Symbol() + ": Did not find any open order! Exiting...");
   }
}

void CleanParams() {
   if (gsa_108[0] != "") {
      if (FileExists("SRS" + gsa_108[1] + gsa_108[4] + gsa_108[5] + ".dat")) FileDelete("SRS" + gsa_108[1] + gsa_108[4] + gsa_108[5] + ".dat");
      deleteObject("line_MyTakeProfit");
      deleteObject("arrow_MyTakeProfit");
      deleteObject("label_MyTakeProfit");
      deleteObject("sRs_down");
      deleteObject("sRs_up");
      gsa_108[0] = "";
      gsa_108[1] = "";
      gsa_108[2] = "";
      gsa_108[3] = "";
      gsa_108[4] = "";
      gsa_108[5] = "";
      gsa_108[6] = "";
      gsa_108[7] = "";
      gsa_108[8] = "";
   }
}

void DoAlert(string as_0) {
   if (as_0 != gs_100) {
      if (Alert_With_Sound) PlaySound("alert.wav");
      MessageBox(as_0, "sRs Trade Management Robot", 0);
      gs_100 = as_0;
   }
}