SuperTrend_001
Download scriptSuperTrend Indicator Logic Overview
The SuperTrend is a visual trading tool designed to help market participants identify the direction of a price trend and establish potential entry or exit points. It functions by calculating a "buffer zone" around the current price, which expands or contracts based on market volatility.
Here is a breakdown of how the logic works in plain English:
1. Measuring Market Volatility
The core of this indicator relies on a metric called the Average True Range (ATR). Instead of just looking at the price, the ATR measures how much the price typically moves up and down over a specific number of candles. By multiplying this volatility by a user-defined factor, the script creates a "safety margin."
2. Establishing the Trend Boundaries
The script identifies two dynamic boundaries based on the midpoint of each candle:
- Upper Boundary: Calculated by adding the volatility margin to the midpoint.
- Lower Boundary: Calculated by subtracting the volatility margin from the midpoint.
These boundaries act as "ceilings" and "floors." As long as the price remains within these bounds, the current trend is considered stable.
3. Detecting Trend Shifts
The script constantly monitors whether the closing price has broken through these boundaries:
- Bullish Shift: If the price closes above the upper boundary of the previous period, the indicator recognizes a shift toward an upward (bullish) trend.
- Bearish Shift: If the price closes below the lower boundary of the previous period, the indicator recognizes a shift toward a downward (bearish) trend.
4. Smoothing and Continuity
To prevent the indicator from reacting to minor market "noise" or tiny fluctuations, the script includes logic to keep the lines smooth. It ensures that:
- In an uptrend, the lower boundary never moves downward unless the trend officially flips.
- In a downtrend, the upper boundary never moves upward unless the trend officially flips.
This creates a continuous, trailing line that follows the price closely but stays far enough away to avoid false signals.
5. Visual Representation
The indicator ultimately plots two distinct lines on your chart:
- Green Line (Trend Up): Appears when the market is in an upward phase. It acts as a trailing support level; as the price rises, the line follows it up.
- Red Line (Trend Down): Appears when the market is in a downward phase. It acts as a trailing resistance level; as the price falls, the line follows it down.
When the colors flip, it serves as a clear visual cue that the momentum has shifted from one direction to another.
//+------------------------------------------------------------------+
//| SuperTrend.mq4 v1.2 |
//| Copyright © 2008, Jason Robinson (jnrtrading). |
//| http://www.spreadtrade2win.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, Jason Robinson."
#property link "http://www.spreadtrade2win.com"
#property indicator_chart_window
#property indicator_color1 Lime
#property indicator_color2 Red
#property indicator_width1 2
#property indicator_width2 2
#property indicator_buffers 2
double TrendUp[], TrendDown[];
int changeOfTrend;
extern int Nbr_Periods = 10;
extern double Multiplier = 3.0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexBuffer(0, TrendUp);
SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 2);
SetIndexLabel(0, "Trend Up");
SetIndexBuffer(1, TrendDown);
SetIndexStyle(1, DRAW_LINE, STYLE_SOLID, 2);
SetIndexLabel(1, "Trend Down");
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int limit, i, flag, flagh, trend[5000];
double up[5000], dn[5000], medianPrice, atr;
int counted_bars = IndicatorCounted();
//---- check for possible errors
if(counted_bars < 0) return(-1);
//---- last counted bar will be recounted
if(counted_bars > 0) counted_bars--;
limit=Bars-counted_bars;
//Print(limit);
//----
for (i = Bars; i >= 0; i--) {
TrendUp[i] = EMPTY_VALUE;
TrendDown[i] = EMPTY_VALUE;
atr = iATR(NULL, 0, Nbr_Periods, i);
//Print("atr: "+atr[i]);
medianPrice = (High[i]+Low[i])/2;
//Print("medianPrice: "+medianPrice[i]);
up[i]=medianPrice+(Multiplier*atr);
//Print("up: "+up[i]);
dn[i]=medianPrice-(Multiplier*atr);
//Print("dn: "+dn[i]);
trend[i]=1;
if (Close[i]>up[i+1]) {
trend[i]=1;
if (trend[i+1] == -1) changeOfTrend = 1;
//Print("trend: "+trend[i]);
}
else if (Close[i]<dn[i+1]) {
trend[i]=-1;
if (trend[i+1] == 1) changeOfTrend = 1;
//Print("trend: "+trend[i]);
}
else if (trend[i+1]==1) {
trend[i]=1;
changeOfTrend = 0;
}
else if (trend[i+1]==-1) {
trend[i]=-1;
changeOfTrend = 0;
}
if (trend[i]<0 && trend[i+1]>0) {
flag=1;
//Print("flag: "+flag);
}
else {
flag=0;
//Print("flagh: "+flag);
}
if (trend[i]>0 && trend[i+1]<0) {
flagh=1;
//Print("flagh: "+flagh);
}
else {
flagh=0;
//Print("flagh: "+flagh);
}
if (trend[i]>0 && dn[i]<dn[i+1])
dn[i]=dn[i+1];
if (trend[i]<0 && up[i]>up[i+1])
up[i]=up[i+1];
if (flag==1)
up[i]=medianPrice+(Multiplier*atr);
if (flagh==1)
dn[i]=medianPrice-(Multiplier*atr);
//-- Draw the indicator
if (trend[i]==1) {
TrendUp[i]=dn[i];
if (changeOfTrend == 1) {
TrendUp[i+1] = TrendDown[i+1];
changeOfTrend = 0;
}
}
else if (trend[i]==-1) {
TrendDown[i]=up[i];
if (changeOfTrend == 1) {
TrendDown[i+1] = TrendUp[i+1];
changeOfTrend = 0;
}
}
}
WindowRedraw();
//----
return(0);
}
//+------------------------------------------------------------------+
Comments