Hi,
I have a question while I'm following the bootcamp video.
It is on the "Buy & Hold with a Trailing Stop Tutorial - Managing Order Risk in QuantConnect" https://www.youtube.com/watch?v=oJstb5nMSZg&list=PLD7-B3LE6mz5jsEb127kdyJVMJrBNfbmI&index=4.
Below is the copy of the code
---------
from AlgorithmImports import *
class T03BuyAndHoldWithATrailStop(QCAlgorithm):
stopMarketTicket=None
stopMarketFilledTime=datetime.min
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.SetStartDate(2018, 12, 1) # Set Start Date
self.SetEndDate(2019, 4, 1) # Set End Date
self.SetCash(100000) # Set Strategy Cash
self.spy=self.AddEquity("SPY", Resolution.Minute)
self.spy.SetDataNormalizationMode(DataNormalizationMode.Raw)
self.lastOrderEvent = None
def OnData(self, data: Slice):
if not self.Portfolio.Invested:
self.stopMarketTicket=self.StopMarketOrder("SPY", -500, 0.90*self.Securities["SPY"].Close)
self.MarketOrder("SPY", 500)
-----------
I don't understand how "StopMarketOrder" is running in this example. If "MarketOrder" is filled, then I'm expecting that self.Portfolio.Invested becomes true and then StopMarketOrder should not run.
Is there anyone who can teach me how "StopMarketOrder" is running ?
Mia Alissi
Hi Sangkook,
In the code you provided, the
StopMarketOrder
is indeed placed when the portfolio is not invested. However, it's important to note that theStopMarketOrder
andMarketOrder
are placed almost simultaneously, and theStopMarketOrder
does not require the portfolio to be not invested to be placed.Here's how it works:
When the portfolio is not invested, the algorithm first places a
StopMarketOrder
. This order will only be filled if the price of "SPY" falls to 90% of the current closing price. This order essentially acts as a stop loss to limit potential losses.Immediately after placing the
StopMarketOrder
, the algorithm places aMarketOrder
to buy 500 shares of "SPY". This order is filled at the next available market price.After these orders are placed,
self.Portfolio.Invested
will indeed beTrue
, but this does not affect theStopMarketOrder
that has already been placed. If the price of "SPY" falls to the stop price, theStopMarketOrder
will be filled, selling the 500 shares that were bought.So, in summary, the
StopMarketOrder
is a protective order placed at the same time as theMarketOrder
to limit potential losses if the price of "SPY" falls.You can refer to the following knowledge sources for more information:
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.
Uhahahaha
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!