//+---------------------------------------------------------------------------+ //| Polynomial Regression Channels | //| ZP_PRC_i03.mq4 | //| by Ron Mauldin | //| BACKGROUND: | //| This Polynomial Regression Channel is based on the i-Regr.mq4 that I | //| found at several locations. When I found it, it only showed the last | //| period (0)... and therefore... all the lookback bars were tainted with | //| future info. | //| | //| Also known as Hurst Bands, see: | //| http://www.esignalcentral.com/university/esignal/addons/arps/hurst.asp | //| | //| Also known as Sigma Channels, see: | //| http://ytrader.com/manuals/SigmaToolKit.pdf | //| | //| CHARACTERISTICS: | //| • | //| | //| POSSIBLE USES: | //| • Channel or Sideways Trading | //| | //| POSSIBLE SETTINGS: | //| At PRC_DegreePower - 1 = linear; 2 = parabolic; 3+ = Polynomial | //| The max for PRC_DegreePower is currently at 10 due to array sizing. This | //| is an artificial limit, but likely a practical one. | //| | //| PRC_LookBackPeriod - With this input you select the number of bars to | //| include in the regression calculation. Higher numbers give a longer bar | //| interval over which to calculate the polynomial regression channel | //| equation. | //| The more bars back you select, the less sensitive the curvature of the | //| indicator will be. | //| A TradeStation implimentation had "Default = 150, Range = 30-250" | //| with the comment "We have found that values between 75 and 150 bars work | //| best. | //| • My first setting 8/3/100. For use during likely Channel periods. | //| | //| VERSION HISTORY: | //| 20090425 - i01 - This version is essentially the same as i-Regr.mq4. It | //| was only been cleaned up, simplified and organized to my standards. | //| 20090447 - i02 - Added my standard template and converted the indicator | //| to calculate multiple periods. This version was artificially chopping | //| off bars at about 135. | //| 20090427 - i03 - //| | //|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| //| | //| Collaborators Wanted | //| Do you have an interesting idea or indicator designed for scalping? | //| Would you like to collaborate and turn it into an EA trading robot? | //| "If" I like your idea, I will help you fully analyze it. | //| If the analysis is good, then I will help you turn your idea into | //| an EA robot that we will both share. | //| | //| Are you a "quality" MT4 programmer with "plenty of time" to work on | //| joint venture (JV) projects? | //| If so, I have plenty of ideas and would like to discuss a JV. | //| | //| NOTE!!! | //| I am NOT looking for back-testers or programmers for hire. | //| I am looking for entrepreneurial technical analysts or programmers | //| who really believe that well coded forex robot could make millions.| //| Please don't yank my chain if you are not serious about hard work | //| with a true desire to collaborate to make a winning system. | //| | //| Email: ronmauldin51-mt4 at yahoo dot com | //+---------------------------------------------------------------------+ #property copyright "Ron Mauldin" #property link "ronmauldin51-mt4 at yahoo dot com" #property indicator_chart_window #property indicator_buffers 3 #property indicator_color1 LimeGreen #property indicator_color2 Gold #property indicator_color3 Gold extern int PRC_DegreePower = 8; extern double PRC_StdDev = 3.0; extern int PRC_LookBackPeriod = 30; //Data Buffer double prcMA[]; //Indicator Buffers double prcCenter[],prcUpper[],prcLower[]; /* void clear() { int total = ObjectsTotal(); for (int i=total-1; i >= 0; i--) { string name = ObjectName(i); if (StringFind(name, prefix) == 0) ObjectDelete(name); } } */ //#include //#include int init(){ //#include IndicatorBuffers(4); IndicatorDigits(Digits+2); SetIndexBuffer(0, prcCenter); SetIndexStyle(0, DRAW_LINE); // SetIndexDrawBegin(0, Bars-PRC_LookBackPeriod-1); // SetIndexEmptyValue(0, 0.0); SetIndexLabel(0,"PRC DP:"+PRC_DegreePower+" SD:" +DoubleToStr(PRC_StdDev,1)+" LB:"+PRC_LookBackPeriod+" Val"); SetIndexBuffer(1, prcUpper); SetIndexStyle(1, DRAW_LINE); // SetIndexDrawBegin(1, Bars-PRC_LookBackPeriod-1); // SetIndexEmptyValue(1, 0.0); SetIndexBuffer(2, prcLower); SetIndexStyle(2, DRAW_LINE); // SetIndexDrawBegin(2, Bars-PRC_LookBackPeriod-1); // SetIndexEmptyValue(2, 0.0); SetIndexBuffer(3, prcMA); SetIndexStyle(3, DRAW_NONE); return(0); } int deinit(){ //clear(); return(0); } int start(){ // Standard loop setup int bar,limit,MinPeriods,InputBuffers,MinCycles,MinBars,counted_bars = IndicatorCounted(); // MinBars needs to consider three ways of using bars: // 1) Periods used in external in Indicators (ie BB_Period) // 2) Bars needed by inputs used by other calculations. HA is often used in this manner. // 3) Cycles used to make up the indicators in internal buffers or external indicators (ie iSAR needs to get back to the start of the trend) //MinPeriods=MathMax(LSMA_Periods,LSMAS_Periods)+1;//Usually composed of other variables. MinPeriods=PRC_LookBackPeriod+1;//Usually composed of other variables. InputBuffers=0;//Usually a count of the bars used plus 1. HA needs 5. MinCycles=0;//A best guess. AO uses 34. iSar could be quite long MinBars=MathMax(MinPeriods,MinCycles)+InputBuffers; if(Bars-1 < MinBars) return(0); if(counted_bars < 0) return(-1); if(counted_bars > 0) counted_bars--; MinBars = Bars - 1 - MinBars; limit = (Bars - 1 - counted_bars); //---- initialization of nulls if(limit > MinBars){ for(bar = Bars - 1; bar >= MinBars; bar--) { prcMA[bar] = 0.0; prcCenter[bar] = 0.0; prcUpper[bar] = 0.0; prcLower[bar] = 0.0; } limit = MinBars; } SetIndexLabel(2,"limit:"+limit); int mi; double ai[10,10],syx[10],x[10],sx[20]; double sum; int n,f; double qq,mm,tt; int ii,jj,kk,ll; double sq; //---- main calculation loop for(bar = limit; bar >= 0; bar--){ //Polynomial Regression Channel Calc int prcDegreePower = PRC_DegreePower+1; //----------------------sx------------------------------------------------------------------- sx[1]=PRC_LookBackPeriod+1; for(mi=1;mi<=prcDegreePower*2-2;mi++){ sum=0; // for(n=bar;n<=bar+PRC_LookBackPeriod;n++){ for(n=bar;n<=bar+PRC_LookBackPeriod;n++){ sum+=MathPow(n,mi); } sx[mi+1]=sum; } for(jj=1;jj<=prcDegreePower;jj++){ //----------------------syx----------- sum=0.00000; for(n=bar;n<=bar+PRC_LookBackPeriod;n++){ if(mi==1) sum+=Close[n]; else sum+=Close[n]*MathPow(n,jj-1); } syx[jj]=sum; } for(jj=1;jj<=prcDegreePower;jj++){ //===============Matrix================== for(ii=1; ii<=prcDegreePower; ii++){ kk=ii+jj-1; ai[ii,jj]=sx[kk]; } } //===============Gauss======================================================================================================== for(kk=1; kk<=prcDegreePower-1; kk++){ ll=0; mm=0; for(ii=kk; ii<=prcDegreePower; ii++){ if(MathAbs(ai[ii,kk])>mm){ mm=MathAbs(ai[ii,kk]); ll=ii; } } if(ll==0) return(0); if(ll!=kk){ for(jj=1; jj<=prcDegreePower; jj++){ tt=ai[kk,jj]; ai[kk,jj]=ai[ll,jj]; ai[ll,jj]=tt; } tt=syx[kk]; syx[kk]=syx[ll]; syx[ll]=tt; } for(ii=kk+1;ii<=prcDegreePower;ii++){ qq=ai[ii,kk]/ai[kk,kk]; for(jj=1;jj<=prcDegreePower;jj++){ if(jj==kk) ai[ii,jj]=0; else ai[ii,jj]=ai[ii,jj]-qq*ai[kk,jj]; } syx[ii]=syx[ii]-qq*syx[kk]; } } x[prcDegreePower]=syx[prcDegreePower]/ai[prcDegreePower,prcDegreePower]; for(ii=prcDegreePower-1;ii>=1;ii--){ tt=0; for(jj=1;jj<=prcDegreePower-ii;jj++){ tt=tt+ai[ii,ii+jj]*x[ii+jj]; x[ii]=(1/ai[ii,ii])*(syx[ii]-tt); } } sq=0.0; for(n=bar;n<=bar+PRC_LookBackPeriod;n++){ sum=0; for(kk=1;kk<=PRC_DegreePower;kk++){ sum+=x[kk+1]*MathPow(n,kk); } prcMA[n]=x[1]+sum; sq+=MathPow(Close[n]-prcMA[n],2); } sq=MathSqrt(sq/(PRC_LookBackPeriod+1))*PRC_StdDev; //Load Indicators prcUpper[bar]= prcMA[bar]+sq; prcCenter[bar]=prcMA[bar]; prcLower[bar]= prcMA[bar]-sq; } return(0); }