• Get $30 off VIP with code SUMMER30. Ends July 27. Happy trading!

ThinkScript count number of bars between lows

british43

Member
Can someone please help with change this code to show bars between lows. Thanks.

Code:
def peak = high > Highest(high[1], 5) and high > Highest(high[-5], 5);

# count the number of bars since the last peak
def count = if peak[1] then 1 else count[1] + 1;

# mark each peak with a down arrow
plot peakArrow = peak;
     peakArrow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

# show the number of bars (the count) since the last peak
AddChartBubble(peak, high + 1, count, Color.WHITE);

I'm not sure what I'm doing wrong but I'm trying to look for how many bull or bear bars exist within a timeframe.

Code:
#price as % of H-L range with coloring above 50% and below 25%
input price = close;
input lenght = 5;
def RPR = round((price – low) / (high – low) *100);

plot Bull_Trend_Bar= RPR > 50;
plot Bear_Trend_Bar= RPR < 25;

#def countup = if Bull_Trend_Bar == lenght
            #then BarNumber()
             # else countup[1];

#AddLabel(1, "Count = " + countup, Color.CYAN);


# count the number of bars since the last peak
def count = if RPR[1] then 1 else count[1] + 1;

#def count1 = if Bull_Trend_Bar[1] then 1 else count1[1] + 1;

addlabel(yes, concat("Bull Trend Bar: " + RPR, "%"), if RPR > 50 then color.green else color.current);

addlabel(yes, concat("Bear Trend Bar: " + RPR, "%"), if RPR < 25 then color.red else color.current);

addlabel(yes, concat("Bull Trend Bar: " +  Bull_Trend_Bar, ""), if  Bull_Trend_Bar is true then color.green else color.current);

addlabel(yes, concat("Bear Trend Bar: " +  Bear_Trend_Bar, ""), if  Bear_Trend_Bar is true then color.red else color.current);

#plot x =1;
 
Solution
You just had to slightly alter the example i gave you in that link.

Label for identifying the bar number (barnumber) for the lowest low within XYZ bars ago

Code:
#Label for identifying the bar number of the lowest low within XYZ bars ago
#by XeoNoX via usethinkscript.com
input barsago = 16;
def bar = barnumber();
def lowestlow = lowest(low,barsago);
def lowestbar = if low == lowestlow then bar else lowestbar[1];

AddLabel (yes, "Lowest Low within " + barsago + " bars is barnumber " +  (lowestbar)  );
AddLabel (yes, "Lowest Low within " + barsago + " Bars Happened " +(bar-lowestbar) + " bars ago" );

dj45

New member
I can use Highest or Lowest to return highest or lowest value in the last x bars. But how do I identify which bar (or more specifically, how many bars ago) was that high or low? Must be something more convenient than running a loop with counter = x to check for equality of the bar high with the value returned from Highest.
 

rad14733

Well-known member
VIP
I can use Highest or Lowest to return highest or lowest value in the last x bars. But how do I identify which bar (or more specifically, how many bars ago) was that high or low? Must be something more convenient than running a loop with counter = x to check for equality of the bar high with the value returned from Highest.

Check out:

https://tlc.thinkorswim.com/center/reference/Tech-Indicators/studies-library/G-L/LookUpHighest

https://tlc.thinkorswim.com/center/reference/Tech-Indicators/studies-library/G-L/LookUpLowest
 

XeoNoX

Well-known member
VIP
I can use Highest or Lowest to return highest or lowest value in the last x bars. But how do I identify which bar (or more specifically, how many bars ago) was that high or low? Must be something more convenient than running a loop with counter = x to check for equality of the bar high with the value returned from Highest.
your trying to get how many bars ago the lowest priced bar occurred during the current trading day?
 

dj45

New member
your trying to get how many bars ago the lowest priced bar occurred during the current trading day?
Yes, how many bars ago is the lowest (or highest) priced bar in the past x bars (I want this to work on any time interval - 5 min bars, 15 min bars, daily bars, weekly bars, etc.).
 

dj45

