Overall Statistics
Total Orders
10948
Average Win
0.05%
Average Loss
-0.06%
Compounding Annual Return
15.296%
Drawdown
32.100%
Expectancy
0.335
Start Equity
10000000
End Equity
40812924.39
Net Profit
308.129%
Sharpe Ratio
0.616
Sortino Ratio
0.635
Probabilistic Sharpe Ratio
14.216%
Loss Rate
24%
Win Rate
76%
Profit-Loss Ratio
0.75
Alpha
0.013
Beta
1.023
Annual Standard Deviation
0.155
Annual Variance
0.024
Information Ratio
0.385
Tracking Error
0.037
Treynor Ratio
0.093
Total Fees
$95562.46
Estimated Strategy Capacity
$830000000.00
Lowest Capacity Asset
BRKB R735QTJ8XC9X
Portfolio Turnover
2.03%
# region imports
from AlgorithmImports import *
# endregion


class TOPTAnalysisAlgorithm(QCAlgorithm):

    def initialize(self):
        self.set_start_date(2014, 12, 31)
        self.set_cash(10_000_000)

        spy = Symbol.create('SPY', SecurityType.EQUITY, Market.USA)
        self.set_security_initializer(
            BrokerageModelSecurityInitializer(self.brokerage_model, FuncSecuritySeeder(self.get_last_known_prices))
        )

        # Add a universe of daily data.
        self.universe_settings.resolution = Resolution.DAILY
        self._universe = self.add_universe(
            self.universe.etf(
                spy, 
                universe_filter_func=lambda constituents: [
                    c.symbol for c in sorted(
                        [c for c in constituents if c.weight], 
                        key=lambda c: c.weight
                    )[-20:]
                ]
            )
        )

        # Create a Scheduled Event to rebelance the portfolio.
        self.schedule.on(
            self.date_rules.every_day(spy),
            self.time_rules.at(0, 1),
            self._rebalance
        )

        self.set_warm_up(timedelta(30))

    def _rebalance(self):
        if self.is_warming_up or not self._universe.selected:
            return
        symbols = [s for s in self._universe.selected if s in self.securities and self.securities[s].price]
        self.set_holdings([PortfolioTarget(symbol, 1/len(symbols)) for symbol in symbols], True)