// (c) kahler@quanttrader.at Inputs: Price(close), Returnperiod(20),avperiod(50), StartYear(1998),EndYear(2020),hh(20),ll(-20),tradestyle(bullbear,bull,bear),showcumprob(true), scaleSumProbFact(1), smooth(1), mode(pct,ab); arrays: N[200], sm[200]; // array to store probabilities Variables: r, i, j, s, s1,av; Variables: tx,signal,x; // % return over given period // set returnperiod to 0 if you want to see the absolute price distribution (e.g. if applied on rsi, stoch, vola...) if returnperiod=0 then r=price else r=iff(mode=pct,100*(price-price[returnperiod])/price[returnperiod],price-price[returnperiod]); av=average(close,avperiod); signal=0; { if year(date)>=StartYear and year(date)<=EndYear then begin //ignore data oustide start/end year if tradestyle=bullbear then signal=1; // all returns, above nd below average if tradestyle=bull and close[returnperiod] of data2 >avperiod then signal=1; // crosses above average if tradestyle=bear and close[returnperiod] of data2 < avperiod then signal=1; // crosses below average end; } if close[returnperiod]<20 then signal=1; // to test absolute levels, otherwise remove and use {...} above.. if signal=1 then begin for j=0 to 199 begin // sort event in right row if r>=ll+(j)*(hh-ll)/200 and r<=ll+(j+1)*(hh-ll)/200 then N[j+1]=N[j+1]+1; if j=0 and r<ll then N[j+1]=N[j+1]+1; // return below min if j=199 and r>hh then N[j+1]=N[j+1]+1; // return above max end; end; if islastbar then begin for j=1 to 200 begin // sum over probability curve s=s+N[j]; end; if smooth>1 then begin // smoothing for x=1 to smooth begin for j=1 to 200 begin if j=1 then sm[1]=(n[1]+0.5*n[2])/1.5 else if j>1 and j<200 then sm[j]=(0.5*n[j-1]+n[j]+0.5*n[j+1])/2 else sm[200]=(0.5*n[199]+n[200])/1.5; end; for j=1 to 200 begin n[j]=sm[j]; end; end; end; for j=1 to 200 begin // draw probabilitiy curve and cum% N[j]=N[j]/s; // norm probability curve drawline[-j](100*N[j],"% probability"); s1=s1+N[j]; // sum probability from low to top if showcumprob then drawline[-j](scaleSumProbFact*s1,"Cum %"); if mod(j,5)=0 or j=1 then begin if j<>1 then tx=round(ll+j*(hh-ll)/200,2) else tx=round(ll,2); drawtext[-j](-0.1,"%",tx); end; end; end; // islastbar