Rank volume 1-10

Department

New member
Hello,
Does someone know how to code a script that will rank volume bars 1 through 10 on a rolling basis. So of the previous 10 candles (inclusive of the candle we are on) the lowest volume bar is 1 and the highest volume bar is 10.

I want to use this code to identify the top 3 volume bars out of the 10 so that I can then say:
if rank >= 8, then...

and

if rank <= 3 then...

Thank you in advance.
 
Solution
here is a modified version of the post2 code by svanoy, which ranks 10 the most recent volume bars.
https://usethinkscript.com/threads/rank-volume-1-10.9504/#post-86495

there were a couple of issues with it
..if the data being ranked has duplicate values, it has duplicate rank numbers.
..the 1st 10 bars also have (random?) rankings.
..the highest bar number variable (finalbar) isn't valid until halfway through all the bars. it worked in his code, but not mine. because of this i replaced it with a highestall().
hal_rank , sort

i saw another post about ranking,
https://usethinkscript.com/threads/rolling-close-prices-in-ascending-order.12727/
and looked at my code and realized i didn't actually provide sorted data...
volume-ranking.png


Code:
declare lower;

def Look_Back = 10;

plot v = volume;
v.setpaintingStrategy(paintingStrategy.HISTOGRAM);
plot zl = 0;

#Rank Last Bar;
def rank = fold i=1 to Look_Back-1 with rankcounter=1 do if volume>GetValue(volume,i) then rankcounter+1 else rankcounter;
addlabel(yes,"Ranking: "+rank,color.white);

#Rolling Rank
def VBar = if !IsNaN(close) then HighestAll(Barnumber()) else VBar[1];

def FinalBar = fold b=0 to VBar while !IsNaN(GetValue(close,-b)) do GetValue(BarNumber(),-b);
AddChartBubble(FinalBar-BarNumber()<Look_Back,volume,fold r=0-(FinalBar-BarNumber()) to Look_Back-(FinalBar-BarNumber()) with rcounter=1 do if volume > GetValue(volume,r) then rcounter+1 else rcounter, color.white);
 
Last edited:

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

@Svanoy that is an amazing, short script

i went a different (and much longer) way, to show values on the main chart. this saves many values into variables, for later use.


this will sort and rank data from the last 10 bars on a chart.
the data is set to volume.

options:
. enter quantity of bars to show the highest volume. default is 3
. show rank number under each bar (last 10 bars)
. show an arrow above the first bar (10 bars back from lastbar)
. show wedges above the 10 bars (tiny arrows)
. a couple of bubbles to show values for testing

some code may seem redundant, i saved the rank data and barnumbers, 2 different ways, to give the user options for altering this to their needs. (ex. a group of 10 variables and a single variable , rank numbers: i1 to i10 , seq )


Ruby:
# rank10bars_01

# sort 10 data values
# halcyonguy
# 22-01-02


def dat = volume;
def bars = 10;
def big = 999999999;
def na = double.nan;
# ----------------------------
def bn = barnumber();
def lastbarbn = HighestAll(If(IsNaN(close), 0, bn));
def lastbar = if (bn == lastbarbn) then 1 else 0;
#def lastbar = !isnan(close[0]) and isnan(close[-1]);

# ----------------------------

input show_rank_numbers = yes;
input mark_x_highest_bars = 3;
input test_labels1 = yes;

# ----------------------------
# true during last x bars on chart
def lastxbars = if ( !IsNaN(close) and IsNaN(close[ -(bars - 0) ] ) ) then 1 else 0;

# true on the x bar before last bar
def xbeforelast =  if ( !IsNaN(close[-(bars - 1) ] ) and IsNaN(close[ -(bars) ] ) ) then 1 else 0;

input test_10_wedges = no;
plot y1 = if (test_10_wedges and lastxbars) then 1 else na;
y1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_up);

input test_start_arrow = no;
plot y2 = if (test_start_arrow and xbeforelast) then high*1.001 else na;
y2.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
y2.setlineweight(3);


# ----------------------------

