How to code percent rank

qykso

Member
HI Guys,

I would like to code the following logic


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

I am not sure how to code for the percent rank part, can it be done?

Is there other ways to sort the strength?
 
HI Guys,

I would like to code the following logic


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

I am not sure how to code for the percent rank part, can it be done?

Is there other ways to sort the strength?

if you want to rank a number, you need to compare it to something. we can find the min and max values of the signal, and use that range as 100%. then compare the current signal to it.
i used the longest average length , 252, as the length for finding highest and lowest.

i think this lower study is what you are asking for.

Code:
# 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;
#



------------------------------


here is an upper study for testing. shows 3 averages

Code:
# avgs3

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 show_average_lines = yes;
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;
#
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
195 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top