Hi,
I've done the Bootcamp lesson 'Buy and Hold with a trailing stop'. It is very clear and useful. But, it sets the trailing stop using the previous day's closing price. I want to create a variant in which the trailing stop is based on the intraday high price. I have been working on it for quite some time now, but have not been able to get that to work - though I suppose it cannot be all that hard. My attempts don't build correctly yet, so I couldn't attach a backtest.
Could anyone please give me some hints on how to change the example Bootcamp solution to minute time resolution? Thanks!
----
Link to Bootcamp page: Creating-a-Trailing-Stop-Loss
Bootcamp solution for daily time resolution, using previous closing price:
class BootCampTask(QCAlgorithm):
# Order ticket for our stop order, Datetime when stop order was last hit
stopMarketTicket = None
stopMarketOrderFillTime = datetime.min
highestSPYPrice = 0
def Initialize(self):
self.SetStartDate(2018, 12, 1)
self.SetEndDate(2018, 12, 10)
self.SetCash(100000)
spy = self.AddEquity("SPY", Resolution.Daily)
spy.SetDataNormalizationMode(DataNormalizationMode.Raw)
def OnData(self, data):
if (self.Time - self.stopMarketOrderFillTime).days < 15:
return
if not self.Portfolio.Invested:
self.MarketOrder("SPY", 500)
self.stopMarketTicket = self.StopMarketOrder("SPY", -500, 0.9 * self.Securities["SPY"].Close)
else:
#1. Check if the SPY price is higher that highestSPYPrice.
if self.Securities["SPY"].Close > self.highestSPYPrice:
#2. Save the new high to highestSPYPrice; then update the stop price to 90% of highestSPYPrice
self.highestSPYPrice = self.Securities["SPY"].Close
updateFields = UpdateOrderFields()
updateFields.StopPrice = self.highestSPYPrice * 0.9
self.stopMarketTicket.Update(updateFields)
#3. Print the new stop price with Debug()
self.Debug("SPY: " + str(self.highestSPYPrice) + " Stop: " + str(updateFields.StopPrice))
def OnOrderEvent(self, orderEvent):
if orderEvent.Status != OrderStatus.Filled:
return
if self.stopMarketTicket is not None and self.stopMarketTicket.OrderId == orderEvent.OrderId:
self.stopMarketOrderFillTime = self.Time
Alethea Lin
Hi Bert,
You would need to change the data resolution to Minute to receive minute bars. Please see the attached backtest, where I have set the trailing stop price to be 99% of the high price in a minute bar because price change would be much smaller on the minute level. I have also changed the time-passed to 15 minutes since we are working with intraday. Feel free to play around with the code! Please note that this will not work in Bootcamp because the validator for Bootcamp is set to only verify results for the intended data resolution.
We are glad that you found the Bootcamp helpful. Hope this helps!
Bert Vaughan Merrick
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!