Skip to content

Commit ca37faa

Browse files
committed
Fixes Monthly Universe Selection Update
- Fixes the issue on the Universe Selection function returning different lists of symbols. We need to keep a single list that reflects the final selection at Fine Fundamental. - Refactors the algorithm to use a dictionary of `Momentum` instead of a `SymbolData` and use an integer to track the month change instead of Schedule Events. - Fixes the securities selection based on momentum: instead of buying those with the hightest momentum, the algorithm was buying those with the lowest.
1 parent 039aa74 commit ca37faa

File tree

3 files changed

+5
-22
lines changed

3 files changed

+5
-22
lines changed

04 Strategy Library/21 Momentum Effect in Stocks/02 Method.html

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,15 @@ <h4>Momentum Calculation</h4>
66
<p>
77
Momentum is the absolute difference in stocks.
88
\[Momentum = Close_{today}-Close_{N-days-ago}\]
9-
LEAN has the Momentum indicator. We create a class to save the momentum value and warm up the indictor for each symbol.
10-
<div class="section-example-container">
11-
<pre class="python">
12-
class SymbolData:
13-
def __init__(self, symbol, lookback):
14-
self.symbol = symbol
15-
self.MOM = Momentum(lookback)
16-
17-
def WarmUpIndicator(self, history):
18-
# warm up the Momentum indicator with the history request
19-
for tuple in history.itertuples():
20-
item = IndicatorDataPoint(self.symbol, tuple.Index, float(tuple.close))
21-
self.MOM.Update(item)
22-
</pre>
23-
</div>
249
<p>
25-
Dictionary <code>self.symbolDataDict</code> is used to save the momentum class instance <code>SymbolData</code> for each symbol.
26-
In <code>OnSecuritiesChanged</code> event method, we add the newly selected symbol to the dictionary and initialize the momentum indicator with the history request. For symbols removed from the universe, we remove it from the dictionary. Each day in <code>OnData</code>, the
27-
Momentum indicator for all symbols in the dictionary will be updated with the latest closing price.
10+
Dictionary <code>self.mom</code> is used to save the LEAN Momentum class instance <code>Momentum</code> for each symbol.
11+
In <code>OnSecuritiesChanged</code> event method, we add the newly selected symbol to the dictionary and initialize the momentum indicator with the history request. For symbols removed from the universe, we remove it from the dictionary and liquidate its positions. Each day in <code>OnData</code>, the Momentum indicator for all symbols in the dictionary will be updated with the latest closing price.
2812
</p>
2913
<p>
3014
We choose a period of 12 months for the momentum indicator. Stocks with the best 12-month momentum (12-month performance) are then added to our portfolio and are weighted equally.
3115
</p>
3216

3317
<h4> Monthly Rebalance</h4>
3418
<p>
35-
The portfolio is rebalanced once a month. The coarse and fine universe selection is set to default to run at midnight once a day. To make the universe selection run at the first trading day each month, we use the bool variable
36-
<code>self.monthly_rebalance</code> to manage the universe selection. At the start of each month, the universe selection will filter new stocks. On all other days, the universe selection function will return the same symbols. In contrast to returning an empty list, returning the same symbols as before is a better way for monthly rebalance universe selection. Since if there are no open positions for certain symbol, returning empty list will stop the data subscription of that symbol halt updates of the indicator.
19+
The portfolio is rebalanced once a month. The coarse and fine universe selection is set to default to run at midnight once a day. To make the universe selection run at the first trading day each month, we use the int variable <code>self.month</code> that tracks the current month to manage the universe selection. At the start of each month, the universe selection will filter new stocks. On all other days, the universe selection function will return the same symbols <code>self.symbols</code>. In contrast to returning an empty list, returning the same symbols as before is a better way for monthly rebalance universe selection. Since if there are no open positions for certain symbol, returning empty list will stop the data subscription of that symbol halt updates of the indicator.
3720
</p>
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_ec30318cc245fe200ca1261b7f26f17b.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_6e3182577984f2c6950b7aacfc17301d.html"></iframe>
55
</div>
66
</div>

quantpedia.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
8 : "336bbb8e9e4a55a6adfa00c0d9f904bb",
99
12: "72532ccafeaa844c81aa75c5696b4b24",
1010
13: "05d0edd633b1438852d1d641af0224ae",
11-
14: "ec30318cc245fe200ca1261b7f26f17b",
11+
14: "6e3182577984f2c6950b7aacfc17301d",
1212
15: "bd83aa417032f8407382a1c065aa7511",
1313
16: "afd2c9d67fd51d602bf8eac2ef28d712",
1414
18: "ba1bb35b26896e2e710a510f62230a24",

0 commit comments

Comments
 (0)