Overall Statistics
Total Trades
1240
Average Win
1.86%
Average Loss
-0.80%
Compounding Annual Return
27.721%
Drawdown
77.400%
Expectancy
1.314
Net Profit
27866.228%
Sharpe Ratio
0.953
Probabilistic Sharpe Ratio
21.305%
Loss Rate
31%
Win Rate
69%
Profit-Loss Ratio
2.34
Alpha
0.167
Beta
0.841
Annual Standard Deviation
0.225
Annual Variance
0.051
Information Ratio
0.876
Tracking Error
0.18
Treynor Ratio
0.255
Total Fees
$10529.45
Estimated Strategy Capacity
$100000.00
Lowest Capacity Asset
CO UHRIGHQMWXNP
#region imports
from AlgorithmImports import *
#endregion
# https://quantpedia.com/strategies/net-current-asset-value-effect/
#
# The investment universe consists of all stocks on the London Exchange. Companies with more than one class of ordinary shares and foreign companies 
# are excluded. Also excluded are companies on the lightly regulated markets and companies which belong to the financial sector. The portfolio of
# stocks is formed annually in July. Only those stocks with an NCAV/MV higher than 1.5 are included in the NCAV/MV portfolio. This Buy-and-hold
# portfolio is held for one year. Stocks are weighted equally.
#
# QC implementation changes:
#   - Instead of all listed stock, we select top 3000 stocks by market cap from QC stock universe.

class NetCurrentAssetValueEffect(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.long = []
        
        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())
            security.SetLeverage(10)

    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.EarningReports.BasicAverageShares.ThreeMonths > 0 and x.MarketCap != 0 and x.ValuationRatios.WorkingCapitalPerShare != 0]
        sorted_by_market_cap = sorted(fine, key = lambda x:x.MarketCap, reverse=True)
        top_by_market_cap = [x for x in sorted_by_market_cap[:self.coarse_count]]
        
        # NCAV/MV calc.
        self.long = [x.Symbol for x in top_by_market_cap if ((x.ValuationRatios.WorkingCapitalPerShare * x.EarningReports.BasicAverageShares.ThreeMonths) / x.MarketCap) > 1.5]
        
        return self.long
    
    def OnData(self, data):
        if not self.selection_flag:
            return
        self.selection_flag = False

        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 symbol in data and data[symbol]:
                self.SetHoldings(symbol, 1 / len(self.long))

        self.long.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"))