ThinkScript count number of bars between lows

british43

Member
VIP
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" );
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.
 

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

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?
 
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.).
 
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:
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 ;
 
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.
 
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
@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.
 
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;
 
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" );
How to label number of bars between any two bar I choose? I select one bar as starting point, another bar as ending point. Then label number of bar between the starting and ending points.
 
Since this is currently one of the highest ranking google results for "how to count number of bars thinkscript", i'm going to post the code that works below. This will automatically count the number of bars as you switch between aggregation periods without needing to update inputs. If I have problems with it, I will come back in and update.

Code:
input Show = no;
def days = CompoundValue(1,
                            if GetDay() != GetDay()[1]
                                then days[1]+1
                                else days[1],1);

plot dd = if Show then days else Double.NaN;
AddLabel(Show,dd,color.white);
AddChartBubble(Show,dd,dd,color.white);
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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