• Get $40 off VIP by signing up for a free account! Sign Up

How to create stop loss based on lowest/highest of last 3 bars?


New member
Hi all, new to Thinkscript. I am working on a strategy in TS where I have my normal long/short exits defined, but I would like to also add stop losses. In particular, I want to close my longs if the current price goes below the the low of any the preceding 3 bars and cover my shorts if the current price goes above the high of any of the preceding bars. I seem to be stuck because I can't find any value that's just "current price" or even "last".

I think the function I want for defining my stop price is "Lowest(low,3)" and "Highest(high,3)", but can't figure out the right trigger to get the orders to actually be incorporated into the code. I tried just entering them with the same conditions I have for my BUY_AUTO and SELL_AUTO orders, but that didn't work.

I'm sure I'm missing something really simple here, but I can't quite figure it out. Can someone point me in the right direction?
So you want to compare the the last three bars...??? How about comparing to close[1], close[2], and close[3]...??? No need making it more complicated than necessary... I've been seeing a lot of that...
Well, I can't get it to work with that either :p

But I'm saying 3 now, I really want to be able to check the last "N", so I'd like to use code more robust than manually calling out each individual one. But, again, it doesn't seem to be working with that either, so I'm clearly doing something else wrong.
Trying to run a ThinkScript backtest strategy at the moment,but I want to adapt that to be live trading once I get it to match what I've been doing manually.

The other thing I'm noticing is that even when I can get it to trigger a stop, it only executes once the entire candle has printed, which sort of negates part of the point. I need the stop to be running such that anytime the current price drops below a sell-to-close stop or above a buy-to-close stop, it closes the trade.

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
328 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.