# do stuff on first bar of the last 10 bars
def d1;
def d2;
def d3;
def d4;
def d5;
def d6;
def d7;
def d8;
def d9;
def d10;
if xbeforelast then {
# find rank of data
    d1 = Lowest( dat[-9], 10 );
    d10 = Highest( dat[-9], 10 );

    d2 = LowestAll( if lastxbars and dat > d1 then dat else big );
    d9 = HighestAll( if lastxbars and dat < d10 then dat else 0 );

    d3 = LowestAll( if lastxbars and dat > d2 then dat else big );
    d8 = HighestAll( if lastxbars and dat < d9 then dat else 0 );

    d4 = LowestAll( if lastxbars and dat > d3 then dat else big );
    d7 = HighestAll( if lastxbars and dat < d8 then dat else 0 );

    d5 = LowestAll( if lastxbars and dat > d4 then dat else big );
    d6 = HighestAll( if lastxbars and dat < d7 then dat else 0 );
} else {
    d1 = d1[1];
    d2 = d2[1];
    d3 = d3[1];
    d4 = d4[1];
    d5 = d5[1];
    d6 = d6[1];
    d7 = d7[1];
    d8 = d8[1];
    d9 = d9[1];
    d10 = d10[1];
}


# sequence number , index, rank. bar#
def i1;
def i2;
def i3;
def i4;
def i5;
def i6;
def i7;
def i8;
def i9;
def i10;
def i1bn;
def i2bn;
def i3bn;
def i4bn;
def i5bn;
def i6bn;
def i7bn;
def i8bn;
def i9bn;
def i10bn;
if lastxbars then {
 i1 = if d1 == dat then 1 else i1[1];
 i2 = if d2 == dat then 2 else i2[1];
 i3 = if d3 == dat then 3 else i3[1];
 i4 = if d4 == dat then 4 else i4[1];
 i5 = if d5 == dat then 5 else i5[1];
 i6 = if d6 == dat then 6 else i6[1];
 i7 = if d7 == dat then 7 else i7[1];
 i8 = if d8 == dat then 8 else i8[1];
 i9 = if d9 == dat then 9 else i9[1];
 i10 = if d10 == dat then 10 else i10[1];

 i1bn = if d1 == dat then bn else i1bn[1];
 i2bn = if d2 == dat then bn else i2bn[1]; 
 i3bn = if d3 == dat then bn else i3bn[1];
 i4bn = if d4 == dat then bn else i4bn[1];
 i5bn = if d5 == dat then bn else i5bn[1];
 i6bn = if d6 == dat then bn else i6bn[1];
 i7bn = if d7 == dat then bn else i7bn[1];
 i8bn = if d8 == dat then bn else i8bn[1];
 i9bn = if d9 == dat then bn else i9bn[1];
 i10bn = if d10 == dat then bn else i10bn[1];

} else {
 i1 = i1[1];
 i2 = i1[1];
 i3 = i1[1];
 i4 = i1[1];
 i5 = i1[1];
 i6 = i1[1];
 i7 = i1[1];
 i8 = i1[1];
 i9 = i1[1];
 i10 = i1[1];

 i1bn = i1bn[1];
 i2bn = i2bn[1];
 i3bn = i3bn[1];
 i4bn = i4bn[1];
 i5bn = i5bn[1];
 i6bn = i6bn[1];
 i7bn = i7bn[1];
 i8bn = i8bn[1];
 i9bn = i9bn[1];
 i10bn = i10bn[1];

}


# compare data (volume) to the values in last 10 bars
#  sequence #s , rank
def seq =
if !lastxbars then 0
 else if d1 == dat then 1
 else if d2 == dat then 2
 else if d3 == dat then 3
 else if d4 == dat then 4
 else if d5 == dat then 5
 else if d6 == dat then 6
 else if d7 == dat then 7
 else if d8 == dat then 8
 else if d9 == dat then 9
 else if d10 == dat then 10
 else 0;


#input show_rank_numbers = yes;
plot t1 = if (show_rank_numbers and lastxbars) then seq else na;
t1.SetPaintingStrategy(PaintingStrategy.VALUES_below); 
t1.setdefaultcolor(color.white);

def seqbn =
if !lastxbars then 0
 else if d1 == dat then bn
 else if d2 == dat then bn
 else if d3 == dat then bn
 else if d4 == dat then bn
 else if d5 == dat then bn
 else if d6 == dat then bn
 else if d7 == dat then bn
 else if d8 == dat then bn
 else if d9 == dat then bn
 else if d10 == dat then bn
 else 0;

input test_rank_bn_bubble = no;
addchartbubble(test_rank_bn_bubble and lastxbars, low*0.99, seq + "\n" + seqbn, color.yellow, no);


# 0 to turn off
#input mark_x_highest_bars = 3;
def qty = if mark_x_highest_bars > bars then bars else mark_x_highest_bars;

plot g = if (lastxbars and qty > 0 and seq >= (bars - qty + 1)) then dat else na;
g.SetPaintingStrategy(PaintingStrategy.VALUES_above); 


