e-Trailing_v1
Download scriptThis tool is an automated management assistant designed for traders using the MetaTrader 4 platform. Its primary purpose is to dynamically adjust "Stop Loss" levels on active trades, a technique commonly known as a "Trailing Stop," to lock in profits as market prices move in your favor.
Core Functionality
The script constantly monitors your open trading positions and automatically moves the safety net (the Stop Loss) based on specific rules you define. By doing this, it helps protect realized gains from being erased by sudden market reversals.
Key Features and Logic
-
Selective Management: You have the option to decide how broad the script's influence should be. You can set it to manage every single open position in your account simultaneously, or restrict it to only affect trades on the specific chart where the script is currently running.
-
Profit Protection Rules: The tool can be configured to only begin moving the Stop Loss once a trade has reached a certain level of profitability. This ensures that the safety net doesn't interfere with your initial entry strategy but kicks in as soon as the price moves into your "winning" zone.
-
Dynamic Movement (Trailing & Steps): The script uses two main measurements to manage price movement:
- Trailing Distance: This determines how far behind the current market price the Stop Loss should stay.
- Trailing Step: To prevent the script from making constant, tiny adjustments every second, the "Step" ensures that the Stop Loss only moves forward once the price has shifted by a significant enough amount.
-
Execution & Feedback: Whenever the script successfully moves a Stop Loss to a more favorable position, it can trigger an audible notification (a sound file). This provides immediate confirmation that the tool is actively working and securing your profits in real-time.
Summary of Workflow
- Scan: The script scans all open trades continuously.
- Identify: It checks if a trade meets your criteria (e.g., "Is it on this symbol?" or "Is it currently in profit?").
- Calculate: It determines if the current price has moved far enough to warrant moving the Stop Loss based on your "Trailing Stop" and "Trailing Step" settings.
- Update: If the criteria are met, it sends a command to the broker to move the Stop Loss to the new, safer level and plays a notification sound if enabled.
//+------------------------------------------------------------------+
//| e-Trailing.mq4 |
//| Êèì Èãîðü Â. aka KimIV |
//| http://www.kimiv.ru |
//| |
//| 12.09.2005 Àâòîìàòè÷åñêèé Trailing Stop âñåõ îòêðûòûõ ïîçèöèé |
//| Âåøàòü òîëüêî íà îäèí ãðàôèê |
//| 21.01.2006 Ïàðàìåòð AllPositions |
//+------------------------------------------------------------------+
#property copyright "Êèì Èãîðü Â. aka KimIV"
#property link "http://www.kimiv.ru"
//------- Âíåøíèå ïàðàìåòðû ------------------------------------------
extern bool AllPositions = False; // Óïðàâëÿòü âñåìè ïîçèöèÿìè
extern bool ProfitTrailing = True; // Òðàëèòü òîëüêî ïðîôèò
extern int TrailingStop = 15; // Ôèêñèðîâàííûé ðàçìåð òðàëà
extern int TrailingStep = 2; // Øàã òðàëà
extern bool UseSound = True; // Èñïîëüçîâàòü çâóêîâîé ñèãíàë
extern string NameFileSound = "expert.wav"; // Íàèìåíîâàíèå çâóêîâîãî ôàéëà
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
void start() {
for (int i=0; i<OrdersTotal(); i++) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if (AllPositions || OrderSymbol()==Symbol()) {
TrailingPositions();
}
}
}
}
//+------------------------------------------------------------------+
//| Ñîïðîâîæäåíèå ïîçèöèè ïðîñòûì òðàëîì |
//+------------------------------------------------------------------+
void TrailingPositions() {
double pBid, pAsk, pp;
pp = MarketInfo(OrderSymbol(), MODE_POINT);
if (OrderType()==OP_BUY) {
pBid = MarketInfo(OrderSymbol(), MODE_BID);
if (!ProfitTrailing || (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 (!ProfitTrailing || 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);
}
//+------------------------------------------------------------------+
Comments