# Algorithm Framework

## Portfolio Construction

### Portfolio Construction

### Introduction

The Portfolio Construction Model receives Insight objects from the Alpha Model and uses them to create PortfolioTarget objects for the execution model. A Portfolio Target provides the number of units of the asset we'd like to hold. To set your portfolio construction model you should use the
`SetPortfolioConstruction( IPortfolioConstructionModel )`

`self.SetPortfolioConstruction( IPortfolioConstructionModel )`

method.
This should be done from your algorithm `Initialize()`

`def Initialize()`

method.

### Portfolio Construction Model Structure

Portfolio Construction Models have one primary method: CreateTargets(). This method takes the algorithm object, and a list of Insight objects. You should seek to answer the question - how many shares/contracts should I buy based on the insight predictions I've been presented?

Like all Algorithm Framework models; the Portfolio Construction Model also receives the OnSecuritiesChanged() event which can optionally be used to perform actions when there are security additions or removals. If you are inheriting from the PortfolioConstructionModel base class this is an optional method.

// Portfolio construction scaffolding class; basic method args. class MyPortfolioConstructionModel : PortfolioConstructionModel { // Create list of PortfolioTarget objects from Insights List<IPortfolioTarget> CreateTargets(QCAlgorithmFramework algorithm, Insight[] insights) { } // OPTIONAL: Security change details void OnSecuritiesChanged(QCAlgorithmFramework algorithm, SecurityChanges changes) { // Security additions and removals are pushed here. // This can be used for setting up algorithm state } }

# Portfolio construction scaffolding class; basic method args. class MyPortfolioConstructionModel(PortfolioConstructionModel): # Create list of PortfolioTarget objects from Insights def CreateTargets(self, algorithm, insights): pass # OPTIONAL: Security change details def OnSecuritiesChanged(self, algorithm, changes): # Security additions and removals are pushed here. # This can be used for setting up algorithm state. # changes.AddedSecurities: # changes.RemovedSecurities: pass

### Creating Portfolio Targets

The PortfolioTarget class accepts two parameters for its constructor: Symbol and Quantity. This is consumed by the execution model which seeks to reach this target as efficiently as possible, you should not assume orders are filled immediately.

// Create a new portfolio target for 1200 IBM shares. var target = new PortfolioTarget("IBM", 1200);

# Create a new portfolio target for 1200 IBM shares. target = PortfolioTarget("IBM", 1200)

Margin accounts can also use the Percent(algorithm, Symbol, percent) helper method. This calculates a quantity equivalent to a percentage of portfolio value.

// Calculate target equivalent to 10% of portfolio value var target = PortfolioTarget.Percent(algorithm, "IBM", 0.1);

# Calculate target equivalent to 10% of portfolio value target = PortfolioTarget.Percent(algorithm, "IBM", 0.1)

Your Portfolio Construction Model should return a targets array from your CreateTargets method:

// Return an array of targets return new PortfolioTarget[] { new PortfolioTarget("IBM", 1200) };

# Return an array of targets return [ PortfolioTarget("IBM", 1200) ]

### Null Portfolio Construction

The NullPortfolioConstructionModel can be used to skip the execution phase of the algorithm i.e. *"do nothing"*. This is useful when you're trying analyze the Alpha Model in isolation. All Alpha Streams algorithms can use Null Portfolio Construction and Null Execution Models.

SetPortfolioConstruction( new NullPortfolioConstructionModel() );

self.SetPortfolioConstruction( NullPortfolioConstructionModel() )

You can view the C# *implementation* of this model in GitHub.You can view the Python *implementation* of this model in GitHub.

### Equal Weighting Portfolio Construction

The Equal Weighting Portfolio Construction Model assigns an equal share of the portfolio to insights supplied to it. This is useful for universe rotation based with simple portfolio strategies. To use it in your algorithm you need to create an instance of EqualWeightingPortfolioConstructionModel.

SetPortfolioConstruction( new EqualWeightingPortfolioConstructionModel() );

self.SetPortfolioConstruction( EqualWeightingPortfolioConstructionModel() )

You can view the C# *implementation* of this model in GitHub.You can view the Python *implementation* of this model in GitHub.

### Black Litterman Portfolio Construction

The Black Litterman Portfolio Construction Model takes Insights from multiple alphas and combines them into a single portfolio. These multiple Alpha Model sources can be seen as the "investor views" required of the classical model.

You can view the C# *implementation* of this model in GitHub.You can view the Python *implementation* of this model in GitHub.