#input test_seq2 = no;
#plot w1 = if (test_seq2 and lastxbars and d1 == dat ) then dv1 else na;
#w1.SetPaintingStrategy(PaintingStrategy.VALUES_below); 
#plot w2 = if (test_seq2 and lastxbars and d2 == dat ) then dv2 else na;
#w2.SetPaintingStrategy(PaintingStrategy.VALUES_below);
#plot w3 = if (test_seq2 and lastxbars and d3 == dat ) then dv3 else na;
#w3.SetPaintingStrategy(PaintingStrategy.VALUES_below);
#plot w4 = if (test_seq2 and lastxbars and d4 == dat ) then dv4 else na;
#w4.SetPaintingStrategy(PaintingStrategy.VALUES_below);


input test1_bubble_all10values = no;
addchartbubble(test1_bubble_all10values and lastxbars , low*0.99,
 d1 
+ "\n" + d2
+ "\n" + d3
+ "\n" + d4
+ "\n" + d5
+ "\n" + d6
+ "\n" + d7
+ "\n" + d8
+ "\n" + d9
+ "\n" + d10
, color.yellow, no);


#input test_labels1 = yes;
addlabel(test_labels1, "            ", color.black);
addlabel(test_labels1, "volumes of last 10 bars: seq# , volume , bar#", color.yellow);
addlabel(test_labels1, i1 + " " + d1 + " " + i1bn, color.yellow);
addlabel(test_labels1, i2 + " " + d2 + " " + i2bn, color.yellow);
addlabel(test_labels1, i3 + " " + d3 + " " + i3bn, color.yellow);
addlabel(test_labels1, i4 + " " + d4 + " " + i4bn, color.yellow);
addlabel(test_labels1, i5 + " " + d5 + " " + i5bn, color.yellow);
addlabel(test_labels1, i6 + " " + d6 + " " + i6bn, color.yellow);
addlabel(test_labels1, i7 + " " + d7 + " " + i7bn, color.yellow);
addlabel(test_labels1, i8 + " " + d8 + " " + i8bn, color.yellow);
addlabel(test_labels1, i9 + " " + d9 + " " + i9bn, color.yellow);
addlabel(test_labels1, i10 + " " + d10 + " " + i10bn, color.yellow);
#

KO 5D 5M chart
labels show, sequence number, volume , barnumber
zxXMNyt.jpg
 
Code:
declare lower;

def Look_Back = 10;

plot v = volume;
v.setpaintingStrategy(paintingStrategy.HISTOGRAM);
plot zl = 0;

#Rank Last Bar;
def rank = fold i=1 to Look_Back-1 with rankcounter=1 do if volume>GetValue(volume,i) then rankcounter+1 else rankcounter;
addlabel(yes,"Ranking: "+rank,color.white);

#Rolling Rank
def VBar = if !IsNaN(close) then HighestAll(Barnumber()) else VBar[1];

def FinalBar = fold b=0 to VBar while !IsNaN(GetValue(close,-b)) do GetValue(BarNumber(),-b);
AddChartBubble(FinalBar-BarNumber()<Look_Back,volume,fold r=0-(FinalBar-BarNumber()) to Look_Back-(FinalBar-BarNumber()) with rcounter=1 do if volume > GetValue(volume,r) then rcounter+1 else rcounter, color.white);


here is a modified version of the post2 code by svanoy, which ranks 10 the most recent volume bars.
https://usethinkscript.com/threads/rank-volume-1-10.9504/#post-86495

there were a couple of issues with it
..if the data being ranked has duplicate values, it has duplicate rank numbers.
..the 1st 10 bars also have (random?) rankings.
..the highest bar number variable (finalbar) isn't valid until halfway through all the bars. it worked in his code, but not mine. because of this i replaced it with a highestall().

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

can pick how many prices to sort/rank.
data being ranked is set to the close price.
1 is the lowest number, x is the highest.
green bubbles above the candles, show the final ranked numbers.


Ruby:
# rank_xbars_03_nodups_cln

# rank the last x close prices. 1 is lowest, x is the highest

# start with post2 code
# 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 lastx = lastbn;

# 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);

# choose the data to be ranked
def data = close;

# choose how many values to rank
input qty = 10;

#def rank = fold i = 1 to qty
# with rankcounter = 1
# do if data > GetValue(data, i) then rankcounter + 1 else rankcounter;

