e-TLE_v.1.7

Author: ��� ����� �. aka KimIV
e-TLE_v.1.7

This script is an automated trade management tool designed to handle open positions by dynamically adjusting safety measures and securing profits as market prices move in your favor. Instead of a simple "set and forget" approach, it monitors your active trades and reacts based on specific profit milestones.

Core Security Features

Account Verification The script includes a security layer that allows you to lock it to a specific trading account number. If the account ID does not match the one specified in the settings, the script will refuse to run, preventing it from being used on unauthorized accounts.

Scope of Management You can choose whether the script manages every single position across your entire portfolio or restricts its actions only to the specific currency pair visible on the current chart.

The Three-Level Exit Strategy

The heart of this tool is a "Three Level Exit" logic. It categorizes your profits into three distinct zones, automatically adjusting your risk profile at each stage:

  • Level 1: Once a position reaches the first profit target, the script moves your safety net (Stop Loss) to a predefined point near the entry price. If enabled, it can also automatically close one-third of that specific position to lock in some immediate gains.
  • Level 2: As the price continues to move favorably and hits the second target, the script moves the safety net further into the profit zone.
  • Level 3: Upon reaching the third and highest target, the safety net is moved even deeper into the profit area to ensure the trade remains protected against sudden market reversals.

Advanced Profit Protection

Beyond the three-level system, the script employs two additional dynamic mechanisms:

  1. Trailing Stop: This feature "shadows" the market price. As the price moves in your favor beyond a specific threshold, the safety net follows it at a set distance. If the price pulls back by that distance, the position is closed. This allows you to capture the maximum possible profit during strong trends while automatically exiting if the momentum fades.
  2. Partial Closing: For those who prefer to take profits incrementally, the script has a specific function to sell off 33% of a position's volume as soon as the first profit milestone is met, helping to reduce total risk while leaving the remainder of the trade open to run.

User Feedback and Monitoring

To keep you informed of its actions, the script can provide real-time updates directly on your trading screen, showing the current status of your trades (including entry prices and active safety levels). It also features an optional audio alert system that plays a sound whenever it successfully modifies a trade or closes a portion of a position.

Orders Execution
Checks for the total of open ordersIt Closes Orders by itself It can change open orders parameters, due to possible stepping strategy
Miscellaneous
It plays sound alerts
8 Views
0 Downloads
0 Favorites
e-TLE_v.1.7
//+------------------------------------------------------------------+
//|                                                  e-TLE_v.1.7.mq4 |
//|                                           Êèì Èãîðü Â. aka KimIV |
//|                                              http://www.kimiv.ru |
//|                                                                  |
//| 20.09.2005 Òð¸õóðîâíåâàÿ ñèñòåìà âûõîäîâ (Three Level Exit).     |
//|            Îòðàáàòûâàåò âñå îòêðûòûå ïîçèöèè.                    |
//|            Âåøàòü òîëüêî íà îäèí ëþáîé ãðàôèê.                   |
//| 22.09.2005 v.1.0  Ââ¸ë âíåøíèé ïàðàìåòð äëÿ óïðàâëåíèÿ çîíîé     |
//|            âëèÿíèÿ ñîâåòíèêà.                                    |
//| 22.09.2005 v.1.1  Ââ¸ë ïàðàìåòðû äëÿ îãðàíè÷åíèé ïî ñ÷¸òó        |
//| 22.09.2005 v.1.2  Èñïðàâèë îøèáêó ñ îïðåäåëåíèåì âàëþòû          |
//| 22.09.2005 v.1.3  Ðåàëèçîâàë ìåõàíèçì îòñå÷êè ñíèæåíèÿ öåíû      |
//| 22.09.2005 v.1.4  Ïîïðàâèë ìåõàíèçì îòñå÷êè ñíèæåíèÿ öåíû        |
//| 23.09.2005 v.1.5  Ðåàëèçîâàë ôàêòè÷åñêóþ óñòàíîâêó ñòîïîâ        |
//| 14.10.2005 v.1.6  Òðè óðîâíÿ ïåðåíîñà ñòîïà                      |
//| 20.10.2005 v.1.7  Çàêðûòèå îäíîé òðåòè ïîçèöèè -                 |
//|   çàêðûâàåò òðåòü ïîçèöèè ïðè äîñòèæåíèè ïåðâîé ïðîôèòíîé çîíû   |
//+------------------------------------------------------------------+
#property copyright "Êèì Èãîðü Â. aka KimIV"
#property link      "http://www.kimiv.ru"

