matlab/matlab-agentic-toolkit / skills-catalog/computational-finance/matlab-optimize-portfolio
matlab-optimize-portfolio
VerifiedFormulate and solve portfolio optimization problems in MATLAB. Routes to the Financial Toolbox Portfolio object for mean-variance (Markowitz), minimum- variance, target-return, maximum Sharpe ratio, and efficient frontier problems with bounds, budget, group, turnover, tracking-error, or cardinality constraints; or to the problem-based framework (optimproblem/optimvar/solve) when constraints exceed what Portfolio can express: integer round lots, whole shares, per-lot or fixed commissions, minimum trade sizes, semi-integer positions, wash-sale rules, realized capital-gains caps, tax-loss harvesting, asymmetric or bucketed transaction- cost caps, or a custom efficient frontier over such constraints. Use when users ask about portfolio optimization, when they attempt quadprog, fmincon, intlinprog, or ga for a portfolio problem, or when code rounds Portfolio weights to whole shares or lots.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 9.0 KB |
| manifest.yaml | config | 762 B |
| references/portfolio-object.md | markdown | 6.3 KB |
| references/portfolio-object/examples.md | markdown | 8.8 KB |
| references/portfolio-object/formulation-max-sharpe.md | markdown | 3.6 KB |
| references/portfolio-object/formulation-mean-variance.md | markdown | 3.7 KB |
| references/portfolio-object/reference-core.md | markdown | 6.8 KB |
| references/problem-based.md | markdown | 19.2 KB |
| references/problem-based/custom-frontier.md | markdown | 9.3 KB |
| references/problem-based/disjunctive-big-m.md | markdown | 5.9 KB |
| references/problem-based/mip-recognition.md | markdown | 4.0 KB |
| references/problem-based/problem-based-framework.md | markdown | 6.1 KB |
| references/problem-based/problem-data-hygiene.md | markdown | 7.4 KB |
| references/problem-based/tax-aware-constraints.md | markdown | 5.0 KB |
| references/problem-based/trade-aux-variables.md | markdown | 5.7 KB |
references/problem-based/tax-aware-constraints.md
View on GitHub# Formulate Tax-Aware Constraints
## Wash-sale — historical (asset-level)
Block buys on assets tied to a recent loss trade. Vectorized indexing on
the buy indicator does exactly this:
```matlab
lossAssets = [3, 7, 12, 22]; % from user data
buyAsset = optimvar("buyAsset", nAssets, Type="integer", LowerBound=0, UpperBound=1);
prob.Constraints.washSaleHistorical = buyAsset(lossAssets) == 0;
```
Vectorized indexing on an `optimvar` expands into one equality per index.
## Wash-sale — within-optimization (lot-level)
When the model itself decides which loss lots to sell in this run, couple
the sell-of-loss-lot decision with the buy indicator for that asset.
Preprocessing (outside the model) identifies which lots are loss lots and
their asset index.
Two formulations are semantically equivalent for the standard rule
("selling any share from any loss lot of asset `a` blocks buying asset
`a`"). Both are correct — pick based on the size and structure of the
problem.
**Form A — per-loss-lot binaries (canonical).** One binary per loss lot.
Directly expresses "did this specific loss lot participate in a sale":
```matlab
% Preprocessing: nLossLots x 1 vectors.
lossLotShares = [300; 100; 600]; % example: shares held per loss lot
lossLots2AssetsIdx = [1; 1; 4]; % example: asset index per loss lot
nLossLots = numel(lossLotShares);
lossLotShareSold = optimvar("lossLotShareSold", nLossLots, LowerBound=0);
lossLotSold = optimvar("lossLotSold", nLossLots, Type="integer", ...
LowerBound=0, UpperBound=1);
% Sell no more than the lot holds; couple amount to indicator
prob.Constraints.lossLotSaleUB = lossLotShareSold <= lossLotShares .* lossLotSold;
% If any loss lot on asset a is sold, cannot buy asset a
prob.Constraints.washSaleWithinOpt = ...
buyAsset(lossLots2AssetsIdx) <= 1 - lossLotSold;
```
The vectorized indexing `buyAsset(lossLots2AssetsIdx)` expands per
loss-lot row.
**Form B — per-loss-asset binary with an auxiliary "loss activity"
variable.** One binary per *asset* with at least one loss lot, plus a
continuous aux capturing how much loss-lot activity is happening on that
asset. Fewer binaries when `nLossLots >> nLossAssets`. Equivalent for the
standard rule because the wash-sale trigger is asset-level — it does not
matter *which* loss lot on asset `a` was touched, only *whether* any was.
```matlab
% Preprocessing: nLossAssets x 1 vectors.
lossAssets = [1; 4]; % example: asset index per loss asset
lossActUB = [400; 600]; % example: upper bound on aux, per loss asset
nLossAssets = numel(lossAssets);
washBlk = optimvar("washBlk", nLossAssets, Type="integer", ...
LowerBound=0, UpperBound=1);
lossAct = optimvar("lossAct", nLossAssets, LowerBound=0);
% Any positive loss-lot activity forces washBlk = 1
prob.Constraints.washTrig = lossAct <= lossActUB .* washBlk;
% Block the corresponding buy when washBlk fires
prob.Constraints.washBlk = buyAsset(lossAssets) <= 1 - washBlk;
% lossAct must be tied to actual loss-lot sales by an explicit accounting
% constraint (per-lot sell variables summed by asset, or lossAct >=
% totalSold - nonLossHoldings). Without that link, the wash block does
% not fire.
```
Trade-off: **Form A** is the safer default (direct mapping, no aux
accounting to get wrong). **Form B** cuts binaries when a few assets
carry many loss lots (20 loss lots across 3 assets → 3 binaries instead
of 20), at the cost of one aux variable per loss asset and an accounting
constraint that must be right.
## Realized cap-gains — per-lot G&L
The realized-gains constraint is a **$ cap** on realized gains across all
sold lots. When each lot's dollar gain-or-loss at current market is known
(a per-lot G&L, signed — positive for gain lots, negative for loss lots),
a partial sale of lot `k` realizes `f_k * G_k`.
```matlab
% Preprocessing: per-lot G&L vector, nLots x 1, signed $.
lotGL = zeros(nLots, 1);
% Per-lot fraction sold in [0,1]. Bind together with the Form A loss-lot indicators above
% so the same "fraction of lot k sold" appears in both constraints.
f = optimvar("f", nLots, LowerBound=0, UpperBound=1);
prob.Constraints.capGains = sum(f .* lotGL) <= capGainsCap;
% units: (fraction) * ($) = $ ✓
```
**Why per-lot G&L and not asset-level `price`/`basis`.** Two lots of the
same asset generally have different cost bases. Collapsing "the basis of
asset `a`" to a single number and using `(price(a) - basis(a)) *
sharesSold` mis-prices the cap-gains total whenever any asset has
multiple lots with distinct G&Ls. Per-lot G&L is the tax-correct
formulation and uses less data.
**Share-denominated form.** If the decision is lot-level shares sold, use
per-share G&L:
```matlab
gainPerShare = lotGL ./ lotShares;
prob.Constraints.capGains = sum(lotSharesSold .* gainPerShare) <= capGainsCap;
% units: shares * ($/share) = $ ✓
```
----
Copyright 2026 The MathWorks, Inc.
----
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| receiving-code-reviewobra/superpowersUse when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementation | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirements | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| using-git-worktreesobra/superpowersUse when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallback | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| verification-before-completionobra/superpowersUse when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions always | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |