Addorder - Need a little help with my stoploss

Ronin13

Member
hi all

i have an order set as such:

Ruby:
AddOrder(OrderType.BUY_AUTO, RSI crosses above oversold, close, quantity, Color.GREEN, Color.GREEN, name = "BTO");
AddOrder(OrderType.SELL_AUTO, RSI crosses below overbought, close, quantity, Color.RED, Color.RED, name = "STO");

AddOrder(OrderType.SELL_TO_CLOSE, useStopLoss and close < EntryPrice() - stoploss_pts, open[-1], name = "BTO_STP");
AddOrder(OrderType.BUY_TO_CLOSE, useStopLoss and close > EntryPrice() + stoploss_pts, open[-1], name = "STO_STP");

it works, but crudely.

because the closing price naturally overshoots my set stop point (typically set at 10 futures points), triggers my stop addorder stop and closes out my position a some points either above or below my actual intended stop.

is there a way i can refine my stoploss addorders to close me at exactly 10 points above or below my entry?

thanks!
 
So to understand this correctly, you want the close order to always be stoploss_pts away, even if in reality, the price blows through that level and you had no chance at ever getting that price? Have you tried putting EntryPrice +/- stoploss_pts in place of open[-1]?
 
hi @JoeDV , i always set stops at market (as oppose to limit price). at any rate, i trade only highly liquid futures contracts. besides, what you mention is an unavoidable risk with stoploss orders anyhow.

more importantly though, the answer to your other question is yes - i have tried other variations such as EntryPrice +/- stoploss_pts, but with similar result.

have a look:

Ruby:
AddOrder(OrderType.SELL_TO_CLOSE, RSI crosses below overbought or close < EntryPrice() - stoploss_pts, close, quantity, Color.RED, Color.PINK);

AddOrder(OrderType.BUY_TO_CLOSE, RSI crosses above oversold or close > EntryPrice() + stoploss_pts, close, quantity, Color.GREEN, Color.CYAN);

am i missing something or can you spot an issue with my expression of the stop above?

thanks so much
 
I would try to replace the "close" that you indicate for your trade price, with Entryprice()-stoploss_pts. Not sure it will work, but that's what I'd try first. So, you'd have:

Ruby:
AddOrder(OrderType.SELL_TO_CLOSE, RSI crosses below overbought or close < EntryPrice() - stoploss_pts, EntryPrice() - stoploss_pts, quantity, Color.RED, Color.PINK);

AddOrder(OrderType.BUY_TO_CLOSE, RSI crosses above oversold or close > EntryPrice() + stoploss_pts, EntryPrice() + stoploss_pts, quantity, Color.GREEN, Color.CYAN);
 
It is also important to remember that in live trading a stop loss will execute at the moment the price breaks the stop price and take the at the market price for execution. When you use the AddOrder() function in ToS (which remember, never actually trades -- just tests) it checks at the close of a bar whether the condition was met in the last bar and if so executes the 'order' at whatever price you have told it to.

That last bit means you could tell it to execute an order where the sell price was always your entry + $2 if you wanted, regardless of what the market had available. It would look great on the back-test but would never work in the market. ToS doesn't do 'at the market' orders as part of the addOrder() function, it trades at whatever price you tell it to, but always at the close of one bar and the open of the next -- which means if your stop loss was breached during a bar but the close came back above, live trading would have closed your position but AddOrder wouldn't sell since it calculates at the end of the bar. You can come closer by using LOW < EntryPrice() - stoploss, but it will still execute at the end of the bar, not during a bar.

Hope that helps,
mashume
 
It is also important to remember that in live trading a stop loss will execute at the moment the price breaks the stop price and take the at the market price for execution. When you use the AddOrder() function in ToS (which remember, never actually trades -- just tests) it checks at the close of a bar whether the condition was met in the last bar and if so executes the 'order' at whatever price you have told it to.

...

Exactly. That's why I questioned the intent of the original question. Personally I always use either open[-1] or even the midpoint of the next bar as the trade price. I'd rather be conservative and assume a less than ideal fill, then to over inflate results.
 
It is also important to remember that in live trading a stop loss will execute at the moment the price breaks the stop price and take the at the market price for execution. When you use the AddOrder() function in ToS (which remember, never actually trades -- just tests) it checks at the close of a bar whether the condition was met in the last bar and if so executes the 'order' at whatever price you have told it to.

That last bit means you could tell it to execute an order where the sell price was always your entry + $2 if you wanted, regardless of what the market had available. It would look great on the back-test but would never work in the market. ToS doesn't do 'at the market' orders as part of the addOrder() function, it trades at whatever price you tell it to, but always at the close of one bar and the open of the next -- which means if your stop loss was breached during a bar but the close came back above, live trading would have closed your position but AddOrder wouldn't sell since it calculates at the end of the bar. You can come closer by using LOW < EntryPrice() - stoploss, but it will still execute at the end of the bar, not during a bar.

Hope that helps,
mashume
thank you @mashume , your explanation is very helpful indeed.

i did not realize this and thought that i could script in an expression that stops me out like a market order would in live environment. this also certainly confirms that i am getting a stoploss backtest outcome consistent with the limitation of the addorder function.

that said, i will go with your expression of "LOW < EntryPrice() - stoploss" to try narrow the distance arising

thanks again!
 

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
503 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