/*
   Generated by EX4-TO-MQ4 decompiler ONE FILE V4.0.218.3
   EX4 ID : 04E20A41724C2645BAEB3FDF008C44EC
   Website: http://purebeam.biz
   E-mail : purebeam@gmail.com
*/
#property copyright "Copyright © 2008, TradeWays"
#property link      "http://www.tradeways.org"

extern string TicketsList = "ALL";
extern bool UseCloseOneThird = TRUE;
extern int LevelProfit1 = 20;
extern int LevelMoving1 = 1;
extern int LevelProfit2 = 35;
extern int LevelMoving2 = 10;
extern int LevelProfit3 = 55;
extern int LevelMoving3 = 30;
extern int TrailingStop = 30;
extern int TrailingStep = 5;
extern int Slippage = 2;
extern bool ShowComment = TRUE;
extern bool UseSound = TRUE;
string gs_132 = "expert.wav";
string gsa_140[];

int init() {
   if (TimeCurrent() > StrToTime("2009.01.20")) {
      Alert("New version available! Download it using re-activated link from Plimus");
      return;
   }
   TicketsList = TicketsList;
   fSplitStrToStr(TicketsList, gsa_140, "/");
   return (0);
}

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

void start() {
   double ld_unused_0;
   int l_digits_8;
   string ls_12 = "";
   for (int l_pos_20 = 0; l_pos_20 < OrdersTotal(); l_pos_20++) {
      if (OrderSelect(l_pos_20, SELECT_BY_POS, MODE_TRADES)) {
         if (OrderSymbol() == Symbol()) {
            for (int l_index_24 = 0; l_index_24 < ArraySize(gsa_140); l_index_24++) {
               if (fStrUCase(gsa_140[l_index_24]) == "ALL" || StrToInteger(gsa_140[l_index_24]) == OrderTicket()) {
                  ThreeLevelSystemOfOutput();
                  l_digits_8 = MarketInfo(OrderSymbol(), MODE_DIGITS);
                  ld_unused_0 = MarketInfo(OrderSymbol(), MODE_POINT);
                  ls_12 = ls_12 + OrderSymbol() + "  Price: " + DoubleToStr(OrderOpenPrice(), l_digits_8) + "  SL = " + DoubleToStr(OrderStopLoss(), l_digits_8) + " (" + StopLossInPoint() + ")\n";
               }
            }
         }
      }
   }
   if (ShowComment) Comment(ls_12);
}

void ThreeLevelSystemOfOutput() {
   int li_0 = ProfitPosition();
   int li_4 = StopLossInPoint();
   int li_unused_8 = MarketInfo(OrderSymbol(), MODE_SPREAD);
   if (li_0 > LevelProfit1 && li_0 <= LevelProfit2 && li_4 < LevelMoving1) {
      ModifyStopLossInPoint(LevelMoving1);
      if (UseCloseOneThird) CloseOneThird();
   }
   if (li_0 > LevelProfit2 && li_0 <= LevelProfit3 && li_4 < LevelMoving2) ModifyStopLossInPoint(LevelMoving2);
   if (li_0 > LevelProfit3 && li_4 < LevelMoving3) ModifyStopLossInPoint(LevelMoving3);
   if (li_0 > LevelMoving3 + TrailingStop + TrailingStep) TrailingPositions();
}

void CloseOneThird() {
   bool l_ord_close_0 = FALSE;
   double l_lots_4 = MathCeil(10.0 * (OrderLots() / 3.0)) / 10.0;
   if (l_lots_4 > 0.0) {
      if (OrderType() == OP_BUY) l_ord_close_0 = OrderClose(OrderTicket(), l_lots_4, Bid, Slippage, CLR_NONE);
      if (OrderType() == OP_SELL) l_ord_close_0 = OrderClose(OrderTicket(), l_lots_4, Ask, Slippage, CLR_NONE);
      if (l_ord_close_0 && UseSound) PlaySound(gs_132);
   }
}

void TrailingPositions() {
   double l_bid_0;
   double l_ask_8;
   double l_point_16 = MarketInfo(OrderSymbol(), MODE_POINT);
   if (OrderType() == OP_BUY) {
      l_bid_0 = MarketInfo(OrderSymbol(), MODE_BID);
      if (ND(l_bid_0 - OrderOpenPrice()) > ND(TrailingStop * l_point_16)) {
         if (ND(OrderStopLoss()) < ND(l_bid_0 - (TrailingStop + TrailingStep - 1) * l_point_16)) {
            ModifyStopLoss(l_bid_0 - TrailingStop * l_point_16);
            return;
         }
      }
   }
   if (OrderType() == OP_SELL) {
      l_ask_8 = MarketInfo(OrderSymbol(), MODE_ASK);
      if (ND(OrderOpenPrice() - l_ask_8) > ND(TrailingStop * l_point_16))
         if (ND(OrderStopLoss()) > ND(l_ask_8 + (TrailingStop + TrailingStep - 1) * l_point_16) || ND(OrderStopLoss()) == 0.0) ModifyStopLoss(l_ask_8 + TrailingStop * l_point_16);
   }
}

