This script is designed to automate the management of your active trades by dynamically adjusting "Stop Loss" levels. Its primary goal is to protect your profits as the market moves in your favor, effectively "locking in" gains while allowing the trade room to continue growing.
Core Functionality
The script monitors your open trading positions and automatically moves the Stop Loss price based on specific rules. It ensures that if the market moves significantly in your direction, your exit point follows closely behind it.
Key Logic Components
-
Scope of Control The tool gives you the flexibility to decide which trades it should manage. You can choose to have the script monitor every single open position in your account simultaneously or restrict its actions to only the specific currency pair or asset currently displayed on your screen.
-
Profit Protection Mode There is a specific toggle that determines whether the script should only activate once a trade has entered a profitable state, or if it should begin moving the Stop Loss immediately from the start of the trade.
-
The "Trailing" Mechanism The script uses two main measurements to determine how it moves your Stop Loss:
- Trailing Distance: This defines the fixed gap between the current market price and your Stop Loss. As the price moves forward, the script attempts to keep the Stop Loss at this specific distance behind the peak.
- Trailing Step: This acts as a sensitivity buffer. The script won't move your Stop Loss for every tiny fluctuation; it will only trigger an update once the market has moved by a predefined amount. This prevents the system from making constant, unnecessary adjustments to your orders.
-
Execution and Feedback When the market meets the criteria (reaching the required distance and moving far enough to trigger a "step"), the script automatically sends a command to the broker to update the trade's Stop Loss. To keep you informed of these actions, the script can provide an audible notification every time a position is successfully updated.
Summary of Behavior
- For Buy Positions: As the price rises, the script moves the Stop Loss upward to stay behind the price at your designated distance.
- For Sell Positions: As the price drops, the script moves the Stop Loss downward to stay below the price at your designated distance.
In short, this tool acts as an automated safety net, constantly chasing the market's progress to ensure that your realized profit is protected against sudden reversals.
//+------------------------------------------------------------------+
//| 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