Fourier_Extrapolation_of_Indicator

Download script
Author: Copyright � 2009, gpwr
Fourier_Extrapolation_of_Indicator

This tool is designed to analyze historical price trends and project potential future movements by identifying underlying mathematical patterns. Instead of simply following the current price, it uses a sophisticated technique called "Fourier Extrapolation" to decompose complex market data into smoother, repeating waves.

How the Logic Works

1. Data Collection and Smoothing The script begins by looking at a specific window of historical price data (determined by the "Past Bars" setting). It identifies a base indicator—in this case, a standard technical oscillator—and calculates its average value. This serves as the "baseline" or "center" from which all other fluctuations are measured.

2. Wave Frequency Analysis The core of the script's intelligence lies in its ability to recognize cycles. It treats market movements as a series of overlapping waves rather than random noise. It uses a specialized mathematical algorithm (the Quinn and Fernandes algorithm) to scan the historical data and determine the dominant frequencies. Essentially, it is trying to figure out how often and with what intensity the price tends to "swing" back and forth.

3. Harmonic Reconstruction Once the primary cycles are identified, the script builds a model by adding multiple "harmonics" together. Think of this like layering different musical notes to create a complex chord; the script layers different frequencies to reconstruct the shape of the historical data as accurately as possible. By doing this, it filters out minor "市場雜訊" (market noise) to focus on the larger, more reliable structural movements of the price.

4. Forward Extrapolation This is where the predictive element occurs. After the script has perfectly modeled the past waves, it projects those mathematical curves into the future. It extends the identified cycles forward into a blank space on the chart (the "Future Bars").

Visual Representation

On your chart, this logic produces three distinct layers of information:

  • Historical Data: Shows the actual path the price took in the past.
  • Past Vector: Shows how the mathematical model fits the historical data, allowing you to see how well the "waves" align with reality.
  • Future Vector: Provides a projected visual path of where the market is mathematically expected to move based on the momentum and cycle patterns identified in the past.

In summary, this script doesn't just guess where the price is going; it calculates the "rhythm" of the market's past movements and draws a mathematical map of what that rhythm suggests for the time ahead.

Indicators Used
Larry William percent range indicator
Miscellaneous
Implements a curve of type %1
13 Views
0 Downloads
0 Favorites
Fourier_Extrapolation_of_Indicator
//+--------------------------------------------------------------------------------------+
//|                                               Fourier Extrapolation of Indicator.mq4 |
//|                                                               Copyright © 2008, gpwr |
//|                                                                   vlad1004@yahoo.com |
//+--------------------------------------------------------------------------------------+
#property copyright "Copyright © 2009, gpwr"
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Black
#property indicator_color2 Blue
#property indicator_color3 Red
#property indicator_width1 1
#property indicator_width2 2
#property indicator_width3 2

//Global constants
#define pi 3.141592653589793238462643383279502884197169399375105820974944592

//Input parameters
extern int     LastBar  =200;  //Last bar in the past data
extern int     PastBars =500;  //Number of past bars
extern int     FutBars  =200;  //Number of bars to predict 
extern int     HarmNo   =10;   //Number of frequencies; HarmNo=0 computes PastBars harmonics
extern double  FreqTOL  =0.0001;//Tolerance of frequency calculation
//FreqTOL > 0.001 may not converge

//Indicator buffers
double in[];
double pv[];
double fv[];

//Global variables
int np,nf,lb;

int init()
{
   lb=LastBar;
   np=PastBars;
   nf=FutBars;
   if(HarmNo==0 || HarmNo>np) HarmNo=np;
   
   IndicatorBuffers(3);
   SetIndexBuffer(0,in);
   SetIndexBuffer(1,pv);
   SetIndexBuffer(2,fv);
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1);
   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2);
   SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,2);
   SetIndexShift(1,-lb);//past data vector 0..np-1; 0 corresponds to bar=lb
   SetIndexShift(2,nf-lb);//future data vector i=0..nf; nf corresponds to bar=lb
   IndicatorShortName("FEoI");  
   return(0);
}

