Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,25 @@
<div class="section-example-container">
<pre class="python">
def Initialize(self):

self.SetStartDate(2001, 1, 1)
self.SetEndDate(2018, 8, 1)
self.SetCash(100000)
self.tickers = [
"IJJ", # iShares S&P MidCap 400 Value Index ETF
"IJS", # iShares S&P SmallCap 600 Value ETF
"IVE", # iShares S&P 500 Value Index ETF
"IVW", # iShares S&P 500 Growth ETF
"IJK", # iShares S&P Mid-Cap 400 Growth ETF
"IJT", # iShares S&P Small-Cap 600 Growth ETF
]
self.symbols = []
for ticker in self.tickers:
self.symbols.append(self.AddEquity(ticker, Resolution.Daily).Symbol)
self.SetWarmUp(timedelta(days=12*20))
# save all momentum indicator in the dictionary
self.mom = {i:self.MOM(i, 12*20, Resolution.Daily) for i in self.symbols}

tickers = ["IJJ", # iShares S&P Mid-Cap 400 Value Index ETF
"IJK", # iShares S&P Mid-Cap 400 Growth ETF
"IJS", # iShares S&P Small-Cap 600 Value ETF
"IJT", # iShares S&P Small-Cap 600 Growth ETF
"IVE", # iShares S&P 500 Value Index ETF
"IVW"] # iShares S&P 500 Growth ETF

lookback = 12*20

# Save all momentum indicator into the dictionary
self.mom = dict()
for ticker in tickers:
symbol = self.AddEquity(ticker, Resolution.Daily).Symbol
self.mom[symbol] = self.MOM(symbol, lookback)
</pre>
</div>
<p>
Expand All @@ -33,12 +35,15 @@
<div class="section-example-container">
<pre class="python">
def Rebalance(self):
# Order the MOM dictionary by value
sorted_mom = sorted(self.mom, key = lambda x: self.mom[x].Current.Value)
invested = [x.Key for x in self.Portfolio if x.Value.Invested]
for i in invested:
if i not in [sorted_mom[0], sorted_mom[1]]:
self.Liquidate(i)
self.SetHoldings(sorted_mom[0], -0.5)
self.SetHoldings(sorted_mom[-1], 0.5)

# Liquidate the ETFs that are no longer selected
for symbol in sorted_mom[1:-1]:
if self.Portfolio[symbol].Invested:
self.Liquidate(symbol, 'No longer selected')

self.SetHoldings(sorted_mom[-1], -0.5) # Short the ETF with lowest MOM
self.SetHoldings(sorted_mom[0], 0.5) # Long the ETF with highest MOM
</pre>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="qc-embed-frame" style="display: inline-block; position: relative; width: 100%; min-height: 100px; min-width: 300px;">
<div class="qc-embed-dummy" style="padding-top: 56.25%;"></div>
<div class="qc-embed-element" style="position: absolute; top: 0; bottom: 0; left: 0; right: 0;">
<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>
<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>
</div>
</div>
2 changes: 1 addition & 1 deletion quantpedia.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
77: "0483e5a7094604254ab37eda8b5141b8",
78: "27fb5f05b0e48f488f0994d8d83ddc77",
83: "fdfcddd132eaf55039d867c03efe3012",
91: "d0b1d400ad21477d83ae2b4f85616318",
91: "95cffbeec0d003da873b791d3a10f60f",
100: "5ee5507fef6bf190ae533ce05ccaa785",
102: "cd2d187e44a00c7b19f64aee8b0895d9",
113: "9b1291a5f08dcc07df86363e46144084",
Expand Down