Hello, I'm new to QuantConnect and Quant programming. I have completed the Boot Camp lessons, which i think were great! So to get started on my own, i created a new file, changed the TrainStop w Plot class name and tried to run it as a new project out side of the BootCamp and the result was just a flat line on the graph and only the setcash price on the dashboard.
Here is the algorithm:
///////////////////////////////////////////////////////////////////////////////////////
class BCTask_BuyHoldTrailStop(QCAlgorithm):
# Order ticket for our stop order, Datetime when stop order was last hit
stopMarketTicket = None
stopMarketOrderFillTime = datetime.min
highestSPYPrice = -1
def Initialize(self):
self.SetStartDate(2020, 1, 1)
self.SetEndDate(2020, 12, 30)
self.SetCash(100000)
spy = self.AddEquity("SPY", Resolution.Daily)
spy.SetDataNormalizationMode(DataNormalizationMode.Raw)
def OnData(self, data):
# 1. Plot the current SPY price to "Data Chart" on series "Asset Price"
self.Plot("Data Chart", "Asset Price", data["SPY"].Close)
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:
#2. Plot the moving stop price on "Data Chart" with "Stop Price" series name
self.Plot("Data Chart", "Stop Price", self.stopMarketTicket.Get(OrderField.StopPrice))
if self.Securities["SPY"].Close > self.highestSPYPrice:
self.highestSPYPrice = self.Securities["SPY"].Close
updateFields = UpdateOrderFields()
updateFields.StopPrice = self.highestSPYPrice * 0.9
self.stopMarketTicket.Update(updateFields)
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
Any help will be greatly appreciated.
Coop
Derek Melchin
Hi Coop,
We were unable to reproduce the error. The attached backtest shows several successful trades.
Best,
Derek Melchin
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.
Coop
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!