SupertrendLX

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

This script is designed to serve as a visual trend-following tool for traders. Its primary goal is to identify the direction of market momentum and provide clear signals on whether the current price action is moving upward or downward. Instead of just looking at raw price movement, the logic combines several technical layers to smooth out "noise" and highlight significant shifts in market sentiment.

How the Logic Works

1. Momentum Assessment The script begins by analyzing a momentum oscillator. It looks at how the current price compares to its recent historical average. By comparing the current momentum value against its previous state, the tool can detect "cross-overs"—moments where the market shifts from one phase to another (e.g., moving from a downward trend into an upward one).

2. Volatility Filtering To ensure that the trend lines aren't jumping erratically due to minor price fluctuations, the script incorporates a volatility measurement. It calculates the average range of price movement over a specific period. This helps the tool understand the "heartbeat" of the market, allowing it to distinguish between a significant trend change and a temporary spike in price.

3. Trend Line Dynamics The indicator maintains two distinct visual paths: one for upward trends and one for downward trends.

  • In an Uptrack: The tool calculates a support level based on the recent lows of the market, adjusted by the current volatility. It is designed to "hold" its position; if a new price calculation would result in a lower line than the previous one, it maintains the previous level. This creates a step-like effect that follows the trend smoothly.
  • In a Downtrack: Conversely, when the momentum suggests a bearish move, the tool calculates a resistance level based on recent highs. Similar to the uptrend logic, it ensures the line never moves "downward" in value unless the market actually breaks through the previous resistance point.

4. Visual Execution The result is a dynamic, trailing line on your chart. When the momentum shifts positive, the script switches the active trend. By using these calculations, the indicator attempts to stay "behind" the price action, providing a trailing visual cue that helps traders identify when a trend is strengthening or when it might be reaching a point of exhaustion.

Indicators Used
Commodity channel indexIndicator of the average true range
Miscellaneous
Implements a curve of type %1
12 Views
0 Downloads
0 Favorites
SupertrendLX
//+------------------------------------------------------------------+
//|                                                   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 Magenta


double TrendUp[];
double TrendDown[];
int st = 0;
//extern int SlowerEMA = 6;



//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators

   SetIndexStyle(0, DRAW_LINE, 1,3);
   SetIndexBuffer(0, TrendUp);
   SetIndexStyle(1, DRAW_LINE, 1,3);
   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