ChandelierExit_v2
Download scriptThe Chandelier Exit is a technical analysis tool designed to help traders identify potential trend reversals and set trailing stop-loss levels. Instead of relying on simple price movements, this script uses a combination of price extremes and market volatility to determine where a trend might be losing momentum.
Core Logic and Mechanism
The logic operates by calculating two primary boundaries based on the following components:
1. Market Volatility Tracking The script utilizes an Average True Range (ATR) calculation. The ATR measures how much an asset typically moves over a specific period. By multiplying this value, the script creates a "buffer zone" that adjusts automatically; if the market becomes highly volatile, the buffer widens, and if the market stays calm, the buffer narrows.
2. High and Low Extremes The tool looks back at a specific number of previous candles (the Range) to identify the highest peaks and the lowest valleys reached during that time.
- The Upper Boundary: Calculated by taking the highest price point in the recent range and subtracting the volatility buffer.
- The Lower Boundary: Calculated by taking the lowest price point in the recent range and adding the volatility buffer.
Trend Detection and Switching
The script maintains a "direction" memory to understand whether the current market trend is bullish (upward) or bearish (downward). It uses these boundaries to decide when to flip its perspective:
- In an Uptrend: The tool monitors the price relative to the Upper Boundary. If the closing price of a candle falls below this upper boundary, the script recognizes a potential breakdown in momentum. It then switches its focus to the Lower Boundary to track the new bearish trend.
- In a Downtrend: The tool monitors the price relative to the Lower Boundary. If the closing price of a candle rises above this lower boundary, it signals a potential reversal to the upside. The script then switches back to tracking the Upper Boundary for the bullish move.
Summary of Purpose
For a user, this logic provides a dynamic visual guide on a chart. Rather than using a static profit target, it provides a "trailing" exit point that follows the price closely during strong trends but moves away quickly when the market shows signs of exhausting its current direction. It effectively acts as a safety net, helping to protect gains by identifying the moment the market's fundamental structure shifts.
//+------------------------------------------------------------------+
//| ChandelierExit.mq4 |
//| MQLService |
//| scripts@mqlservice.com |
//+------------------------------------------------------------------+
#property copyright "MQLService"
#property link "scripts@mqlservice.com"
//----
#property indicator_chart_window
#property indicator_buffers 4
//#property indicator_color1 Blue
//#property indicator_color2 Red
#property indicator_color3 Orange
#property indicator_color4 Magenta
//---- input parameters
extern int Range=15;
extern int Shift=1;
extern int ATRPeriod=14;
extern int MultipleATR=4;
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
int direction=1;
double ATRvalue;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
//SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,ExtMapBuffer1);
//SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1,ExtMapBuffer2);
SetIndexStyle(2,DRAW_LINE);
SetIndexBuffer(2,ExtMapBuffer3);
SetIndexEmptyValue(2,0.0);
SetIndexStyle(3,DRAW_LINE);
SetIndexBuffer(3,ExtMapBuffer4);
SetIndexEmptyValue(3,0.0);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int limit;
int counted_bars=IndicatorCounted();
if(counted_bars<0) counted_bars=0;
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
for(int i=limit; i>=0; i--)
{
ATRvalue=iATR(NULL,0,ATRPeriod,0)*MultipleATR;
ExtMapBuffer1[i]=High[Highest(NULL,0,MODE_HIGH,Range,i+Shift)]-ATRvalue;
ExtMapBuffer2[i]=Low[Lowest(NULL,0,MODE_LOW,Range,i+Shift)]+ATRvalue;
if(direction==1)
{
if(Close[i+1]<ExtMapBuffer1[i+1])
{
direction=-1;
ExtMapBuffer3[i]=ExtMapBuffer2[i];
}
else
ExtMapBuffer3[i]=ExtMapBuffer1[i];
}
if(direction==-1)
{
if(Close[i+1]>ExtMapBuffer2[i+1])
{
direction=1;
ExtMapBuffer4[i]=ExtMapBuffer1[i];
}
else
ExtMapBuffer4[i]=ExtMapBuffer2[i];
}
}
return(0);
}
//+------------------------------------------------------------------+
Comments