Skip to content

portfolio-construction Specification

Purpose

How a strategy builds a target portfolio and the orders that reach it, the same in C#, Python and TypeScript (strategy-devkit): return estimates and mean-variance allocation give target weights; a rebalance plan turns holdings and a target into limit orders that follow the venue's lot size, tick size and minimum order value. Allocation is floating point; rebalancing is exact, and weights become exact quantities only in the plan.

Related specs: - Behavior — Decide, and Decision State Across Activations (own open orders count as held; unknown holdings are not traded). - Pub/Sub Topics — Instrument Snapshot Payload (tick size, lot size, min_notional). - Core Data Types — exact decimal amounts, never rounded silently.

Requirements

Requirement: Rebalance Plan

strategy-devkit SHALL offer, in C#, Python and TypeScript, a pure function from the current portfolio, a target, prices, instruments and rules to a rebalance plan: limit orders, the symbols skipped with their reasons, and each symbol's residual (the part of its target the orders do not reach). A target SHALL be quantities per symbol, or weights per symbol with the portfolio value they apply to.

Scenario: Weights to orders

  • WHEN a portfolio worth 10,000 USDT holds no BTC and the target gives BTC_USDT weight 0.5 at price 50,000
  • THEN the plan SHALL hold one buy of 0.1 BTC, as a limit order

Requirement: Current Exposure

The plan SHALL count the strategy's own open orders as held (holdings plus pending), and SHALL never trade a symbol whose holding is unknown: that symbol SHALL be skipped with the reason holding unknown (behavior, Decision State Across Activations).

Scenario: An order already open

  • WHEN the target is 1 BTC, the strategy holds 0.4 and has a buy of 0.6 open
  • THEN the plan SHALL hold no BTC order

Requirement: Lot and Tick Rounding

Each order's quantity SHALL be rounded toward zero to the instrument's lot size, so a plan never overshoots its target. Its limit price SHALL be rounded to the tick size away from the market: a buy down, a sell up.

Scenario: A quantity between lots

  • WHEN the delta is 0.123456 BTC and the lot size is 0.0001
  • THEN the order SHALL be for 0.1234 BTC and the residual 0.000056

Requirement: Minimum Trade Size

A delta whose rounded quantity is zero, whose value at the limit price is below the instrument's minimum order value (min_notional) or below the rules' minimum order value, or whose weight change is inside the rules' drift band, SHALL be skipped with its reason.

Scenario: Below the minimum order value

  • WHEN a rounded buy is worth 4 USDT and the instrument's minimum order value is 5 USDT
  • THEN the symbol SHALL be skipped with the reason below minimum order value

Requirement: Sells Before Buys

The plan SHALL list sells before buys. When the buys' total value exceeds the quote balance plus the sells' proceeds, less the rules' buffer for fees and price moves, the largest buy (by value, then symbol) SHALL be cut by the fewest whole lots that remove the excess, then the next, until they fit; a buy cut below a minimum is skipped.

Scenario: Not enough cash

  • WHEN the buys need 1,200 USDT and the quote balance plus the sells' proceeds, less the buffer, is 1,000 USDT
  • THEN the buys SHALL be reduced to fit 1,000 USDT, and the residuals SHALL show what is left out

Requirement: Limit Price Policy

The rules SHALL name how a limit price is chosen: at the mid, or at the near touch (best bid for a buy, best ask for a sell), optionally a stated number of ticks toward the market. There SHALL be no market orders.

Scenario: At the touch

  • WHEN the policy is at the touch and the best bid is 49,999.99
  • THEN a buy SHALL have the limit price 49,999.99

Requirement: Deterministic Order Ids

Each order's id SHALL be derived from the plan's id and the symbol, so a plan made again from the same inputs, for example when a decision is redelivered, gives the same ids.

Scenario: A redelivered decision

  • WHEN the same plan id and inputs are planned twice
  • THEN both plans SHALL hold the same orders with the same ids

Requirement: Weight Conversion

A weight SHALL become an exact decimal from its shortest round-trip text, rounded half to even to 9 fractional digits; a target quantity is then weight × portfolio value ÷ valuation price (the mid) in exact rational arithmetic, and a residual is reported rounded toward zero to 8 fractional digits.

Scenario: A weight from a solver

  • WHEN a weight is the double nearest 0.1 + 0.2
  • THEN it SHALL be used as 0.300000000

Requirement: One Quote Currency

A plan SHALL hold symbols of one quote currency. A target that mixes quote currencies SHALL be refused with an error naming them, never converted.

Scenario: USDT and USDC

  • WHEN a target holds BTC_USDT and ETH_USDC
  • THEN planning SHALL fail naming USDT and USDC

Requirement: Return Estimates

strategy-devkit SHALL estimate, from a window of prices per symbol at one bar length, the expected returns (mean log return) and their covariance, both per bar and optionally scaled to a stated horizon, with optional Ledoit–Wolf shrinkage of the covariance. A strategy MAY pass its own estimates instead.

Scenario: Shrinkage

  • WHEN the covariance is estimated with shrinkage
  • THEN it SHALL be the estimated intensity δ (between 0 and 1) times the constant-correlation matrix plus 1 − δ times the sample covariance (divisor n), as Ledoit and Wolf's covCor

Requirement: Mean-Variance Allocation

strategy-devkit SHALL solve, for a universe and estimates, the minimum-variance portfolio, the minimum-variance portfolio at a target expected return, and the maximum-Sharpe portfolio at a stated risk-free rate. The result SHALL be weights per symbol with the portfolio's expected return and volatility.

Scenario: Minimum volatility for a drift

  • WHEN the target expected return lies between the minimum-variance portfolio's and the largest single asset's
  • THEN the weights SHALL meet the target return and have the least variance of all portfolios that do, within the stated tolerance

Requirement: Allocation Constraints

The weights SHALL sum to one (fully invested). They SHALL be long-only (each weight at least zero) unless the problem gives per-asset bounds, which MAY allow short positions. The maximum-Sharpe portfolio SHALL be long-only.

Scenario: Long-only by default

  • WHEN the unconstrained solution would short an asset
  • THEN the default problem SHALL give that asset weight zero or more

Requirement: Infeasible Targets

A target expected return no long-only (or bounded) portfolio reaches SHALL be reported as an infeasible result naming the reachable range, not thrown.

Scenario: Too much drift

  • WHEN the target expected return is above every asset's expected return under long-only
  • THEN the result SHALL be infeasible with the reachable range of expected returns

Requirement: Numeric Libraries

The implementations SHALL use MathNet.Numerics (C#), numpy and scipy (Python) and ml-matrix (TypeScript). Weights are floating point; they SHALL become exact quantities only in the rebalance plan, at a stated precision.

Scenario: Weights become quantities

  • WHEN a weight target is planned
  • THEN each quantity SHALL be computed in exact decimals from the weight at the stated precision, then rounded to the lot size

Requirement: Portfolio Construction Conformance

Shared vectors SHALL keep the languages alike: conformance/rebalance.v1.json exactly (decimal text in and out) and conformance/allocation.v1.json within a stated tolerance on weights. Every language SHALL run both in its CI.

Scenario: Three languages, one plan

  • WHEN the C#, Python and TypeScript implementations plan the same vector
  • THEN they SHALL give the same orders, skips and residuals