int deinit(){return(0);}

int start()
{
   ArrayInitialize(in,EMPTY_VALUE);
   ArrayInitialize(pv,EMPTY_VALUE);
   ArrayInitialize(fv,EMPTY_VALUE);  
   
//Choose indicator and find the average of its np past values
   double x[]; //stores indicator values
   ArrayResize(x,np);
   double av=0.0;
   for(int i=-lb;i<np;i++)
   {
      in[i+lb]=0.5+iWPR(NULL,0,50,i+lb)/100.0; //change indicator here
      if(i>=0)
      {
         x[i]=in[i+lb];
         av+=x[i];
      }
   }
   av/=np;
   
//Prepare modeled data
   for(i=0;i<np;i++)
   {
      pv[i]=av;
      if(i<=nf) fv[i]=av;
   }

//Fit trigomometric series
   double w,m,c,s;
   for(int harm=1;harm<=HarmNo;harm++)
   {
      Freq(x,w,m,c,s);
      for(i=0;i<np;i++) 
      {
         pv[i]+=m+c*MathCos(w*i)+s*MathSin(w*i);
         if(i<=nf) fv[i]+=m+c*MathCos(w*i)-s*MathSin(w*i);
      }       
   }
   
//Reorder the predicted vector
   for(i=0;i<=(nf-1)/2;i++)
   {
      double tmp=fv[i];
      fv[i]=fv[nf-i];
      fv[nf-i]=tmp;
   } 
   return(0); 
}
//+--------------------------------------------------------------------------------------+
//Quinn and Fernandes algorithm
void Freq(double x[], double& w, double& m, double& c, double& s)
{
   double z[],num,den;
   ArrayResize(z,np);
   double a=0.0;
   double b=2.0;
   z[0]=x[0]-pv[0];
   while(MathAbs(a-b)>FreqTOL)
   {
      a=b;
      z[1]=x[1]-pv[1]+a*z[0];
      num=z[0]*z[1];
      den=z[0]*z[0];
      for(int i=2;i<np;i++)
      {
         z[i]=x[i]-pv[i]+a*z[i-1]-z[i-2];
         num+=z[i-1]*(z[i]+z[i-2]);
         den+=z[i-1]*z[i-1];
      }
      b=num/den;
   }
   w=MathArccos(b/2.0);
   Fit(x,w,m,c,s);
   return;
}
//+-------------------------------------------------------------------------+
void Fit(double x[], double w, double& m, double& c, double& s)
{
   double Sc=0.0;
   double Ss=0.0;
   double Scc=0.0;
   double Sss=0.0;
   double Scs=0.0;
   double Sx=0.0;
   double Sxc=0.0;
   double Sxs=0.0;
   for(int i=0;i<np;i++)
   {
      double cos=MathCos(w*i);
      double sin=MathSin(w*i);
      Sc+=cos;
      Ss+=sin;
      Scc+=cos*cos;
      Sss+=sin*sin;
      Scs+=cos*sin;
      Sx+=(x[i]-pv[i]);
      Sxc+=(x[i]-pv[i])*cos;
      Sxs+=(x[i]-pv[i])*sin;
   }
   Sc/=np;
   Ss/=np;
   Scc/=np;
   Sss/=np;
   Scs/=np;
   Sx/=np;
   Sxc/=np;
   Sxs/=np;
   if(w==0.0)
   {
      m=Sx;
      c=0.0;
      s=0.0;
   }
   else
   {
      //calculating c, s, and m
      double den=MathPow(Scs-Sc*Ss,2)-(Scc-Sc*Sc)*(Sss-Ss*Ss);
      c=((Sxs-Sx*Ss)*(Scs-Sc*Ss)-(Sxc-Sx*Sc)*(Sss-Ss*Ss))/den;
      s=((Sxc-Sx*Sc)*(Scs-Sc*Ss)-(Sxs-Sx*Ss)*(Scc-Sc*Sc))/den;
      m=Sx-c*Sc-s*Ss;
   }
   return;
}

Comments