//------- Âíåøíèå ïàðàìåòðû ñîâåòíèêà --------------------------------
extern bool   UseOneAccount    = True;   // Òîðãîâàòü òîëüêî íà îäíîì ñ÷¸òå
extern int    NumberAccount    = 82946;  // Íîìåð òîðãîâîãî ñ÷¸òà
extern bool   AllPositions     = False;  // Óïðàâëÿòü âñåìè ïîçèöèÿìè
extern int    LevelProfit1     = 20;     // Ïåðâûé óðîâåíü ïðîôèòà
extern int    LevelMoving1     = 1;      // Ïåðâûé óðîâåíü ïåðåíîñà
extern bool   UseCloseOneThird = True;   // Óïðàâëÿòü âñåìè ïîçèöèÿìè
extern int    LevelProfit2     = 35;     // Âòîðîé óðîâåíü ïðîôèòà
extern int    LevelMoving2     = 20;     // Âòîðîé óðîâåíü ïåðåíîñà
extern int    LevelProfit3     = 55;     // Òðåòèé óðîâåíü ïðîôèòà
extern int    LevelMoving3     = 30;     // Òðåòèé óðîâåíü ïåðåíîñà
extern int    TrailingStop     = 40;     // Ðàçìåð òðàëà
extern int    TrailingStep     = 5;      // Øàã òðàëà
extern int    Slippage         = 3;      // Ïðîñêàëüçûâàíèå öåíû
extern bool   ShowComment      = True;         // Ïîêàçûâàòü êîììåíòàðèè
extern bool   UseSound         = True;         // Èñïîëüçîâàòü çâóêîâîé ñèãíàë
extern string NameFileSound    = "expert.wav"; // Íàèìåíîâàíèå çâóêîâîãî ôàéëà

//------- Ãëîáàëüíûå ïåðåìåííûå ñîâåòíèêà ----------------------------

//+------------------------------------------------------------------+
//| Custor expert deinitialization function                          |
//+------------------------------------------------------------------+
void deinit() {
  Comment("");
}

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
void start() {
  double pp;
  int    nd;
  string comm="";

  if (UseOneAccount && AccountNumber()!=NumberAccount) {
    Comment("Ðàáîòà íà ñ÷¸òå: "+AccountNumber()+" ÇÀÏÐÅÙÅÍÀ!");
    return;
  }
  for (int i=0; i<OrdersTotal(); i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
      if (AllPositions || OrderSymbol()==Symbol()) {
        ThreeLevelSystemOfOutput();
        nd=MarketInfo(OrderSymbol(), MODE_DIGITS);
        pp=MarketInfo(OrderSymbol(), MODE_POINT);
        comm=comm+OrderSymbol()+"  Öåíà: "+DoubleToStr(OrderOpenPrice(),nd)+
             "  SL = " + DoubleToStr(OrderStopLoss(),nd)+
             " ("+StopLossInPoint()+")\n";
      }
    }
  }
  if (ShowComment) Comment(comm);
}

//+------------------------------------------------------------------+
//| Òð¸õóðîâíåâàÿ ñèñòåìà âûõîäîâ                                    |
//+------------------------------------------------------------------+
void ThreeLevelSystemOfOutput() {
  int pr=ProfitPosition(), sl=StopLossInPoint();
  int sp=MarketInfo(OrderSymbol(), MODE_SPREAD);

  if (pr>LevelProfit1 && pr<=LevelProfit2 && sl<LevelMoving1) {
    ModifyStopLossInPoint(LevelMoving1);
    if (UseCloseOneThird) CloseOneThird();
  }
  if (pr>LevelProfit2 && pr<=LevelProfit3 && sl<LevelMoving2) ModifyStopLossInPoint(LevelMoving2);
  if (pr>LevelProfit3 && sl<LevelMoving3) ModifyStopLossInPoint(LevelMoving3);
  if (pr>LevelMoving3+TrailingStop+TrailingStep) {
    TrailingPositions();
  }
}

//+------------------------------------------------------------------+
//| Çàêðûòèå îäíîé òðåòè ïîçèöèè                                     |
//+------------------------------------------------------------------+
void CloseOneThird() {
  bool   fc=False;
  double Lots=MathCeil(OrderLots()/3*10)/10;

  if (Lots>0) {
    if (OrderType()==OP_BUY)
      fc=OrderClose(OrderTicket(), Lots, Bid, Slippage, CLR_NONE);
    if (OrderType()==OP_SELL)
      fc=OrderClose(OrderTicket(), Lots, Ask, Slippage, CLR_NONE);
    if (fc && UseSound) PlaySound(NameFileSound);
  }
}

