Supertrend1_v1

Author: Copyright � 2005, Jason Robinson (jnrtrading).
Supertrend1_v1

The Supertrend indicator is designed to provide traders with a visual representation of market momentum, helping to identify the primary direction of price movement in real-time. By analyzing historical price data, it acts as a dynamic guide that distinguishes between bullish (upward) and bearish (downward) trends on a trading chart.

Core Logic and Mechanics

The indicator functions by processing three distinct types of financial data: momentum, volatility, and price range.

1. Momentum Tracking The script utilizes a Commodity Channel Index (CCI) to measure the current market sentiment. It compares the current momentum against a set threshold to determine if the market is gaining strength or losing steam. When the momentum crosses this established "tipping point," the indicator signals a potential shift in trend direction.

2. Volatility Adjustment To ensure that the trend lines react realistically to market conditions, the script incorporates an Average True Range (ATR) calculation. This measures how much the price typically moves over a specific period. By factoring in this volatility, the indicator creates a "buffer zone" around the price; this prevents the trend line from flickering or changing direction due to minor, insignificant price noise, ensuring that only significant movements are captured.

3. Trend Line Positioning The logic automatically calculates two separate paths for the price:

  • In an Upward Trend: The indicator places a support line below the current price. It is programmed to "lock" in place unless the price breaks significantly below it, providing a visual floor for potential buy entries.
  • In a Downward Trend: The indicator places a resistance line above the current price. Similarly, it acts as a visual ceiling that only moves if the price pushes higher, marking potential sell zones.

How it Appears on the Chart

The result is a clean, dual-colored trend line that follows the price action. When the market is moving up, the indicator tracks beneath the candles, providing a "trailing" support level. When the market shifts to a downward move, the line flips and moves above the candles to act as a trailing resistance level.

For the user, this simplifies market analysis by filtering out "choppy" price action and highlighting the overarching trend, allowing for clearer decision-making on when to stay with the current momentum or look for a potential reversal.

Indicators Used
Commodity channel indexIndicator of the average true range
Miscellaneous
Implements a curve of type %1
14 Views
0 Downloads
0 Favorites
Supertrend1_v1
/*-----------------------------+
|			       |
| Shared by www.Aptrafx.com    |
|			       |
+------------------------------*/

//+------------------------------------------------------------------+
//|                                                   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