# Coding a stop-loss into a strategy?

#### joetimismith

##### New member
I want the strategy to sell at the open[0] IF the open[0] is less than 75% of (close[1]-open[1]), ALSO if the intraday price of candle[0] crosses below that 75% of (close[1]-open[1]) level, sell (at the close of that HOURLY candle). If the open[0] is greater than that level, sell at the close[0]. Thanks!

Last edited:
I want the strategy to sell at the open[0] IF the open[0] is less than 75% of (close[1]-open[1]), ALSO if the intraday price of candle[0] crosses below that 75% of (close[1]-open[1]) level, sell (at the close of that HOURLY candle). If the open[0] is greater than that level, sell at the close[0]. Thanks!

I'm assuming you're aware TOS strategies don't create any real orders and are only used for testing.

I'm guessing you meant `close[1] - (close[1]-open[1])` rather than just `close[1]-open[1]`. If open[1] is 100 and close[1] is 110 then close[1]-open[1] is 10. 75% of that is 7.50. I don't think you want to wait for price to get below \$7.50 before you exit.

Additionally, unless other rules in your strategy eliminate this case, you need to consider that not every candle is either green or an exit. close[1] may not be greater than open[1] and therefore you'd be subtracting a negative number which is equivalent to addition. If open[1] is 110 and close[1] is 100 then close[1]-open[1] is -10. 75% of that is -7.50. Subtracted from close[1], your stop loss would become 107.50, which is above close[1] of 100. You can use the `Max` and `Min` functions to solve this.

It's good you've considered what these rules would look like in code. As a next step, I'd encourage you to try getting a plot working to show where your stop loss should be. It's always easier to build and test these things with a plot first and then use AddOrder functionality once the plot works correctly.

A hint... to have intraday charts know the open and close of the daily chart you need to specify the period when referencing open/high/low/close, such as `open(period=AggregationPeriod.DAY)`.

87k+ Posts
345 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?