void ModifyStopLoss(double a_price_0) {
   int l_bool_8 = OrderModify(OrderTicket(), OrderOpenPrice(), a_price_0, OrderTakeProfit(), 0, CLR_NONE);
   if (l_bool_8 && UseSound) PlaySound(gs_132);
}

void ModifyStopLossInPoint(int ai_0) {
   double l_price_8 = 0;
   double l_point_16 = MarketInfo(OrderSymbol(), MODE_POINT);
   if (OrderType() == OP_BUY) l_price_8 = OrderOpenPrice() + ai_0 * l_point_16;
   if (OrderType() == OP_SELL) l_price_8 = OrderOpenPrice() - ai_0 * l_point_16;
   int l_bool_4 = OrderModify(OrderTicket(), OrderOpenPrice(), l_price_8, OrderTakeProfit(), 0, CLR_NONE);
   if (l_bool_4 && UseSound) PlaySound(gs_132);
}

int ProfitPosition() {
   double l_bid_0;
   double l_ask_8;
   double l_point_16 = MarketInfo(OrderSymbol(), MODE_POINT);
   double ld_24 = 0;
   if (OrderType() == OP_BUY) {
      l_bid_0 = MarketInfo(OrderSymbol(), MODE_BID);
      ld_24 = ND0((l_bid_0 - OrderOpenPrice()) / l_point_16);
   }
   if (OrderType() == OP_SELL) {
      l_ask_8 = MarketInfo(OrderSymbol(), MODE_ASK);
      ld_24 = ND0((OrderOpenPrice() - l_ask_8) / l_point_16);
   }
   return (MathRound(ld_24));
}

int StopLossInPoint() {
   double l_point_0 = MarketInfo(OrderSymbol(), MODE_POINT);
   double ld_8 = 0;
   if (OrderType() == OP_BUY) ld_8 = ND0((OrderStopLoss() - OrderOpenPrice()) / l_point_0);
   if (OrderType() == OP_SELL) ld_8 = ND0((OrderOpenPrice() - OrderStopLoss()) / l_point_0);
   if (OrderStopLoss() == 0.0) ld_8 = (-OrderOpenPrice()) / l_point_0;
   return (MathRound(ld_8));
}

double ND0(double ad_0) {
   return (NormalizeDouble(ad_0, 0));
}

double ND(double ad_0) {
   return (NormalizeDouble(ad_0, Digits));
}

string fStrUCase(string as_0) {
   int li_12;
   for (int li_8 = 0; li_8 < StringLen(as_0); li_8++) {
      li_12 = StringGetChar(as_0, li_8);
      if (li_12 > '`' && li_12 < '{') as_0 = StringSetChar(as_0, li_8, li_12 - 32);
      else {
         if (li_12 > 'ß' && li_12 < 256) as_0 = StringSetChar(as_0, li_8, li_12 - 32);
         else
            if (li_12 == '¸') as_0 = StringSetChar(as_0, li_8, '¨');
      }
   }
   return (as_0);
}

void fSplitStrToStr(string as_0, string &asa_8[], string as_12) {
   string ls_20 = "";
   string ls_28 = "";
   ArrayResize(asa_8, 0);
   for (int li_36 = 0; li_36 < StringLen(as_0); li_36++) {
      ls_28 = StringSubstr(as_0, li_36, 1);
      if (ls_28 == as_12) {
         if (StringTrimLeft(StringTrimRight(ls_20)) != "") {
            ArrayResize(asa_8, ArraySize(asa_8) + 1);
            asa_8[ArraySize(asa_8) - 1] = ls_20;
         }
         ls_20 = "";
      } else
         if (ls_28 != " ") ls_20 = ls_20 + ls_28;
   }
   if (StringTrimLeft(StringTrimRight(ls_20)) != "") {
      ArrayResize(asa_8, ArraySize(asa_8) + 1);
      asa_8[ArraySize(asa_8) - 1] = ls_20;
   }
   ArraySize(asa_8);
}
