Supertrend_003
Download scriptThis tool is designed to help traders visualize market trends by identifying the primary direction of price movement. It works by filtering out "market noise" and providing clear visual cues on whether the current momentum is leaning toward an upward or downward trajectory.
How the Logic Works
The script achieves its results by combining three distinct mathematical layers:
1. Momentum Tracking The tool first looks at a specific momentum indicator (the CCI) to determine the overall "strength" of the market. It compares the current momentum value against a specific threshold. By looking at whether this value is rising or falling relative to that threshold, the script identifies the core sentiment of the price action.
2. Volatility Filtering To ensure the trend lines aren't jittery or overly sensitive to tiny price fluctuations, the tool incorporates an Average True Range (ATR). This allows the script to calculate a "buffer zone" around the price. Instead of following every small tick, it uses this volatility measurement to determine how far away the trend line should sit from the actual candles, creating a smoother and more reliable visual experience.
3. Trend Consistency & Smoothing One of the key features of this logic is its "memory." The script is programmed to ensure that the trend lines only move in a specific direction:
- In an Uptrend: The line is designed to never move downward. If a new price calculation suggests a lower point, the script holds the previous higher value, ensuring a steady upward progression.
- In a Downtrend: The line is designed to never move upward. It retains the previous lower value to maintain a consistent downward slope.
Visual Representation
The logic results in two primary visual elements on your chart:
- The Trend Up (Lime): This appears when the momentum signals a bullish bias. It follows below the price action, moving up as long as the market remains strong.
- The Trend Down (Red): This appears when the momentum signals a bearish bias. It follows above the price action, descending as long as the downward pressure continues.
Summary for the Trader
Essentially, this script acts as a dynamic "floor" or "ceiling." When the trend switches from red to lime, it signals that momentum has shifted upward; when it switches from lime to red, it suggests the market is beginning to head lower. By using volatility and historical momentum together, it aims to provide a cleaner, more stable view of the market's direction than looking at raw price candles alone.
//+------------------------------------------------------------------+
//| Supertrend.mq4 |
//| Copyright © 2005, Jason Robinson (jnrtrading). |
//| http://www.jnrtrading.co.uk |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, Jason Robinson (jnrtrading)."
#property link "http://www.jnrtrading.co.uk"
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Lime
#property indicator_color2 Red
double TrendUp[];
double TrendDown[];
int st = 0;
//extern int SlowerEMA = 6;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 2);
SetIndexBuffer(0, TrendUp);
SetIndexStyle(1, DRAW_LINE, STYLE_SOLID, 2);
SetIndexBuffer(1, TrendDown);
/*SetIndexStyle(0, DRAW_ARROW, EMPTY);
SetIndexArrow(0, 159);
SetIndexBuffer(0, TrendUp);
SetIndexStyle(1, DRAW_ARROW, EMPTY);
SetIndexArrow(1, 159);
SetIndexBuffer(1, TrendDown);*/
/*for(int i = 0; i < Bars; i++) {
TrendUp[i] = NULL;
TrendDown[i] = NULL;
}*/
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
/*for(int i = 0; i < Bars; i++) {
TrendUp[i] = NULL;
TrendDown[i] = NULL;
}*/
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int limit, i, counter;
double Range, AvgRange, cciTrendNow, cciTrendPrevious, var;
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;
for(i = limit; i >= 0; i--) {
cciTrendNow = iCCI(NULL, 0, 50, PRICE_TYPICAL, i);
cciTrendPrevious = iCCI(NULL, 0, 50, PRICE_TYPICAL, i+1);
//st = st * 100;
counter = i;
Range = 0;
AvgRange = 0;
for (counter = i; counter >= i-9; counter--) {
AvgRange = AvgRange + MathAbs(High[counter]-Low[counter]);
}
Range = AvgRange/10;
if (cciTrendNow >= st && cciTrendPrevious < st) {
TrendUp[i+1] = TrendDown[i+1];
}
if (cciTrendNow <= st && cciTrendPrevious > st) {
TrendDown[i+1] = TrendUp[i+1];
}
if (cciTrendNow >= st) {
TrendUp[i] = Low[i] - iATR(NULL, 0, 5, i);
if (TrendUp[i] < TrendUp[i+1]) {
TrendUp[i] = TrendUp[i+1];
}
}
else if (cciTrendNow <= st) {
TrendDown[i] = High[i] + iATR(NULL, 0, 5, i);
if (TrendDown[i] > TrendDown[i+1]) {
TrendDown[i] = TrendDown[i+1];
}
}
}
//----
//----
return(0);
}
//+------------------------------------------------------------------+
Comments