//+------------------------------------------------------------------+ //| 3X_ParabolicRegression.mq5 | //| Copyright © 2007, Mr.WT, Senior Linux Hacker | //| http://w-tiger.narod.ru/wk2/ | //+------------------------------------------------------------------+ //---- авторство индикатора #property copyright "Copyright © 2007, Mr.WT, Senior Linux Hacker" //---- ссылка на сайт автора #property link "http://w-tiger.narod.ru/wk2/" //---- номер версии индикатора #property version "2.01" //---- отрисовка индикатора в главном окне #property indicator_chart_window #property indicator_buffers 0 #property indicator_plots 0 //+-----------------------------------+ //| объявление перечисления | //+-----------------------------------+ enum WIDTH { Width_1=1, // 1 Width_2, // 2 Width_3, // 3 Width_4, // 4 Width_5 // 5 }; //+-----------------------------------+ //| объявление перечисления | //+-----------------------------------+ enum STYLE { SOLID_, // Сплошная линия DASH_, // Штриховая линия DOT_, // Пунктирная линия DASHDOT_, // Штрих-пунктирная линия DASHDOTDOT_ // Штрих-пунктирная линия с двойными точками }; //+----------------------------------------------+ //| Входные параметры индикатора | //+----------------------------------------------+ //---- параметры криволинейного канала input uint RegressionDegree_=5; input double KNL_Dev=2.72; input color RegressionColor1 = SpringGreen; // Цвет регрессии 1 input color RegressionColor2 = Red; // Цвет регрессии 2 input color RegressionColor3 = BlueViolet; // Цвет регрессии 3 input color RegressionColor4 = Magenta; // Цвет регрессии 4 input STYLE linesStyle=DASH_; // Стиль линий input WIDTH linesWidth=Width_1; // Толщина линий //---- параметры канала стандартных отклонений 1 input color channelColor1=Blue; // Цвет канала 1 input STYLE channelStyle1=DASH_; // Стиль канала 1 input WIDTH channelWidth1=Width_1; // Толщина линий канала 1 //---- параметры канала стандартных отклонений 2 input color channelColor2=Gold; // Цвет канала 2 input STYLE channelStyle2=SOLID_; // Стиль канала 2 input WIDTH channelWidth2=Width_1; // Толщина линий канала 2 //+----------------------------------------------+ //---- объявление глобальных переменных double fx,fx1; double a[10][10],b[10],x[10],sx[20]; double sum,sum1,sq; int p,p1,p2,nn,kt; //---- datetime te,te1,tp,t0; int i0,ip,pn,i0n,ipn,RegressionDegree; string str; //+------------------------------------------------------------------+ //| init | //+------------------------------------------------------------------+ bool init(int RatesTotal,const datetime &Time[]) { //---- too small history if(p>RatesTotal) { Comment("\n\n ERROR - TOO SMALL HISTORY, RETURN NOW!"); return(false); } //---- ar kt=PeriodSeconds(); nn=RegressionDegree+1; //---------------------- t0=Time[0]; i0=0; ip=i0+p; tp=Time[ip]; pn=p; //---- LR ObjectCreate(0,"LR1",OBJ_REGRESSION,0,Time[0],0,Time[0],0); ObjectSetInteger(0,"LR1",OBJPROP_COLOR,channelColor1); ObjectSetInteger(0,"LR1",OBJPROP_RAY,true); ObjectSetInteger(0,"LR1",OBJPROP_RAY_RIGHT,true); ObjectSetInteger(0,"LR1",OBJPROP_STYLE,channelStyle1); ObjectSetInteger(0,"LR1",OBJPROP_WIDTH,Width_1); //---- ar ObjectCreate(0,"LR2",OBJ_REGRESSION,0,Time[0],0,Time[0],0); ObjectSetInteger(0,"LR2",OBJPROP_COLOR,channelColor2); ObjectSetInteger(0,"LR2",OBJPROP_RAY,true); ObjectSetInteger(0,"LR2",OBJPROP_RAY_RIGHT,true); ObjectSetInteger(0,"LR2",OBJPROP_STYLE,channelStyle2); ObjectSetInteger(0,"LR2",OBJPROP_WIDTH,Width_2); for(int j=-p/2; j8) RegressionDegree=8; //---- определение точности отображения значений индикатора IndicatorSetInteger(INDICATOR_DIGITS,_Digits); //---- создание меток для отображения в DataWindow //---- и имени для отображения в отдельном подокне и во всплывающей подсказке IndicatorSetString(INDICATOR_SHORTNAME,"Parabolic Regression"); //---- } //+------------------------------------------------------------------+ //| Custom indicator deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //---- for(int j=p; j>=-p/2; j--) { string sJ=str+")"+string(j); ObjectDelete(0,"_ar("+sJ); ObjectDelete(0,"_arH("+sJ); ObjectDelete(0,"_arL("+sJ); } ObjectDelete(0,"LR1"); ObjectDelete(0,"LR2"); Comment(""); //---- } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, // количество истории в барах на текущем тике const int prev_calculated,// количество истории в барах на предыдущем тике 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[]) { //---- if(rates_total=-p/2; j--) { string sJ=str+")"+string(j); ObjectDelete(0,"_ar("+sJ); ObjectDelete(0,"_arH("+sJ); ObjectDelete(0,"_arL("+sJ); } if(!init(rates_total,Time)) return(0); //---- ObjectMove(0,"LR1",0,Time[p1],0); ObjectMove(0,"LR1",1,Time[1],0); if(p2) { ObjectMove(0,"LR2",0,Time[p2],0); ObjectMove(0,"LR2",1,Time[1],0); } //---- int i,j,n,k; //---- if(i0n!=i0 || ipn!=ip) { p=ip-i0; i0n=ip; ipn=ip; if(pn=-p/2; j--) { string sJ=str+")"+string(j); ObjectCreate(0,"_ar("+sJ,OBJ_TREND,0,Time[i0+1+j],0,Time[i0+j],0); ObjectSetInteger(0,"_ar("+sJ,OBJPROP_RAY,false); ObjectSetInteger(0,"_ar("+sJ,OBJPROP_STYLE,linesStyle); ObjectSetInteger(0,"_ar("+sJ,OBJPROP_WIDTH,linesWidth); ObjectCreate(0,"_arH("+sJ,OBJ_TREND,0,Time[i0+1+j],0,Time[i0+j],0); ObjectSetInteger(0,"_arH("+sJ,OBJPROP_RAY,false); ObjectSetInteger(0,"_arH("+sJ,OBJPROP_STYLE,linesStyle); ObjectSetInteger(0,"_arH("+sJ,OBJPROP_WIDTH,linesWidth); ObjectCreate(0,"_arL("+sJ,OBJ_TREND,0,Time[i0+1+j],0,Time[i0+j],0); ObjectSetInteger(0,"_arL("+sJ,OBJPROP_RAY,false); ObjectSetInteger(0,"_arL("+sJ,OBJPROP_STYLE,linesStyle); ObjectSetInteger(0,"_arL("+sJ,OBJPROP_WIDTH,linesWidth); } pn=p; } if(pn>p) { for(j=pn; j>=p; j--) { string sJ=str+")"+string(j); ObjectDelete(0,"_ar("+sJ); ObjectDelete(0,"_arH("+sJ); ObjectDelete(0,"_arL("+sJ); } for(j=-p/2; j>=-pn/2; j--) { string sJ=str+")"+string(j); ObjectDelete(0,"_ar("+sJ); ObjectDelete(0,"_arH("+sJ); ObjectDelete(0,"_arL("+sJ); } 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 af_Gauss(nn); //---- 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=KNL_Dev*MathSqrt(sq/(p+1)); //---- for(n=p; n>=-p/2; n--) { sum=0.0; sum1=0.0; string sN=str+")"+string(n); 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 && nfx1) { ObjectSetInteger(0,"_ar("+sN,OBJPROP_COLOR,RegressionColor1); ObjectSetInteger(0,"_arH("+sN,OBJPROP_COLOR,RegressionColor1); ObjectSetInteger(0,"_arL("+sN,OBJPROP_COLOR,RegressionColor1); } if(fx=0) { ObjectMove(0,"_ar("+sN,0,Time[n+i0+1],fx1); ObjectMove(0,"_ar("+sN,1,Time[n+i0],fx); ObjectMove(0,"_arH("+sN,0,Time[n+i0+1],fx1+sq); ObjectMove(0,"_arH("+sN,1,Time[n+i0],fx+sq); ObjectMove(0,"_arL("+sN,0,Time[n+i0+1],fx1-sq); ObjectMove(0,"_arL("+sN,1,Time[n+i0],fx-sq); } if((n+i0)<0) { te=Time[0]-(n+i0)*kt; te1=Time[0]-(n+i0+1)*kt; ObjectMove(0,"_ar("+sN,0,te1,fx1); ObjectMove(0,"_ar("+sN,1,te,fx); ObjectMove(0,"_arH("+sN,0,te1,fx1+sq); ObjectMove(0,"_arH("+sN,1,te,fx+sq); ObjectMove(0,"_arL("+sN,0,te1,fx1-sq); ObjectMove(0,"_arL("+sN,1,te,fx-sq); } if(fx>fx1) { ObjectSetInteger(0,"_ar("+sN,OBJPROP_COLOR,RegressionColor3); ObjectSetInteger(0,"_arH("+sN,OBJPROP_COLOR,RegressionColor3); ObjectSetInteger(0,"_arL("+sN,OBJPROP_COLOR,RegressionColor3); } if(fxm) {m=MathAbs(a[i][k]); l=i;} } if(l==0) return; if(l!=k) { for(j=1; j<=n; j++) { t=a[k][j]; a[k][j]=a[l][j]; a[l][j]=t; } t=b[k]; b[k]=b[l]; b[l]=t; } for(i=k+1;i<=n;i++) { q=a[i][k]/a[k][k]; for(j=1;j<=n;j++) { if(j==k) a[i][j]=0; else a[i][j]=a[i][j]-q*a[k][j]; } b[i]=b[i]-q*b[k]; } } x[n]=b[n]/a[n][n]; for(i=n-1;i>=1;i--) { t=0; for(j=1;j<=n-i;j++) { t=t+a[i][i+j]*x[i+j]; x[i]=(1/a[i][i])*(b[i]-t); } } //---- } //+------------------------------------------------------------------+