How t o find 4th highest close in last 100 bars?

Solution
Dear Community, please help with writing code condition to find 4th highest close value in last 100 bars. I referred few scripts for guidance but could not understand due to my level of coding expertise.

https://usethinkscript.com/threads/what-is-the-valuewhen-tradingview-equivalent-in-thinkscript.3666/
https://usethinkscript.com/threads/guidance-assistance-with-array-for-loops.10995/

Humble Appreciate you help. Thank you.

here is one way

Code:
#fourth_hi
#How to find 4th highest close in last 100 bars?

input length = 100;
def cls = close;

def cls1 = fold i1 = 0 to length
 with temp1
 do Max(getvalue(cls,i1), temp1);

def cls2 = fold i2 = 0 to length
 with temp2
 do if cls1 > getvalue(cls,i2) then...
Try this -

Python:
def length = 100;

def closeArray = close;
def sortedCloses = fold i = 0 to length with temp = Double.NaN do if IsNaN(temp) then close else Max(closeArray[i], temp);
def fourthHighestClose = fold j = 0 to length with h = Double.NaN do if IsNaN(h) then sortedCloses else Max(sortedCloses[j], h);
def isFourthHighest = close == fourthHighestClose;
plot fourthHighestClosePlot = if isFourthHighest then fourthHighestClose else Double.NaN;

AddLabel(yes, "4th Highest Close: " + fourthHighestClose, Color.GREEN);
 

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

Try this -

Python:
def length = 100;

def closeArray = close;
def sortedCloses = fold i = 0 to length with temp = Double.NaN do if IsNaN(temp) then close else Max(closeArray[i], temp);
def fourthHighestClose = fold j = 0 to length with h = Double.NaN do if IsNaN(h) then sortedCloses else Max(sortedCloses[j], h);
def isFourthHighest = close == fourthHighestClose;
plot fourthHighestClosePlot = if isFourthHighest then fourthHighestClose else Double.NaN;

AddLabel(yes, "4th Highest Close: " + fourthHighestClose, Color.GREEN);

this doesn't appear to work. it just finds the highest in the range
 
Dear Community, please help with writing code condition to find 4th highest close value in last 100 bars. I referred few scripts for guidance but could not understand due to my level of coding expertise.

https://usethinkscript.com/threads/what-is-the-valuewhen-tradingview-equivalent-in-thinkscript.3666/
https://usethinkscript.com/threads/guidance-assistance-with-array-for-loops.10995/

Humble Appreciate you help. Thank you.

here is one way

Code:
#fourth_hi
#How to find 4th highest close in last 100 bars?

input length = 100;
def cls = close;

def cls1 = fold i1 = 0 to length
 with temp1
 do Max(getvalue(cls,i1), temp1);

def cls2 = fold i2 = 0 to length
 with temp2
 do if cls1 > getvalue(cls,i2) then Max(getvalue(cls,i2), temp2) else temp2;

def cls3 = fold i3 = 0 to length
 with temp3
 do if cls2 > getvalue(cls,i3) then Max(getvalue(cls,i3), temp3) else temp3;

def cls4 = fold i4 = 0 to length
 with temp4
 do if cls3 > getvalue(cls,i4) then Max(getvalue(cls,i4), temp4) else temp4;


#AddLabel(yes, "len: " + length, Color.GREEN);
AddLabel(yes, "4th Highest Close: " + cls4, Color.GREEN);

def x = (!isnan(close[-(length-1)]) and isnan(close[-(length+0)]));
addverticalline(x, "-", color.cyan);

input test1 = no;
addchartbubble(test1, low,
cls1 + "\n" +
cls2 + "\n" +
cls3 + "\n" +
cls4 + "\n" 
, color.yellow, no);
#
 
Solution
here is one way

Code:
#fourth_hi
#How to find 4th highest close in last 100 bars?

input length = 100;
def cls = close;

def cls1 = fold i1 = 0 to length
 with temp1
 do Max(getvalue(cls,i1), temp1);

def cls2 = fold i2 = 0 to length
 with temp2
 do if cls1 > getvalue(cls,i2) then Max(getvalue(cls,i2), temp2) else temp2;

def cls3 = fold i3 = 0 to length
 with temp3
 do if cls2 > getvalue(cls,i3) then Max(getvalue(cls,i3), temp3) else temp3;

def cls4 = fold i4 = 0 to length
 with temp4
 do if cls3 > getvalue(cls,i4) then Max(getvalue(cls,i4), temp4) else temp4;


#AddLabel(yes, "len: " + length, Color.GREEN);
AddLabel(yes, "4th Highest Close: " + cls4, Color.GREEN);

def x = (!isnan(close[-(length-1)]) and isnan(close[-(length+0)]));
addverticalline(x, "-", color.cyan);

input test1 = no;
addchartbubble(test1, low,
cls1 + "\n" +
cls2 + "\n" +
cls3 + "\n" +
cls4 + "\n"
, color.yellow, no);
#
Thank you! At present I cannot confirm if this working, as I could not validate entire output.

Could you please be able to help with Finding 4th lowest close. I tried revers engineering above code but.. alas coudn't.
 
Thank you! At present I cannot confirm if this working, as I could not validate entire output.

