Overall Statistics
Total Trades
922
Average Win
0.08%
Average Loss
-0.01%
Compounding Annual Return
-0.915%
Drawdown
3.400%
Expectancy
-0.975
Net Profit
-3.416%
Sharpe Ratio
-3.149
Probabilistic Sharpe Ratio
0.000%
Loss Rate
100%
Win Rate
0%
Profit-Loss Ratio
10.42
Alpha
-0.006
Beta
0.001
Annual Standard Deviation
0.002
Annual Variance
0
Information Ratio
-0.741
Tracking Error
0.176
Treynor Ratio
-5.544
Total Fees
$3062.46
Estimated Strategy Capacity
$170000000.00
Lowest Capacity Asset
MSFT R735QTJ8XC9X
"""
Buy 20 stocks showing strength vs SPY. Buy them. Liquidate at EOD

"""
class JumpingLightBrownScorpion(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2018, 1, 1)  # Set Start Date
        self.SetCash(100000) # Set Cash
        
        self.stocks = ["AAPL", "MSFT"] # stocks I'm interested in
            
        # Dictionary to hold Symbol Data
        self.symbolData = {}

        for stock in self.stocks:
            # Add equity data
            symbol = self.AddEquity(stock, Resolution.Daily).Symbol
            # Create symbol data for respective symbol
            self.symbolData[symbol] = SymbolData(self, symbol)
        
        self.spy = self.AddEquity("SPY", Resolution.Minute).Symbol # intialize SPY
        self.SetWarmUp(200, Resolution.Daily) # warm up indicators
        self.yest_close = self.SMA(self.spy, 1, Resolution.Daily, Field.Close) # set SMA with close price as field
        
        

    def OnData(self, data):
        if self.IsWarmingUp or not self.yest_close.IsReady or not len(data.Bars) > 0: # if indicators warming up, not ready, or no data bars then stop
            return
        
        # Make sure indicators and rolling windows are ready
        for symbol in self.symbolData.values():
            if not symbol.IsReady:
                return
        
        
        if self.Time.hour == 9 and self.Time.minute == 45: # if it's 9:45am eastern
            price = self.Securities[self.spy].Price        # set price to SPYs price
            yest_close = self.yest_close.Current.Value     # set yesterday's closing price to variable
            
            if price < yest_close:# If SPY fell from yesterday,
                selected = []
                for symbol, value in self.symbolData.items():
                    # If the SMA today is higher than SMA yesterday
                    sma_value = str(value.smaWindow[1])
                    sma_value = float((sma_value.split("-")[3]).strip())
                    if self.Securities[symbol].Price > sma_value:
                        selected.append(symbol)
                for stock in selected:
                    self.SetHoldings(stock,1/len(selected))
                
                        
        if self.Time.hour == 15 and self.Time.minute == 45: # if it's 3:45pm eastern, liquidate portfolio
            self.Liquidate()
            
class SymbolData:
    
    def __init__(self, algorithm, symbol):
        self.algorithm = algorithm
        self.symbol = symbol
        # Define our indicator
        self.sma = algorithm.SMA(symbol, 1, Resolution.Daily, Field.Close)
        # Define our rolling window to hold indicator points
        self.smaWindow = RollingWindow[IndicatorDataPoint](2)
        # Set our event handler
        self.sma.Updated += self.OnSMAUpdated
        
    def OnSMAUpdated(self, sender, updated):
        # Add updated indicator data to rolling window
        self.smaWindow.Add(updated)
        
    @property
    def IsReady(self):
        return self.sma.IsReady and self.smaWindow.IsReady