| Overall Statistics |
|
Total Trades 831 Average Win 0.76% Average Loss -1.14% Compounding Annual Return 13.985% Drawdown 27.600% Expectancy 0.245 Net Profit 270.626% Sharpe Ratio 0.643 Probabilistic Sharpe Ratio 6.996% Loss Rate 25% Win Rate 75% Profit-Loss Ratio 0.67 Alpha -0.01 Beta 1.133 Annual Standard Deviation 0.173 Annual Variance 0.03 Information Ratio 0.038 Tracking Error 0.102 Treynor Ratio 0.098 Total Fees $2777.92 Estimated Strategy Capacity $88000000.00 Lowest Capacity Asset LLY R735QTJ8XC9X |
#region imports
from AlgorithmImports import *
#endregion
# https://quantpedia.com/Screener/Details/14
class MomentumEffectAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2009, 7, 1) # Set Start Date
self.SetEndDate(2019, 7, 1) # Set Start Date
self.SetCash(100000) # Set Strategy Cash
self.UniverseSettings.Resolution = Resolution.Daily
self.momp = {} # Dict of Momentum indicator keyed by Symbol
self.lookback = 252 # Momentum indicator lookback period
self.num_coarse = 100 # Number of symbols selected at Coarse Selection
self.num_fine = 50 # Number of symbols selected at Fine Selection
self.num_long = 5 # Number of symbols with open positions
self.month = -1
self.rebalance = False
self.AddUniverse(self.CoarseSelectionFunction, self.FineSelectionFunction)
def CoarseSelectionFunction(self, coarse):
'''Drop securities which have no fundamental data or have too low prices.
Select those with highest by dollar volume'''
if self.month == self.Time.month:
return Universe.Unchanged
self.rebalance = True
self.month = self.Time.month
selected = sorted([x for x in coarse if x.HasFundamentalData and x.Price > 5],
key=lambda x: x.DollarVolume, reverse=True)
return [x.Symbol for x in selected[:self.num_coarse]]
def FineSelectionFunction(self, fine):
'''Select security with highest market cap'''
selected = sorted(fine, key=lambda f: f.MarketCap, reverse=True)
return [x.Symbol for x in selected[:self.num_fine]]
def OnData(self, data):
# Update the indicator
for symbol, mom in self.momp.items():
mom.Update(self.Time, self.Securities[symbol].Close)
if not self.rebalance:
return
# Selects the securities with highest momentum
sorted_mom = sorted([k for k,v in self.momp.items() if v.IsReady],
key=lambda x: self.momp[x].Current.Value, reverse=True)
selected = sorted_mom[:self.num_long]
# Liquidate securities that are not in the list
for symbol, mom in self.momp.items():
if symbol not in selected:
self.Liquidate(symbol, 'Not selected')
# Buy selected securities
for symbol in selected:
self.SetHoldings(symbol, 1/self.num_long)
self.rebalance = False
def OnSecuritiesChanged(self, changes):
# Clean up data for removed securities and Liquidate
for security in changes.RemovedSecurities:
symbol = security.Symbol
if self.momp.pop(symbol, None) is not None:
self.Liquidate(symbol, 'Removed from universe')
for security in changes.AddedSecurities:
if security.Symbol not in self.momp:
self.momp[security.Symbol] = MomentumPercent(self.lookback)
# Warm up the indicator with history price if it is not ready
addedSymbols = [k for k,v in self.momp.items() if not v.IsReady]
history = self.History(addedSymbols, 1 + self.lookback, Resolution.Daily)
history = history.close.unstack(level=0)
for symbol in addedSymbols:
ticker = symbol.ID.ToString()
if ticker in history:
for time, value in history[ticker].dropna().items():
item = IndicatorDataPoint(symbol, time.date(), value)
self.momp[symbol].Update(item)