Author: ��� ����� �. aka KimIV
e-Trailing

This script is designed to automate the management of your open trades by dynamically adjusting your "Stop Loss" levels as the market moves in your favor. Essentially, it acts as an automated security guard that follows the price to lock in profits while minimizing the risk of a winning trade turning into a loss.

How it Works

The script continuously scans all currently active positions on your trading account. For every open trade, it evaluates whether the current market price has moved far enough to warrant moving the safety net (Stop Loss).

Key Behaviors for Different Trade Types:

  • For Buy Positions: If the market price rises, the script monitors the distance between the current price and your entry point. Once the price reaches a specific target, the script automatically moves your Stop Loss upward. This ensures that if the market suddenly reverses, you exit with a profit rather than letting the gains evaporate.
  • For Sell Positions: If the market price drops (moving in your favor), the script monitors the downward movement. It will systematically lower your Stop Loss to stay "above" the falling price, securing your gains as the trend continues.

Core Features and Customization:

  • Profit Protection: You can choose whether the script should only start moving the Stop Loss once a trade has entered a positive profit zone, or if it should begin protecting the position immediately based on distance.
  • Precision Movement (Trailing Step): To avoid making unnecessary microscopic adjustments to your trades, the script uses a "Step" logic. It will only update your Stop Loss when the price moves by a specific amount, ensuring smooth and efficient management.
  • Audio Notifications: The script can provide immediate feedback by playing a sound every time it successfully modifies a trade's safety level, allowing you to keep track of its activity without constantly staring at the charts.

In short, this tool automates the "trailing" process—a manual task where traders move their stop losses to lock in gains—allowing for a more hands-off approach to trade management while maintaining a disciplined risk profile.

Orders Execution
Checks for the total of open ordersIt can change open orders parameters, due to possible stepping strategy
Miscellaneous
It plays sound alerts
17 Views
0 Downloads
0 Favorites
e-Trailing
//+------------------------------------------------------------------+
//|                                                   e-Trailing.mq4 |
//|                                           Êèì Èãîðü Â. aka KimIV |
//|                                              http://www.kimiv.ru |
//|                                                                  |
//| 12.09.2005 Àâòîìàòè÷åñêèé Trailing Stop âñåõ îòêðûòûõ ïîçèöèé    |
//|            Âåøàòü òîëüêî íà îäèí ãðàôèê                          |
//+------------------------------------------------------------------+
#property copyright "Êèì Èãîðü Â. aka KimIV"
#property link      "http://www.kimiv.ru"

//------- Âíåøíèå ïàðàìåòðû ------------------------------------------
extern bool   ProfitTrailing = True;  // Òðàëèòü òîëüêî ïðîôèò
extern int    TrailingStop   = 8;     // Ôèêñèðîâàííûé ðàçìåð òðàëà
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)) {
      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