Overall Statistics
Total Trades
16437
Average Win
0.19%
Average Loss
-0.16%
Compounding Annual Return
10.903%
Drawdown
22.400%
Expectancy
0.173
Net Profit
826.674%
Sharpe Ratio
1.034
Probabilistic Sharpe Ratio
47.608%
Loss Rate
47%
Win Rate
53%
Profit-Loss Ratio
1.21
Alpha
0.093
Beta
0.004
Annual Standard Deviation
0.09
Annual Variance
0.008
Information Ratio
0.097
Tracking Error
0.197
Treynor Ratio
21.068
Total Fees
$2002.57
Estimated Strategy Capacity
$170000.00
Lowest Capacity Asset
LOOP WPPM5RZ0K7S5
# https://quantpedia.com/strategies/asset-growth-effect/
#
# The investment universe consists of all non-financial U.S. stocks listed on NYSE, AMEX, and NASDAQ. Stocks are then sorted each year at the end 
# of June into ten equal groups based on the percentage change in total assets for the previous year. The investor goes long decile with low asset
# growth firms and short decile with high asset growth firms. The portfolio is weighted equally and rebalanced every year.
#
# QC implementation changes:
#   - Top 3000 stocks by market cap are selected from QC stock universe.

class AssetGrowthEffect(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2000, 1, 1)
        self.SetCash(100000)
        
        self.symbol = self.AddEquity("SPY", Resolution.Daily).Symbol

        self.long = []
        self.short = []
        
        self.coarse_count = 3000
        
        # Latest assets data.
        self.total_assets = {}
        
        self.selection_flag = False
        self.UniverseSettings.Resolution = Resolution.Daily
        self.AddUniverse(self.CoarseSelectionFunction, self.FineSelectionFunction)
        self.Schedule.On(self.DateRules.MonthEnd(self.symbol), self.TimeRules.AfterMarketOpen(self.symbol), self.Selection)

    def OnSecuritiesChanged(self, changes):
        for security in changes.AddedSecurities:
            security.SetFeeModel(CustomFeeModel(self))
            security.SetLeverage(5)
            
    def CoarseSelectionFunction(self, coarse):
        if not self.selection_flag:
            return Universe.Unchanged
        
        # Select all stocks in universe.
        return [x.Symbol for x in coarse if x.HasFundamentalData and x.Market == 'usa']
    
    def FineSelectionFunction(self, fine):
        fine = [x for x in fine if x.FinancialStatements.BalanceSheet.TotalAssets.TwelveMonths > 0 and
                ((x.SecurityReference.ExchangeId == "NYS") or (x.SecurityReference.ExchangeId == "NAS") or (x.SecurityReference.ExchangeId == "ASE"))]
                
        if len(fine) > self.coarse_count:
            sorted_by_market_cap = sorted(fine, key = lambda x: x.MarketCap, reverse=True)
            top_by_market_cap = sorted_by_market_cap[:self.coarse_count]
        else:
            top_by_market_cap = fine
            
        assets_growth = {}
        for stock in top_by_market_cap:
            symbol = stock.Symbol
            
            if symbol not in self.total_assets:
                self.total_assets[symbol] = None
                
            current_assets = stock.FinancialStatements.BalanceSheet.TotalAssets.TwelveMonths
            
            # There is not previous assets data.
            if not self.total_assets[symbol]:
                self.total_assets[symbol] = current_assets
                continue
            
            # Assets growth calc.
            assets_growth[symbol] = (current_assets - self.total_assets[symbol]) / self.total_assets[symbol]
            
            # Update data.
            self.total_assets[symbol] = current_assets
        
        # Asset growth sorting.
        sorted_by_assets_growth = sorted(assets_growth.items(), key = lambda x: x[1], reverse = True)
        decile = int(len(sorted_by_assets_growth) / 10)
        self.long = [x[0] for x in sorted_by_assets_growth[-decile:]]
        self.short = [x[0] for x in sorted_by_assets_growth[:decile]]
        
        return self.long + self.short
        
    def OnData(self, data):
        if not self.selection_flag:
            return
        self.selection_flag = False
        
        # Trade execution.
        stocks_invested = [x.Key for x in self.Portfolio if x.Value.Invested]
        for symbol in stocks_invested:
            if symbol not in self.long:
                self.Liquidate(symbol)

        for symbol in self.long:
            if self.Securities[symbol].Price != 0 and self.Securities[symbol].IsTradable:  # Prevent error message.
                self.SetHoldings(symbol, 1 / len(self.long))
        for symbol in self.short:
            if self.Securities[symbol].Price != 0 and self.Securities[symbol].IsTradable:  # Prevent error message.
                self.SetHoldings(symbol, -1 / len(self.short))

        self.long.clear()
        self.short.clear()
            
    def Selection(self):
        if self.Time.month == 6:
            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"))