// Downloaded From https://www.WiseStockTrader.com _SECTION_BEGIN("Runs Test for Randomness"); /* Implementation of Runs Test for Randomness based on javascript implementation by Dr. Hossein Arsham through google search. All Credits duly acknowledged. Ported & re-written to AFL by sundarr */ function k(x) { return 1 / (1+0.2316419*x); } function HASpre(x) { return 1 - (exp(-0.5*x^2)/sqrt(2*3.141592654))*(0.31938153*k(x)- 0.356563782*k(x)^2+1.781477937*k(x)^3-1.821255978*k(x) ^4+1.330274429*k(x)^5); } function HAS(x) { return IIf(x<-6, 0, IIf(x>6, 1, IIf( x>0, HASpre(x), 1 - HASpre(-x)))) ; } function NORMSDIST(z1) { z=(z1); if(z>0) t=z; else t=-z; P1=(1+t*(0.049867347 + t*(0.0211410061 + t*(0.0032776263 + t*(0.0000380036 + t*(0.0000488906 + t*(0.000005383))))))) ^-16; //This is P-value p=1-P1/2; //Rounding the value //p=round(100000*p)/100000; if(z>0) t=1-p; else t=1-(1-p); //rounding the value t1=round(100000*t); t2=t1/100000; //Rounded P-Value return t2; } function GetRunsP(Values, E) { //E= Total number of inputs to run the hypothesis on //values is array holding the actual input array to process N=0; N1=0; N2=0; S=0.0; R=1; retValue=1; RunsP=Null; //Calculate the mean Mean=MA(Values,E); //Error check if (E<=BarCount) { for(j=E-1;j<BarCount;j++) { x=Mean[j]; for(i=j-E+1;i<=j;i++) { //if more than mean if(Values[i]>x) { N1[j]++; a=i; while (a>0) { a--; if(Values[a]!=x) break; }//while a>0 if(Values[a]<x) R[j]++; }//if values>x // if less than mean else if(Values[i]<x) { N2[j]++; a=i; while (a>0) { a--; if(Values[a]!=x) break; }//while a>0 if(Values[a]>x) R[j]++; } //else if values<x }//for i //Compute the expected mean and vaiance of R EM=1+(2*N1[j]*N2[j])/(N1[j]+N2[j]); //Expected Mean or Mu SD1=2*N1[j]*N2[j]*(2*N1[j]*N2[j]-N1[j]-N2[j]); SD2=(N1[j]+N2[j])^2; SD3=N1[j]+N2[j]-1; SD4=SD1/(SD2*SD3); //Standard Deviation or Sigma SD=sqrt(SD4); z1=(R[j]-EM)/SD; RunsP[j]=NORMSDIST(abs(z1)); //RunsP[j]=HAS((z1)); }//for j }//if (E<=BarCount) return RunsP; /* Conclusions t2<0.01 indicates very strong evidence against randomness (strong trend exists) t2>=0.01 AND t2<0.05 indicates moderate evidence against randomness (trend exists) t2>=0.05 AND t2<0.10 indicates suggestive evidence against randomness (mild trend may exist) t2>=0.10 indicates little or no real evidences against randomness (no trend exists) Any other (else) t2 value (like negative perhaps?) indicates Strong evidence against randomness (strong trend exists) */ }//function GetRunsP() //Prob1=GetRunsP((O+H+L+C)/4,Param("Prob Period",14,8,200,1)); Prob1=GetRunsP(C,Param("Prob Period",14,8,200,1)); //Plot(((Prob1)),"Prob Slow",ParamColor("Prob Color",colorGrey50),ParamStyle("Prob Style",styleArea)); Plot(0.05,"1",colorRed); Plot(0.1,"1",colorGrey50); Plot(0.2,"2",colorGrey50); AP= MA(Prob1,8); Plot(AP,"MA of Prob1",colorRed,styleLine|styleArea); //Plot(log(0.10),"",colorGrey40); //Plot(log(0.05),"",colorGrey40); //Prob2=GetRunsP(C,8); //Plot(Prob2,"Prob Fast",colorRed,styleDashed|styleOwnScale); //Plot(NORMSDIST(-0.85),"",colorBlue, styleOwnScale); //Plot(z1,"Z1",colorRed,styleOwnScale); _SECTION_END();