Overall Statistics
Total Orders
1561
Average Win
0.40%
Average Loss
-0.45%
Compounding Annual Return
9.260%
Drawdown
53.100%
Expectancy
0.540
Start Equity
100000
End Equity
896733.01
Net Profit
796.733%
Sharpe Ratio
0.359
Sortino Ratio
0.375
Probabilistic Sharpe Ratio
0.120%
Loss Rate
19%
Win Rate
81%
Profit-Loss Ratio
0.89
Alpha
0.015
Beta
0.883
Annual Standard Deviation
0.146
Annual Variance
0.021
Information Ratio
0.212
Tracking Error
0.046
Treynor Ratio
0.059
Total Fees
$214.08
Estimated Strategy Capacity
$200000000.00
Lowest Capacity Asset
CED RWTPESR2XAAT
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
        self.settings.daily_precise_end_time = False
        
        # 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"))