Overall Statistics
Total Orders
3990
Average Win
0.24%
Average Loss
-0.30%
Compounding Annual Return
2.308%
Drawdown
5.400%
Expectancy
0.004
Net Profit
12.102%
Sharpe Ratio
-0.006
Sortino Ratio
-0.007
Probabilistic Sharpe Ratio
6.501%
Loss Rate
44%
Win Rate
56%
Profit-Loss Ratio
0.80
Alpha
-0.004
Beta
0.035
Annual Standard Deviation
0.038
Annual Variance
0.001
Information Ratio
-0.635
Tracking Error
0.156
Treynor Ratio
-0.007
Total Fees
$4251.63
Estimated Strategy Capacity
$160000000.00
Lowest Capacity Asset
JNJ R735QTJ8XC9X
Portfolio Turnover
69.09%
#region imports
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Globalization;
    using System.Drawing;
    using QuantConnect;
    using QuantConnect.Algorithm.Framework;
    using QuantConnect.Algorithm.Framework.Selection;
    using QuantConnect.Algorithm.Framework.Alphas;
    using QuantConnect.Algorithm.Framework.Portfolio;
    using QuantConnect.Algorithm.Framework.Execution;
    using QuantConnect.Algorithm.Framework.Risk;
    using QuantConnect.Algorithm.Selection;
    using QuantConnect.Parameters;
    using QuantConnect.Benchmarks;
    using QuantConnect.Brokerages;
    using QuantConnect.Util;
    using QuantConnect.Interfaces;
    using QuantConnect.Algorithm;
    using QuantConnect.Indicators;
    using QuantConnect.Data;
    using QuantConnect.Data.Consolidators;
    using QuantConnect.Data.Custom;
    using QuantConnect.DataSource;
    using QuantConnect.Data.Fundamental;
    using QuantConnect.Data.Market;
    using QuantConnect.Data.UniverseSelection;
    using QuantConnect.Notifications;
    using QuantConnect.Orders;
    using QuantConnect.Orders.Fees;
    using QuantConnect.Orders.Fills;
    using QuantConnect.Orders.Slippage;
    using QuantConnect.Scheduling;
    using QuantConnect.Securities;
    using QuantConnect.Securities.Equity;
    using QuantConnect.Securities.Future;
    using QuantConnect.Securities.Option;
    using QuantConnect.Securities.Forex;
    using QuantConnect.Securities.Crypto;   
    using QuantConnect.Securities.Interfaces;
    using QuantConnect.Storage;
    using QCAlgorithmFramework = QuantConnect.Algorithm.QCAlgorithm;
    using QCAlgorithmFrameworkBridge = QuantConnect.Algorithm.QCAlgorithm;
#endregion

namespace QuantConnect
{
    public class ETFConstituentsDataAlgorithm : QCAlgorithm
    { 
        private Symbol _spy;
        private Universe _universe;
        private Dictionary<Symbol, decimal> _weightBySymbol = new Dictionary<Symbol, decimal>();
        
        public override void Initialize()
        {
            SetStartDate(2016, 1, 1);
            SetEndDate(2021, 1, 1);
            SetCash(100000);
            UniverseSettings.Asynchronous = true;
            UniverseSettings.Resolution = Resolution.Minute;

            // Requesting data
            _spy = AddEquity("SPY").Symbol;            
            _universe = AddUniverse(Universe.ETF(_spy, UniverseSettings, ETFConstituentsFilter));

            // Historical Universe data
            var history = History(_universe, 30, Resolution.Daily);
            foreach (var constituents in history)
            {
                foreach (ETFConstituentUniverse constituent in constituents)
                {
                    Debug($"{constituent.Symbol} weight at {constituent.EndTime}: {constituent.Weight}");
                }
            }
            
            Schedule.On(
                DateRules.EveryDay(_spy),
                TimeRules.AfterMarketOpen(_spy, 1),
                Rebalance);
        }
        
        private IEnumerable<Symbol> ETFConstituentsFilter(IEnumerable<ETFConstituentUniverse> constituents)
        {
            // Get the 10 securities with the largest weight in the index
            _weightBySymbol = constituents.OrderByDescending(c => c.Weight).Take(10)
                .ToDictionary(c => c.Symbol, c => c.Weight ?? 0m);

            return _weightBySymbol.Keys;
        }
        
        private void Rebalance()
        {
            var spyWeight = _weightBySymbol.Values.Sum();
                
            if (spyWeight > 0)
            {
                foreach(var symbol in Portfolio.Keys)
                {
                	if (!_weightBySymbol.ContainsKey(symbol))
                	{
                		Liquidate(symbol);
                	}
                }
	            
	            foreach(var kvp in _weightBySymbol)
	            {
	                SetHoldings(kvp.Key, 0.5m * kvp.Value / spyWeight);
	            }
	            
	            SetHoldings(_spy, -0.5m);
            }
        }
        
        public override void OnSecuritiesChanged(SecurityChanges changes)
        {
            foreach (var security in changes.RemovedSecurities.Where(x => x.Invested))
            {
                 Liquidate(security.Symbol, "Removed From Universe");
            }

            foreach (var security in changes.AddedSecurities)
            {
                // Historical data
                var history = History(security.Symbol, 7, Resolution.Daily);
                Debug($"We got {history.Count()} from our history request for {security.Symbol}");
            }
        }
    }
}