Overall Statistics
Total Trades
61
Average Win
0.08%
Average Loss
-0.35%
Compounding Annual Return
-19.869%
Drawdown
0.900%
Expectancy
-0.508
Net Profit
-0.605%
Sharpe Ratio
-7.001
Probabilistic Sharpe Ratio
0.776%
Loss Rate
60%
Win Rate
40%
Profit-Loss Ratio
0.23
Alpha
-0.177
Beta
0.053
Annual Standard Deviation
0.022
Annual Variance
0
Information Ratio
-6.601
Tracking Error
0.084
Treynor Ratio
-2.963
Total Fees
$61.00
Estimated Strategy Capacity
$3500000000.00
Lowest Capacity Asset
LLY R735QTJ8XC9X
Portfolio Turnover
57.16%
from AlgorithmImports import *

class ETFConstituentsDataAlgorithm(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2020, 1, 1)
        self.SetEndDate(2020, 1, 10)
        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}')