Instant Trendline Filter

Download script
Author: Copyright � 2006, Luis Guilherme Damiani
Instant Trendline Filter

This tool is designed to provide traders with a visual representation of market trends by filtering out "noise" and identifying the underlying direction of price movement. Unlike standard moving averages that might lag significantly behind the current price, this script focuses on creating a smoothed flow of data that helps distinguish between temporary fluctuations and established momentum.

How it Works

1. Data Selection (The Foundation) The logic begins by determining what specific price point to use for its calculations. You can choose between two primary methods:

  • Median Price: The tool calculates the midpoint between the highest and lowest prices of a candle. This is often preferred for seeing where the "middle" of the action occurred.
  • Typical Price: The tool averages the high, low, and closing prices together. This provides a slightly different perspective by including the final price of the period.

2. Smoothing and Weighting (The Filter) Once the base price is selected, the script applies a sophisticated mathematical weighting system. Instead of treating every candle with equal importance, it uses a specific "Alpha" coefficient to weigh the relationship between the current price and the prices of previous candles.

This creates a weighted average that seeks to balance two things:

  • Responsiveness: Ensuring the line follows the market movements closely enough to be relevant.
  • Stability: Smoothing out erratic spikes or "wicks" that don't represent a true change in trend.

3. Trendline Generation The final output consists of two distinct visual components:

  • Primary Trend Line (Lime): This represents the primary filtered smoothed value. It acts as the main guide for the current market direction.
  • Secondary Dynamic Line (Red): The script performs an additional calculation on the primary line to derive a secondary indicator. This helps in observing the rate of change or the "velocity" of the trend, allowing traders to see if the current momentum is accelerating or slowing down.

Summary for the User

In practical terms, this script translates raw, messy price action into two clean lines. By looking at the relationship between these lines and the price candles, you can better identify whether the market is in a sustained uptrend, a downtrend, or moving sideways. It serves as a "filter" that helps clear away the distractions of minor price jumps, leaving you with a clearer picture of where the market is actually heading.

Miscellaneous
Implements a curve of type %1
13 Views
0 Downloads
0 Favorites
Instant Trendline Filter
//+------------------------------------------------------------------+
//|                                    Instantaneous Trendline Filter|
//|                         Copyright © 2006, Luis Guilherme Damiani |
//|                                      http://www.damianifx.com.br |
//|
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Luis Guilherme Damiani"
#property link      "http://www.damianifx.com.br"

#property indicator_buffers 2
//#property indicator_separate_window
#property indicator_chart_window

#property indicator_color1 Lime
#property indicator_color2 Red
//#property indicator_color3 Yellow

//---- input parameters
extern double       alpha=0.07;
extern int       maxbars=2000;
extern string 
Price_Choice="-----  Typical or Median  --------------";
extern bool is_median=true;

//---- buffers
double ind1[];
double ind2[];
//double AuxBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ind1);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,ind2);
 //  SetIndexStyle(2,DRAW_LINE);
//   SetIndexBuffer(2,AuxBuffer);
//   SetLevelValue(0,0.8);
//   SetLevelValue(1,-0.8);
//   ArrayInitialize(ind1,0);
//   ArrayInitialize(ind2,0);
//   ArrayInitialize(AuxBuffer,0);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
//---- 
   
//----
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

int start()
  {
      double pr0,pr1,pr2=0;
      
      int    counted_bars=IndicatorCounted();
      
      //double Normalized=0;
      //double Fish=0;
      //---- check for possible errors
      if(counted_bars<0) return(-1);
      int limit=Bars-counted_bars;
      if(limit>maxbars)limit=maxbars;      
      //if (limit>Bars-1)limit=Bars-1;   
      //---- 
      for (int i = limit; i>=0;i--)
      {
         if(is_median)
         {
            pr0=(High[i]+Low[i])/2;
            pr1=(High[i+1]+Low[i+1])/2;
            pr2=(High[i+2]+Low[i+2])/2;      
         }
         else
         {
            pr0=(High[i]+Low[i]+Close[i])/3;
            pr1=(High[i+1]+Low[i+1]+Close[i+1])/3;
            pr2=(High[i+2]+Low[i+2]+Close[i+2])/3;      
         }
	      
	      ind1[i]=(alpha-MathPow(alpha/2,2))*pr0
	           +(MathPow(alpha,2)/2)*pr1
	           -(alpha-3*MathPow(alpha,2)/4)*pr2
	           + 2*(1-alpha)*ind1[i+1]
	           -MathPow(1-alpha,2)*ind1[i+2];
	      ind2[i]=2*ind1[i]-ind1[i+2];
         
       }
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

Comments