| Overall Statistics |
|
Total Orders
16460
Average Win
0.40%
Average Loss
-0.42%
Compounding Annual Return
-4.126%
Drawdown
77.800%
Expectancy
-0.026
Start Equity
100000
End Equity
35275.12
Net Profit
-64.725%
Sharpe Ratio
-0.199
Sortino Ratio
-0.196
Probabilistic Sharpe Ratio
0.000%
Loss Rate
50%
Win Rate
50%
Profit-Loss Ratio
0.94
Alpha
-0.029
Beta
-0.14
Annual Standard Deviation
0.177
Annual Variance
0.031
Information Ratio
-0.309
Tracking Error
0.252
Treynor Ratio
0.252
Total Fees
$1571.22
Estimated Strategy Capacity
$16000000.00
Lowest Capacity Asset
CVLT TM78KLLUMAG5
Portfolio Turnover
8.12%
|
# https://quantpedia.com/strategies/momentum-factor-combined-with-asset-growth-effect/
#
# The investment universe consists of NYSE, AMEX and NASDAQ stocks (data for the backtest in the source paper are from Compustat).
# Stocks with a market capitalization less than the 20th NYSE percentile (smallest stocks) are removed. The asset growth variable
# is defined as the yearly percentage change in balance sheet total assets. Data from year t-2 to t-1 are used to calculate asset
# growth, and July is the cut-off month. Every month, stocks are then sorted into deciles based on asset growth and only stocks
# with the highest asset growth are used. The next step is to sort stocks from the highest asset growth decile into quintiles,
# based on their past 11-month return (with the last month’s performance skipped in the calculation). The investor then goes long
# on stocks with the strongest momentum and short on stocks with the weakest momentum. The portfolio is equally weighted and is
# rebalanced monthly. The investor holds long-short portfolios only during February-December -> January is excluded as this month
# has been repeatedly documented as a negative month for a momentum strategy (see “January Effect Filter and Momentum in Stocks”).
#
# QC implementation changes:
# - Universe consists of 1000 largest stocks traded on NYSE, AMEX, or NASDAQ.
from AlgorithmImports import *
import numpy as np
from pandas.core.frame import DataFrame
from pandas.core.series import Series
class MomentumFactorAssetGrowthEffect(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.FundamentalSelectionFunction)
self.Settings.MinimumOrderMarginPortfolioPercentage = 0.0
self.settings.daily_precise_end_time = False
self.exchange_codes: list[str] = ['NYS', 'NAS', 'ASE']
self.fundamental_count: int = 1_000
self.fundamental_sorting_key = lambda x: x.MarketCap
self.months_in_year: int = 12
self.days_in_month: int = 21
self.total_assets_history_period: int = 2
self.decile: int = 10
self.quintile: int = 5
self.excluded_month: int = 1
# Monthly close prices and total assets
self.symbol_data: dict[Symbol, SymbolData] = {}
self.long_symbols: dict[Symbol] = []
self.short_symbols: dict[Symbol] = []
self.selection_flag: bool = False
market: Symbol = self.AddEquity('SPY', Resolution.Daily).Symbol
self.Schedule.On(self.DateRules.MonthStart(market),
self.TimeRules.AfterMarketOpen(market),
self.Selection)
def FundamentalSelectionFunction(self, fundamental: List[Fundamental]) -> List[Symbol]:
if not self.selection_flag:
return Universe.Unchanged
# Update the rolling window every month.
for security in fundamental:
if security.Symbol in self.symbol_data:
self.symbol_data[security.Symbol].update_price(security.AdjustedPrice)
filtered: list[Fundamental] = [f for f in fundamental if f.HasFundamentalData
and f.SecurityReference.ExchangeId in self.exchange_codes
and not np.isnan(f.FinancialStatements.BalanceSheet.TotalAssets.TwelveMonths)
and f.FinancialStatements.BalanceSheet.TotalAssets.TwelveMonths > 0]
sorted_filter: List[Fundamental] = sorted(filtered,
key=self.fundamental_sorting_key,
reverse=True)[:self.fundamental_count]
# Warmup price rolling windows.
for f in sorted_filter:
if f.Symbol in self.symbol_data:
continue
self.symbol_data[f.Symbol] = SymbolData(f.Symbol, self.months_in_year, self.total_assets_history_period)
history: DataFrame = self.History(f.Symbol, self.months_in_year * self.days_in_month, Resolution.Daily)
if history.empty:
self.Log(f"Not enough data for {f.Symbol} yet.")
continue
closes: Series = history.loc[f.Symbol].close
# Find monthly closes.
for index, time_close in enumerate(closes.items()):
# index out of bounds check.
if index + 1 < len(closes.keys()):
date_month = time_close[0].date().month
next_date_month = closes.keys()[index + 1].month
# Find last day of month.
if date_month != next_date_month:
self.symbol_data[f.Symbol].update_price(time_close[1])
ready_securities: list[Fundamental] = [x for x in sorted_filter if self.symbol_data[x.Symbol].price_is_ready()]
# Asset growth calc.
asset_growth: dict[Symbol, float] = {}
for security in ready_securities:
if self.symbol_data[security.Symbol].asset_data_is_ready():
asset_growth[security.Symbol] = self.symbol_data[security.Symbol].asset_growth()
self.symbol_data[security.Symbol].update_assets(security.FinancialStatements.BalanceSheet.TotalAssets.TwelveMonths)
sorted_by_growth: list[tuple[Symbol, float]] = sorted(asset_growth.items(), key=lambda x: x[1], reverse=True)
decile: int = int(len(sorted_by_growth) / self.decile)
top_by_growth: list[Symbol] = [x[0] for x in sorted_by_growth][:decile]
performance: dict[Symbol, float] = {x: self.symbol_data[x].performance() for x in top_by_growth}
sorted_by_performance: list[tuple[Symbol, float]] = sorted(performance.items(), key=lambda x: x[1], reverse=True)
quintile = int(len(sorted_by_performance) / self.quintile)
self.long_symbols = [x[0] for x in sorted_by_performance][:quintile]
self.short_symbols = [x[0] for x in sorted_by_performance][-quintile:]
return self.long_symbols + self.short_symbols
def OnSecuritiesChanged(self, changes: SecurityChanges) -> None:
for security in changes.AddedSecurities:
security.SetFeeModel(CustomFeeModel())
def OnData(self, slice: Slice) -> None:
if not self.selection_flag:
return
self.selection_flag = False
# Trade execution.
targets: List[PortfolioTarget] = []
for i, portfolio in enumerate([self.long_symbols, self.short_symbols]):
for symbol in portfolio:
if slice.ContainsKey(symbol) and slice[symbol] is not None:
targets.append(PortfolioTarget(symbol, ((-1) ** i) / len(portfolio)))
self.SetHoldings(targets, True)
self.long_symbols.clear()
self.short_symbols.clear()
def Selection(self) -> None:
# Exclude January trading.
if self.Time.month != self.excluded_month:
self.selection_flag = True
else:
self.Liquidate()
class SymbolData():
def __init__(self, symbol: Symbol, period: int, total_assets_history_period: int) -> None:
self.Symbol: Symbol = symbol
self.Price: RollingWindow = RollingWindow[float](period)
self.TotalAssets: RollingWindow = RollingWindow[float](total_assets_history_period)
def update_price(self, value) -> None:
self.Price.Add(value)
def update_assets(self, assets_value) -> None:
self.TotalAssets.Add(assets_value)
def asset_data_is_ready(self) -> bool:
return self.TotalAssets.IsReady
def asset_growth(self) -> float:
asset_values: list[float] = [x for x in self.TotalAssets]
return (asset_values[0] - asset_values[1]) / asset_values[1]
def price_is_ready(self) -> bool:
return self.Price.IsReady
# Performance, one month skipped.
def performance(self, values_to_skip: int = 1) -> float:
closes: list[float] = [x for x in self.Price][values_to_skip:]
return (closes[0] / closes[-1] - 1)
# 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"))