//+------------------------------------------------------------------+
//|                                                      Kaufman.mq4 |
//|                             Copyright © -2005, by konKop & wellx |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, by konKop,wellx"
#property link      "http://www.metaquotes.net"
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 Sienna
#property indicator_color2 Blue
#property indicator_color3 OrangeRed
#property indicator_color4 Red
//---- input parameters
extern int       periodAMA=10;
extern int       nfast=2;
extern int       nslow=15;//30
extern double    G=6.0;
extern double    dK=10000;
extern int    RSI=250;

//---- buffers
double kAMAbuffer[];
double kAMAupsig[];
double kAMAdownsig[];
double NewIndikator[];
double trend[];
//+------------------------------------------------------------------+
int    k=0,cbars=0,prevbars=0,prevtime=0;
double slowSC,fastSC,AMA0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   IndicatorBuffers(5);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,kAMAbuffer);
   SetIndexStyle(1,DRAW_ARROW,0,3);
   SetIndexArrow(1,159);
   SetIndexBuffer(1,kAMAupsig);
   SetIndexStyle(2,DRAW_ARROW,0,3);
   SetIndexArrow(2,159);
   SetIndexBuffer(2,kAMAdownsig);

   SetIndexStyle(3,DRAW_LINE);
   SetIndexBuffer(3,NewIndikator);

   SetIndexBuffer(4,trend);
//SetIndexDrawBegin(0,nslow+nfast);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
//---------------NewIndikator
//---------------
   int    i,pos=0;
   double noise=0.000000001,AMA,signal,ER;
   double dSC,ERSC,SSC,ddK;
//---- TODO: add your code here
   slowSC=(2.0 /(nslow+1));
   fastSC=(2.0 /(nfast+1));
   
   if(Bars<=(periodAMA+2)) return(0);
//---- check for possible errors   
   int counted_bars=IndicatorCounted();
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   int limit=Bars-counted_bars;
   if(counted_bars==0) limit-=1+periodAMA+1;   
      
   for(pos=limit-1;pos>=0;pos--)
     {
      NewIndikator[pos]=iCCI(NULL,0,RSI,0,pos);
      kAMAupsig[pos]  =NULL;
      kAMAdownsig[pos]=NULL;
      if(pos==Bars-periodAMA-2) AMA0=iCCI(NULL,0,RSI,0,pos+1);
      signal=MathAbs(iCCI(NULL,0,RSI,0,pos)-iCCI(NULL,0,RSI,0,pos+periodAMA));
      noise=0.000000001;
      for(i=0;i<periodAMA;i++)
        {
         noise=noise+MathAbs(iCCI(NULL,0,RSI,0,pos+i)-iCCI(NULL,0,RSI,0,pos+i+1));
        }
      ER =signal/noise;
      dSC=(fastSC-slowSC);
      ERSC=ER*dSC;
      SSC=ERSC+slowSC;
      AMA=AMA0+(MathPow(SSC,G)*(iCCI(NULL,0,RSI,0,pos)-AMA0));
      kAMAbuffer[pos]=AMA;
      //----
      ddK=(AMA-AMA0);
      while(true)
        {
         trend[pos]=trend[pos+1];
         if((MathAbs(ddK) > (dK*Point)) && (ddK > 0))  {kAMAupsig[pos]  =AMA;kAMAdownsig[pos]=EMPTY_VALUE;trend[pos]=1;break;}
         if((MathAbs(ddK)) > (dK*Point) && (ddK < 0))  {kAMAdownsig[pos]=AMA;kAMAupsig[pos]  =EMPTY_VALUE;trend[pos]=-1;break;}
         kAMAupsig[pos]  =EMPTY_VALUE;
         kAMAdownsig[pos]=EMPTY_VALUE;
         break;
        }
      AMA0=AMA;

     }
//----
   return(0);
  }
//+------------------------------------------------------------------+
