/*
   Generated by EX4-TO-MQ4 decompiler V4.0.224.1 []
   Website: http://purebeam.biz
   E-mail : purebeam@gmail.com
*/
#property copyright "Copyright © 2010, VIKRAV"
#property link      "http://www.vikrav.com"

string gs_76 = "www.vikrav.com BreakEven";
extern int LockInPipsAt = 20;
extern int LockInPips = 5;
extern int Trades = 3;
extern bool ModifyTrades = TRUE;
extern bool ModifyTrade3 = FALSE;
extern int FontSize = 10;
extern color FontColour = Aqua;
double gd_unused_112;
double gd_120;
double gd_128;
string gs_144 = "expert.wav";
string gs_dummy_152;
int gi_160 = 3;
int g_slippage_164 = 0;
int gi_168 = 0;

int init() {
   gd_120 = Point;
   gd_unused_112 = Digits;
   g_slippage_164 = gi_160;
   if (Digits == 5 || Digits == 3) {
      gd_unused_112 = Digits - 1;
      gd_120 = 10.0 * Point;
      g_slippage_164 = 10 * gi_160;
   }
   if (NormalizeDouble(MarketInfo(Symbol(), MODE_LOTSTEP), 2) == 0.01) gi_168 = 2;
   else gi_168 = 1;
   if (Trades > 3) Trades = 3;
   if (Trades < 1) Trades = 1;
   ObjectCreate("EA_Version", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("EA_Version", gs_76 + " - Lock in " + LockInPips + " pips after " + LockInPipsAt + " pips profit", FontSize, "Arial Bold", FontColour);
   ObjectSet("EA_Version", OBJPROP_XDISTANCE, 240);
   ObjectSet("EA_Version", OBJPROP_YDISTANCE, 0);
   if (Trades > 1) {
      ObjectCreate("Monitor1", OBJ_LABEL, 0, 0, 0);
      ObjectSetText("Monitor1", "Monitoring Targets: TP1=" + LockInPipsAt + " Pips", FontSize, "Arial Bold", FontColour);
      ObjectSet("Monitor1", OBJPROP_CORNER, 0);
      ObjectSet("Monitor1", OBJPROP_XDISTANCE, 240);
      ObjectSet("Monitor1", OBJPROP_YDISTANCE, FontSize + 5);
   }
   if (Trades > 1) {
      ObjectCreate("Monitor2", OBJ_LABEL, 0, 0, 0);
      ObjectSetText("Monitor2", "|| TP2=" + (LockInPipsAt * 2000) + " Pips", FontSize, "Arial Bold", FontColour);
      ObjectSet("Monitor2", OBJPROP_CORNER, 0);
      ObjectSet("Monitor2", OBJPROP_XDISTANCE, 455);
      ObjectSet("Monitor2", OBJPROP_YDISTANCE, FontSize + 5);
   }
   if (Trades == 3) {
      ObjectCreate("Monitor3", OBJ_LABEL, 0, 0, 0);
      ObjectSetText("Monitor3", "|| TP3=" + (3000 * LockInPipsAt) + " Pips", FontSize, "Arial Bold", FontColour);
      ObjectSet("Monitor3", OBJPROP_CORNER, 0);
      ObjectSet("Monitor3", OBJPROP_XDISTANCE, 555);
      ObjectSet("Monitor3", OBJPROP_YDISTANCE, FontSize + 5);
   }
   return (0);
}

int deinit() {
   ObjectDelete("EA_Version");
   ObjectDelete("Monitor1");
   ObjectDelete("Monitor2");
   ObjectDelete("Monitor3");
   return (0);
}

int start() {
   if (Digits == 5 || Digits == 3) gd_128 = MarketInfo(Symbol(), MODE_SPREAD) / 10.0;
   else gd_128 = MarketInfo(Symbol(), MODE_SPREAD);
   if (LockInPipsAt > 0) {
      for (int l_pos_0 = 0; l_pos_0 < OrdersTotal(); l_pos_0++) {
         OrderSelect(l_pos_0, SELECT_BY_POS, MODE_TRADES);
         if (OrderSymbol() == Symbol()) {
            switch (OrderType()) {
            case OP_BUY:
               if (iClose(OrderSymbol(), 0, 0) - OrderOpenPrice() >= LockInPipsAt * gd_120) {
                  if (OrderStopLoss() < OrderOpenPrice()) {
                     OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() + LockInPips * gd_120, OrderTakeProfit(), 0, CLR_NONE);
                     Print("Stop Loss adjusted to ", LockInPips, " pips");
                     PlaySound(gs_144);
                     if (Trades > 1) {
                        OrderClose(OrderTicket(), NormalizeDouble(OrderLots() / Trades, gi_168), Bid, g_slippage_164, CLR_NONE);
                        Print("Partial order closed to lock in profits by Breakeven EA");
                     }
                  }
               }
               if (Trades == 3 && ModifyTrades && iClose(OrderSymbol(), 0, 0) - OrderOpenPrice() >= LockInPipsAt * 2 * gd_120) {
                  OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() + LockInPipsAt * gd_120, OrderTakeProfit(), 0, CLR_NONE);
                  Print("Stop Loss adjusted to ", LockInPipsAt, " pips");
                  Sleep(500);
                  OrderClose(OrderTicket(), NormalizeDouble(OrderLots() / 2.0, gi_168), Bid, g_slippage_164, CLR_NONE);
                  Print("Partial order closed to lock in profits");
                  ModifyTrades = FALSE;
                  PlaySound(gs_144);
               }
               if (Trades == 3 && ModifyTrade3 && iClose(OrderSymbol(), 0, 0) - OrderOpenPrice() >= 3 * LockInPipsAt * gd_120) {
                  OrderClose(OrderTicket(), OrderLots(), Bid, g_slippage_164, CLR_NONE);
                  Print("Final Order closed by Breakeven EA");
                  ModifyTrade3 = FALSE;
                  PlaySound(gs_144);
               }
               break;
            case OP_SELL:
               if (OrderOpenPrice() - iClose(OrderSymbol(), 0, 0) >= LockInPipsAt * gd_120) {
                  if (OrderStopLoss() > OrderOpenPrice()) {
                     OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() - LockInPips * gd_120, OrderTakeProfit(), 0, Yellow);
                     Print("Stop Loss adjusted to ", LockInPips, " pips");
                     PlaySound(gs_144);
                     if (Trades > 1) {
                        OrderClose(OrderTicket(), NormalizeDouble(OrderLots() / Trades, gi_168), Ask, g_slippage_164, CLR_NONE);
                        Print("Partial order closed to lock in profits by Breakeven EA");
                     }
                  }
               }
               if (Trades == 3 && ModifyTrades && OrderOpenPrice() - iClose(OrderSymbol(), 0, 0) >= LockInPipsAt * 2 * gd_120) {
                  OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() - LockInPipsAt * gd_120, OrderTakeProfit(), 0, CLR_NONE);
                  Print("Stop Loss adjusted to ", LockInPipsAt, " pips");
                  Sleep(500);
                  OrderClose(OrderTicket(), NormalizeDouble(OrderLots() / 2.0, gi_168), Ask, g_slippage_164, CLR_NONE);
                  Print("Partial order closed to lock in profits by Breakeven EA");
                  ModifyTrades = FALSE;
                  PlaySound(gs_144);
               }
               if (Trades == 3 && ModifyTrade3 && OrderOpenPrice() - iClose(OrderSymbol(), 0, 0) >= 3 * LockInPipsAt * gd_120) {
                  OrderClose(OrderTicket(), OrderLots(), Ask, g_slippage_164, CLR_NONE);
                  Print("Final Order closed by Breakeven EA");
                  ModifyTrade3 = FALSE;
                  PlaySound(gs_144);
               }
            }
         }
      }
   }
   return (0);
}