Skip to content

Commit c0e8050

Browse files
authored
Merge pull request #229 from AlexCatarino/bug-quantpedia-91
Fixes Logic in Momentum and Style Rotation Effect
2 parents 759086b + 4af0db1 commit c0e8050

File tree

3 files changed

+28
-23
lines changed

3 files changed

+28
-23
lines changed

04 Strategy Library/91 Momentum and Style Rotation Effect/02 Method.html

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,25 @@
66
<div class="section-example-container">
77
<pre class="python">
88
def Initialize(self):
9+
910
self.SetStartDate(2001, 1, 1)
1011
self.SetEndDate(2018, 8, 1)
1112
self.SetCash(100000)
12-
self.tickers = [
13-
"IJJ", # iShares S&P MidCap 400 Value Index ETF
14-
"IJS", # iShares S&P SmallCap 600 Value ETF
15-
"IVE", # iShares S&P 500 Value Index ETF
16-
"IVW", # iShares S&P 500 Growth ETF
17-
"IJK", # iShares S&P Mid-Cap 400 Growth ETF
18-
"IJT", # iShares S&P Small-Cap 600 Growth ETF
19-
]
20-
self.symbols = []
21-
for ticker in self.tickers:
22-
self.symbols.append(self.AddEquity(ticker, Resolution.Daily).Symbol)
23-
self.SetWarmUp(timedelta(days=12*20))
24-
# save all momentum indicator in the dictionary
25-
self.mom = {i:self.MOM(i, 12*20, Resolution.Daily) for i in self.symbols}
13+
14+
tickers = ["IJJ", # iShares S&P Mid-Cap 400 Value Index ETF
15+
"IJK", # iShares S&P Mid-Cap 400 Growth ETF
16+
"IJS", # iShares S&P Small-Cap 600 Value ETF
17+
"IJT", # iShares S&P Small-Cap 600 Growth ETF
18+
"IVE", # iShares S&P 500 Value Index ETF
19+
"IVW"] # iShares S&P 500 Growth ETF
20+
21+
lookback = 12*20
22+
23+
# Save all momentum indicator into the dictionary
24+
self.mom = dict()
25+
for ticker in tickers:
26+
symbol = self.AddEquity(ticker, Resolution.Daily).Symbol
27+
self.mom[symbol] = self.MOM(symbol, lookback)
2628
</pre>
2729
</div>
2830
<p>
@@ -33,12 +35,15 @@
3335
<div class="section-example-container">
3436
<pre class="python">
3537
def Rebalance(self):
38+
# Order the MOM dictionary by value
3639
sorted_mom = sorted(self.mom, key = lambda x: self.mom[x].Current.Value)
37-
invested = [x.Key for x in self.Portfolio if x.Value.Invested]
38-
for i in invested:
39-
if i not in [sorted_mom[0], sorted_mom[1]]:
40-
self.Liquidate(i)
41-
self.SetHoldings(sorted_mom[0], -0.5)
42-
self.SetHoldings(sorted_mom[-1], 0.5)
40+
41+
# Liquidate the ETFs that are no longer selected
42+
for symbol in sorted_mom[1:-1]:
43+
if self.Portfolio[symbol].Invested:
44+
self.Liquidate(symbol, 'No longer selected')
45+
46+
self.SetHoldings(sorted_mom[-1], -0.5) # Short the ETF with lowest MOM
47+
self.SetHoldings(sorted_mom[0], 0.5) # Long the ETF with highest MOM
4348
</pre>
44-
</div>
49+
</div>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<div class="qc-embed-frame" style="display: inline-block; position: relative; width: 100%; min-height: 100px; min-width: 300px;">
22
<div class="qc-embed-dummy" style="padding-top: 56.25%;"></div>
33
<div class="qc-embed-element" style="position: absolute; top: 0; bottom: 0; left: 0; right: 0;">
4-
<iframe class="qc-embed-backtest" height="100%" width="100%" style="border: 1px solid #ccc; padding: 0; margin: 0;" src="https://www.quantconnect.com/terminal/index.php?key=processCache&request=embedded_backtest_d0b1d400ad21477d83ae2b4f85616318.html"></iframe>
4+
<iframe class="qc-embed-backtest" height="100%" width="100%" style="border: 1px solid #ccc; padding: 0; margin: 0;" src="https://www.quantconnect.com/terminal/processCache?request=embedded_backtest_95cffbeec0d003da873b791d3a10f60f.html"></iframe>
55
</div>
66
</div>

quantpedia.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
77: "0483e5a7094604254ab37eda8b5141b8",
3535
78: "27fb5f05b0e48f488f0994d8d83ddc77",
3636
83: "fdfcddd132eaf55039d867c03efe3012",
37-
91: "d0b1d400ad21477d83ae2b4f85616318",
37+
91: "95cffbeec0d003da873b791d3a10f60f",
3838
100: "5ee5507fef6bf190ae533ce05ccaa785",
3939
102: "cd2d187e44a00c7b19f64aee8b0895d9",
4040
113: "9b1291a5f08dcc07df86363e46144084",

0 commit comments

Comments
 (0)