e_Trailing_v1

Author: ��� ����� �. aka KimIV
e_Trailing_v1

This automated tool is designed to manage your open trades by dynamically adjusting your "Stop Loss" levels. Its primary purpose is to protect your gains as the market moves in your favor, effectively "locking in" profit automatically so that a sudden price reversal doesn't turn a winning trade into a losing one.

How the Logic Works

The script constantly monitors all currently open positions on your trading platform and applies the following logic to each one:

For Buy Positions (When you expect the price to go up):

  • The tool checks if the current market price has moved sufficiently far above your entry price.
  • If it meets your specified requirements, the script looks at your current Stop Loss.
  • If the Stop Loss is still below a certain threshold, the script automatically moves it upward (closer to the current price).
  • This ensures that as the price climbs, your safety net follows it, preserving your realized profit.

For Sell Positions (When you expect the price to go down):

  • The tool monitors the market price to see if it is dropping below your entry point.
  • Once the price drop reaches your specified threshold, the script evaluates the current Stop Loss.
  • If the Stop Loss is too far down, the script automatically moves it upward (closer to the falling price).
  • This creates a "trailing" effect where your profit remains protected even as the market continues to move in your direction.

Key Management Features

  • Customizable Distance: You can define exactly how many points away from the current price the Stop Loss should stay. This allows you to choose between a "tight" trail (protecting small gains quickly) or a "wide" trail (giving the trade more room to breathe).
  • Step Control: The logic includes a "step" feature. This prevents the script from making tiny, constant adjustments to your Stop Loss. Instead, it only moves the Stop Loss once the price has moved a significant enough distance to justify a new update.
  • Profit Protection Toggle: There is an option to choose whether the trailing logic should apply to all trades or only to those that have already moved into a profitable state.
  • Audio Alerts: To keep you informed of active management, the script can trigger a sound notification every time it successfully moves your Stop Loss to a more favorable position.

In summary, this script acts as an automated security guard for your trades. It removes the manual effort of constantly moving your Stop Loss by "shadowing" the market price, ensuring that your gains are guarded in real-time.

Orders Execution
Checks for the total of open ordersIt can change open orders parameters, due to possible stepping strategy
Miscellaneous
It plays sound alerts
19 Views
0 Downloads
0 Favorites
e_Trailing_v1
/*-----------------------------+
|			       |
| Shared by www.Aptrafx.com    |
|			       |
+------------------------------*/

//+------------------------------------------------------------------+
//|                                                   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