liveBook · Manning

Long-only constraint

As explained in Build a Robo-Advisor with Python (From Scratch)

A long-only constraint requires every portfolio weight to be nonnegative, prohibiting short positions.

A long-only constraint requires every portfolio weight or position to be nonnegative. It prevents short positions by enforcing w ≥ 0 in an optimization model. section 10.3.1 section 13.3.2

In a mean-variance allocation, LongOnlyConstraint is combined with FullInvestmentConstraint and other limits, such as a volatility bound. The constraint itself needs no asset-specific inputs: it simply applies a nonnegativity condition to the optimization variable w. section 10.3.1

Portfolio allocation

A maximum-expected-return problem can combine LongOnlyConstraint(), FullInvestmentConstraint(), and a 15% volatility constraint. The resulting weights allocate 73.1976% to VWO and 26.8023% to AGG, with zero weights for VTI, VEA, BNDX, and EMB; every position remains nonnegative and the weights sum to one.

The same rule applies during rebalancing, where the variables represent monetary positions rather than normalized weights. Its generate_constraint method returns one condition v ≥ 0 for every position, alongside a full-investment condition requiring total invested value to equal the desired amount. section 13.3.2

How Build a Robo-Advisor with Python (From Scratch) covers it

How deep it goes
Implements nonnegativity in Python and cvxpy, but does not develop the underlying optimization theory.
Examples
Uses ETF allocation outputs and rebalancing position variables to demonstrate nonnegative portfolio holdings.
What it assumes
Assumes portfolio weights or positions and introduces the constraint-class interface used to generate optimization conditions.
What you can do after
Build it from scratch as a reusable constraint class for cvxpy portfolio models.
How it connects
Connects portfolio optimization with full investment, volatility constraint, rebalancing, and tax minimization.

This explainer was generated by Manning's knowledge graph from the text of Build a Robo-Advisor with Python (From Scratch). The section links point to the parts of the book it draws on.