Overall Statistics Total Trades 1202 Average Win 0.72% Average Loss -0.40% Compounding Annual Return 14.516% Drawdown 31.800% Expectancy 0.575 Net Profit 276.480% Sharpe Ratio 0.766 Loss Rate 44% Win Rate 56% Profit-Loss Ratio 1.80 Alpha 0.032 Beta 0.926 Annual Standard Deviation 0.188 Annual Variance 0.035 Information Ratio 0.169 Tracking Error 0.134 Treynor Ratio 0.155 Total Fees \$30188.21
#https://quantpedia.com/strategies/net-current-asset-value-effect/
from QuantConnect.Data.UniverseSelection import *
import math
import numpy as np
import pandas as pd
import scipy as sp

class NetCurrentAssetValueEffect(QCAlgorithm):

def Initialize(self):

self.SetStartDate(2010, 1, 1)
self.SetEndDate(datetime.now())
self.SetCash(1000000)
self.UniverseSettings.Resolution = Resolution.Daily
self.sorted_by_ncav = None
self.Schedule.On(self.DateRules.MonthStart("SPY"), self.TimeRules.AfterMarketOpen("SPY"), self.rebalance)
# Count the number of months that have passed since the algorithm starts
self.months = -1
self.yearly_rebalance = True
def CoarseSelectionFunction(self, coarse):
if self.yearly_rebalance:
# drop stocks which have no fundamental data or have low price
self.filtered_coarse = [x.Symbol for x in coarse if (x.HasFundamentalData)]
return self.filtered_coarse
else:
return []

def FineSelectionFunction(self, fine):
if self.yearly_rebalance:
# Filter stocks with nonzero Total Assets
fine = [x for x in fine if (x.FinancialStatements.BalanceSheet.TotalAssets.TwelveMonths != 0)]
for i in fine:
i.MarketCap = float(i.EarningReports.BasicAverageShares.ThreeMonths * (i.EarningReports.BasicEPS.TwelveMonths*i.ValuationRatios.PERatio))
fine = [x for x in fine if (x.MarketCap != 0)]

fine = [x for x in fine if ((x.ValuationRatios.WorkingCapitalPerShare*x.EarningReports.BasicAverageShares.Value)/x.MarketCap > 1.5)]
self.sorted_by_ncav = [i.Symbol for i in fine]
self.Debug(str(len(fine)))
for i in fine:
self.Debug(str(i.ValuationRatios.WorkingCapitalPerShare*i.EarningReports.BasicAverageShares.Value/i.MarketCap))

return self.sorted_by_ncav
else:
return []

def rebalance(self):
# yearly rebalance
self.months += 1
if self.months%12 == 0:
self.yearly_rebalance = True

def OnData(self, data):
if not self.yearly_rebalance: return
if self.sorted_by_ncav:
portfolio_size = int(len(self.sorted_by_ncav))
stocks_invested = [x.Key for x in self.Portfolio]
for i in stocks_invested:
#liquidate the stocks not in the filtered
if i not in self.sorted_by_ncav:
self.Liquidate(i)
#long the stocks in the list
elif i in self.sorted_by_ncav:
self.SetHoldings(i, 1/(portfolio_size))
self.yearly_rebalance = False