How to determine if an event happened within the last xx bars

Norm6257

New member
VIP
I've tried searching for this answer, but so far I haven't found it...

How do I determine if an event happened within the last xx bars. For example, say I have a MA that crosses over some signal line. In general, how do I determine if that cross happened within the last 10 bars. Something like the below (yes, it's not real code, just pseudo code.)

Code:
def trigger = ma crosses signal within 1 to 10 bars ago
 

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

I've tried searching for this answer, but so far I haven't found it...

How do I determine if an event happened within the last xx bars. For example, say I have a MA that crosses over some signal line. In general, how do I determine if that cross happened within the last 10 bars. Something like the below (yes, it's not real code, just pseudo code.)

Code:
def trigger = ma crosses signal within 1 to 10 bars ago

this will check if a signal occured, a certain quantity of times, in the past few bars.
replace trigger formula with actual one.

Code:
input bars_back = 10;
input min_qty = 1;
def trigger = ma crosses signal ....
def x = if (sum(trigger, bars_back) >= min_qty)  then 1 else 0;
#

https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Math---Trig/Sum
 
I have a related question. Can I find out the bar number that a signal happened?

there are different ways to do things, and the answer will depend on what you want to do.

can have a variable equal to the barnumber of the most recent signal.

def bn = barnumber();
def a = if signal then bn else a[1];

could use a fold loop to read some past value.
 
there are different ways to do things, and the answer will depend on what you want to do.

can have a variable equal to the barnumber of the most recent signal.

def bn = barnumber();
def a = if signal then bn else a[1];

could use a fold loop to read some past value.
will using fold always allow repaint?
 
that question is extremely vague and unanswerable.

anytime data is read from future bars, the data values could change, causing repainting.
FOLD in of itself does not cause repainting.

You see FOLD use recursively with repainting HIGHESTALL() type functions and as @halcyonguy said with repainting future bars.
So it is easy to see why you would wonder if the FOLD operation was causing the repainting.

It is not. It is the other functions that are causing the repainting.
FOLD is just a cleaner simpler method of running through a myriad of conditions.
When those conditions contain repainting logic; there will be repainting.
 
Last edited:
there are different ways to do things, and the answer will depend on what you want to do.

can have a variable equal to the barnumber of the most recent signal.

def bn = barnumber();
def a = if signal then bn else a[1];

could use a fold loop to read some past value.
Thanks a lot! It worked so well for me. I'm impressed also by the simplicity of the code. Best!
 

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
491 Online
Create Post

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