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

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
377 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.
Back
Top