# percent_rank_3avgs
#https://usethinkscript.com/threads/how-to-code-percent-rank.17505/
#How to code percent rank
# close/MovingAverage( AverageType.SIMPLE, close, 63) *2
# +close/MovingAverage( AverageType.SIMPLE, close, 126)
# +close/MovingAverage( AverageType.SIMPLE, close, 252)
#Then percent rank the result in 1-99 numbers
declare lower;
def na = double.nan;
def bn = barnumber();
def data = close;
#input avg1_type = AverageType.exponential;
input avg1_type = AverageType.Simple;
input avg1_length = 63;
def avg1 = MovingAverage(avg1_type , data , avg1_length );
input avg2_type = AverageType.Simple;
input avg2_length = 126;
def avg2 = MovingAverage(avg2_type , data , avg2_length );
input avg3_type = AverageType.Simple;
input avg3_length = 252;
def avg3 = MovingAverage(avg3_type ,data ,avg3_length );
input avg1_factor = 2.0;
input avg2_factor = 1.0;
input avg3_factor = 1.0;
def avg1_ratio = (avg1_factor*close/avg1);
def avg2_ratio = (avg2_factor*close/avg2);
def avg3_ratio = (avg3_factor*close/avg3);
def t = avg1_ratio + avg1_ratio + avg1_ratio;
def maxt = highest(t, avg3_length);
def mint = lowest(t, avg3_length);
def rngt = (maxt - mint);
def adjt = (t-mint);
def per = round(100*adjt/rngt,0);
addlabel(1, per + " %", color.yellow);
#-----------------------------
addchartbubble(0, 0,
t + "\n" +
per + " %"
, color.yellow, no);
input show_avg_ratios = no;
plot z1 = if show_avg_ratios then avg1_ratio else na;
plot z2 = if show_avg_ratios then avg2_ratio else na;
plot z3 = if show_avg_ratios then avg3_ratio else na;
plot zp = if show_avg_ratios then na else per;
input show_average_lines = no;
plot zavg1 = if show_average_lines then avg1 else na;
plot zavg2 = if show_average_lines then avg2 else na;
plot zavg3 = if show_average_lines then avg3 else na;
#