LaguerreFilterWithSignal

Author: © mladen, 2018
0 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

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---