Overall Statistics
Total Orders
1507
Average Win
0.40%
Average Loss
-0.46%
Compounding Annual Return
9.229%
Drawdown
52.600%
Expectancy
0.512
Start Equity
100000
End Equity
852073.67
Net Profit
752.074%
Sharpe Ratio
0.36
Sortino Ratio
0.377
Probabilistic Sharpe Ratio
0.124%
Loss Rate
19%
Win Rate
81%
Profit-Loss Ratio
0.86
Alpha
0.016
Beta
0.885
Annual Standard Deviation
0.147
Annual Variance
0.022
Information Ratio
0.246
Tracking Error
0.045
Treynor Ratio
0.06
Total Fees
$195.58
Estimated Strategy Capacity
$14000000.00
Lowest Capacity Asset
RY R735QTJ8XC9X
Portfolio Turnover
0.17%
# https://quantpedia.com/strategies/net-payout-yield-effect/
#
# The investment universe consists of all stocks on NYSE, AMEX, and NASDAQ. At the end of June of each year t, ten portfolios are formed based on ranked 
# values net payout yield. The net payout yield is the ratio of dividends plus repurchases minus common share issuances in year t to year-end market
# capitalization. There are two measures of payout yield, one based on the statement of cash flows, the other based on the change in Treasury stocks. 
# For the net payout yield, we use the cash flow-based measure of repurchases. The portfolio with the highest net payout yield is bought and held for
# one year, after which it is rebalanced.
#
# QC implementation changes:
#   - Instead of all listed stocks, we selected 500 most liquid stocks traded on NYSE, AMEX, or NASDAQ.

from AlgorithmImports import *
import numpy as np

class NetPayoutYieldEffect(QCAlgorithm):

    def Initialize(self) -> None:
        self.SetStartDate(2000, 1, 1)  
        self.SetCash(100_000) 

        self.UniverseSettings.Leverage = 5
        self.UniverseSettings.Resolution = Resolution.Daily
        self.AddUniverse(self.FundamentalFunction)
        self.Settings.MinimumOrderMarginPortfolioPercentage = 0.0
        
        # Fundamental Filter Parameters
        self.exchange_codes: List[str] = ['NYS', 'NAS', 'ASE']
        self.fundamental_count: int = 500
        self.quantile: int = 10

        self.long_symbols: List[Symbol] = []
        
        self.rebalancing_month: int = 6
        self.selection_flag: bool = True

        self.exchange: Symbol = self.AddEquity('SPY', Resolution.Daily).Symbol
        self.Schedule.On(self.DateRules.MonthEnd(self.exchange), 
                        self.TimeRules.AfterMarketOpen(self.exchange), 
                        self.Selection)

    def FundamentalFunction(self, fundamental: List[Fundamental]) -> List[Symbol]:
        if not self.selection_flag:
            return Universe.Unchanged

        filtered: List[Fundamental] = [f for f in fundamental if f.HasFundamentalData
                    and f.SecurityReference.ExchangeId in self.exchange_codes
                    and not np.isnan(f.MarketCap)
                    and f.MarketCap !=0
                    and not np.isnan(f.ValuationRatios.TotalYield)
                    and f.ValuationRatios.TotalYield != 0
                    and not np.isnan(f.FinancialStatements.CashFlowStatement.CommonStockIssuance.TwelveMonths)
                    and f.FinancialStatements.CashFlowStatement.CommonStockIssuance.TwelveMonths != 0]
        
        top_by_dollar_volume: List[Fundamental] = sorted(filtered, 
                                                        key=lambda x: x.DollarVolume, 
                                                        reverse=True)[:self.fundamental_count]

        payout_yield = lambda x: ((x.ValuationRatios.TotalYield * (x.MarketCap)) - \
                    (x.FinancialStatements.CashFlowStatement.CommonStockIssuance.TwelveMonths / (x.MarketCap)))
        
        sorted_by_payout: List[Fundamental] = sorted(top_by_dollar_volume, key = payout_yield, reverse=True)

        if len(sorted_by_payout) >= self.quantile:
            quantile: int = int(len(sorted_by_payout) / self.quantile)
            self.long_symbols = [x.Symbol for x in sorted_by_payout[:quantile]]

        return self.long_symbols
    
    def OnData(self, slice: Slice) -> None:
        if not self.selection_flag:
            return
        self.selection_flag = False

        # Trade Execution
        portfolio: List[PortfolioTarget] = [PortfolioTarget(symbol, 1 / len(self.long_symbols)) 
                                            for symbol in self.long_symbols 
                                            if slice.ContainsKey(symbol) and slice[symbol] is not None]

        self.SetHoldings(portfolio, True)
        self.long_symbols.clear()
    
    def Selection(self) -> None:
        if self.Time.month == self.rebalancing_month:
            self.selection_flag = True

    def OnSecuritiesChanged(self, changes: SecurityChanges) -> None:
        for security in changes.AddedSecurities:
            security.SetFeeModel(CustomFeeModel())

# Custom fee model
class CustomFeeModel(FeeModel):
    def GetOrderFee(self, parameters: OrderFeeParameters) -> OrderFee:
        fee: float = parameters.Security.Price * parameters.Order.AbsoluteQuantity * 0.00005
        return OrderFee(CashAmount(fee, "USD"))