How To Add "within 2 bars"

Buckbull

Active member
How do I specify "within 2 bars" in thinkscript editor I have a script but cant get into condition wizard . Its not highlighted . So i guess I have to come up with a script to specify that Thanks
 
Solution
to check whether the condition was met yesterday, I would do this:
Code:
plot SmaTouch = triggerHigh[1] or triggerLow[1];
rather than the "within x days" construction.

but that's just me.

0mashume
So let's look at an example to make sense of this. Our example will be:
Close has crossed above the SMA(9) within 2 bars.
Code:
def sma9 = SimpleMovingAvg(price = CLOSE, length = 9);
plot condition_met = if close crosses above sma9 within 2 bars then low else double.nan;
You can use the "within x bars" construct in your code. :-D

-mashume
 
So let's look at an example to make sense of this. Our example will be:
Close has crossed above the SMA(9) within 2 bars.
Code:
def sma9 = SimpleMovingAvg(price = CLOSE, length = 9);
plot condition_met = if close crosses above sma9 within 2 bars then low else double.nan;
You can use the "within x bars" construct in your code. :-D

-mashume
I should have pasted the script I am using sorry I use this script below to let me know when a candle is touching the 21 sma . But I always forget to write them down lol so If i put within 2 candles I can scan the next day to remember what stock were touching the 21 . So how would I add that to my script ? Thanks for your response !

input SMA_Length = 21;

def ma = simpleMovingAvg("length"= Sma_Length);
def triggerLow = open > ma and low <= ma;
def triggerHigh = open < ma and high >= ma;
def MasterTrigger = TriggerHigh and TriggerLow;
plot SmaTouch = triggerHigh or triggerLow;
 
to check whether the condition was met yesterday, I would do this:
Code:
plot SmaTouch = triggerHigh[1] or triggerLow[1];
rather than the "within x days" construction.

but that's just me.

0mashume
 
Solution
So let's look at an example to make sense of this. Our example will be:
Close has crossed above the SMA(9) within 2 bars.
Code:
def sma9 = SimpleMovingAvg(price = CLOSE, length = 9);
plot condition_met = if close crosses above sma9 within 2 bars then low else double.nan;
You can use the "within x bars" construct in your code. :-D

-mashume
That is super handy -- thanks!
 

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
374 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