Skip to content
All library documents

Sector-Weighted Portfolio Construction with Fundamental Universes

Article Strategy library · Author: QuantConnect

Summary

This example shows how to connect a custom coarse and fine fundamental universe selector to a sector-weighted portfolio construction model in an algorithm framework. It sets daily data resolution, defines a short test period and starting cash, then wires universe selection, a constant one-day bullish price insight, and sector-based portfolio construction together.

The coarse selector returns a hand-picked list of U.S. equities that changes during the sample, while the fine selector passes through the resulting symbols. The example illustrates framework integration and how sector weighting can be applied after universe selection; it does not explain the weighting formula or provide performance results. Its short date range and hard-coded symbol lists make it a demonstration of model setup rather than evidence that the approach is profitable or broadly generalizable.

Key ideas

  • A custom coarse selector can supply candidate equities, and a fine selector can pass their symbols into the framework.
  • The portfolio construction model applies sector-based weights to the selected holdings.
  • A constant bullish price insight provides the example's signal input.
  • The code demonstrates framework wiring rather than a tested investment thesis.

Tags

Full text
# SectorWeightingFrameworkAlgorithm


# SectorWeightingFrameworkAlgorithm









This example algorithm defines its own custom coarse/fine fundamental selection model
    with sector weighted portfolio.

This example algorithm defines its own custom coarse/fine fundamental selection model with sector weighted portfolio.

## 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 *

### <summary>
### This example algorithm defines its own custom coarse/fine fundamental selection model
### with sector weighted portfolio.
### </summary>
class SectorWeightingFrameworkAlgorithm(QCAlgorithm):
    '''This example algorithm defines its own custom coarse/fine fundamental selection model
    with sector weighted portfolio.'''

    def initialize(self):

        # Set requested data resolution
        self.universe_settings.resolution = Resolution.DAILY

        self.set_start_date(2014, 4, 2)
        self.set_end_date(2014, 4, 6)
        self.set_cash(100000)

        # set algorithm framework models
        self.set_universe_selection(FineFundamentalUniverseSelectionModel(self.select_coarse, self.select_fine))
        self.set_alpha(ConstantAlphaModel(InsightType.PRICE, InsightDirection.UP, timedelta(1)))
        self.set_portfolio_construction(SectorWeightingPortfolioConstructionModel())

    def on_order_event(self, order_event):
        if order_event.status == OrderStatus.FILLED:
            self.debug(f"Order event: {order_event}. Holding value: {self.securities[order_event.symbol].holdings.absolute_holdings_value}")

    def select_coarse(self, coarse):
        # IndustryTemplateCode of AAPL, IBM and GOOG is N, AIG is I, BAC is B. SPY have no fundamentals
        tickers = ["AAPL", "AIG", "IBM"] if self.time.date() < date(2014, 4, 4) else [ "GOOG", "BAC", "SPY" ]
        return [Symbol.create(x, SecurityType.EQUITY, Market.USA) for x in tickers]

    def select_fine(self, fine):
        return [f.symbol for f in fine]

```

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.