def rank1 =  fold r = (0 - (lastx - BarNumber() )) to qty - (lastx - BarNumber())
 with rcounter = 1
 do if data > GetValue(data, r) then rcounter + 1 else rcounter;

# ---------------------------------

# get bn , x bars back from lastbar
def rngbar1 = lastx - qty + 1;

# reset values before the range of bars to be ranked, to 0
def rank2 = if bn >= rngbar1 then rank1 else 0;

# duplicate numbers will have the same rank number. 
# when there is a duplicate, a ranking number will be skipped.
# look at prev bars and find duplicate rank2 numbers and make an offset
def dup1 = fold g = 1 to qty
with h 
do h + (if rank2 == getvalue(rank2, g) then 1 else 0);

#---------------------------------
# renumber dups
# rank3 is an adjusted, unique rank numbers
#  rank2 + dup offset
def rank3 = if bn >= rngbar1 then (rank2 + dup1) else rank2;
#---------------------------------

#---------------------------------

input test_rank1_numbers = yes;
AddChartBubble(test_rank1_numbers and bn > qty and lastx - BarNumber() < qty , high, rank1, color.gray, yes);

input test_rank2_numbers = no;
addchartbubble(test_rank2_numbers, high, rank2 , color.yellow, yes);

input test_duplicate_offsets = yes;
addchartbubble(test_duplicate_offsets and bn >= rngbar1, high, dup1 , (if dup1 > 0 then color.cyan else color.gray), yes);

# rank3 is the final rank numbers
input rank3_numbers = yes;
addchartbubble(rank3_numbers and bn >= rngbar1, high, rank3 , color.green, yes);

input test_barnumbers = no;
addchartbubble(test_barnumbers and bn >= rngbar1, low, bn , color.yellow, no);
#


top row, final ranking, green, rank3
middle row, duplicates of rank2, cyan bubbles
bottom row, original ranking formula, gray, rank1
TTGAqEY.jpg

hal_rank , sort
 
volume-ranking.png


Code:
declare lower;

def Look_Back = 10;

plot v = volume;
v.setpaintingStrategy(paintingStrategy.HISTOGRAM);
plot zl = 0;

#Rank Last Bar;
def rank = fold i=1 to Look_Back-1 with rankcounter=1 do if volume>GetValue(volume,i) then rankcounter+1 else rankcounter;
addlabel(yes,"Ranking: "+rank,color.white);

#Rolling Rank
def VBar = if !IsNaN(close) then HighestAll(Barnumber()) else VBar[1];

def FinalBar = fold b=0 to VBar while !IsNaN(GetValue(close,-b)) do GetValue(BarNumber(),-b);
AddChartBubble(FinalBar-BarNumber()<Look_Back,volume,fold r=0-(FinalBar-BarNumber()) to Look_Back-(FinalBar-BarNumber()) with rcounter=1 do if volume > GetValue(volume,r) then rcounter+1 else rcounter, color.white);

volume-ranking.png


Code:
declare lower;

def Look_Back = 10;

plot v = volume;
v.setpaintingStrategy(paintingStrategy.HISTOGRAM);
plot zl = 0;

#Rank Last Bar;
def rank = fold i=1 to Look_Back-1 with rankcounter=1 do if volume>GetValue(volume,i) then rankcounter+1 else rankcounter;
addlabel(yes,"Ranking: "+rank,color.white);

#Rolling Rank
def VBar = if !IsNaN(close) then HighestAll(Barnumber()) else VBar[1];

def FinalBar = fold b=0 to VBar while !IsNaN(GetValue(close,-b)) do GetValue(BarNumber(),-b);
AddChartBubble(FinalBar-BarNumber()<Look_Back,volume,fold r=0-(FinalBar-BarNumber()) to Look_Back-(FinalBar-BarNumber()) with rcounter=1 do if volume > GetValue(volume,r) then rcounter+1 else rcounter, color.white);



Can the ranking above be combined with code below to rank volume bars during a specified defined time period ?







Ruby:
# hilo_xday_first_xbars_0

input first_x_bars = 7;
def na = double.nan;
def bn = barnumber();

# day count
def istoday = if GetLastDay() == GetDay() then 1 else 0;
def newday = if getday() != getday()[1] then 1 else 0;
def daycnt = if bn == 1 then 1 else if (!isnan(close) and newday) then daycnt[1] + 1 else daycnt[1];
def maxdays = highestall(daycnt);
def revdaycnt = maxdays - daycnt + 1;

addlabel(1, maxdays + " days on chart", color.yellow);

