lacuna.costs
Cost models, stress scenarios, break-even cost, capacity and liquidity.
Generated from Lacuna 0.14.3. For what these do and when to use them, read the guide.
Classes
BorrowCostModel
class BorrowCostModel(
borrow_rate: str = 'borrow_rate',
holding_days: str = 'holding_days',
borrow_available_time: str | None = None,
day_count: float = 365.0,
missing: MissingBorrowPolicy = 'unknown',
conservative_rate: float | None = None,
currency: str = 'USD',
columns: TradeColumns = ...,
quantity_convention: QuantityConvention = 'signed',
allow_existing: bool = False,
) Annualized borrow charged over explicit short holding intervals.
The model is stateless: it holds no position history. Every row with a negative signed
quantity is charged notional * rate * holding_days / day_count, so holding_days
must be the short exposure interval that row opens. A sell that closes or reduces a long
position opens no short exposure and must carry holding_days = 0 (or be excluded);
otherwise it is charged as if it were a short sale.
| Field | Type | Default |
|---|---|---|
borrow_rate | str | 'borrow_rate' |
holding_days | str | 'holding_days' |
borrow_available_time | str | None | None |
day_count | float | 365.0 |
missing | MissingBorrowPolicy | 'unknown' |
conservative_rate | float | None | None |
currency | str | 'USD' |
columns | TradeColumns | set when created |
quantity_convention | QuantityConvention | 'signed' |
allow_existing | bool | False |
BorrowCostModel.required_fields
def required_fields() -> tuple[str, ...] BorrowCostModel.estimate
def estimate(trades: object, market: object | None = None) -> CostEstimate CapacityScenario
class CapacityScenario(
name: str,
impact_coefficient: float,
spread_bps: float = 0.0,
slippage_bps: float = 0.0,
) Declared nonlinear impact and linear-friction assumptions.
| Field | Type | Default |
|---|---|---|
name | str | required |
impact_coefficient | float | required |
spread_bps | float | 0.0 |
slippage_bps | float | 0.0 |
CapacityScenario.to_parameters
def to_parameters() -> dict[str, JsonValue] CommissionModel
class CommissionModel(
fixed_per_trade: float = 0.0,
per_unit: float = 0.0,
notional_bps: float = 0.0,
minimum: float = 0.0,
currency: str = 'USD',
columns: TradeColumns = ...,
quantity_convention: QuantityConvention = 'signed',
allow_existing: bool = False,
) Fixed, per-unit, and notional commission with an explicit minimum.
| Field | Type | Default |
|---|---|---|
fixed_per_trade | float | 0.0 |
per_unit | float | 0.0 |
notional_bps | float | 0.0 |
minimum | float | 0.0 |
currency | str | 'USD' |
columns | TradeColumns | set when created |
quantity_convention | QuantityConvention | 'signed' |
allow_existing | bool | False |
CommissionModel.required_fields
def required_fields() -> tuple[str, ...] CommissionModel.estimate
def estimate(trades: object, market: object | None = None) -> CostEstimate CompositeCostModel
class CompositeCostModel(
models: tuple[CostModel, ...],
name: str = 'composite',
version: int = 1,
) Validated additive composition that preserves named components.
| Field | Type | Default |
|---|---|---|
models | tuple[CostModel, ...] | required |
name | str | 'composite' |
version | int | 1 |
CompositeCostModel.required_fields
def required_fields() -> tuple[str, ...] CompositeCostModel.estimate
def estimate(trades: object, market: object | None = None) -> CostEstimate CostEstimate
class CostEstimate(
model_name: str,
model_version: int,
currency: str,
components: Mapping[str, tuple[float | None, ...]],
assumptions: Mapping[str, JsonValue],
input_fingerprint: str,
findings: tuple[Finding, ...] = (),
unit: CostUnit = CostUnit.CURRENCY,
) Immutable per-trade cost components with explicit unknown values.
| Field | Type | Default |
|---|---|---|
model_name | str | required |
model_version | int | required |
currency | str | required |
components | Mapping[str, tuple[float | None, ...]] | required |
assumptions | Mapping[str, JsonValue] | required |
input_fingerprint | str | required |
findings | tuple[Finding, ...] | () |
unit | CostUnit | CostUnit.CURRENCY |
CostEstimate.trade_count
trade_count: int Number of rows aligned across components.
CostEstimate.per_trade_costs
per_trade_costs: tuple[float | None, ...] Sum components row by row while preserving unknown totals.
CostEstimate.unknown_rows
unknown_rows: int Number of rows whose all-in cost is not known.
CostEstimate.known_total_cost
known_total_cost: float Sum known row costs without disguising incomplete coverage.
CostEstimate.total_cost
total_cost: float | None Complete total, or None when any row is unknown.
CostEstimate.component_totals
component_totals: Mapping[str, float | None] Complete totals for each named component.
CostEstimate.to_result
def to_result() -> AnalysisResult Convert the estimate to Lacuna’s shared evidence envelope.
CostModel
class CostModel(*args, **kwargs) Pure model protocol for path-independent trade-level cost estimates.
CostModel.name
name: str CostModel.version
version: int CostModel.required_fields
def required_fields() -> tuple[str, ...] CostModel.estimate
def estimate(trades: object, market: object | None = None) -> CostEstimate CostScenario
class CostScenario(
name: str,
spread_bps: float = 0.0,
slippage_bps: float = 0.0,
commission_bps: float = 0.0,
) One deterministic, potentially correlated linear-cost scenario.
| Field | Type | Default |
|---|---|---|
name | str | required |
spread_bps | float | 0.0 |
slippage_bps | float | 0.0 |
commission_bps | float | 0.0 |
CostScenario.to_parameters
def to_parameters() -> dict[str, JsonValue] ParticipationImpactModel
class ParticipationImpactModel(
coefficient: float,
exponent: float = 1.0,
volume: str = 'volume',
volatility: str | None = None,
market_available_time: str | None = None,
volume_horizon: str = 'execution_horizon',
volatility_horizon: str | None = None,
max_participation: float | None = None,
impact_cap: float | None = None,
currency: str = 'USD',
columns: TradeColumns = ...,
quantity_convention: QuantityConvention = 'signed',
) Power-law participation impact, linear by default.
| Field | Type | Default |
|---|---|---|
coefficient | float | required |
exponent | float | 1.0 |
volume | str | 'volume' |
volatility | str | None | None |
market_available_time | str | None | None |
volume_horizon | str | 'execution_horizon' |
volatility_horizon | str | None | None |
max_participation | float | None | None |
impact_cap | float | None | None |
currency | str | 'USD' |
columns | TradeColumns | set when created |
quantity_convention | QuantityConvention | 'signed' |
ParticipationImpactModel.required_fields
def required_fields() -> tuple[str, ...] ParticipationImpactModel.estimate
def estimate(trades: object, market: object | None = None) -> CostEstimate SlippageModel
class SlippageModel(
fixed_per_unit: float = 0.0,
slippage_bps: float = 0.0,
currency: str = 'USD',
columns: TradeColumns = ...,
quantity_convention: QuantityConvention = 'signed',
allow_on_observed_execution: bool = False,
allow_existing: bool = False,
) Adverse fixed-per-unit and proportional slippage magnitude.
| Field | Type | Default |
|---|---|---|
fixed_per_unit | float | 0.0 |
slippage_bps | float | 0.0 |
currency | str | 'USD' |
columns | TradeColumns | set when created |
quantity_convention | QuantityConvention | 'signed' |
allow_on_observed_execution | bool | False |
allow_existing | bool | False |
SlippageModel.required_fields
def required_fields() -> tuple[str, ...] SlippageModel.estimate
def estimate(trades: object, market: object | None = None) -> CostEstimate SpreadModel
class SpreadModel(
quoted_spread_bps: float | None = None,
mode: SpreadMode = 'half',
bid: str = 'bid',
ask: str = 'ask',
currency: str = 'USD',
columns: TradeColumns = ...,
quantity_convention: QuantityConvention = 'signed',
allow_on_observed_execution: bool = False,
allow_existing: bool = False,
) Observed or assumed quoted spread charged as half- or full-spread.
| Field | Type | Default |
|---|---|---|
quoted_spread_bps | float | None | None |
mode | SpreadMode | 'half' |
bid | str | 'bid' |
ask | str | 'ask' |
currency | str | 'USD' |
columns | TradeColumns | set when created |
quantity_convention | QuantityConvention | 'signed' |
allow_on_observed_execution | bool | False |
allow_existing | bool | False |
SpreadModel.required_fields
def required_fields() -> tuple[str, ...] SpreadModel.estimate
def estimate(trades: object, market: object | None = None) -> CostEstimate SquareRootImpactModel
class SquareRootImpactModel(
coefficient: float,
volume: str = 'adv',
volatility: str = 'volatility',
market_available_time: str | None = None,
volume_horizon: str = 'daily',
volatility_horizon: str = 'daily',
max_participation: float | None = None,
impact_cap: float | None = None,
currency: str = 'USD',
columns: TradeColumns = ...,
quantity_convention: QuantityConvention = 'signed',
) Square-root impact scaled by volatility and participation.
| Field | Type | Default |
|---|---|---|
coefficient | float | required |
volume | str | 'adv' |
volatility | str | 'volatility' |
market_available_time | str | None | None |
volume_horizon | str | 'daily' |
volatility_horizon | str | 'daily' |
max_participation | float | None | None |
impact_cap | float | None | None |
currency | str | 'USD' |
columns | TradeColumns | set when created |
quantity_convention | QuantityConvention | 'signed' |
SquareRootImpactModel.required_fields
def required_fields() -> tuple[str, ...] SquareRootImpactModel.estimate
def estimate(trades: object, market: object | None = None) -> CostEstimate TradeColumns
class TradeColumns(
decision_time: str = 'decision_time',
execution_time: str = 'execution_time',
instrument: str = 'instrument',
side: str = 'side',
quantity: str = 'quantity',
price: str = 'price',
reference_price: str = 'reference_price',
) Semantic columns for Lacuna’s normalized trade contract.
| Field | Type | Default |
|---|---|---|
decision_time | str | 'decision_time' |
execution_time | str | 'execution_time' |
instrument | str | 'instrument' |
side | str | 'side' |
quantity | str | 'quantity' |
price | str | 'price' |
reference_price | str | 'reference_price' |
TradeColumns.required
def required() -> tuple[str, ...] Return the complete normalized trade schema.
TradeColumns.to_parameters
def to_parameters() -> dict[str, JsonValue] VolatilitySlippageModel
class VolatilitySlippageModel(
coefficient: float,
volatility: str = 'volatility',
volatility_available_time: str | None = None,
volatility_horizon: str = 'execution_horizon',
estimator: str = 'user_supplied',
currency: str = 'USD',
columns: TradeColumns = ...,
quantity_convention: QuantityConvention = 'signed',
allow_on_observed_execution: bool = False,
) Slippage proportional to an explicitly sourced volatility estimate.
| Field | Type | Default |
|---|---|---|
coefficient | float | required |
volatility | str | 'volatility' |
volatility_available_time | str | None | None |
volatility_horizon | str | 'execution_horizon' |
estimator | str | 'user_supplied' |
currency | str | 'USD' |
columns | TradeColumns | set when created |
quantity_convention | QuantityConvention | 'signed' |
allow_on_observed_execution | bool | False |
VolatilitySlippageModel.required_fields
def required_fields() -> tuple[str, ...] VolatilitySlippageModel.estimate
def estimate(trades: object, market: object | None = None) -> CostEstimate Functions
break_even_cost
def break_even_cost(
trades: object,
*,
gross_pnl: str = 'gross_pnl',
metric: BreakEvenMetric = 'net_pnl',
threshold: float = 0.0,
lower_bps: float = 0.0,
upper_bps: float = 1000.0,
tolerance_bps: float = 1e-06,
max_iterations: int = 100,
capital: float | None = None,
period: str = 'execution_time',
annualization: float | None = None,
columns: TradeColumns | None = None,
quantity_convention: QuantityConvention = 'signed',
) -> AnalysisResult Bracket and bisect the all-in bps where a declared metric crosses a threshold.
capacity_curve
def capacity_curve(
trades: object,
*,
capital: Sequence[float],
base_capital: float,
scenarios: Sequence[CapacityScenario],
gross_pnl: str = 'gross_pnl',
volume: str = 'adv',
volatility: str = 'volatility',
available_time: str | None = None,
classification_mode: LiquidityMode,
max_participation: float = 0.1,
volume_horizon: str = 'daily',
volatility_horizon: str = 'daily',
estimation_lag: str = 'user_declared',
period: str = 'execution_time',
annualization: float | None = None,
columns: TradeColumns | None = None,
quantity_convention: QuantityConvention = 'signed',
) -> AnalysisResult Return scenario capacity curves over capital with explicit unknown liquidity.
liquidity_diagnostics
def liquidity_diagnostics(
trades: object,
*,
volume: str = 'adv',
available_time: str | None = None,
classification_mode: LiquidityMode,
max_participation: float = 0.1,
volume_horizon: str = 'daily',
estimation_lag: str = 'user_declared',
columns: TradeColumns | None = None,
quantity_convention: QuantityConvention = 'signed',
) -> AnalysisResult Measure participation coverage and constraints without fabricating missing liquidity.
stress
def stress(
trades: object,
*,
gross_pnl: str = 'gross_pnl',
spread_bps: Sequence[float] = (0.0, 2.0, 5.0, 10.0, 20.0),
slippage_bps: Sequence[float] = (0.0, 2.0, 5.0, 10.0),
commission_bps: Sequence[float] = (0.0,),
scenarios: Sequence[CostScenario] | None = None,
base_models: Sequence[CostModel] = (),
capital: float | None = None,
period: str = 'execution_time',
annualization: float | None = None,
currency: str = 'USD',
columns: TradeColumns | None = None,
quantity_convention: QuantityConvention = 'signed',
allow_on_observed_execution: bool = False,
) -> AnalysisResult Evaluate deterministic linear-cost grids or explicit correlated scenarios.
Enums
CostUnit
class CostUnit(StrEnum) Unit of every built-in monetary estimate.
| Member | Value |
|---|---|
CURRENCY | 'currency' |
Types and constants
BreakEvenMetric
BreakEvenMetric = Literal['net_pnl', 'net_return', 'net_sharpe', 'cagr'] LiquidityMode
LiquidityMode = Literal['point_in_time', 'retrospective'] MissingBorrowPolicy
MissingBorrowPolicy = Literal['raise', 'unknown', 'conservative'] QuantityConvention
QuantityConvention = Literal['signed', 'absolute'] SpreadMode
SpreadMode = Literal['half', 'full']