//+------------------------------------------------------------------+
//| Ñîïðîâîæäåíèå ïîçèöèè ïðîñòûì òðàëîì                             |
//+------------------------------------------------------------------+
void TrailingPositions() {
  double pBid, pAsk, pp=MarketInfo(OrderSymbol(), MODE_POINT);

  if (OrderType()==OP_BUY) {
    pBid=MarketInfo(OrderSymbol(), MODE_BID);
    if (pBid-OrderOpenPrice()>TrailingStop*pp) {
      if (OrderStopLoss()<pBid-(TrailingStop+TrailingStep-1)*pp) {
        ModifyStopLoss(pBid-TrailingStop*pp);
        return;
      }
    }
  }
  if (OrderType()==OP_SELL) {
    pAsk=MarketInfo(OrderSymbol(), MODE_ASK);
    if (OrderOpenPrice()-pAsk>TrailingStop*pp) {
      if (OrderStopLoss()>pAsk+(TrailingStop+TrailingStep-1)*pp || OrderStopLoss()==0) {
        ModifyStopLoss(pAsk+TrailingStop*pp);
        return;
      }
    }
  }
}

//+------------------------------------------------------------------+
//| Ïåðåíîñ óðîâíÿ StopLoss                                          |
//| Ïàðàìåòðû:                                                       |
//|   ldStopLoss - óðîâåíü StopLoss                                  |
//+------------------------------------------------------------------+
void ModifyStopLoss(double ldStopLoss) {
  bool fm;         // Ôëàã ìîäèôèêàöèè ïîçèöèè

  fm=OrderModify(OrderTicket(),OrderOpenPrice(),ldStopLoss,OrderTakeProfit(),0,CLR_NONE);
  if (fm && UseSound) PlaySound(NameFileSound);
}

//+------------------------------------------------------------------+
//| Ïåðåíîñ óðîâíÿ StopLoss                                          |
//| Ïàðàìåòðû:                                                       |
//|   ldStopLoss - óðîâåíü StopLoss                                  |
//+------------------------------------------------------------------+
void ModifyStopLossInPoint(int pp) {
  bool   fm;         // Ôëàã ìîäèôèêàöèè ïîçèöèè
  double ldSL=0, mp=MarketInfo(OrderSymbol(), MODE_POINT);

  if (OrderType()==OP_BUY)  ldSL=OrderOpenPrice()+pp*mp;
  if (OrderType()==OP_SELL) ldSL=OrderOpenPrice()-pp*mp;
  fm=OrderModify(OrderTicket(),OrderOpenPrice(),ldSL,OrderTakeProfit(),0,CLR_NONE);
  if (fm && UseSound) PlaySound(NameFileSound);
}

//+------------------------------------------------------------------+
//| Âîçâðàùàåò ïðîôèò ïîçèöèè â ïóíêòàõ                              |
//+------------------------------------------------------------------+
int ProfitPosition() {
  double pBid, pAsk, pp=MarketInfo(OrderSymbol(), MODE_POINT);
  double pr=0;

  if (OrderType()==OP_BUY) {
    pBid=MarketInfo(OrderSymbol(), MODE_BID);
    pr=(pBid-OrderOpenPrice())/pp;
  }
  if (OrderType()==OP_SELL) {
    pAsk=MarketInfo(OrderSymbol(), MODE_ASK);
    pr=(OrderOpenPrice()-pAsk)/pp;
  }
  return(MathRound(pr));
}

//+------------------------------------------------------------------+
//| Âîçâðàùàåò StopLoss ïîçèöèè â ïóíêòàõ                            |
//+------------------------------------------------------------------+
int StopLossInPoint() {
  double pp=MarketInfo(OrderSymbol(), MODE_POINT);
  double sp=0;

  if (OrderType()==OP_BUY) {
    sp=(OrderStopLoss()-OrderOpenPrice())/pp;
  }
  if (OrderType()==OP_SELL) {
    sp=(OrderOpenPrice()-OrderStopLoss())/pp;
  }
  if (OrderStopLoss()==0) sp=-OrderOpenPrice()/pp;
  return(MathRound(sp));
}
//+------------------------------------------------------------------+

Comments