# rank_x_values_03
# halcyonguy 22-09-18
# list the actual data, ranked, small to big, in last x bars
# rank_10values_Svanoy_01
# rank the last 10 volume values
# Svanoy
# https://usethinkscript.com/threads/rank-volume-1-10.9504/
def bn = barnumber();
def na = double.nan;
def lastbn = HighestAll(If(IsNaN(close), 0, bn));
def lastbar = if (bn == lastbn) then 1 else 0;
# def lastbar = !isnan(close[0]) and isnan(close[-1]);
# data = the variable to be ranked
def data = close;
input qty = 10;
def rank = fold i = 1 to qty
with rankcounter = 1
do if data > GetValue(data, i) then rankcounter + 1 else rankcounter;
# counts up by 2 till the middle of the bars on the chart, then it is max bar#
def FinalBar = fold b = 0 to BarNumber()
while !IsNaN(GetValue(close, -b))
do GetValue(BarNumber(), -b);
def FinalBar_c = fold c = 0 to BarNumber()
while !IsNaN(GetValue(close, -c))
# halfway thru set of bars, this will have the max bn
do c + bn;
# counts up till the middle of the bars on the chart, then counts down
# do c + 1;
#addlabel(yes, "Ranking: " + rank, color.white);
#def e = FinalBar - BarNumber();
addchartbubble(0, 0,
bn + " bn\n" +
finalbar + " f\n" +
#e + " e\n" +
finalbar_c + " c\n"
, color.yellow, yes);
def rank1 = fold r = (0 - (lastbn - BarNumber() )) to qty - (lastbn - BarNumber())
with rcounter = 1
do if data > GetValue(data, r) then rcounter + 1 else rcounter;
input test_show_ranking_bubbles = yes;
AddChartBubble(test_show_ranking_bubbles and
# FinalBar - BarNumber() < 10,
( bn > qty and (FinalBar - BarNumber() < qty)), high,
rank1, color.gray, yes);
#
# ---------------------------------
# get bn , x bars back from lastbar
# 1st bn in rng, last x bars
def rngbar1 = lastbn - qty + 1;
# cnt , countdown thru the rng. last bar = 1
def cntdwn_in_rng = lastbn - bn + 1;
# bn, countup thru the rng, 1st bar = 1 , last bar = qty
def cntup_in_rng = qty - cntdwn_in_rng + 1;
def rank2 = if bn >= rngbar1 then rank1 else 0;
#addchartbubble( 1, high, rank2 , color.yellow, yes);
# find dup #s and make an offset
def dup1 = fold g = 1 to qty
with h
do h + (if rank2 == getvalue(rank2, g) then 1 else 0);
addchartbubble(test_show_ranking_bubbles and bn >= rngbar1, high, dup1 , (if dup1 > 0 then color.cyan else color.gray), yes);
# adjusted rank, add dup offset, all unique #s
def rank3 = if bn >= rngbar1 then (rank2 + dup1) else rank2;
addchartbubble(test_show_ranking_bubbles and bn >= rngbar1, high, rank3 , color.green, yes);
#----------------------------
# rev rank - highest to lowest
def rank4 = if bn >= rngbar1 then (qty - rank3 + 1) else 0;
addchartbubble(test_show_ranking_bubbles and bn >= rngbar1, high, rank4 , color.magenta, yes);
#-------------------------------------
# put the data in order, rank, for the last x bars
# search for matches to index #s, then read data value
# as the current bar moves forward thru the range of bars,
# the offset used in getvalue is adjusted back, to keep looking at all 'qty' of bars
# cntup_in_rng , is a counter, 1 = lowest , 10 = biggest
# put data values in a var, in rank order, small to big
def data_rank = fold k = 0 to qty
with q
do (if getvalue(rank3, -(k-cntup_in_rng +1)) == cntup_in_rng then getvalue(data, -(k-cntup_in_rng+1)) else q);
# plot a line, during the last x bars, of the ranked data
input plot_ranked_data = yes;
plot zr = if (plot_ranked_data and (bn >= rngbar1) and !isnan(close)) then data_rank else na;
zr.setdefaultcolor(color.cyan);
#------------------------------
input test_ranked_data = yes;
addchartbubble(test_ranked_data and bn >= rngbar1, low*0.998,
cntup_in_rng + "\n" +
data_rank
, color.yellow, no);
input test_rng_counts_data = no;
addchartbubble(test_rng_counts_data and bn >= rngbar1, low*0.998,
bn + "\n" +
data + " dat\n" +
rngbar1 + "\n" +
cntdwn_in_rng + "\n" +
cntup_in_rng + "\n" +
data_rank
, color.yellow, no);
#