LaguerreFilterWithSignal

Download script
Author: © mladen, 2018

Indicator Overview

The Laguerre Filter with Arrows is a sophisticated technical analysis tool designed to help traders identify the underlying trend of a financial market while filtering out minor price fluctuations (noise). Unlike standard moving averages that can sometimes be "laggy" or react too slowly, this script uses a specialized mathematical approach to provide smoother data and clear visual signals for potential entry points.

Core Logic & Mechanics

The functionality of this script can be broken down into three primary components:

1. The Laguerre Filtering System

At its heart, the indicator utilizes a "Laguerre Filter." This is a specific type of mathematical algorithm that calculates a smoothed price value by weighting historical data in a way that prioritizes current momentum while maintaining a steady average.

  • Smoothing: It processes price data to create a continuous line that represents the general "path" of the market.
  • Price Selection: The script is versatile; it can calculate this filter based on various data points, such as the Closing Price, Open Price, High/Low prices, or even Median and Weighted averages, depending on your preference.

2. Dynamic Trend Detection (Color Coding)

The indicator doesn't just draw a line; it analyzes the direction of that line in real-time:

  • Bullish Sentiment: When the filtered price is trending upward compared to its previous value, the line changes color (typically to a greenish hue), signaling strength.
  • Bearish Sentiment: When the filtered price begins to trend downward, the line changes color (typically to a crimson/red hue), signaling weakness.
  • Neutrality: If there is no significant change in direction, it maintains a neutral gray tone.

3. Visual Entry Signals (The Arrows)

To make the data actionable for traders, the script includes an automated "Signal" system represented by arrows on the chart:

  • Buy Signals: An arrow appears specifically at the moment the filter detects a shift from a downward trend to an upward move.
  • Sell Signals: An arrow appears when the filter detects a shift from an upward trend to a downward move.
  • Visual Offset: The script includes a customizable "Shift" feature, which allows these arrows to be placed slightly ahead or behind the current candle to ensure they are clearly visible and do not overlap with the price bars.

Summary for Traders

Essentially, this tool acts as a high-tech "trend compass." It takes raw, messy price data, cleans it through the Laguerre Filter to show you the true direction of the market, and then automatically highlights the exact moments where that direction flips, providing you with clear Buy and Sell visual cues.

8 Views
0 Downloads
0 Favorites
LaguerreFilterWithSignal
ÿþ//------------------------------------------------------------------

#property copyright   "© mladen, 2018"

#property link        "mladenfx@gmail.com"

#property description "Laguerre filter - no gamma with arrow"

//------------------------------------------------------------------

#property indicator_chart_window

#property indicator_buffers 4

#property indicator_plots   3

#property indicator_label1  "Laguerre filter With Arrow"

#property indicator_type1   DRAW_COLOR_LINE

#property indicator_color1  clrDarkGray,clrMediumSeaGreen,clrCrimson

#property indicator_width1  2



#property indicator_type2   DRAW_ARROW

#property indicator_type3   DRAW_ARROW

#property indicator_color2  clrRed

#property indicator_color3  clrLime

#property indicator_label2  "Buy"

#property indicator_label3  "Sell"



//--- input parameters

input double             inpPeriod = 14;          // Period

input ENUM_APPLIED_PRICE inpPrice  = PRICE_CLOSE; // Price

input double             inpArrowShift = 15;      // Shift

//--- indicator buffers

double val[],valc[];

double ExtUpperBuffer[];

double ExtLowerBuffer[];

//--- 10 pixels upper from high price

int    ExtArrowShift=15;



//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

int OnInit()

  {

  ExtArrowShift = inpArrowShift;

//--- indicator buffers mapping

   SetIndexBuffer(0,val,INDICATOR_DATA);

   SetIndexBuffer(1,valc,INDICATOR_COLOR_INDEX);

   SetIndexBuffer(2,ExtUpperBuffer,INDICATOR_DATA);

   SetIndexBuffer(3,ExtLowerBuffer,INDICATOR_DATA);

   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);

//---- sets first bar from what index will be drawn

   PlotIndexSetInteger(2,PLOT_ARROW,234);

   PlotIndexSetInteger(1,PLOT_ARROW,233);

//---- arrow shifts when drawing

   PlotIndexSetInteger(2,PLOT_ARROW_SHIFT,-ExtArrowShift);

   PlotIndexSetInteger(1,PLOT_ARROW_SHIFT,ExtArrowShift);

