Following instructions from this link, I'm trying to create a stop loss order.
However, the order self.ticket created in on_data event doesn't seem to be found in the on_order_event. Could anyone please shed some light on this?
# region imports
from AlgorithmImports import *
# endregion
class TestStopLoss(QCAlgorithm):
def initialize(self):
# Locally Lean installs free sample data, to download more data please visit https://www.quantconnect.com/docs/v2/lean-cli/datasets/downloading-data
self.set_start_date(2013, 10, 7) # Set Start Date
self.set_end_date(2013, 10, 11) # Set End Date
self.set_cash(100000) # Set Strategy Cash
self.add_equity("SPY", Resolution.MINUTE)
def on_data(self, slice: Slice):
self.ticket = self.market_order("SPY", 1)
# Get the fill price from the OnOrderEvent event handler
def on_order_event(self, order_event: OrderEvent) -> None:
if order_event.status == OrderStatus.FILLED and self.ticket.order_id == order_event.order_id:
fill_price = order_event.fill_price
self.debug(f"Order filled at {fill_price}")
Mia Alissi
Key improvements:
This structure will reliably link your entry and stop-loss orders.
Sources: Order Tickets - QuantConnect.com, Stop Limit Order Regression Algorithm, Trailing Stop Orders - QuantConnect.com, Opening Range Breakout for Stocks in Play, Key Concepts - QuantConnect.com
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Moho wu
Adding a self.ticket is not None certainly doesn't result in any error. But it never gets into this if statement. So the entry_ticket never gets matched in the on_order_event.
It's very frustrating that even these official examples don't work out of the box.
BTW, where's the Downvote button for these AI responses?
Moho wu
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!