Could you please be able to help with Finding 4th lowest close. I tried revers engineering above code but.. alas coudn't.

i redid the code, and added the lows
the first version looked backwards from the current bar to figure out the highs.
this version finds the first bar in a period ( that is 100 bars before the last bar) then looks at future bars to find the highs and lows. this way the values exist for all x bars in period.

draw lines at the price levels
draw dots on the highs and lows
can choose high and low prices: high, low, close,....
draw a vertical line before first bar in period.


Code:
#fourth_hilo

#https://usethinkscript.com/threads/how-t-o-find-4th-highest-close-in-last-100-bars.19755/
#How to find 4th highest close in last 100 bars?
#rewardiaz

input length = 100;
input hi = close;
input lo = close;
def big = 99999;

def na = double.nan;
def bn = barnumber();
def lastbn = HighestAll(If(IsNaN(close), 0, bn));
def lastbar = if (bn == lastbn) then 1 else 0;

# first bar in period , x bars before last bar
def first = (!IsNaN(close[-(length-1)]) and IsNaN(close[-length]));
addverticalline(first,"==",color.cyan);


def prhi1;
def prhi2;
def prhi3;
def prhi4;
def prlo1;
def prlo2;
def prlo3;
def prlo4;
if bn == 1 or isnan(close) then {
 prhi1 = na;
 prhi2 = na;
 prhi3 = na;
 prhi4 = na;
 prlo1 = na;
 prlo2 = na;
 prlo3 = na;
 prlo4 = na;
} else if first then {
 prhi1 = fold i1 = 0 to length
 with t1
 do Max(getvalue(hi,-i1), t1);

 prhi2 = fold i2 = 0 to length
 with t2
 do if prhi1 > getvalue(hi,-i2) then Max(getvalue(hi,-i2), t2) else t2;

 prhi3 = fold i3 = 0 to length
 with t3
 do if prhi2 > getvalue(hi,-i3) then Max(getvalue(hi,-i3), t3) else t3;

 prhi4 = fold i4 = 0 to length
 with t4
 do if prhi3 > getvalue(hi,-i4) then Max(getvalue(hi,-i4), t4) else t4;

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

 prlo1 = fold j1 = 0 to length
 with u1 = big
 do Min(getvalue(lo,-j1), u1);

 prlo2 = fold j2 = 0 to length
 with u2 = big
 do if prlo1 < getvalue(lo,-j2) then Min(getvalue(lo,-j2), u2) else u2;

 prlo3 = fold j3 = 0 to length
 with u3 = big
 do if prlo2 < getvalue(lo,-j3) then Min(getvalue(lo,-j3), u3) else u3;

 prlo4 = fold j4 = 0 to length
 with u4 = big
 do if prlo3 < getvalue(lo,-j4) then Min(getvalue(lo,-j4), u4) else u4;

} else {
 prhi1 = prhi1[1];
 prhi2 = prhi2[1];
 prhi3 = prhi3[1];
 prhi4 = prhi4[1];

 prlo1 = prlo1[1];
 prlo2 = prlo2[1];
 prlo3 = prlo3[1];
 prlo4 = prlo4[1];
}

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

input show_price_levels = yes;
plot zh1 = if show_price_levels then prhi1 else na;
plot zh2 = if show_price_levels then prhi2 else na;
plot zh3 = if show_price_levels then prhi3 else na;
plot zh4 = if show_price_levels then prhi4 else na;
plot zl1 = if show_price_levels then prlo1 else na;
plot zl2 = if show_price_levels then prlo2 else na;
plot zl3 = if show_price_levels then prlo3 else na;
plot zl4 = if show_price_levels then prlo4 else na;

input show_hilo_dots = yes;
plot zhdot = if show_hilo_dots and (prhi1 == hi or prhi2 == hi or prhi3 == hi or prhi4 == hi) then hi else na;
zhdot.SetPaintingStrategy(PaintingStrategy.POINTS);
zhdot.SetDefaultColor(Color.cyan);
zhdot.setlineweight(3);
zhdot.hidebubble();

plot zldot = if show_hilo_dots and (prlo1 == lo or prlo2 == lo or prlo3 == lo or prlo4 == lo) then lo else na;
zldot.SetPaintingStrategy(PaintingStrategy.POINTS);
zldot.SetDefaultColor(Color.cyan);
zldot.setlineweight(3);
zldot.hidebubble();


#AddLabel(yes, "len: " + length, Color.GREEN);
AddLabel(yes, "4th Highest price: " + prhi4, Color.GREEN);

#AddLabel(yes, "len: " + length, Color.GREEN);
AddLabel(yes, "4th Lowest price: " + prlo4, Color.magenta);

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

input test1 = no;
addchartbubble(test1, low*0.998,
prhi1 + "  h1\n" +
prhi2 + "  h2\n" +
prhi3 + "  h3\n" +
prhi4 + "  h4\n" +

prlo4 + "  l4\n" +
prlo3 + "  l3\n" +
prlo2 + "  l2\n" +
prlo1 + "  l1\n"
, color.yellow, no);
#
 

Attachments

  • img2.JPG
    img2.JPG
    65.7 KB · Views: 61

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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