//---- sets drawing line empty value--

   PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,EMPTY_VALUE);

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE);

//---- initialization done

//--- indicator short name assignment

   IndicatorSetString(INDICATOR_SHORTNAME,"Laguerre filter -ng ("+(string)inpPeriod+")");

//---

   return (INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| Custom indicator de-initialization function                      |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

  {

  }

//+------------------------------------------------------------------+

//| Custom indicator iteration function                              |

//+------------------------------------------------------------------+

int OnCalculate(const int rates_total,const int prev_calculated,const datetime &time[],

                const double &open[],

                const double &high[],

                const double &low[],

                const double &close[],

                const long &tick_volume[],

                const long &volume[],

                const int &spread[])

  {



   int i=(int)MathMax(prev_calculated-1,0);

   for(; i<rates_total && !_StopFlag; i++)

     {

      val[i]=iLaGuerreFil(getPrice(inpPrice,open,close,high,low,i),inpPeriod,i);

      valc[i]=(i>0) ?(val[i]>val[i-1]) ? 1 :(val[i]<val[i-1]) ? 2 : valc[i-1]: 0;

      if(i == 0)

        {

         ExtUpperBuffer[i] = EMPTY_VALUE;

         ExtLowerBuffer[i] = EMPTY_VALUE;

        }

      else

        {

         if(valc[i] == 1 && valc[i-1] != 1)

           {

            ExtUpperBuffer[i] = val[i];

            ExtLowerBuffer[i] = EMPTY_VALUE;

           }

         else

            if(valc[i] == 2 && valc[i-1] != 2)

              {

               ExtLowerBuffer[i] = val[i];

               ExtUpperBuffer[i] = EMPTY_VALUE;

              }

            else

              {

               ExtUpperBuffer[i] = EMPTY_VALUE;

               ExtLowerBuffer[i] = EMPTY_VALUE;

              }



        }



     }

   return(i);

  }

//+------------------------------------------------------------------+

//| Custom functions                                                 |

//+------------------------------------------------------------------+

#define _lagFilInstances 1

#define _lagFilInstancesSize 4

#define _lagFilRingSize 6

double workLagFil[_lagFilRingSize][_lagFilInstances*_lagFilInstancesSize];

double iLaGuerreFil(double price, double period, int i, int instance=0)

  {

   int _indC = (i)%_lagFilRingSize;

   int _inst = instance*_lagFilInstancesSize;



//

//---

//



   if(i>0 && period>1)

     {

      int    _indP  = (i-1)%_lagFilRingSize;

      double _gamma = 1.0 - 10.0/(period+9.0);

      workLagFil[_indC][_inst  ] =  price                      + _gamma*(workLagFil[_indP][_inst  ] - price);

      workLagFil[_indC][_inst+1] =  workLagFil[_indP][_inst  ] + _gamma*(workLagFil[_indP][_inst+1] - workLagFil[_indC][_inst  ]);

      workLagFil[_indC][_inst+2] =  workLagFil[_indP][_inst+1] + _gamma*(workLagFil[_indP][_inst+2] - workLagFil[_indC][_inst+1]);

      workLagFil[_indC][_inst+3] =  workLagFil[_indP][_inst+2] + _gamma*(workLagFil[_indP][_inst+3] - workLagFil[_indC][_inst+2]);

     }

   else

      for(int k=0; k<_lagFilInstancesSize; k++)

         workLagFil[_indC][_inst+k]=price;



//

//---

//



   return((workLagFil[_indC][_inst]+2.0*workLagFil[_indC][_inst+1]+2.0*workLagFil[_indC][_inst+2]+workLagFil[_indC][_inst+3])/6.0);

  }

//

//---

//

double getPrice(ENUM_APPLIED_PRICE tprice,const double &open[],const double &close[],const double &high[],const double &low[],int i)

  {

   if(i>=0)

      switch(tprice)

        {

         case PRICE_CLOSE:

            return(close[i]);

         case PRICE_OPEN:

            return(open[i]);

         case PRICE_HIGH:

            return(high[i]);

         case PRICE_LOW:

            return(low[i]);

         case PRICE_MEDIAN:

            return((high[i]+low[i])/2.0);

         case PRICE_TYPICAL:

            return((high[i]+low[i]+close[i])/3.0);

         case PRICE_WEIGHTED:

            return((high[i]+low[i]+close[i]+close[i])/4.0);

        }

   return(0);

  }

//+------------------------------------------------------------------+

Comments