Overall Statistics
Total Trades
13
Average Win
0%
Average Loss
-1.64%
Compounding Annual Return
-94.281%
Drawdown
10.000%
Expectancy
-1
Net Profit
-7.540%
Sharpe Ratio
-8.088
Loss Rate
100%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
-2.851
Beta
-0.566
Annual Standard Deviation
0.266
Annual Variance
0.071
Information Ratio
-2.235
Tracking Error
0.412
Treynor Ratio
3.807
Total Fees
$32.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.Minute)
        spy.SetDataNormalizationMode(DataNormalizationMode.Raw)
    
    def OnData(self, data):
        self.Debug(f'Minute Bar High: {self.Securities["SPY"].High}')
        if (self.Time - self.stopMarketOrderFillTime).seconds/60 < 15:
            return
        
        if not self.Portfolio.Invested:
            self.MarketOrder("SPY", 500)
            self.stopMarketTicket = self.StopMarketOrder("SPY", -500, 0.99 * self.Securities["SPY"].High)
        
        else:
        
            #1. Check if the SPY price is higher that highestSPYPrice.
            if self.Securities["SPY"].High > self.highestSPYPrice:
                #2. Save the new high to highestSPYPrice; then update the stop price to 90% of highestSPYPrice
                self.highestSPYPrice = self.Securities["SPY"].High
                updateFields = UpdateOrderFields()
                updateFields.StopPrice = self.highestSPYPrice * 0.99
                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