Close trade after X bars thinkscript strategy?


New member
Hi...newbie here.

Does anyone know of any script I can add to a thinkscript strategy backtest that closes the trade after x amount of bars?

For ex: "After price breaks above 20 day moving average, close trade after 5 bars". Or 3 bars etc...

So I change number of bars to see the strength of a typical breakout per each moveing average breakout or any other strategy. Obviously, the script would replace the "auto buy to close" or auto sel to closel" at the bottom of a TOS strategy. Thanks in advance.
Last edited by a moderator:


@toncuz ToS does have support for the
within X bars

For example, MovAvgExponential()."AvgExp" crosses close within 5 bars. Give that a try and see if you can replicate it to your need. But I'm not sure if what you needed is available in ThinkorSwim.
Last edited:


New member
Thank you...

Sorry...I meant after a buy signal.

"After price breaks above 20 day moving average, buy-to-open, THEN...sell-to-close trade after 5 bars"

Last edited:

Similar threads