Skip to content

Instantly share code, notes, and snippets.

@Sanyambansal76
Last active September 13, 2020 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sanyambansal76/4e51b93f8080420b78cd82ae8e39d781 to your computer and use it in GitHub Desktop.
Save Sanyambansal76/4e51b93f8080420b78cd82ae8e39d781 to your computer and use it in GitHub Desktop.
Trailing Stop Loss Order - Quantconnect - BootCamp
class BootCampTask(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(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):
# 1. Plot the current SPY price to "Data Chart" on series "Asset Price"
self.Plot("Levels", "Asset Price", self.Securities["SPY"].Price)
self.Plot("Levels", "Stop Price", self.Securities["SPY"].Price * 0.9)
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
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment