Overall Statistics
Total Trades
1
Average Win
0%
Average Loss
0%
Compounding Annual Return
-24.445%
Drawdown
0.400%
Expectancy
0
Net Profit
-0.383%
Sharpe Ratio
-8.559
Probabilistic Sharpe Ratio
16.236%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
-0.074
Beta
0.196
Annual Standard Deviation
0.025
Annual Variance
0.001
Information Ratio
4.994
Tracking Error
0.102
Treynor Ratio
-1.098
Total Fees
$2.50
Estimated Strategy Capacity
$4300000000.00
Lowest Capacity Asset
SPY R735QTJ8XC9X
#region imports
from AlgorithmImports import *
#endregion
class DancingMagentaGoshawk(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(2022, 4, 4)
        self.SetEndDate(2022, 4, 8)
        self.SetCash(1000000)
        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", self.Securities["SPY"].Price)
        
        if (self.Time - self.stopMarketOrderFillTime).days < 2:
            return

        if not self.Portfolio.Invested:
            self.MarketOrder("SPY", 500)
            self.stopMarketTicket = self.StopMarketOrder("SPY", -500, 0.95 * 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.Securities["SPY"].Price * 0.9)
            
            #1. Check if the SPY price is higher that highestSPYPrice.
            if self.Securities['SPY'].Close > self.highestSPYPrice:
                #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.95
                self.stopMarketTicket.Update(updateFields)
                #3. Print the new stop price with Debug()
                self.Debug(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