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?


Well-known member
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...


New member
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.


New member
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.

Similar threads