TralingStop_v3

Download script
Author: Copyright � 2005, Cleon Adonis Santos
TralingStop_v3

This automated script is designed to manage your active trading positions by dynamically adjusting your "Stop Loss" levels based on recent price action. Instead of keeping a static safety net, the tool "trails" the market movements to help protect your profits as the price moves in your favor.

Core Functionality

The logic operates by constantly monitoring the most recent price fluctuations on the chart. It looks back at a specific number of previous price bars (user-defined) to identify the lowest and highest points reached during that timeframe.

Based on these extremes, the script calculates two dynamic safety boundaries:

  • For Buy Positions: It identifies the lowest price point in the recent period and sets a floor just below it. If the market price rises, this floor follows upward, "locking in" gains.
  • For Sell Positions: It identifies the highest price point in the recent period and sets a ceiling just above it. As the market price drops, this ceiling follows downward to protect the trade.

How It Works in Real-Time

The script continuously scans all your open trades on the current symbol. For every active position, it performs the following checks:

  1. Market Awareness: It accounts for the current "Spread" (the difference between the buying and selling price) to ensure the safety boundaries are placed accurately relative to the live market.
  2. Dynamic Updating: It compares your current Stop Loss level against the newly calculated best possible safety point.
    • If the new calculated price is "better" than your current setting (e.g., it moves further into profit), the script automatically moves your trade's Stop Loss to that new level.
    • If the market hasn't moved favorably enough to trigger a change, the script leaves your original Stop Loss untouched.
  3. Order Variety: The logic is versatile enough to handle standard market orders (Buy/Sell) as well as pending orders (Buy Stop/Sell Stop), ensuring consistent protection across different types of entries.

User Impact

For a trader, this means you don't have to manually move your stop losses every time the market fluctuates. The script automates the "tightening" of your safety net, reacting to new lows and highs automatically to ensure that as your trade moves into profit, your risk is progressively reduced.

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

//+------------------------------------------------------------------+
//|                                                  TralingStop.mq4 |
//|                            Copyright © 2005, Cleon Adonis Santos |
//|                                                    www.fw380.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, Cleon Adonis Santos"
#property link      "www.fw380.com/forex"

extern int  BarsNum = 5;
extern int  PipDistance = 1;
double      LTS, STS, Spread;

int start() {

   int count = 0;

   // ----- Spread calculation -----
   Spread = MarketInfo(Symbol(), MODE_SPREAD) * Point;

   // ----- Stop Loss Calculation -----
   LTS = NormalizeDouble(Low[Lowest(NULL, 0, MODE_LOW, BarsNum, 1)] - (PipDistance * Point), Digits);
   STS = NormalizeDouble(High[Highest(NULL, 0, MODE_HIGH, BarsNum, 1)] + Spread + (PipDistance * Point), Digits);
   Comment("LTS: ", LTS, " - STS: ", STS);
   if (OrdersTotal() != 0) {
      while (count < OrdersTotal()) {
         OrderSelect(count, SELECT_BY_POS, MODE_TRADES);
         if (OrderSymbol() == Symbol()) {
            if (OrderType() == OP_BUY && (LTS > OrderStopLoss() || OrderStopLoss() == 0)) {
               OrderModify(OrderTicket(), 0, LTS, 0, 0, Blue);
               Alert("OP_BUY Error: ", GetLastError(), " - ", LTS, " - ", OrderTicket());
            }
            if (OrderType() == OP_SELL && (STS < OrderStopLoss() || OrderStopLoss() == 0)) {
               OrderModify(OrderTicket(), 0, STS, 0, 0, Red);
               Alert("OP_SELL Error: ", GetLastError(), " - ", STS, " - ", OrderTicket());
            }
            if (OrderType() == OP_BUYSTOP && (LTS > OrderStopLoss() || OrderStopLoss() == 0)) {
               OrderModify(OrderTicket(), OrderOpenPrice(), LTS, 0, 0, Blue);
               Alert("OP_BUYSTOP Error: ", GetLastError(), " - ", LTS, " - ", OrderTicket());
            }
            if (OrderType() == OP_SELLSTOP && (STS < OrderStopLoss() || OrderStopLoss() == 0)) {
               OrderModify(OrderTicket(), OrderOpenPrice(), STS, 0, 0, Red);
               Alert("OP_SELLSTOP Error: ", GetLastError(), " - ", STS, " - ", OrderTicket());
            }
         }
         count++;
      }
   }
   return(0);
}

Comments