Overall Statistics |
Total Trades 3148 Average Win 0.24% Average Loss -0.27% Compounding Annual Return 8.476% Drawdown 24.300% Expectancy 0.072 Net Profit 73.458% Sharpe Ratio 0.613 Loss Rate 44% Win Rate 56% Profit-Loss Ratio 0.91 Alpha 0.078 Beta -0.033 Annual Standard Deviation 0.121 Annual Variance 0.015 Information Ratio -0.202 Tracking Error 0.18 Treynor Ratio -2.208 Total Fees $3269.17 |
from System.Collections.Generic import List from QuantConnect.Data.UniverseSelection import * class BasicTemplateAlgorithm(QCAlgorithm): def __init__(self): # set the flag for rebalance self.reb = 1 # Number of stocks to pass CoarseSelection process self.num_coarse = 130 # Number of stocks to long/short self.num_fine = 20 self.symbols = None self.first_month = 1 def Initialize(self): self.SetCash(100000) self.SetStartDate(2011,1,1) # if not specified, the Backtesting EndDate would be today #self.SetEndDate(2011,2,1) self.spy = self.AddEquity("SPY", Resolution.Daily).Symbol self.UniverseSettings.Resolution = Resolution.Daily self.AddUniverse(self.CoarseSelectionFunction,self.FineSelectionFunction) # Schedule the rebalance function to execute at the begining of each month self.Schedule.On(self.DateRules.MonthStart(self.spy), self.TimeRules.AfterMarketOpen(self.spy,5), Action(self.rebalance)) def CoarseSelectionFunction(self, coarse): if self.reb == 1: filtered_coarse = [x for x in coarse if x.HasFundamentalData] sortedByDollarVolume = sorted(filtered_coarse, key=lambda x: x.DollarVolume, reverse=True) self.coarse_list = [ x.Symbol for x in sortedByDollarVolume[:self.num_coarse] ] return self.coarse_list def FineSelectionFunction(self, fine): # drop stocks which don't have the information we need. # you can try replacing those factor with your own factors here filtered_fine = [x for x in fine if x.ValuationRatios.PERatio and x.ValuationRatios.BookValueYield and x.ValuationRatios.PricetoEBITDA] self.Log('remained to select %d'%(len(filtered_fine))) # rank stocks by three factor. sortedByfactor1 = sorted(filtered_fine, key=lambda x: x.ValuationRatios.PERatio, reverse=True) sortedByfactor2 = sorted(filtered_fine, key=lambda x: x.ValuationRatios.BookValueYield, reverse=False) sortedByfactor3 = sorted(filtered_fine, key=lambda x: x.ValuationRatios.PricetoEBITDA, reverse=True) stock_dict = {} # assign a score to each stock, you can also change the rule of scoring here. for i,ele in enumerate(sortedByfactor1): rank1 = i rank2 = sortedByfactor2.index(ele) rank3 = sortedByfactor3.index(ele) score = sum([rank1*0.5,rank2*0.3,rank3*0.2]) stock_dict[ele] = score # sort the stocks by their scores self.sorted_stock = sorted(stock_dict.items(), key=lambda d:d[1],reverse=False) sorted_symbol = [x[0] for x in self.sorted_stock] self.Log(str([x.Symbol.Value for x in sorted_symbol])) # sotre the top stocks into the long_list and the bottom ones into the short_list self.long = [x.Symbol for x in sorted_symbol[:self.num_fine]] self.short = [x.Symbol for x in sorted_symbol[-self.num_fine:]] topFine = self.long+self.short return topFine def OnData(self, data): pass def rebalance(self): self.first_month = 1 # if this month the stock are not going to be long/short, liquidate it. long_short_list = self.long + self.short for i in self.Portfolio.Values: if (i.Invested) and (i.Symbol not in long_short_list): self.Liquidate(i.Symbol) # Alternatively, you can liquidate all the stocks at the end of each month. # Which method to choose depends on your investment philosiphy # if you prefer to realized the gain/loss each month, you can choose this method. #self.Liquidate() # Assign each stock equally. Alternatively you can design your own portfolio construction method for i in self.long: self.SetHoldings(i,1.0/self.num_fine) for i in self.short: self.SetHoldings(i,-1.0/self.num_fine) self.reb += 1 if self.reb == 12: self.reb = 0