Ensure exit is not on same candle as the entry, also trading hours?


New member
Hello All,
If I have an 'entry' criteria and a 'exit' criteria at the same time my strat will enter and exit at the same candle, how can I force it to at least wait till the next candle before exiting ?

, open[-1],
stock_quantity, Color.GREEN, Color.GREEN);
# I want like a IF here so it will skip the sell if it just bought on this candle
open[-1], stock_quantity, Color.RED, Color.RED);

I want to make the 'sell to close' only fire if the 'buy to open' has NOT fired for that candle, so it will fire on the next candle. I've simplified my code it actually looks at an RSI but that's irrelevant.

Also does anyone know how to make it so it will only enter or exit orders during normal market hours, and extra bonus plus if you can tell me how to make it exit any current orders at the end of the day.

Thanks for your time,
Last edited by a moderator:
input usetimefilter = Yes;
input rthopen = 0930;
input rthclose = 1031;
def RTH = if usetimefilter == yes then if SecondsFromTime(rthopen) >= 0 and
SecondsTillTime(rthclose) >= 0
then 1
else 0 else 1;

AddOrder(OrderType.BUY_TO_OPEN, RTH and BuySignal[-1], price = buyorderprice, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "CDS_LE_G");

Set the time you wish to trade
I generally define my buy and sell criteria and then compare to make sure there wasn't a buy during the same iteration of the script code... Usually something like "if sell and !buy", or something to that effect...
#Method One
AddOrder(OrderType.SELL_TO_CLOSE, DownSignal and !UpSignal, open[0], 100, Color.ORANGE, Color.ORANGE, "SS1 @ " + open[0]);

#Method Two
def SellSignalTwo = if close[-1] < high[-1] - HighStopLoss and !BuySignalOne then 1 else 0;

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