Supertrend_v1

Download script
Author: Copyright � 2006, MetaQuotes Software Corp.
Supertrend_v1

This tool is designed to help traders visually identify the prevailing direction of the market by plotting a dynamic trend line on the chart. It simplifies complex price movements into two primary signals: an upward trend and a downward trend.

Core Methodology

The logic operates by monitoring three specific financial components to determine where the "trend" sits at any given moment:

  • Momentum Tracking: The script looks at the Commodity Channel Index (CCI) to measure the current strength of price movement relative to its recent history. It compares the current value against a threshold to detect shifts in momentum.
  • Volatility Measurement: To ensure the trend line isn't too sensitive to minor price "noise," the script incorporates an Average True Range (ATR). This calculates the average distance between highs and lows over a set period, allowing the tool to place the trend line at a meaningful distance from the actual price candles.
  • Price Range Analysis: It examines the relative spread of high and low prices over the last 10 bars to understand the current market environment.

How the Signals Work

The indicator creates two distinct lines on your chart, which switch based on how the price interacts with these calculations:

The Bullish Trend (Upward): When momentum signals a positive shift, the script calculates a support level by subtracting a portion of the volatility (ATR) from the current low price. Crucially, it includes a "memory" feature: if the newly calculated level is lower than the previous one, the script keeps the higher value. This ensures that the trend line only moves in your favor and doesn't "drop" during minor pullbacks.

The Bearish Trend (Downward): Conversely, when momentum signals a negative shift, the script calculates a resistance level by adding the volatility (ATR) to the current high price. Similar to the upward trend, it maintains a "memory" of the previous position—if a new calculation suggests a lower price than the previous bar, it holds onto the higher value. This prevents the trend line from jumping upward during temporary price spikes.

Visual Summary

By combining these factors, the script filters out minor fluctuations and highlights the broader structure of the market. When the lines flip, it indicates a potential change in market sentiment, providing a clearer picture of whether the current environment is favorablly trending up or down.

Indicators Used
Commodity channel indexIndicator of the average true range
Miscellaneous
Implements a curve of type %1
8 Views
0 Downloads
0 Favorites
Supertrend_v1
//+------------------------------------------------------------------+
//|                                                   Supertrend.mq4 |
//|                      Copyright © 2006, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#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