//+------------------------------------------------------------------+ //| Extrapolator.mq5 | //| Copyright © 2008, gpwr | //| vlad1004@yahoo.com | //+------------------------------------------------------------------+ #property copyright "Copyright © 2008, gpwr" #property link "vlad1004@yahoo.com" //---- indicator version #property version "1.00" //---- drawing the indicator in the main window #property indicator_chart_window //---- two buffers are used for calculation and drawing the indicator #property indicator_buffers 2 //---- two plots are used #property indicator_plots 2 //---- drawing indicator 1 as a line #property indicator_type1 DRAW_LINE //---- red color is used as the color of the indicator line #property indicator_color1 Red //---- line of the indicator 1 is a continuous line #property indicator_style1 STYLE_SOLID //---- thickness of line of the indicator 1 is equal to 1 #property indicator_width1 1 //---- displaying the indicator line label #property indicator_label1 "" //---- drawing indicator 2 as a line #property indicator_type2 DRAW_LINE //---- blue color is used as the color of the indicator line #property indicator_color2 Blue //---- line of the indicator 2 is a continuous curve #property indicator_style2 STYLE_SOLID //---- thickness of line of the indicator 2 is equal to 1 #property indicator_width2 1 //---- displaying a label of the indicator #property indicator_label2 "" //---- #define pi 3.141592653589793238462643383279502884197169399375105820974944592 //+-----------------------------------+ //| Indicator input parameters | //+-----------------------------------+ enum Method_ //Type of constant { Extrapolation = 1, //Extrapolation Method Autocorrelation, //Autocorrelation Method Weight, //Weighted Burg Method Burg, //Burg Method with Helme-Nikias weighting function Itakura, //Itakura-Saito (geometric) method covariance //Modified covariance method }; input Method_ Method = 1; input int LastBar = 30; input int PastBars = 300; input double LPOrder = 0.6; input int FutBars = 100; input int HarmNo = 20; input double FreqTOL = 0.0001; input int BurgWin = 0; //---- declaration of dynamic arrays that // will be used as indicator buffers double FV_Buffer[]; double PV_Buffer[]; double ETA,INFTY,SMNO; int np,nf,lb,no,it,StartBar; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ void OnInit() { //---- initialization of constants lb=LastBar; np=PastBars; no=int (LPOrder*PastBars); nf=FutBars; if(Method>1) nf=np-no-1; //---- initialization of constants StartBar=PastBars; //---- set MAMABuffer dynamic array as indicator buffer SetIndexBuffer(0,FV_Buffer,INDICATOR_DATA); //---- create label to display in DataWindow PlotIndexSetString(0,PLOT_LABEL,""); //---- performing the shift of beginning of indicator drawing PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,StartBar); //---- indexing elements in arrays as timeseries ArraySetAsSeries(FV_Buffer,true); //---- shifting the indicator horizontally by nf-lb PlotIndexSetInteger(0,PLOT_SHIFT,nf-lb); //---- set FAMABuffer dynamic array as indicator buffer SetIndexBuffer(1,PV_Buffer,INDICATOR_DATA); //---- create label to display in DataWindow PlotIndexSetString(1,PLOT_LABEL,""); //---- performing the shift of beginning of indicator drawing PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,StartBar); //---- indexing elements in arrays as timeseries ArraySetAsSeries(PV_Buffer,true); //---- shifting the indicator horizontally by -lb PlotIndexSetInteger(1,PLOT_SHIFT,-lb); //---- initializations of variable for indicator short name string shortname="Extrapolator"; //---- creating a name for displaying in a separate sub-window and in a tooltip IndicatorSetString(INDICATOR_SHORTNAME,shortname); //---- determination of accuracy of displaying of the indicator values IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1); //---- } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, // number of bars in history at the current tick const int prev_calculated,// number of bars calculated at previous call 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[]) { //---- checking the number of bars to be enough for the calculation if(rates_total=1;i--) a[i]=a[i-1]; } } if(Method>1) { for(int n=no;nFreqTOL) { a=b; z[1]=Open[1+lb]-PV_Buffer[1]+a*z[0]; num=z[0]*z[1]; den=z[0]*z[0]; for(int i=2;i1) r=1.0; if(r<-1.0) r=-1.0; } else { for(i=k;i=k;i--) { tmp=df[i]; df[i]+=r*db[i-1]; db[i]=db[i-1]+r*tmp; } } //---- } //+------------------------------------------------------------------+ //| win function | //+------------------------------------------------------------------+ double win(int i,int k,int n,int w) { //---- if(w==0) return(1.0); if(w==1) return(0.54-0.46*MathCos(pi*(2.0*(i-k)+1.0)/(n-k))); if(w==2) return(6.0*(i-k+1.0)*(n-i)/(n-k)/(n-k+1.0)/(n-k+2.0)); //---- return(0.0); } //+------------------------------------------------------------------+ //| HNBurg function | //+------------------------------------------------------------------+ void HNBurg(double &x[],int p,double &a[]) { //---- int n=ArraySize(x); double df[],db[]; ArrayResize(df,n); ArrayResize(db,n); int i,k,kh,ki; double w,tmp,num,den,r; for(i=0;i1) r=1.0; if(r<-1.0) r=-1.0; } else { w=0.0; for(i=1;i=k;i--) { tmp=df[i]; df[i]+=r*db[i-1]; db[i]=db[i-1]+r*tmp; } } //---- } //+------------------------------------------------------------------+ //| Geom function | //+------------------------------------------------------------------+ void Geom(double &x[],int p,double &a[]) { //---- int n=ArraySize(x); double df[],db[]; ArrayResize(df,n); ArrayResize(db,n); int i,k,kh,ki; double tmp,num,denf,denb,r; for(i=0;i=k;i--) { tmp=df[i]; df[i]+=r*db[i-1]; db[i]=db[i-1]+r*tmp; } } //---- } //+------------------------------------------------------------------+ //| MCov function | //+------------------------------------------------------------------+ void MCov(double &x[],int ip,double &a[],bool &stop) { //---- int n=ArraySize(x); double c[],d[],r[],v; ArrayResize(c,ip+1); ArrayResize(d,ip+1); ArrayResize(r,ip); int k,m,mk; double r1,r2,r3,r4,r5,delta,gamma,lambda,theta,psi,xi; double save1,save2,save3,save4,c1,c2,c3,c4,ef,eb; r1=0.0; for(k=1;k0) { for(k=1;k<=m;k++) { theta+=x[k]*d[k]; psi+=x[k]*c[k]; xi+=x[n-1-k]*d[k]; r[k-1]-=(x[m]*x[m-k]+x[n-1-m]*x[n-1-m+k]); save1+=r[k-1]*a[m-k]; } } c1=-save1/v; a[m]=c1; v*=(1.0-c1*c1); if(m>0) { for(k=0;k<(m+1)/2;k++) { mk=m-k-1; save1=a[k]; a[k]=save1+c1*a[mk]; if(k!=mk) a[mk]+=c1*save1; } } if(m==ip-1) { v*=(0.5/(n-1-m)); break; } r1=1.0/(delta*gamma-lambda*lambda); c1=(theta*lambda+psi*delta)*r1; c2=(psi*lambda+theta*gamma)*r1; c3=(xi*lambda+theta*delta)*r1; c4=(theta*lambda+xi*gamma)*r1; for(k=0;k<=m/2;k++) { mk=m-k; save1=c[k]; save2=d[k]; save3=c[mk]; save4=d[mk]; c[k]+=(c1*save3+c2*save4); d[k]+=(c3*save3+c4*save4); if(k!=mk) { c[mk]+=(c1*save1+c2*save2); d[mk]+=(c3*save1+c4*save2); } } r2=psi*psi; r3=theta*theta; r4=xi*xi; r5=gamma-(r2*delta+r3*gamma+2.0*psi*lambda*theta)*r1; r2=delta-(r3*delta+r4*gamma+2.*theta*lambda*xi)*r1; gamma=r5; delta=r2; lambda+=(c3*psi+c4*theta); if(v<=0.0) { Print(__FUNCTION__,"(): Error! : Negative or zero value of the v variable"); stop=true; return; } if(delta<=0.0 || delta>1.0 || gamma<=0.0 || gamma>1.0) { Print(__FUNCTION__,"(): (): Error! : delta and gamma variables values out of the range (0,1)"); stop=true; return; } r1=1.0/v; r2=1.0/(delta*gamma-lambda*lambda); ef=x[n-m-2]; eb=x[m+1]; for(k=0;k<=m;k++) { ef+=a[k]*x[n-1-m+k]; eb+=a[k]*x[m-k]; } c1=eb*r1; c2=ef*r1; c3=(eb*delta+ef*lambda)*r2; c4=(ef*gamma+eb*lambda)*r2; for(k=m;k>=0;k--) { save1=a[k]; a[k]=save1+c3*c[k]+c4*d[k]; c[k+1]=c[k]+c1*save1; d[k+1]=d[k]+c2*save1; } c[0]=c1; d[0]=c2; r3=eb*eb; r4=ef*ef; v-=(r3*delta+r4*gamma+2.0*ef*eb*lambda)*r2; delta-=r4*r1; gamma-=r3*r1; lambda+=ef*eb*r1; if(v<=0.0) { Print(__FUNCTION__,"(): Error! : Negative or zero value of the v variable"); stop=true; return; } if(delta<=0.0 || delta>1.0 || gamma<=0.0 || gamma>1.0) { Print(__FUNCTION__,"(): (): Error! : delta and gamma variables values out of the range (0,1)"); stop=true; return; } } //---- } //+------------------------------------------------------------------+