//+------------------------------------------------------------------+ //| ^X_ParabolicRegression_StopAndReverse.mq4 | //| Version 1.1 | //| Copyright © 2007, Mr.WT, Senior Linux Hacker | //| http://www.metaquotes.net/ | //+------------------------------------------------------------------+ #property copyright "Copyright © 2007, Mr.WT, Senior Linux Hacker" #property link "http://www.metaquotes.net/" //---- #property indicator_chart_window #property indicator_buffers 4 #property indicator_color1 Aqua #property indicator_color2 Orange #property indicator_color3 DeepSkyBlue #property indicator_color4 Red //---- extern int _RegressionDegree=2; extern double _K_DeviationChannel=2; extern color _TrendLineColor=Gold; extern color _ResLineColor = OrangeRed; extern color _SupLineColor = DeepSkyBlue; extern color _RegressionColor1 = Yellow; extern color _RegressionColor2 = Lime; extern color _RegressionColor3 = Red; extern color _RegressionColor4 = RoyalBlue; //---- double _K_Fibo1 = 1.618; double _K_Fibo2 = 2.618; //---- double fx,fx1; double a[10,10],b[10],x[10],sx[20]; double sum,sum1,sq; int p,nn,kt; //---- int i0,ip,pn,i0n,ipn; long t0,tp,te,te1; long _N_Time,_Cur_Period; //---- buffers double Support1[]; double Support2[]; double Resist1[]; double Resist2[]; bool objects_initialized=false; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { //---- IndicatorBuffers(4); SetIndexStyle(0,DRAW_ARROW); SetIndexArrow(0,159); SetIndexBuffer(0,Support1); //---- SetIndexStyle(1,DRAW_ARROW); SetIndexArrow(1,159); SetIndexBuffer(1,Resist1); //---- SetIndexStyle(2,DRAW_ARROW); SetIndexArrow(2,159); SetIndexBuffer(2,Support2); //---- SetIndexStyle(3,DRAW_ARROW); SetIndexArrow(3,159); SetIndexBuffer(3,Resist2); //---- SetIndexDrawBegin(0,p); SetIndexDrawBegin(1,p); SetIndexDrawBegin(2,p); SetIndexDrawBegin(3,p); //---- SetIndexLabel(0,"1-st Level Stop/Reverse"); SetIndexLabel(1,"1-st Level Stop/Reverse"); SetIndexLabel(2,"2-nd Level Stop/Reverse"); SetIndexLabel(3,"2-nd Level Stop/Reverse"); //---- IndicatorShortName("Stop And Reverse"); //---- if(Period()==PERIOD_MN1) { p=13; } else { if(Period()==PERIOD_W1) { p=21; } else { if(Period()==PERIOD_D1) { p=89; } else { if(Period()==PERIOD_H4) { p=144; } else p=233; } } } _N_Time=0; _Cur_Period=0; objects_initialized=false; return(0); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ bool ObjectsInitialize() { //--- too small history if(p>Bars) { Comment("\n\n ERROR - TOO SMALL HISTORY, RETURN NOW!"); return(false); // then exit } // ar kt = Period()*60; nn = _RegressionDegree + 1; //---- t0 = Time[0]; i0 = iBarShift(Symbol(), Period(), t0); ip = i0 + p; tp = Time[ip]; pn = p; // ar for(int j=-p/2; j
=0) { ObjectCreate("ar"+(string)j,2,0,Time[i0+1+j],0,Time[i0+j],0); ObjectSet("ar"+(string)j,OBJPROP_RAY,0); ObjectCreate("arH"+(string)j,2,0,Time[i0+1+j],0,Time[i0+j],0); ObjectSet("arH"+(string)j,OBJPROP_RAY,0); ObjectCreate("arL"+(string)j,2,0,Time[i0+1+j],0,Time[i0+j],0); ObjectSet("arL"+(string)j,OBJPROP_RAY,0); } } // LR ObjectCreate("LR",OBJ_TREND,0,Time[0],0,Time[0],0); ObjectSet("LR",OBJPROP_COLOR,_TrendLineColor); ObjectSet("LR",OBJPROP_RAY,1); ObjectSet("LR",OBJPROP_STYLE,STYLE_DASH); //---- ObjectCreate("2-nd Level ResLine",OBJ_TREND,0,Time[0],0, Time[0],0); ObjectSet("2-nd Level ResLine",OBJPROP_COLOR,_ResLineColor); ObjectSet("2-nd Level ResLine",OBJPROP_RAY,1); ObjectSet("2-nd Level ResLine",OBJPROP_STYLE,STYLE_DASH); //---- ObjectCreate("2-nd Level SupLine",OBJ_TREND,0,Time[0],0, Time[0],0); ObjectSet("2-nd Level SupLine",OBJPROP_COLOR,_SupLineColor); ObjectSet("2-nd Level SupLine",OBJPROP_RAY,1); ObjectSet("2-nd Level SupLine",OBJPROP_STYLE,STYLE_DASH); //---- return(true); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int deinit() { for(int j=p; j>=-p/2; j--) { ObjectDelete("ar"+(string)j); ObjectDelete("arH"+(string)j); ObjectDelete("arL"+(string)j); } ObjectDelete("LR"); ObjectDelete("2-nd Level ResLine"); ObjectDelete("2-nd Level SupLine"); Comment(""); return(0); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int start() { if(!objects_initialized) objects_initialized=ObjectsInitialize(); if(_N_Time!=Time[0] || _Cur_Period!=Period()) { int i,n,k; //---- if(i0n!=i0 || ipn!=ip) { p=ip-i0; i0n = ip; ipn = ip; //---- if(pn
=-p/2; j--) { ObjectCreate("ar"+(string)j,2,0,Time[i0+1+j],0, Time[i0+j],0); ObjectSet("ar"+(string)j,OBJPROP_RAY,0); ObjectCreate("arH"+(string)j,2,0,Time[i0+1+j],0, Time[i0+j],0); ObjectSet("arH"+(string)j,OBJPROP_RAY,0); ObjectCreate("arL"+(string)j,2,0,Time[i0+1+j],0, Time[i0+j],0); ObjectSet("arL"+(string)j,OBJPROP_RAY,0); } pn=p; } if(pn>p) { for(j=pn; j>=p; j--) { ObjectDelete("ar"+(string)j); ObjectDelete("arH"+(string)j); ObjectDelete("arL"+(string)j); } for(j=-p/2; j>=-pn/2; j--) { ObjectDelete("ar"+(string)j); ObjectDelete("arH"+(string)j); ObjectDelete("arL"+(string)j); } pn=p; } } // PR sx[1]=p+1; // sx for(i=1; i<=nn*2-2; i++) { sum=0.0; for(n=i0; n<=i0+p; n++) sum+= MathPow(n,i); sx[i+1]= sum; } // syx for(i=1; i<=nn; i++) { sum=0.0; for(n=i0; n<=i0+p; n++) { if(i==1) sum+=Close[n]; else sum+=Close[n]*MathPow(n,i-1); } b[i]=sum; } // Matrix for(j=1; j<=nn; j++) { for(i=1; i<=nn; i++) { k=i+j-1; a[i,j]=sx[k]; } } // Gauss Print("nn=",nn); af_Gauss(nn,a,b,x); // SQ sq=0.0; for(n=p; n>=0; n--) { sum=0.0; for(k=1; k<=_RegressionDegree; k++) { sum+=x[k+1]*MathPow(i0+n,k); sum1+=x[k+1]*MathPow(i0+n+1,k); } fx = x[1]+ sum; sq+= MathPow(Close[n+i0] - fx,2); } sq=_K_DeviationChannel*MathSqrt(sq/(p+1)); //---- for(n=p; n>=-p/2; n--) { sum=0.0; sum1=0.0; //---- for(k=1; k<=_RegressionDegree; k++) { sum+=x[k+1]*MathPow(i0+n,k); sum1+=x[k+1]*MathPow(i0+n+1,k); } fx=x[1]+sum; fx1=x[1]+sum1; //---- if(n>=0 && n
fx1)
{
ObjectSet("ar"+(string)n,OBJPROP_COLOR,_RegressionColor1);
ObjectSet("arH"+(string)n,OBJPROP_COLOR,_RegressionColor1);
ObjectSet("arL"+(string)n,OBJPROP_COLOR,_RegressionColor1);
}
//----
if(fx