Overall Statistics
Total Trades
32
Average Win
0.07%
Average Loss
-0.19%
Compounding Annual Return
-0.809%
Drawdown
2.900%
Expectancy
-0.498
Net Profit
-1.615%
Sharpe Ratio
-0.875
Probabilistic Sharpe Ratio
0.098%
Loss Rate
62%
Win Rate
38%
Profit-Loss Ratio
0.34
Alpha
-0
Beta
-0.028
Annual Standard Deviation
0.006
Annual Variance
0
Information Ratio
-0.868
Tracking Error
0.217
Treynor Ratio
0.199
Total Fees
$32.00
Estimated Strategy Capacity
$900000000.00
Lowest Capacity Asset
SPY R735QTJ8XC9X
# region imports
from AlgorithmImports import *
# endregion

class UpgradedYellowScorpion(QCAlgorithm):
   
    stopMarketTicket = None
    stopMarketOrderFillTime = datetime.min
    highestSPYPrice = -1

    def Initialize(self):

        self.SetStartDate(2020, 1, 1)
        self.SetEndDate(2022, 1, 1)
        self.SetCash(10000)
        spy = self.AddEquity("SPY" , Resolution.Daily)
        spy.SetDataNormalizationMode(DataNormalizationMode.Raw)
        self.Pair = "SPY"
        self.symbols = [self.Pair]
        self.prevPrices = { symbol : RollingWindow[TradeBar](7) for symbol in self.symbols }
        self.Long = None
        self.Short = None

    def OnData(self, data):

        for symbol in self.symbols:
            if data.ContainsKey(symbol):
                self.prevPrices[symbol].Add( data[symbol] )

        if not all([ window.IsReady for window in self.prevPrices.values() ]):
            return
        
        Pair1_window = self.prevPrices[self.Pair]
        Pair1_1D = Pair1_window[1].Close
        Pair1_0D = Pair1_window[0].Close

# short position
        if not self.Portfolio.Invested and Pair1_0D > Pair1_1D:
            self.Short = self.MarketOrder("SPY", -1)
            self.stopMarketTicket = self.StopMarketOrder("SPY", 1, 1.1 * self.Securities["SPY"].Close)
        
        if self.Short is not None and self.Securities ["SPY"].Close < self.highestSPYPrice:
            self.highestSPYPrice = self.Securities ["SPY"].Close
            updateFields = UpdateOrderFields()
            updateFields.StopPrice = self.highestSPYPrice * 1.1
            self.stopMarketTicket.Update(updateFields)

# long position
        if not self.Portfolio.Invested and Pair1_0D < Pair1_1D:
            self.Long = self.MarketOrder("SPY", 1)
            self.stopMarketTicket = self.StopMarketOrder("SPY", -1, 0.9 * self.Securities["SPY"].Close)

        if self.Long is not None and 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