e-TLE_v.1.6
Download scriptThis script is an automated trade management tool designed to handle active positions by dynamically adjusting "Stop Loss" levels based on the profit achieved. Instead of letting a trade run with a static safety net, it moves your protection closer as the market moves in your favor, effectively "locking in" gains.
Core Functionality
The logic operates on three distinct stages of profit progression and a final trailing mechanism:
1. Three-Level Protective Adjustment The script monitors the current profit of your open positions. As the price reaches specific predefined profit milestones, the tool automatically updates your Stop Loss to a new position:
- First Level: Once a baseline profit is reached, the tool moves the Stop Loss to a set distance from your entry point.
- Second Level: When a higher profit milestone is hit, the protection level is tightened further.
- Third Level: At a high profit threshold, the script moves the Stop Loss to its most conservative position to protect the majority of the gains.
2. Dynamic Trailing Stop Once a trade exceeds a very high profit threshold (beyond the third level), the script activates a "Trailing Stop" mode. In this mode, the Stop Loss follows the price movement continuously. If the market continues to move in your favor, the protection moves with it; if the market reverses, the position is closed at the most recent trailing point.
3. Safety and Monitoring Features
- Account Validation: The tool can be configured to run on one specific account number only, providing a layer of security for multi-account users.
- Scope Control: You can choose whether the script manages every open position in your portfolio or only the positions on the specific chart where it is active.
- Real-time Feedback: The script provides visual updates on the screen regarding the current price, entry points, and current Stop Loss distances. It also features an optional audio alert to notify you whenever a protection level is successfully modified.
Summary of Logic Flow
The tool constantly scans your active trades. It calculates how far the price has moved from your entry and compares it against your chosen profit "levels." Whenever the price crosses into one of these zones, the script automatically executes a command to move your Stop Loss. This ensures that as a trade becomes more successful, your risk decreases automatically without requiring manual intervention.
//+------------------------------------------------------------------+
//| e-TLE_v.1.6.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 Òðè óðîâíÿ ïåðåíîñà ñòîïà - |
//| ðàáîòàåò òîëüêî â ïðîôèòíîé çîíå. |
//+------------------------------------------------------------------+
#property copyright "Êèì Èãîðü Â. aka KimIV"
#property link "http://www.kimiv.ru"
//------- Âíåøíèå ïàðàìåòðû ñîâåòíèêà --------------------------------
extern bool UseOneAccount = false; // Òîðãîâàòü òîëüêî íà îäíîì ñ÷¸òå
extern int NumberAccount = 5596; // Íîìåð òîðãîâîãî ñ÷¸òà
extern bool AllPositions = True; // Óïðàâëÿòü âñåìè ïîçèöèÿìè
extern int LevelProfit1 = 35; // Ïåðâûé óðîâåíü ïðîôèòà
extern int LevelMoving1 = 2; // Ïåðâûé óðîâåíü ïåðåíîñà
extern int LevelProfit2 = 45; // Âòîðîé óðîâåíü ïðîôèòà
extern int LevelMoving2 = 10; // Âòîðîé óðîâåíü ïåðåíîñà
extern int LevelProfit3 = 90; // Òðåòèé óðîâåíü ïðîôèòà
extern int LevelMoving3 = 45; // Òðåòèé óðîâåíü ïåðåíîñà
extern int TrailingStop = 55; // Ðàçìåð òðàëà
extern int TrailingStep = 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 (pr>LevelProfit2 && pr<=LevelProfit3 && sl<LevelMoving2) ModifyStopLossInPoint(LevelMoving2);
if (pr>LevelProfit3 && sl<LevelMoving3) ModifyStopLossInPoint(LevelMoving3);
if (pr>LevelMoving3+TrailingStop+TrailingStep) {
TrailingPositions();
}
}
//+------------------------------------------------------------------+
//| Ñîïðîâîæäåíèå ïîçèöèè ïðîñòûì òðàëîì |
//+------------------------------------------------------------------+
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