Introduction
The momentum anomaly says that what was strongly going up in the near past will probably continue to go up shortly. Stocks which outperform peers on 3-12 month period tend to perform well also in the future. This algorithm will explore the momentum effect on large-cap stocks.
Method
Universe Selection
We use the universe selection API to create a momentum portfolio. Our coarse-universe selection eliminates stocks with a price lower than $5 and ETFs which do not have fundamental data. Fine-universe selection chooses the 50 largest companies ranked by market capitalization.
Momentum Percent Calculation
Momentum percent is the relative difference in stocks. \[Momentum = \frac{c_{t}-c_{t-n}}{c_{t-n}}\]
where \(c_{t}\) is the close price on day \(t\) and \(c_{t-n}\) is the close price on day \({t-n}\). The dictionary self.momp
is used to save the LEAN MomentumPercent class instance MomentumPercent
for each Symbol.
In the OnSecuritiesChanged
method, we add the newly selected symbol to the dictionary and initialize the momentum indicator with a history request. For symbols removed from the universe, we remove it from the dictionary and liquidate its positions. Each day in OnData
, the Momentum indicator for all symbols in the dictionary will be updated with the latest closing price.
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.
Monthly Rebalance
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 self.month
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 Universe.UNCHANGED
. In contrast to returning an empty list or a list of previously selected symbols, returning Universe.UNCHANGED
is the best way for monthly rebalance universe selection. If there are no open positions for certain symbol, returning an empty list will stop the data subscription of that symbol and halt updates of the indicator.
Derek Melchin
See the attached backtest for an updated version of the algorithm in PEP8 style.
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Jing Wu
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!