Trailing Stop Loss - Level
Download scriptThis script is designed as a visual aid for traders who want to manage their risk by automatically tracking "Trailing Stop Loss" levels on a price chart. Its primary goal is to identify specific price points where a trader might consider exiting a position to lock in profits or minimize potential losses as the market moves.
Core Functionality
The tool calculates and displays a dynamic line on your chart that follows the price action. Depending on how you configure it, this line adjusts its distance based on either a fixed numerical value or a calculation tied to market volatility.
How the Logic Works
The script determines the placement of the trailing line using three distinct logic paths:
1. Volatility-Based Adjustment (Automatic Mode) In its primary mode, the script looks at the Average True Range (ATR), which is a standard measure of how much an asset typically moves over a set period. It smooths this data out to create a "buffer zone." This means that during periods of high market turbulence, the trailing stop will widen to prevent being "shaken out" by noise; during calmer periods, it narrows to stay closer to the price.
2. Fixed-Distance Adjustment (Manual Mode) Alternatively, the script can be set to a manual mode where you define a specific, constant price distance. This is ideal for traders who prefer a consistent, predictable gap between the current price and their stop level regardless of market volatility.
3. Movement Tracking & Protection The script monitors the relationship between the current closing price and the previous stop level:
- Upward Trends: As the price moves higher, the stop level climbs with it, maintaining the designated buffer. It is designed to "ratchet" upward—meaning it can move up to protect profits but will not drop back down if the price retreats.
- Downward Trends: Conversely, in a falling market, the line follows the price downward to maintain the buffer, ensuring that the stop remains relevant to the current market structure.
- Price Crosses: If the price moves significantly enough to cross the previous level, the script recalibrates the line immediately based on the new position of the price to ensure the trailing mechanism remains active and accurate.
Summary for the User
Essentially, this tool acts as a "moving floor" (for long positions) or a "moving ceiling" (for short positions). It automates the mental math of calculating where to move your stop loss based on either fixed pip amounts or real-time market volatility, helping you stay disciplined in your risk management.
//+------------------------------------------------------------------+
//| Trailing Stop Loss - Level.mq4 |
//| S.B.T. |
//| http://sufx.core.t3-ism.net/ |
//+------------------------------------------------------------------+
#property copyright "S.B.T."
#property link "http://sufx.core.t3-ism.net/"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 DeepPink
//---- input parameters
extern int Mode = 0;
extern double DeltaPrice = 0.003;
//---- buffers
double TrStop[];
double ATR[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0, DRAW_LINE);
SetIndexBuffer(0, TrStop);
SetIndexStyle(1, DRAW_NONE);
SetIndexBuffer(1, ATR);
string short_name = "Trailing Stop Loss - Level(" + Mode + ", " + DeltaPrice + ")";
IndicatorShortName(short_name);
SetIndexLabel(0, short_name);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted_bars = IndicatorCounted();
int limit;
int i;
double DeltaStop;
limit = Bars;
for(i = 0; i < limit; i ++) {
ATR[i] = iATR(NULL, 0, 14, i);
}
for(i = limit - 1; i >= 0; i --) {
if (Mode == 0) {
DeltaStop = iMAOnArray(ATR, 0, (14 * 2) - 1, 0, MODE_EMA, i) * 2.824;
} else {
DeltaStop = DeltaPrice;
}
if (Close[i] == TrStop[i + 1]) {
TrStop[i] = TrStop[i + 1];
} else {
if (Close[i + 1] < TrStop[i + 1] && Close[i] < TrStop[i + 1]) {
TrStop[i] = MathMin(TrStop[i + 1], Close[i] + DeltaStop);
} else {
if (Close[i + 1] > TrStop[i + 1] && Close[i] > TrStop[i + 1]) {
TrStop[i] = MathMax(TrStop[i + 1], Close[i] - DeltaStop);
} else {
if (Close[i] > TrStop[i + 1]) TrStop[i] = Close[i] - DeltaStop; else TrStop[i] = Close[i] + DeltaStop;
}
}
}
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments