RiskEngine.Portfolio (risk_engine v0.1.0)

Copy Markdown View Source

Client API for interacting with a portfolio process.

Each portfolio is a GenServer (see RiskEngine.Portfolio.Server) started under RiskEngine.PortfolioSupervisor and registered in RiskEngine.PortfolioRegistry under its portfolio_id. This module never holds a pid directly — every call is routed through the registry via via/1, so callers only ever need to know a portfolio's id.

Summary

Types

Identifies a portfolio, and the process that owns its state.

Functions

Builds a child specification for starting a portfolio under a supervisor.

Fetches the current state of the portfolio identified by portfolio_id.

Starts a portfolio process for portfolio_id, registering it in RiskEngine.PortfolioRegistry.

Asynchronously submits a trade to the portfolio identified by portfolio_id.

Types

portfolio_id()

@type portfolio_id() :: String.t()

Identifies a portfolio, and the process that owns its state.

Functions

child_spec(portfolio_id)

@spec child_spec(portfolio_id()) :: Supervisor.child_spec()

Builds a child specification for starting a portfolio under a supervisor.

Uses a :transient restart strategy: the process is restarted if it crashes, but not if it exits normally.

get_state(portfolio_id)

Fetches the current state of the portfolio identified by portfolio_id.

start_link(portfolio_id)

@spec start_link(portfolio_id()) :: GenServer.on_start()

Starts a portfolio process for portfolio_id, registering it in RiskEngine.PortfolioRegistry.

Not usually called directly — use RiskEngine.PortfolioSupervisor.ensure_started/1 instead, which starts the process under supervision and is safe to call even if one is already running.

submit_trade(portfolio_id, trade)

@spec submit_trade(portfolio_id(), RiskEngine.TradeIngestion.trade()) :: :ok

Asynchronously submits a trade to the portfolio identified by portfolio_id.

Returns :ok immediately; the portfolio's exposure, volatility, and drawdown are recomputed and broadcast once the cast is processed.