-
Notifications
You must be signed in to change notification settings - Fork 639
/
momentum-in-mutual-fund-returns.py
116 lines (89 loc) · 4.65 KB
/
momentum-in-mutual-fund-returns.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# https://quantpedia.com/strategies/momentum-in-mutual-fund-returns/
#
# The investment universe consists of equity funds from the CRSP Mutual Fund database.
# This universe is then shrunk to no-load funds (to remove entrance fees).
# Investors then sort mutual funds based on their past 6-month return and divide them into deciles.
# The top decile of mutual funds is then picked into an investment portfolio (equally weighted), and funds are held for three months.
# Other measures of momentum could also be used in sorting (fund’s closeness to 1 year high in NAV and momentum factor loading),
# and it is highly probable that the combined predictor would have even better results than only the simple 6-month momentum.
#
# QC Implementation:
# - Universe consist of approximately 850 mutual funds.
#region imports
from AlgorithmImports import *
#endregion
class MomentuminMutualFundReturns(QCAlgorithm):
def Initialize(self):
# NOTE: most of the data start from 2014 and until 2015 there wasn't any trade
self.SetStartDate(2014, 1, 1)
self.SetCash(100000)
self.data = {}
self.symbols = []
self.period = 21 * 6 # Storing 6 months of daily prices
self.quantile = 10
self.symbol = self.AddEquity('SPY', Resolution.Daily).Symbol
# Load csv file with etf symbols and split line with semi-colon
etf_symbols_csv = self.Download("data.quantpedia.com/backtesting_data/equity/mutual_funds/symbols.csv")
splitted_csv = etf_symbols_csv.split(';')
for symbol in splitted_csv:
self.symbols.append(symbol)
# Subscribe for QuantpediaETF by etf symbol, then set fee model and leverage
data = self.AddData(QuantpediaETF, symbol, Resolution.Daily)
data.SetFeeModel(CustomFeeModel())
data.SetLeverage(5)
self.data[symbol] = RollingWindow[float](self.period)
self.recent_month = -1
def OnData(self, data):
# Update daily prices of etfs
for symbol in self.symbols:
if symbol in data and data[symbol]:
price = data[symbol].Value
self.data[symbol].Add(price)
if self.recent_month == self.Time.month:
return
self.recent_month = self.Time.month
# Rebalance quarterly
if self.recent_month % 3 != 0:
return
performance = {}
for symbol in self.symbols:
# If data for etf are ready calculate it's 6 month performance
if self.data[symbol].IsReady:
if self.Securities[symbol].GetLastData() and (self.Time.date() - self.Securities[symbol].GetLastData().Time.date()).days <= 3:
prices = [x for x in self.data[symbol]]
performance[symbol] = (prices[0] - prices[-1]) / prices[-1]
if len(performance) < self.quantile:
self.Liquidate()
return
decile = int(len(performance) / self.quantile)
# sort dictionary by performance and based on it create sorted list
sorted_by_perf = [x[0] for x in sorted(performance.items(), key=lambda item: item[1], reverse=True)]
# select top decile etfs for investment based on performance
long = sorted_by_perf[:decile]
# Trade execution
invested_etfs = [x.Key for x in self.Portfolio if x.Value.Invested]
for symbol in invested_etfs:
if symbol not in long:
self.Liquidate(symbol)
long_length = len(long)
for symbol in long:
self.SetHoldings(symbol, 1 / long_length)
# Quantpedia data
# NOTE: IMPORTANT: Data order must be ascending (datewise)
class QuantpediaETF(PythonData):
def GetSource(self, config, date, isLiveMode):
return SubscriptionDataSource("data.quantpedia.com/backtesting_data/equity/mutual_funds/{0}.csv".format(config.Symbol.Value), SubscriptionTransportMedium.RemoteFile, FileFormat.Csv)
def Reader(self, config, line, date, isLiveMode):
data = QuantpediaETF()
data.Symbol = config.Symbol
if not line[0].isdigit(): return None
split = line.split(';')
data.Time = datetime.strptime(split[0], "%d.%m.%Y") + timedelta(days=1)
data['settle'] = float(split[1])
data.Value = float(split[1])
return data
# Custom fee model
class CustomFeeModel(FeeModel):
def GetOrderFee(self, parameters):
fee = parameters.Security.Price * parameters.Order.AbsoluteQuantity * 0.00005
return OrderFee(CashAmount(fee, "USD"))