Back

Trailing Stops

Hello,

The boot camp for the trailing stop loss was helpful. Great bootcamp! I was also able to modify the code from the boot camp to buy with this type of code.

I am having difficulties combining a trailing stop buy with trailing stop sell. I basically want to place the trailing stop sell as soon as the trailing stop buy fills. Can anyone help rustle up some code for this?

Thanks!

class BootCampTask(QCAlgorithm):

# Order ticket for our stop order, Datetime when stop order was last hit
stopMarketTicket = None
stopMarketOrderFillTime = datetime.min
highestSPYPrice = -1
lowestSPYPrice = 9999999


def Initialize(self):
self.SetStartDate(2018, 12, 1)
self.SetEndDate(2018, 12, 10)
self.SetCash(100000)
spy = self.AddEquity("SPY", Resolution.Minute)
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 < 1:
return

if not self.Portfolio.Invested:
self.stopMarketTicket = self.StopMarketOrder("SPY", 500, 1.002 * self.Securities["SPY"].Close)
#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.lowestSPYPrice:

self.lowestSPYPrice = self.Securities["SPY"].Close
updateFields = UpdateOrderFields()
updateFields.StopPrice = self.lowestSPYPrice * 1.002
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

else:
self.stopMarketTicket = self.StopMarketOrder("SPY", -500, 0.998 * self.Securities["SPY"].Close)



#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.998
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

 

Update Backtest







0

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.


Hey PK,

I've changed your algorithm to place a trailing stop loss as soon as the trailing stop buy fills.
I've also changed some things around to improve the logic. Read through my comments to see how each part works.
If you have any further questions, please don't hesitate to ask.

Best
Rahul

1

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.


Thank you!

0

Update Backtest





0

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.


Loading...

This discussion is closed