input days_back = 2;
def days_back2 = if days_back < 0 then 0 else if days_back > maxdays then maxdays else days_back;
def isday = (revdaycnt == days_back2);

def start = 0930;
#def end = 1600;
#def daytime = if secondsfromTime(start) >= 0 and secondstillTime(end) > 0 then 1 else 0;
def daystart = if secondsfromTime(start) == 0 then 1 else 0;

#-------------------------------------------
# https://usethinkscript.com/threads/current-price-line-indicator-for-thinkorswim.8793/
# Line At Price   Mobius
# Alternative to using the HighestAll() function
# if a chart has more than 1000 bars, increase this number. 1999 max
def barsBack = 1000;
def c2 = if !IsNaN(close) and IsNaN(close[-1])
        then bn
        else c2[1];
def lastbn = if isNaN(close[-barsBack])
            then c2[-barsBack]
            else Double.NaN;

def length = first_x_bars;
def rngfirst = daystart and isday;
def rngfirstbn = if rngfirst then bn else rngfirstbn[1];
def rnglastbn = (rngfirstbn + length - 1);

def hi = if bn == 1 or bn > rnglastbn or !isday then na
  else if rngfirst then highest( high[-(length-1)], length)
  else hi[1];
def hibnoff = if bn == 1 or bn > rnglastbn then na
  else if rngfirst then GetMaxValueOffset( high[-(length-1)], length)
  else hibnoff[1];
def hibn = rnglastbn - hibnoff;
plot zhi = hi;
zhi.AssignValueColor( if bn >= (hibn + 1) then color.magenta else color.orange);

def lo = if bn == 1 or bn > rnglastbn or !isday then na
  else if rngfirst then lowest(low[-(length-1)], length)
  else lo[1];
def lobnoff = if bn == 1 or bn > rnglastbn then na
  else if rngfirst then GetminValueOffset(low[-(length-1)], length)
  else lobnoff[1];
def lobn = rnglastbn - lobnoff;
plot zlo = lo;
zlo.AssignValueColor( if bn >= (lobn + 1) then color.magenta else color.orange);

AddChartBubble(bn == hibn, hi, "HIGH", Color.WHITE, yes);
AddChartBubble(bn == lobn, lo, "LOW", Color.WHITE, no);

AddChartBubble(
# first bar
#    rngfirst,
# last bar
   bn == rnglastbn,
    (High+low)/2,
    length+"\nBar",
    Color.GRAY,
    yes);

#plot bbH = if isday and rngfirst then High + TickSize()*1 else na;
plot bbH = if bn == rnglastbn then High + TickSize()*1 else na;
bbH.SetPaintingStrategy(PaintingStrategy.SQUARES);
bbH.SetDefaultColor(Color.MAGENTA);
bbH.SetLineWeight(5);
bbH.HideBubble();

#plot bbL = if isday and rngfirst then Low - TickSize()*1 else na;
plot bbL = if bn == rnglastbn then Low - TickSize()*1 else na;
bbL.SetPaintingStrategy(PaintingStrategy.SQUARES);
bbL.SetDefaultColor(Color.MAGENTA);
bbL.SetLineWeight(5);
bbL.HideBubble();
#
#
 
I added a timeframe variable to Svanoy code below and got I it to produce a rank during the specified time, but the rank has duplicate numbers. Any ideas what is needed to fix so the rank produces a unique rank number for each volume candle during specified timeframe?



Ruby:
declare lower;

def Look_Back = 14;

plot v = volume;
v.setpaintingStrategy(paintingStrategy.HISTOGRAM);
plot zl = 0;



#Rolling Rank
def VBar = if !IsNaN(volume) then HighestAll(Barnumber()) else VBar[1];

def TimeFrame = if secondsTillTime(900) <=0 and secondsTillTime(1600) >0 then 0 else 1; # ADDED NEW

def FinalBar = fold b= 0 to Vbar while TimeFrame do GetValue(BarNumber(),-b);

AddChartBubble(FinalBar-BarNumber()<Look_Back,volume,fold r=0-(FinalBar-BarNumber()) to Look_Back-(FinalBar-BarNumber()) with rcounter=1 do if volume > GetValue(volume,r) then rcounter+1 else rcounter,color.white);
 
*Update
Below I was able to get the rank to work inside a timeframe by setting a variable and having it rank the Look_Back number going back from the SecondsTillTime number. the only issue is for some reason it always has one duplicate rank in the set timeframe. In the image instance below you can see the rank 6 is the only one to rank twice. In this scenario it should rank lowest to highest volume bars ending at time 1600 going back 14 bars. So 1 to 14 chartbubbles should populate with no rank duplicates. Anyone see a possible error to resolve so rank does not duplicate for the Look_Back ?






