Overall Statistics
Total Trades
28493
Average Win
0.09%
Average Loss
-0.07%
Compounding Annual Return
9.691%
Drawdown
33.600%
Expectancy
0.198
Net Profit
636.979%
Sharpe Ratio
0.679
Probabilistic Sharpe Ratio
0.611%
Loss Rate
49%
Win Rate
51%
Profit-Loss Ratio
1.34
Alpha
0.086
Beta
0.008
Annual Standard Deviation
0.128
Annual Variance
0.016
Information Ratio
0.055
Tracking Error
0.217
Treynor Ratio
10.517
Total Fees
$1499.86
Estimated Strategy Capacity
$56000.00
Lowest Capacity Asset
LBTYB SZC2UFSQNK9X
# https://quantpedia.com/strategies/investment-factor/
#
# The investment universe consists of all NYSE, Amex, and NASDAQ stocks. Firstly, stocks are allocated to five Size groups (Small to Big) at the end of each June
# using NYSE market cap breakpoints. Stocks are allocated independently to five Investment (Inv) groups (Low to High) still using NYSE breakpoints.
# The intersections of the two sorts produce 25 Size-Inv portfolios. For portfolios formed in June of year t, Inv is the growth of total assets for
# the fiscal year ending in t-1 divided by total assets at the end of t-1. Long portfolio with the highest Size and simultaneously with the lowest
# Investment. Short portfolio with the highest Size and simultaneously with the highest Investment. The portfolios are value-weighted.
#
# QC implementation changes:
#   - Universe consists of top 3000 US stock by market cap from NYSE, AMEX and NASDAQ.
#   - Equally weighting is used.

class InvestmentFactor(QCAlgorithm):

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

        self.symbol = self.AddEquity('SPY', Resolution.Daily).Symbol
        
        self.coarse_count = 3000
        
        self.weight = {}
        
        self.last_year_data = {}
        
        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

        selected = [x.Symbol for x in coarse if x.HasFundamentalData and x.Market == 'usa']
        return selected
        
    def FineSelectionFunction(self, fine):
        fine = [x for x in fine if x.MarketCap != 0 and x.FinancialStatements.BalanceSheet.TotalAssets.TwelveMonths != 0 and x.OperationRatios.TotalAssetsGrowth.OneYear 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
            
        # Sorting by investment factor.
        sorted_by_inv_factor = sorted(top_by_market_cap, key = lambda x: (x.OperationRatios.TotalAssetsGrowth.OneYear / x.FinancialStatements.BalanceSheet.TotalAssets.TwelveMonths), reverse=True)
        quintile  = int(len(sorted_by_inv_factor) / 5)
        self.long = [x.Symbol for x in sorted_by_inv_factor[-quintile:]]
        self.short = [x.Symbol for x in sorted_by_inv_factor[:quintile]]
        
        return self.long + self.short
    
    def OnData(self, data):
        if not self.selection_flag:
            return
        self.selection_flag = False

        # Trade execution.
        long_count = len(self.long)
        short_count = len(self.short)
        
        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.short:
                self.Liquidate(symbol)

        for symbol in self.long:
            if self.Securities[symbol].Price != 0:  # Prevent error message.
                self.SetHoldings(symbol, 1 / long_count)
        
        for symbol in self.short:
            if self.Securities[symbol].Price != 0:  # Prevent error message.
                self.SetHoldings(symbol, -1 / short_count)
        
        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"))