Overall Statistics
Total Trades
2018
Average Win
3.16%
Average Loss
-1.53%
Compounding Annual Return
44.262%
Drawdown
49.200%
Expectancy
0.647
Net Profit
529768.007%
Sharpe Ratio
1.059
Probabilistic Sharpe Ratio
26.292%
Loss Rate
46%
Win Rate
54%
Profit-Loss Ratio
2.07
Alpha
0.305
Beta
0.837
Annual Standard Deviation
0.336
Annual Variance
0.113
Information Ratio
0.954
Tracking Error
0.309
Treynor Ratio
0.425
Total Fees
$854579.56
Estimated Strategy Capacity
$0
Lowest Capacity Asset
GHLD XIW85JA47811
#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(1999, 1, 1)
        self.SetEndDate(2022, 5, 20)
        self.SetCash(100000) 

        self.symbol = self.AddEquity('SPY', Resolution.Minute).Symbol
        
        self.long = []
        self.flag = True
         
        self.selection_flag = True
        self.yearlyflag = False
        self.UniverseSettings.Resolution = Resolution.Minute
        self.AddUniverse(self.CoarseSelectionFunction, self.FineSelectionFunction)
        
        self.Schedule.On(self.DateRules.MonthEnd(self.symbol), self.TimeRules.AfterMarketOpen(self.symbol, 30), self.Selection)
    def OnSecuritiesChanged(self, changes):
        for security in changes.AddedSecurities:
            security.SetFeeModel(CustomFeeModel())
            security.SetLeverage(10)
            security.SetSlippageModel(VolumeShareSlippageModel())

    def CoarseSelectionFunction(self, coarse):
        selected = [x.Symbol for x in coarse if x.HasFundamentalData and x.Market == 'usa']
        return selected
    
    def FineSelectionFunction(self, fine):
        filtered_fine = [x for x in fine if (x.OperationRatios.TotalDebtEquityRatio.ThreeMonths < 0.6) 
            and (x.OperationRatios.ROE.ThreeMonths > 0.2) 
            if (x.CompanyReference.CountryId == "USA")
            and (x.CompanyReference.PrimaryExchangeID in ["NYS","NAS"])
            and (x.FinancialStatements.BalanceSheet.StockholdersEquity.TwelveMonths > 0.0)
            and (x.CompanyProfile.TotalEmployeeNumber > 2)
            and (x.Symbol.Value not in "SJT")
            and ((self.Time - x.SecurityReference.IPODate).days > 365)]
        #roe_fine = sorted(filtered_fine, key=lambda x: (x.OperationRatios.ROE.ThreeMonths), reverse=True)
        #dte_fine = sorted(filtered_fine, key=lambda x: (x.OperationRatios.TotalDebtEquityRatio.ThreeMonths))
        #matched = set(roe_fine[:100]).intersection(dte_fine[:100])
        #self.long = [x.Symbol for x in matched]
        #if self.flag:
        #    for x in matched:
         #       self.Debug(x.Symbol)
         #       self.Debug(x.OperationRatios.TotalDebtEquityRatio.ThreeMonths)
          #      self.Debug(x.OperationRatios.ROE.ThreeMonths)
        #self.flag = False
        self.long = [x.Symbol for x in filtered_fine]
        for symbol in self.long:
            self.Log(symbol)
        return self.long
    
    def OnData(self, data):
        if self.yearlyflag:
            self.yearlyflag = False
            self.Liquidate()
            for symbol in self.long:
                if symbol in data and data[symbol]:
                    self.Debug("BUY: " + str(symbol))
                    self.Log("BUY: " + str(symbol))  
                    self.SetHoldings(symbol, 1 / len(self.long))
            return
        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.Debug("LIQUIDATE: " + str(symbol))
                self.Log("LIQUIDATE: " + str(symbol))                
                self.Liquidate(symbol)

        for symbol in self.long:
            if symbol in data and data[symbol] and symbol not in stocks_invested:
                self.Debug("BUY: " + str(symbol))
                self.Log("BUY: " + str(symbol))  
                self.SetHoldings(symbol, 1 / len(self.long))

        self.long.clear()
    
    def Selection(self):
        if self.Time.month == 12:
            self.yearlyflag = True
            return
        self.Debug("SELECTION FLAG TRUE")
        self.Log("SELECTION FLAG TRUE")
        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"))