Overall Statistics
Total Orders
90296
Average Win
0.04%
Average Loss
-0.04%
Compounding Annual Return
1.867%
Drawdown
46.900%
Expectancy
0.025
Start Equity
100000
End Equity
158360.99
Net Profit
58.361%
Sharpe Ratio
-0.058
Sortino Ratio
-0.065
Probabilistic Sharpe Ratio
0.000%
Loss Rate
49%
Win Rate
51%
Profit-Loss Ratio
1.02
Alpha
0.002
Beta
-0.174
Annual Standard Deviation
0.09
Annual Variance
0.008
Information Ratio
-0.234
Tracking Error
0.205
Treynor Ratio
0.03
Total Fees
$2115.94
Estimated Strategy Capacity
$38000000.00
Lowest Capacity Asset
DUO X95P0920YMW5
Portfolio Turnover
3.02%
# https://quantpedia.com/strategies/roa-effect-within-stocks/
#
# The investment universe contains all stocks on NYSE and AMEX and Nasdaq with Sales greater than 10 million USD. Stocks are then sorted into
# two halves based on market capitalization. Each half is then divided into deciles based on Return on assets (ROA) calculated as quarterly
# earnings (Compustat quarterly item IBQ – income before extraordinary items) divided by one-quarter-lagged assets (item ATQ – total assets).
# The investor then goes long the top three deciles from each market capitalization group and goes short bottom three deciles. The strategy is
# rebalanced monthly, and stocks are equally weighted.
#
# QC implementation changes:
#   - The investment universe contains 1000 most liquid stocks on NYSE and AMEX and Nasdaq with Sales greater than 10 million USD.

from AlgorithmImports import *

class ROAEffectWithinStocks(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2000, 1, 1)
        self.SetCash(100000) 

        market:Symbol = self.AddEquity('SPY', Resolution.Daily).Symbol
        self.quantile:int = 10
        self.leverage:int = 5
        self.sales_threshold:float = 1e7
        self.exchange_codes:List[str] = ['NYS', 'NAS', 'ASE']
        
        self.long:List[Symbol] = []
        self.short:List[Symbol] = []

        self.fundamental_count:int = 500
        self.fundamental_sorting_key = lambda x: x.DollarVolume

        self.settings.daily_precise_end_time = False
        self.settings.minimum_order_margin_portfolio_percentage = 0.

        self.selection_flag:bool = False
        self.UniverseSettings.Resolution = Resolution.Daily
        self.AddUniverse(self.FundamentalSelectionFunction)
        self.Schedule.On(self.DateRules.MonthEnd(market), self.TimeRules.AfterMarketOpen(market), self.Selection)

    def OnSecuritiesChanged(self, changes: SecurityChanges) -> None:
        for security in changes.AddedSecurities:
            security.SetFeeModel(CustomFeeModel())
            security.SetLeverage(self.leverage)
            
    def FundamentalSelectionFunction(self, fundamental: List[Fundamental]) -> List[Symbol]:
        if not self.selection_flag:
            return Universe.Unchanged
        
        selected:List[Fundamental] = [x for x in fundamental if x.HasFundamentalData and x.Market == 'usa' and x.SecurityReference.ExchangeId in self.exchange_codes and \
            x.ValuationRatios.SalesPerShare * x.EarningReports.DilutedAverageShares.Value > self.sales_threshold and \
            not np.isnan(x.OperationRatios.ROA.ThreeMonths) and x.OperationRatios.ROA.ThreeMonths != 0]

        if len(selected) > self.fundamental_count:
            selected = [x for x in sorted(selected, key=self.fundamental_sorting_key, reverse=True)[:self.fundamental_count]]
                    
        # Sorting by market cap.
        sorted_by_market_cap = sorted(selected, key = lambda x: x.MarketCap, reverse=True)
        half:int = int(len(sorted_by_market_cap) / 2)
        top_mc = [x for x in sorted_by_market_cap[:half]]
        bottom_mc = [x for x in sorted_by_market_cap[half:]]
        
        if len(top_mc) >= self.quantile and len(bottom_mc) >= self.quantile:
            # Sorting by ROA.
            sorted_top_by_roa:List[Fundamental] = sorted(top_mc, key = lambda x:(x.OperationRatios.ROA.Value), reverse=True)
            quantile:int = int(len(sorted_top_by_roa) / self.quantile)
            long_top:List[Symbol] = [x.Symbol for x in sorted_top_by_roa[:quantile*3]]
            short_top:List[Symbol] = [x.Symbol for x in sorted_top_by_roa[-(quantile*3):]]
            
            sorted_bottom_by_roa:List[Fundamental] = sorted(bottom_mc, key = lambda x:(x.OperationRatios.ROA.Value), reverse=True)
            quantile = int(len(sorted_bottom_by_roa) / self.quantile)
            long_bottom:List[Symbol] = [x.Symbol for x in sorted_bottom_by_roa[:quantile*3]]
            short_bottom:List[Symbol] = [x.Symbol for x in sorted_bottom_by_roa[-(quantile*3):]]
            
            self.long = long_top + long_bottom 
            self.short = short_top + short_bottom

        return self.long + self.short
    
    def OnData(self, data: Slice) -> None:
        if not self.selection_flag:
            return
        self.selection_flag = False

        # order execution
        targets:List[PortfolioTarget] = []
        for i, portfolio in enumerate([self.long, self.short]):
            for symbol in portfolio:
                if symbol in data and data[symbol]:
                    targets.append(PortfolioTarget(symbol, ((-1) ** i) / len(portfolio)))
        
        self.SetHoldings(targets, True)

        self.long.clear()
        self.short.clear()
    
    def Selection(self) -> None:
        self.selection_flag = True

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