Using a Mean-Reversion Portfolio Model in a Lean Example Algorithm
Summary
This short QuantConnect Lean example shows how to assemble an algorithm around a mean-reversion portfolio construction model. It initializes a historical simulation with daily data for SPY and AAPL, sets cash and a test window, and registers a security initializer that supplies each security with its last known price. The portfolio construction model is then attached to the algorithm.
The example also adds a constant one-day upward price insight. That signal does not itself demonstrate a mean-reversion entry rule; the mean-reversion behavior is delegated to the named portfolio construction component, whose internals are not included here. The source therefore illustrates framework wiring rather than a complete strategy specification. It provides no performance results, trading rationale, risk controls, or comparison against alternatives, so it is useful primarily as a minimal integration example for researchers already familiar with Lean's insight and portfolio model abstractions.
Key ideas
- The example attaches a mean-reversion portfolio construction model to a Lean algorithm.
- It subscribes to daily data for SPY and AAPL over a defined historical test period.
- The algorithm emits a constant one-day upward price insight rather than a described mean-reversion signal.
- The portfolio model's internal allocation rules are not shown in the document.
- No performance results or risk management approach are provided.
Tags
Full text
# MeanReversionPortfolioAlgorithm
# MeanReversionPortfolioAlgorithm
Example algorithm of using MeanReversionPortfolioConstructionModel
## Source (Apache-2.0)
```python
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
# Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from AlgorithmImports import *
from Portfolio.MeanReversionPortfolioConstructionModel import *
class MeanReversionPortfolioAlgorithm(QCAlgorithm):
'''Example algorithm of using MeanReversionPortfolioConstructionModel'''
def initialize(self):
# Set starting date, cash and ending date of the backtest
self.set_start_date(2020, 9, 1)
self.set_end_date(2021, 2, 28)
self.set_cash(100000)
self.set_security_initializer(lambda security: security.set_market_price(self.get_last_known_price(security)))
# Subscribe to data of the selected stocks
self._symbols = [self.add_equity(ticker, Resolution.DAILY).symbol for ticker in ["SPY", "AAPL"]]
self.add_alpha(ConstantAlphaModel(InsightType.PRICE, InsightDirection.UP, timedelta(1)))
self.set_portfolio_construction(MeanReversionPortfolioConstructionModel())
```Shown in full with attribution under the source's licence. Licence: Apache-2.0
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.