Overall Statistics
Total Trades
329
Average Win
0.76%
Average Loss
-0.59%
Compounding Annual Return
-1.112%
Drawdown
11.300%
Expectancy
-0.021
Net Profit
-0.480%
Sharpe Ratio
-0.023
Probabilistic Sharpe Ratio
21.127%
Loss Rate
57%
Win Rate
43%
Profit-Loss Ratio
1.28
Alpha
0.017
Beta
-0.266
Annual Standard Deviation
0.104
Annual Variance
0.011
Information Ratio
-0.157
Tracking Error
0.49
Treynor Ratio
0.009
Total Fees
$333.74
Estimated Strategy Capacity
$3100000000.00
Lowest Capacity Asset
LLY R735QTJ8XC9X
Portfolio Turnover
67.78%
from AlgorithmImports import *

class ETFConstituentsDataAlgorithm(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2020, 1, 1)
        self.SetEndDate(2020, 6, 6)
        self.SetCash(100000)
        
        self.UniverseSettings.Resolution = Resolution.Daily
        
        # Requesting data
        self.spy = self.AddEquity("SPY").Symbol
        self.AddUniverse(self.Universe.ETF(self.spy, self.UniverseSettings, self.ETFConstituentsFilter))
        
        self.weightBySymbol = {}
        
        self.Schedule.On(
            self.DateRules.EveryDay(self.spy),
            self.TimeRules.AfterMarketOpen(self.spy, 1),
            self.Rebalance)

    def ETFConstituentsFilter(self, constituents):
        # Get the 10 securities with the largest weight in the index
        selected = sorted([c for c in constituents if c.Weight],
            key=lambda c: c.Weight, reverse=True)
        self.weightBySymbol = {c.Symbol: c.Weight for c in selected}
        
        self.Log(self.weightBySymbol)

        return list(self.weightBySymbol.keys())

    def Rebalance(self):
        spyWeight = sum(self.weightBySymbol.values())

        if spyWeight > 0:
            for symbol in self.Portfolio.Keys:
                if symbol not in self.weightBySymbol:
                    self.Liquidate(symbol)
    
            for symbol, weight in self.weightBySymbol.items():
                self.SetHoldings(symbol, 0.5 * weight / spyWeight)
                
            self.SetHoldings(self.spy, -0.5)

    def OnSecuritiesChanged(self, changes):
        for security in changes.RemovedSecurities:
            if security.Invested:
                self.Liquidate(security.Symbol, 'Removed From Universe')

        for security in changes.AddedSecurities:
            # Historical data
            history = self.History(security.Symbol, 7, Resolution.Daily)
            self.Debug(f'We got {len(history)} from our history request for {security.Symbol}')