Find the most recent bar with a certain quality

temtam

New member
I'm basically trying to ask ThinkScript "What is the most recent bar where THIS condition was true?"

Probably simple solution but I can't seem to figure it out.

Solution
I'm not super familiar with the getValue function. It is always returning 1 in this case, is it because of the first argument? In the examples I've seen, the first argument is usually something like "close" or "high", e.g a value for that bar.

Using @halcyonguy 's code above, the following has a 'cond' example to help identify the highestall barnumber and high based upon the 'cond'.

Ruby:
``````def bn = BarNumber();
def cond = if close crosses above Average(close, 9) then bn else cond[1];

# this finds the highest barnumber , from a set of bars that have cond
def last_cond_bn = HighestAll(cond);

# read data from that bar, for example the high
def last_cond_hi = GetValue(high, (bn - last_cond_bn));

you can use highestall( ) to find the biggest barnumber for the bar that the condition is true. you can put an if-then in a highestall( )

Code:
``````def bn = barnumber();
def cond = .....
# this finds the biggest barnumber , from a set of bars that have cond == 1
def last_cond_bn = highestAll( if cond == 1 then bn else 0);
#
#  this is true on the last bar that cond is true
def islast_cond = (last_cond_bn == bn);
#
# read data from that bar
#  i think this works...
def data = getvalue(cond, (bn - last_cond_bn) );
#``````

you can use highestall( ) to find the biggest barnumber for the bar that the condition is true. you can put an if-then in a highestall( )

Code:
``````def bn = barnumber();
def cond = .....
# this finds the biggest barnumber , from a set of bars that have cond == 1
def last_cond_bn = highestAll( if cond == 1 then bn else 0);
#
#  this is true on the last bar that cond is true
def islast_cond = (last_cond_bn == bn);
#
# read data from that bar
#  i think this works...
def data = getvalue(cond, (bn - last_cond_bn) );
#``````

I'm not super familiar with the getValue function. It is always returning 1 in this case, is it because of the first argument? In the examples I've seen, the first argument is usually something like "close" or "high", e.g a value for that bar.

I'm not super familiar with the getValue function. It is always returning 1 in this case, is it because of the first argument? In the examples I've seen, the first argument is usually something like "close" or "high", e.g a value for that bar.

Using @halcyonguy 's code above, the following has a 'cond' example to help identify the highestall barnumber and high based upon the 'cond'.

Ruby:
``````def bn = BarNumber();
def cond = if close crosses above Average(close, 9) then bn else cond[1];

# this finds the highest barnumber , from a set of bars that have cond
def last_cond_bn = HighestAll(cond);

# read data from that bar, for example the high
def last_cond_hi = GetValue(high, (bn - last_cond_bn));

AddLabel(1, last_cond_bn + " " + last_cond_hi, Color.WHITE);
#``````

I tired to decode the guidance above to solve my similar problem but failed. I use the code below to find a low (MACDValleyPrice ), but also need to find out which bar has that low.

Ruby:
``````def MACDValleyPrice = If(!IsNaN(MACDValley[0]), Lowest(low[-1], maxLookback4Price + 1), Double.NaN);

AddChartBubble(MACDValleyPrice && !lowerWindow, MACDValleyPrice, "bar " + barnumber() + "   low" + MACDValleyPrice, Color.PINK, no);``````

I tired to copy the code above. I made
Ruby:
`` Lowest(low[-1], maxLookback4Price + 1)``
my cond

Ruby:
``````# this finds the highest barnumber , from a set of bars that have cond
def last_cond_bn = HighestAll(cond);``````

but don't get a barnumber for the local low value

I get the correct low but don't know how to get the barnumber for the low. I could fold back until i find it, but that seems like a lot of work for something TOS already found

example image

Last edited by a moderator:
@Cre8able The barnumber logic is in the post above yours. It appears as a label but the same logic can be used in your bubble.
As you have only shown snippets of your code and not a complete script, it is not possible for anyone to assist further.

Please give back to the community, by coming back and give us an update

Last edited:
thanks. I was looking at he wrong version above. One gives a bar number and the other gives a price. I was using the wrong one
thanks

thanks. I was looking at he wrong version above. One gives a bar number and the other gives a price. I was using the wrong one
thanks
Cre8able,

would it be possible to share the complete version of the code you got to work for finding bar that met a specific condition?

regards,

Not the exact question you're looking for?

87k+ Posts
331 Online

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?