New member
I can use Highest or Lowest to return highest or lowest value in the last x bars. But how do I identify which bar (or more specifically, how many bars ago) was that high or low? Must be something more convenient than running a loop with counter = x to check for equality of the bar high with the value returned from Highest.
Thinking about this a bit more, running a loop counter as I suggested earlier is horribly inefficient since it would be running that loop on every bar. A better approach would be to start/reset a counter every time a bar's high exceeded a prior bar's high, and then increment that counter for every bar that does not exceed that high bar. So on any bar, you know how many bars ago the high bar is. Haven't written it out, but seems this approach would work and not be so resource intensive.

But it seems to me that there is probably an even easier method. With the various functions that look for bar characteristics such as various prices and volume, there might be a built-in function that returns the bar number.

Here's the gist of what I'm trying to do. Let's say I have a signal I'm looking for (high volume reversal bar), but I want to narrow down to only those signals that occur a minimum of x bars after a local high (local high I would define as highest high in last y bars). I could use RSI or MACD to approximate this, but those are very crude approximations unless I start playing around with parameters of those functions.
 
Last edited:

XeoNoX

Well-known member
VIP
you stated:
"how many bars ago is the lowest (or highest) priced bar in the past x bars"
so in your case it would be

Highest High price within past 60 bars
Code:
input barsago = 60;
def HighestPrice =  Highest(high, barsago);
plot scan = HighestPrice;

Lowest Low price within past 60 bars
Code:
#
input barsago = 60;
def Lowestprice =  lowest(low, barsago);
plot scan = Lowestprice ;
 

dj45

New member
you stated:
"how many bars ago is the lowest (or highest) priced bar in the past x bars"
so in your case it would be

Highest High price within past 60 bars
Code:
input barsago = 60;
def HighestPrice =  Highest(high, barsago);
plot scan = HighestPrice;

Lowest Low price within past 60 bars
Code:
#
input barsago = 60;
def Lowestprice =  lowest(low, barsago);
plot scan = Lowestprice ;
Unless I'm missing something, this code returns the highest (or lowest) price within the last 60 bars. It does not return which bar that is <-- that's what I'm looking for.

you stated:
"how many bars ago is the lowest (or highest) priced bar in the past x bars"
Exactly! How many bars ago, not what is the highest (or lowest) price of that bar. Hopefully I'm being clear, but to further clarify, say the highest high of the last 60 bars is 100 and this bar is 50 bars ago, I want the function (or subscript) to return 50.
 

XeoNoX

Well-known member
VIP
You just had to slightly alter the example i gave you in that link.

Label for identifying the bar number (barnumber) for the lowest low within XYZ bars ago

Code:
#Label for identifying the bar number of the lowest low within XYZ bars ago
#by XeoNoX via usethinkscript.com
input barsago = 16;
def bar = barnumber();
def lowestlow = lowest(low,barsago);
def lowestbar = if low == lowestlow then bar else lowestbar[1];

AddLabel (yes, "Lowest Low within " + barsago + " bars is barnumber " +  (lowestbar)  );
AddLabel (yes, "Lowest Low within " + barsago + " Bars Happened " +(bar-lowestbar) + " bars ago" );
 
Solution

rad14733

Well-known member
VIP
@XeoNoX That was kinda what I was going to work on but am about two days behind... Simply keep a resetting bar counter... I was going to have it reset to 1 (or zero) every time there was a new high/low and count up from there...
 

XeoNoX

Well-known member
VIP
@XeoNoX That was kinda what I was going to work on but am about two days behind... Simply keep a resetting bar counter... I was going to have it reset to 1 (or zero) every time there was a new high/low and count up from there...
ive been working on a nice study similar to what you mentioned, its pretty much done, im just making small adjustments, i normally test it for a week or so before i release.
 

jc085

New member
Unless I'm missing something, this code returns the highest (or lowest) price within the last 60 bars. It does not return which bar that is <-- that's what I'm looking for.

Exactly! How many bars ago, not what is the highest (or lowest) price of that bar. Hopefully I'm being clear, but to further clarify, say the highest high of the last 60 bars is 100 and this bar is 50 bars ago, I want the function (or subscript) to return 50.
I've been working in similar need, I could solve it finding the offset of the lowest low from X bars ago, for this, we can use getminValueOffset, then you just make the arithmetics:


X is an integer number

def bn = barnumber();
def minoff = getminValueOffset(low, X);
def bn_at_lowest = bn - minoff;
 
Top