//------------------------------------------------------------------
//  FATL
//  Dmitry Yakovlev 
//  dmitry_yakovlev@rambler.ru
//  WebMoney R865705290089
//------------------------------------------------------------------
// --- Parameters DF: 
// FATL:   P1=14, D1=10, A1=40, P2=41, D2=31, A2=10, Ripple=0.0864, Delay=0

#property copyright "Dmitry Yakovlev, Russia,Omsk WM R865705290089"
#property link      "dmitry_yakovlev@rambler.ru"

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Black
#property indicator_color2 Lime
#property indicator_color3 Red
#property indicator_width1 0
#property indicator_width2 2
#property indicator_width3 2

#import  "shell32.dll"           //Connect a dll (provided with Windows)             
  int ShellExecuteA(int hwnd,string Operation,string File,string Parameters,string Directory,int ShowCmd); 
#import "user32.dll"
  int MessageBoxA(int hWnd ,string lpText,string lpCaption,int uType);
//------------------------------------------------------------------------------------------------------

// ПАРАМЕТРЫ
extern string  _tf="Таймфрейм (0-текущий)";
extern int     tf=60;
extern string  _pr="Цена 0-cl,1-op,2-hi,3-lo";
extern int     pr=0;

//---- buffers
double iBuffer[];
double up[];
double dn[];
double trend[];

double F[]=
{
0.342152940854,+0.3093292710818,+0.2494886026077,+0.1729967470992,+0.0925637619799,+0.02063133334364,-0.0330807757871,
-0.0633224564657,-0.0699201252728,-0.0571825815615,-0.0326138365275,-0.00472222203391,+0.01887417755416,+0.0330626229331,
+0.0359799385551,+0.02892153255004,+0.01547756053013,+0.0002774619935816,-0.01239612720349,-0.01962143471368,
-0.02040646532991,-0.01564282987562,-0.00754279142733,+0.001162950482821,+0.00803410120806,+0.01154650780504,
+0.01135097902722,+0.00815882017765,+0.00337147099221,-0.001454786740187,-0.00509465397315,-0.00705602263847,
-0.00791744078702,-0.00996601702916,+0.00455978659094
};
int FilterSize=0;
//+------------------------------------------------------------------+
//| Digital filter indicator initialization function                 |
//+------------------------------------------------------------------+
int init()
{
   CheckDonate();
   
   FilterSize=ArraySize(F);
   
   if(tf<Period()) tf=Period();
   if(pr>1)pr=0;
//---- indicator line

   IndicatorShortName("FATL-"+tf);
   
   IndicatorBuffers(4);
   //---- indicator line
   SetIndexStyle(0,DRAW_NONE);
   SetIndexBuffer(0,iBuffer);

   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2);
   SetIndexBuffer(1,up);
   SetIndexLabel(1,"Up");

   SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,2);
   SetIndexBuffer(2,dn);
   SetIndexLabel(2,"Dn");

   SetIndexStyle(3,DRAW_NONE);
   SetIndexBuffer(3,trend);
   SetIndexLabel(3,"trend");

   ArraySetAsSeries(iBuffer, true); 
   ArraySetAsSeries(trend, true);
//----
   return(0);
}
//+------------------------------------------------------------------+
double price(int i)
{
   if(pr==0) return(iClose(Symbol(),tf,i));
   else if(pr==1) return(iOpen(Symbol(),tf,i));
   else if(pr==2) return(iHigh(Symbol(),tf,i));
   else if(pr==3) return(iLow(Symbol(),tf,i));
}

//+------------------------------------------------------------------+
int start()
{
   int k, nn, nn1, i, j, counted_bars=IndicatorCounted();
   int koef=(tf/Period());
   double res=0, step=0;
   // <--- расчитываем значения индикатора
   if (FilterSize>0)
   {
      if(Bars<=FilterSize) return(0);
      //-------------------------------
      i=(Bars-counted_bars+FilterSize)/koef+1;
      //-------------------------------
      while(i>=0)
      {
         res=0;
         for (j=0; j<FilterSize; j++)
            res+=F[j]*price(i+j);
         
         nn=i*koef;
         iBuffer[nn]=res;//NormalizeDouble(res,Digits);
         
         nn1=(i+1)*koef;
         if(iBuffer[nn1]!=EMPTY_VALUE && iBuffer[nn1]!=0)
         {
            step=(iBuffer[nn1]-iBuffer[nn])/koef;
            for(k=1;k<koef;k++) iBuffer[k+nn]=iBuffer[nn]+step*k;
         }
         i--;
      }
   } else return;
   // расчитываем -->
   //-------------------------------------------------------------------------
   // <-- раскрашиваем 
   for(int x=Bars;x>=0;x--)
   {
      trend[x] = trend[x+1];
      if (iBuffer[x]> iBuffer[x+1]) trend[x] =1;
      if (iBuffer[x]< iBuffer[x+1]) trend[x] =-1;
 
      if (trend[x]>0)
      { up[x] = iBuffer[x]; 
        if (trend[x+1]<0) up[x+1]=iBuffer[x+1];
        dn[x] = EMPTY_VALUE;
      }
      else              
      if (trend[x]<0)
      { 
        dn[x] = iBuffer[x]; 
        if (trend[x+1]>0) dn[x+1]=iBuffer[x+1];
        up[x] = EMPTY_VALUE;
      }              
   }
   // раскрашиваем -->
   //-------------------------------------------------------------------------
   return(0);
}

void CheckDonate()
{
   int fd=0; string pay="0"; datetime dt=0;
   string fn="DigFlts1.txt";
   fd=FileOpen(fn,FILE_READ|FILE_CSV,";");
   if(fd>=1)
   {
      pay=FileReadString(fd); if(pay!="0" && pay!="1") pay="0";
      dt=StrToTime(FileReadString(fd));
   }
   else
   {
      dt=TimeCurrent();
      fd=FileOpen(fn,FILE_WRITE|FILE_CSV,";");
      FileWrite(fd,"0",TimeToStr(dt,TIME_DATE));
   }
   FileClose(fd);
   
   if(pay=="0" && (TimeCurrent()-dt)>10*24*60*60) // 5 дней
   {

      dt=TimeCurrent();
      
      fd=FileOpen(fn,FILE_WRITE|FILE_CSV,";");
      FileWrite(fd,pay,TimeToStr(dt,TIME_DATE));
      FileClose(fd);
   }
   FileClose(fd);
}

