Overall Statistics
Total Trades
1
Average Win
0%
Average Loss
0%
Compounding Annual Return
-95.522%
Drawdown
8.400%
Expectancy
0
Net Profit
-8.158%
Sharpe Ratio
-3.276
Probabilistic Sharpe Ratio
0.000%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
-1.021
Beta
-0.126
Annual Standard Deviation
0.284
Annual Variance
0.08
Information Ratio
-0.504
Tracking Error
0.385
Treynor Ratio
7.394
Total Fees
$2.50
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)
            self.Debug("original stop price: {}".format(self.stopMarketTicket.Get(OrderField.StopPrice)))
        
        else:
            
            #1. Check if the SPY price is higher that highestSPYPrice.
            if self.Securities["SPY"].Close > self.highestSPYPrice:
                
                self.Debug("stop price before update: {}".format(self.stopMarketTicket.Get(OrderField.StopPrice)))
                
                #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.Securities["SPY"].Close * 0.9
                self.stopMarketTicket.Update(updateFields)
                self.Debug("new stop price should be: {}".format(self.Securities["SPY"].Close * 0.9))
                
                # alternative way to update?
                self.stopMarketTicket.UpdateStopPrice(self.Securities["SPY"].Close * 0.9)
                
                #3. Print the new stop price with Debug()
            self.Debug("stop price after update: {}".format(self.stopMarketTicket.Get(OrderField.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