declare lower;

def Look_Back = 14;

plot v = volume;
v.setpaintingStrategy(paintingStrategy.HISTOGRAM);
plot zl = 0;



#Rolling Rank
def VBar = if !IsNaN(volume) then HighestAll(Barnumber()) else VBar[1];

def TimeFrame = if SecondsTillTime(1600) then 1 else 0;

def FinalBar = fold b=0 to VBar while (GetValue(TimeFrame,-b)) do GetValue(BarNumber(),-b);

AddChartBubble(FinalBar-BarNumber()<Look_Back,volume,fold r=0-(FinalBar-BarNumber()) to Look_Back-(FinalBar-BarNumber()) with rcounter=1 do if volume > GetValue(volume,r) then rcounter+1 else rcounter,color.white);









I added a timeframe variable to Svanoy code below and got I it to produce a rank during the specified time, but the rank has duplicate numbers. Any ideas what is needed to fix so the rank produces a unique rank number for each volume candle during specified timeframe?



Ruby:
declare lower;

def Look_Back = 14;

plot v = volume;
v.setpaintingStrategy(paintingStrategy.HISTOGRAM);
plot zl = 0;



#Rolling Rank
def VBar = if !IsNaN(volume) then HighestAll(Barnumber()) else VBar[1];

def TimeFrame = if secondsTillTime(900) <=0 and secondsTillTime(1600) >0 then 0 else 1; # ADDED NEW

def FinalBar = fold b= 0 to Vbar while TimeFrame do GetValue(BarNumber(),-b);

AddChartBubble(FinalBar-BarNumber()<Look_Back,volume,fold r=0-(FinalBar-BarNumber()) to Look_Back-(FinalBar-BarNumber()) with rcounter=1 do if volume > GetValue(volume,r) then rcounter+1 else rcounter,color.white);

22.png

b
 
@VIP_TOS You will be better off using @halcyonguy modification above. My original code does not have the ability to do what you want as the code variables for the bars beyond the look back period are overwritten.
Thank you for the reply, Svanoy! is there a way to resolve my last updated post with your code? it seems as it is almost working but give one duplicate rank each time.
 
@VIP_TOS you have a look back period of 14, but you have ranks for 15 bars. The last bar is always going to be duplicated because it is ranked based on the 13 bars before it.

This is why is said my code wouldn't truly work for what you are trying to do.
 
@VIP_TOS you could change the display condition of the chart bubble to not show the 15th bar.
Code:
AddChartBubble(FinalBar-BarNumber()<Look_Back and Between(FinalBar-BarNumber(),0,Look_Back-1),volume,fold r=0-(FinalBar-BarNumber()) to Look_Back - (FinalBar-BarNumber()) with rcounter=1 do if volume > GetValue(volume,r) then rcounter+1 else rcounter,color.white);
 
@VIP_TOS you could change the display condition of the chart bubble to not show the 15th bar.
Code:
AddChartBubble(FinalBar-BarNumber()<Look_Back and Between(FinalBar-BarNumber(),0,Look_Back-1),volume,fold r=0-(FinalBar-BarNumber()) to Look_Back - (FinalBar-BarNumber()) with rcounter=1 do if volume > GetValue(volume,r) then rcounter+1 else rcounter,color.white);
I had attempted to make that happen with no luck... You got it! Thank you Svanoy!
 
here is a modified version of the post2 code by svanoy, which ranks 10 the most recent volume bars.
https://usethinkscript.com/threads/rank-volume-1-10.9504/#post-86495

there were a couple of issues with it
..if the data being ranked has duplicate values, it has duplicate rank numbers.
..the 1st 10 bars also have (random?) rankings.
..the highest bar number variable (finalbar) isn't valid until halfway through all the bars. it worked in his code, but not mine. because of this i replaced it with a highestall().
hal_rank , sort

i saw another post about ranking,
https://usethinkscript.com/threads/rolling-close-prices-in-ascending-order.12727/
and looked at my code and realized i didn't actually provide sorted data, just the ranking of the offsets.
so i made another version.
this one writes sorted data, small to big, in the last x bars of , data_rank
it plots a line of the data

several test bubbles can be turned on to verify the data

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

EDIT 2024/10
add rev rank - highest to lowest , rank4

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

rm6Hyvi.jpg
 
Last edited:
Solution

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
228 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