Bourse.PortfolioRisk.Exposure (bourse v0.1.0)

Copy Markdown View Source

Pure exposure math for portfolio-risk snapshots.

Option position quantities are converted to canonical underlying units before Greeks are applied. Aggregation keys include the underlying, both currencies, and the complete semantic unit convention, so incompatible risks cannot be added accidentally.

Summary

Functions

Aggregates contributions only inside exact semantic buckets.

Builds signed asset and debt lots from a balance.

Blocks every supported option-Greek bucket for a lot after a read failure.

Converts a non-option lot into a signed delta contribution.

Applies per-instrument Greeks to an option lot.

Builds signed pending-exposure lots from one open order and its market.

Builds a signed current-exposure lot from one position and its market.

Types

lot()

@type lot() :: %{
  :venue => String.t(),
  :account => term(),
  :symbol => String.t(),
  :underlying => String.t(),
  :settlement_currency => String.t() | nil,
  :quantity => number(),
  :quantity_unit => String.t(),
  :source => atom(),
  :pending => boolean(),
  :observed_at => integer(),
  :market => Bourse.Market.t() | nil,
  optional(:contract_multiplier) => number() | nil,
  optional(:source_timestamp) => integer() | nil
}

provenance()

@type provenance() :: %{venue: String.t(), account: term(), observed_at: integer()}

Functions

aggregate(contributions, blockers)

@spec aggregate([map()], [map()]) :: [map()]

Aggregates contributions only inside exact semantic buckets.

A bucket with any blocker exposes only partial_value; its coherent value is nil.

balance_lots(provenance, balance)

@spec balance_lots(provenance(), Bourse.Balance.t()) :: [lot()]

Builds signed asset and debt lots from a balance.

block_option(lot, conventions, reason)

@spec block_option(lot(), map(), term()) :: [map()]

Blocks every supported option-Greek bucket for a lot after a read failure.

delta_contribution(lot)

@spec delta_contribution(lot()) :: map()

Converts a non-option lot into a signed delta contribution.

option_contribution(lot, greeks)

@spec option_contribution(lot(), Bourse.InstrumentGreeks.t()) ::
  {map() | nil, [map()]}

Applies per-instrument Greeks to an option lot.

Missing supported Greeks become blocked buckets; unsupported Greeks are omitted because the venue declares that no such bucket exists.

order_lots(provenance, order, market)

@spec order_lots(provenance(), Bourse.Order.t(), Bourse.Market.t()) ::
  {:ok, [lot()]} | {:error, atom()}

Builds signed pending-exposure lots from one open order and its market.

position_lot(provenance, position, market)

@spec position_lot(provenance(), Bourse.Position.t(), Bourse.Market.t()) ::
  {:ok, lot()} | {:error, atom()}

Builds a signed current-exposure lot from one position and its market.