Overall Statistics
Total Trades
47
Average Win
0.44%
Average Loss
-0.17%
Compounding Annual Return
0.254%
Drawdown
1.400%
Expectancy
0.565
Net Profit
3.095%
Sharpe Ratio
0.537
Probabilistic Sharpe Ratio
3.164%
Loss Rate
57%
Win Rate
43%
Profit-Loss Ratio
2.60
Alpha
-0.001
Beta
0.021
Annual Standard Deviation
0.003
Annual Variance
0
Information Ratio
-0.803
Tracking Error
0.138
Treynor Ratio
0.083
Total Fees
$47.00
Estimated Strategy Capacity
$31000000000.00
Lowest Capacity Asset
SPY R735QTJ8XC9X
# region imports
from AlgorithmImports import *
# endregion

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

    def Initialize(self):

        self.SetStartDate(2010, 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
        chart = Chart("Enter")
        self.AddChart(chart)
        chart.AddSeries(Series("update order", SeriesType.Scatter, "$", Color.Green, ScatterMarkerSymbol.Triangle))
        chart.AddSeries(Series("Buying and setting stop loss", SeriesType.Scatter, "$", Color.Red, ScatterMarkerSymbol.Triangle))


    def OnData(self, data):
        self.Plot("Invested", "active or not", len([x.Key for x in self.Portfolio if x.Value.Invested and x.Value.Type==SecurityType.Equity]))
        self.Plot("Indicator", "highestSPY price", self.highestSPYPrice)
        self.Plot("Indicator", "highestSPY price * value", self.highestSPYPrice * 0.95)

        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
        
        self.Plot("Indicator", "closing Price", self.Securities ["SPY"].Close)
# 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.95 * self.Securities["SPY"].Close)
            #self.highestSPYPrice = self.Securities ["SPY"].Close 
            self.Plot("Enter", "Buying and setting stop loss", self.Securities[symbol].Price - 10)
            


        if self.Long is not None and self.Securities ["SPY"].Close > self.highestSPYPrice:
            self.Plot("Enter", "update order", self.Securities[symbol].Price - 10)
            self.highestSPYPrice = self.Securities ["SPY"].Close 
            updateFields = UpdateOrderFields()
            updateFields.StopPrice = self.highestSPYPrice * 0.95
            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