Meta: subchart(false); Inputs: minperiod(10), maxperiod(50), showme(bestaverage,edge,distribution); Array: periodwin[maxperiod],counter[maxperiod], periodwinpercent[maxperiod],pwpsmooth[maxperiod]; Variables: i,c1,c3, av, period,colour,indication, wish; c1=close[1];// yestedays close c3=close[3]; for i=minperiod to maxperiod begin // loop all averages and sum up results av=average(c1,i); if av>average(c3,i) then begin counter[i]=counter[i]+1; if close>c1 then periodwin[i]=periodwin[i]+1; end; end; for i=minperiod to maxperiod begin // calc percent winning bars if counter[i]>0 then periodwinpercent[i]=100*periodwin[i]/counter[i]; end; for i=minperiod+2 to maxperiod-2 begin // a little bit of smoothing pwpsmooth[i]=(periodwinpercent[i-2]+periodwinpercent[i-1]+periodwinpercent[i]+periodwinpercent[i+1]+periodwinpercent[i+2])/5; end; period=indexofhighestarray(pwpsmooth); // best period av=average(close,period); indication=highestarray(pwpsmooth); // winning percentage (smoothed) colour=red; if av>average(close[2],period) and indication>50 then colour=darkgreen; if showme=bestaverage then drawline(av,"best av indication",stylesolid,2,colour); if showme=edge then drawforest(0,indication-50,"0","advantage",2,colour); if showme=distribution and (islastbar) then begin for i=minperiod+2 to maxperiod-2 begin drawforest[-i](0,pwpsmooth[i]-50,"0","edge"); end; end; if not(islastbar) then print(datetime,"Best Period:",period,"% Win:",indication); //(c) kahler quanttrader.com