How to reference bar number of met condition

greco26

Active member
Does anyone know how to get the bar number of a condition that is met so that I can reference it later within the same script?

Ex:
1) Close crosses above pivot high
2) get bar number that crossed pivot high in #1
3) look for condition only after the bar number that crossed pivot high

Thanks, Tim
 
Ruby:
# referencing the 1st post, numbered items

# 1. set a var to a condition, true/false
def cond = ( your condition formula )

# 2.  when cond is true, save the barnumber to a variable.
# keep that number until the next time cond is true.
# setting initial value to a big number, so it won't be < barnumber until it it set.
def cond_bn = if barnumber() == 1 then 99999 else if cond then barnumber() else cond_bn[1];

# 3. check if current bar number is greater than the cond_bn number, and set a variable to true.
def bn_bigger = ( barnumber() > cond_bn );
 
Last edited:
Amazing. I will work this in. The more I get into Thinkscript, the more I realize how little I know. The TOS documentation doesn't really help too much. Thank you much!
agree, the examples on the td thinkscript site are horrible. there are 1000s of code examples on this site to learn from. copy some code ( not nessesarily a complete study) , try it , change it. when you understand it, go find another.
 
Ruby:
# referencing the 1st post, numbered items

# 1. set a var to a condition, true/false
def cond = ( your condition formula )

# 2.  when cond is true, save the barnumber to a variable.
# keep that number until the next time cond is true.
# setting initial value to a big number, so it won't be < barnumber until it it set.
def cond_bn = if barnumber() == 1 then 99999 else if cond then barnumber() else cond_bn[1];

# 3. check if current bar number is greater than the cond_bn number, and set a variable to true.
def bn_bigger = ( barnumber() > cond_bn );

I am interested if this code could be used to detect repainting.

If said condition is true at this bar number, would it be possible to then check if the initial condition remains true, at this bar, downstream? Perhaps using getvalue()?
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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