//--------------------------------------------------------------------------------------
#property copyright "www.forex-station.com"
#property link      "www.forex-station.com"
//--------------------------------------------------------------------------------------
#property indicator_chart_window
#property indicator_buffers 6
#property indicator_color1 DarkOrange
#property indicator_color2 DarkOrange
#property indicator_color3 LimeGreen
#property indicator_color4 DarkOrange
#property indicator_color5 LimeGreen
#property indicator_color6 DarkOrange
#property indicator_width2 2
#property indicator_style1 STYLE_DOT
#property indicator_style5 STYLE_DOT
#property indicator_style6 STYLE_DOT

#import "fourier extrapolation.dll"
    void fourierExtrapolation(double& source[], double& past[], double& prediction[], int pastBars, int lastBar, int futureBars, int harmonicsNo, double harmonicsTolerance);
#import

//
//
//
//
//

extern int     MaPeriod   =   15;    
extern int     MaMethod   =   MODE_SMA;
extern double  BandsMultiplier = 3;
extern int     AtrPeriod  =  100;
extern int     LastBar    =    0;    // Last bar in the past data
extern int     PastBars   = 1000;    // Number of past bars
extern int     FutBars    =  200;    // Number of bars to predict 
extern int     HarmNo     =    8;    // Number of frequencies; HarmNo=0 computes PastBars harmonics
extern double  FreqTOL    = 0.0001;  // Tolerance of frequency calculation - FreqTOL > 0.001 may not converge

double indicatorValues[];
double pastValues[];
double pastValuesBu[];
double pastValuesBd[];
double futureValues[];
double futureValuesBu[];
double futureValuesBd[];

double source[];
double result[];
double prediction[];

//--------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------
//
//
//
//
//

int init()
{
   if(HarmNo==0 || HarmNo>PastBars) HarmNo=PastBars;
   IndicatorBuffers(7);
      SetIndexBuffer(0,pastValues);      SetIndexLabel(0,"past");
      SetIndexBuffer(1,futureValues);    SetIndexLabel(1,"prediction");
      SetIndexBuffer(2,pastValuesBu);  
      SetIndexBuffer(3,pastValuesBd);  
      SetIndexBuffer(4,futureValuesBu);
      SetIndexBuffer(5,futureValuesBd);
      SetIndexBuffer(6,indicatorValues);
   IndicatorShortName("Fourier extrapolation - Pip");
   return(0);
}
int deinit(){return(0);}

//--------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------
//
//
//
//
//

int start()
{
   int counted_bars = IndicatorCounted();
      if(counted_bars < 0) return(-1);
      if(counted_bars > 0) counted_bars--;
         int limit = MathMin(Bars-counted_bars,Bars-1);
         int np = PastBars; if (np>Bars)    np = Bars;
         int nf = FutBars;  if (nf<LastBar) nf = LastBar;

         if (ArraySize(source)     !=np)   ArrayResize(source,np);
         if (ArraySize(result)     !=np)   ArrayResize(result,np);
         if (ArraySize(prediction) !=nf+1) ArrayResize(prediction,nf+1);
               SetIndexShift(0,  -LastBar);
               SetIndexShift(1,nf-LastBar);
               SetIndexShift(2,  -LastBar);
               SetIndexShift(3,  -LastBar);
               SetIndexShift(4,nf-LastBar);
               SetIndexShift(5,nf-LastBar);

      //
      //
      //
      //
      //

      double BandsWidth = iATR(NULL,0,AtrPeriod,LastBar);
      for(int i=limit; i>=0; i--) indicatorValues[i]=iMA(NULL,0,MaPeriod,0,MaMethod,PRICE_CLOSE,i);
         ArrayCopy(source,indicatorValues,0,LastBar,np);  fourierExtrapolation(source,result,prediction,np,LastBar,nf+1,HarmNo,FreqTOL); ArrayCopy(pastValues  ,result); ArrayCopy(futureValues  ,prediction);
         for(i=np; i>=0; i--) 
         { 
            pastValuesBu[i]=pastValues[i]+BandsWidth*BandsMultiplier;
            pastValuesBd[i]=pastValues[i]-BandsWidth*BandsMultiplier;
         }            
         for(i=nf; i>=0; i--) 
         { 
            futureValuesBu[i]=futureValues[i]+BandsWidth*BandsMultiplier;
            futureValuesBd[i]=futureValues[i]-BandsWidth*BandsMultiplier;
         }            

      //
      //
      //
      //
      //
      
      SetIndexDrawBegin(0,Bars-np-LastBar);
      SetIndexDrawBegin(1,Bars-nf-1);
      SetIndexDrawBegin(2,Bars-np-LastBar);
      SetIndexDrawBegin(3,Bars-np-LastBar);
      SetIndexDrawBegin(4,Bars-nf-1);
      SetIndexDrawBegin(5,Bars-nf-1);

   return(0); 
}

