Supertrend1

Download script
Author: Copyright � 2005, Jason Robinson (jnrtrading).
Supertrend1

This technical script is designed to function as a visual trend-following tool for trading charts. It automates the identification of market direction by analyzing price momentum and volatility, providing traders with clear visual cues on whether the current market sentiment is bullish (upward) or bearish (downward).

Core Logic and Mechanism

The script operates using three primary analytical components to determine how the trend should be displayed:

1. Momentum Detection The tool utilizes a specific momentum oscillator (CCI) to measure the strength of price movements relative to their recent averages. By comparing the current value of this momentum against its previous value, the script identifies "crossover" points. These crossovers act as triggers that signal a potential shift in market direction—transitioning from an upward trend to a downward trend, or vice versa.

2. Volatility Measurement To ensure the trend lines react realistically to market conditions, the script incorporates an Average True Range (ATR) calculation. This measures current market volatility. Instead of drawing lines based on static price points, it uses this volatility data to create "buffer zones" around the price action. This helps prevent the indicator from reacting too aggressively to minor price fluctuations while still capturing significant moves.

3. Dynamic Trend Smoothing The script employs a recursive logic to maintain the continuity of the trend lines:

  • For Upward Trends: It calculates a position below the current price based on volatility. If the newly calculated position is lower than the previous one, the script "holds" the previous higher value. This ensures the line moves in a steady upward trajectory without jagged erratic drops.
  • For Downward Trends: It calculates a position above the current price. Similarly, if a new calculation suggests a move that would break the downward continuity, it maintains the previous lower value.

Visual Output

The final result is a dynamic dual-line system displayed directly on the trading chart:

  • When momentum leans positive, an upward trend line is plotted to help identify support levels and "buy" zones.
  • When momentum leans negative, a downward trend line is plotted to identify resistance levels and "sell" zones.

By combining momentum triggers with volatility-based smoothing, the script aims to provide a clean visual representation of the prevailing market trend, filtering out "noise" to help users see the larger movement of the price.

Indicators Used
Commodity channel indexIndicator of the average true range
Miscellaneous
Implements a curve of type %1
11 Views
0 Downloads
0 Favorites
Supertrend1
//+------------------------------------------------------------------+
//|                                                   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 2
#property indicator_color1 Lime
#property indicator_color2 HotPink
#property indicator_width1 2
#property indicator_width2 2

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);
  }
//+------------------------------------------------------------------+
//| Custom 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