Count Days Since Condition Met

rh19

New member
Is there a way to count the number of days since a condition was met? Example: Trading days since a recent high.

I imagine I need to either somehow get the date the condition was met, and then do CountTradingDays, i.e.

input len = 252
Def RecentHigh = Highest(High, len);
Then get the date the high occurred
AddLabel(yes, CountTradingDays(DateOfRecentHigh, GetYYYYMMDD());

Or get the bar number that the high occurred, and subtract that from the current bar number, but I don't know how to get that either.

Any suggestions?
 
Solution
I do this by initializing a counter when a condition is met:
Code:
def counter = if condition then 1 else counter[1] + 1;
AddLabel(yes, "Days since condition: " + counter, color.gray);

perhaps something like that will work for you, though I'm not sure of your exact implementation.

You can set the counter to zero index by changing the 1 to a 0, of course.

Happy Trading,
mashume
I do this by initializing a counter when a condition is met:
Code:
def counter = if condition then 1 else counter[1] + 1;
AddLabel(yes, "Days since condition: " + counter, color.gray);

perhaps something like that will work for you, though I'm not sure of your exact implementation.

You can set the counter to zero index by changing the 1 to a 0, of course.

Happy Trading,
mashume
 
Solution

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

I do this by initializing a counter when a condition is met:
Code:
def counter = if condition then 1 else counter[1] + 1;
AddLabel(yes, "Days since condition: " + counter, color.gray);

perhaps something like that will work for you, though I'm not sure of your exact implementation.

You can set the counter to zero index by changing the 1 to a 0, of course.

Happy Trading,
mashume
I'm currently just trying to figure out how to count the days since the 52 week high. I tried the counter thing with the code below, but it's just returning a 0, even though today is not the 52 week high.

Input Length = 252;

def RecentHigh = Highest(high, length);
def counter = if RecentHigh then 0 else counter[1] + 1;
AddLabel(yes, "Days since high: " + counter, color.gray);
 
So try it this way:
Code:
input Length = 252;

def bar_number = BarNumber();
def current_bar = highestAll(bar_number) - bar_number;
def most_recent_high = if high > most_recent_high and current_bar <= length then high else most_recent_high[1];
def bars_since_high = if high > most_recent_high and current_bar <= length then 1 else bars_since_high[1] + 1;

or something like that...

mashume
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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