//+------------------------------------------------------------------+
//|                                          Hodrick Prescott MA.mq4 |
//|                                               Yuriy Tokman (YTG) |
//|     Mod_Pacois                                https://ytg.com.ua |
//+------------------------------------------------------------------+
#property copyright "Yuriy Tokman (YTG)"
#property link      "https://ytg.com.ua"
#property version   "1.00"
#property strict
#property indicator_separate_window

#property indicator_buffers 4
#property indicator_color1 clrNONE//clrRed
#property indicator_color2 clrNONE //clrGold
#property indicator_color3 clrGray
#property indicator_color4 clrLime
#property indicator_width1 2
#property indicator_width2 2
#property indicator_level1 0
input int Shift    = 1;
input int Filter_1 = 999;
input int Filter_2 = 1599;
input int Price;
extern bool HistoR=false;
extern bool HistoRC=false;
extern string _______AlertUp1________;
extern double LimeDn=0.0;
extern bool BullOn=false;
extern bool SoundBull=false;
extern string FileSoundBull = "alert.wav";
extern string _______AlertDn1_________;
extern double LimeUp=10;
extern bool BearOn=false;
extern bool SoundBear=false;
extern string FileSoundBear = "alert2.wav";



int PlayedSoundHBull = False;
int PlayedSoundLBear = False;
double B0[];
double B1[];
double Range[];
double RangeClose[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   string short_name = "Hodrick Prescott MA";
   IndicatorShortName(short_name);
   SetIndexLabel(1,short_name+" slow");
   SetIndexLabel(0,short_name+" fast");   
//--- indicator buffers mapping
   SetIndexBuffer(0,B0);
   SetIndexStyle(0,DRAW_NONE);
   SetIndexShift(0,Shift);

   SetIndexBuffer(1,B1);
   SetIndexStyle(1,DRAW_NONE);  
   
   SetIndexBuffer(2,Range);
   SetIndexStyle(2,DRAW_LINE);  if(HistoR)  SetIndexStyle(2,DRAW_HISTOGRAM,0,2); 
   
      SetIndexBuffer(3,RangeClose);
   SetIndexStyle(3,DRAW_LINE);  if(HistoRC)  SetIndexStyle(3,DRAW_HISTOGRAM,0,2); 
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 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[])
  {
//---
   Hodrick_Prescott_Filter(Filter_1,Filter_2,B0);
   Hodrick_Prescott_Filter(Filter_1,Filter_2,B1);   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
void Hodrick_Prescott_Filter(int filtr1,int filtr2,double &buff[])
  {
   double a[],b[],c[],variable1=0,variable2=0,variable3=0,variable4=0,variable5=0,variable6,variable7=0,
   variable8=0,variable9=0,variable10,variable11,variable12;
   ArrayResize(a,filtr1);
   ArrayResize(b,filtr1);
   ArrayResize(c,filtr1);
   for(int i=0;i<filtr1;i++) buff[i]=iMA(NULL,0,1,0,0,Price,i);

   a[0]=1.0+filtr2;
   b[0]=-2.0*filtr2;
   c[0]=filtr2;
   for(int i=1;i<filtr1-2;i++)
     {
      a[i]=6.0*filtr2+1.0;
      b[i]=-4.0*filtr2;
      c[i]=filtr2;
     }
   a[1]=5.0*filtr2+1;
   a[filtr1-1]=1.0+filtr2;
   a[filtr1-2]=5.0*filtr2+1.0;
   b[filtr1-2]=-2.0*filtr2;
   b[filtr1-1]=0.0;
   c[filtr1-2]=0.0;
   c[filtr1-1]=0.0;

   for(int i=0;i<filtr1;i++)
     {
      variable12=a[i]-variable4*variable1-variable9*variable7;
      variable10=b[i];
      variable6=variable1;
      variable1=(variable10-variable4*variable2)/variable12;
      b[i]=variable1;
      variable11=c[i];
      variable7=variable2;
      variable2=variable11/variable12;
      c[i]=variable2;
      a[i]=(buff[i]-variable8*variable9-variable3*variable4)/variable12;
      variable8=variable3;
      variable3=a[i];
      variable4=variable10-variable5*variable6;
      variable9=variable5;
      variable5=variable11;
     }

   variable2=0;
   variable1=a[filtr1-1];
   buff[filtr1-1]=variable1;
   for(int i=filtr1-2;i>=0;i--)
     {
      buff[i]=a[i]-b[i]*variable1-c[i]*variable2;
      variable2=variable1;
      variable1=buff[i];
      
      //Mod_Pacois Mod_Pacois   Mod_Pacois   Mod_Pacois  Mod_Pacois       
        Range[i]=(B1[i]/Point*10-B0[i+Shift ]/Point*10);
      
       RangeClose[i]=(Close[i]/Point-B1[i ]/Point);
      
     }
     
     
     
         if(    Close[0]/Point-Open[0]/Point<10         ){PlayedSoundHBull = False;}
   
         if(   Close[0]-Open[0]>0          ){PlayedSoundLBear = False;} 
    
             
     if(  Close[0]/Point-Open[0]/Point>10
          && Volume[0]>10
          &&  Range[0]>10  
         &&  Range[0]>  Range[1] 
        && RangeClose[0]<LimeDn
       &&RangeClose[0]>RangeClose[1]
        &&  Range[1]>  RangeClose[1] 
        &&PlayedSoundHBull == False){
     if(BullOn)Alert(Symbol(), " (", Period(), " min)Hprescott separate_Up |" ,Range[0],  " Range|  ");
     if (SoundBull) PlaySound(FileSoundBull);
    PlayedSoundHBull = True;}
    
   if(    Close[0]-Open[0]<0 
        &&   Range[1]<-10     
        && RangeClose[1]>LimeUp
    && PlayedSoundLBear == False){
     if(BearOn)Alert(Symbol(), " (", Period(), " min)Hprescott separate_Dn |",Range[0],  " Range |  ");
    if (SoundBear) PlaySound(FileSoundBear);
    PlayedSoundLBear = True;}  
     
     
     
     
     
     
   
  }
//----

