finance
finance
Section titled “finance”The finance module provides financial data types, technical indicators, risk
management, and backtesting primitives. It is organized into several sub-modules.
Import
Section titled “Import”use std::financefrom std::finance::types use { Candle, Trade }from std::finance::risk use { sharpe_ratio, max_drawdown, kelly_criterion }from std::finance::indicators::moving_averages use { sma, ema }Core Types
Section titled “Core Types”Candle
Section titled “Candle”Canonical OHLCV candle for market data:
| Field | Type | Description |
|---|---|---|
timestamp | timestamp | Bar timestamp |
open | number | Opening price |
high | number | Highest price |
low | number | Lowest price |
close | number | Closing price |
volume | number | Traded volume |
Trade record emitted by backtests:
| Field | Type | Description |
|---|---|---|
id | string | Trade identifier |
symbol | string | Instrument symbol |
side | string | "buy" or "sell" |
entry_price | number | Entry fill price |
exit_price | number | Exit fill price |
quantity | number | Filled quantity |
entry_time | timestamp | Entry timestamp |
exit_time | timestamp | Exit timestamp |
pnl | number | Profit/loss in quote currency |
pnl_pct | number | Profit/loss as fraction of entry |
Risk Management (std::finance::risk)
Section titled “Risk Management (std::finance::risk)”Position Sizing
Section titled “Position Sizing”fixed_fractional_size(account_balance, risk_percent?, stop_loss_amount)— Fixed fractional position sizing (default 2% risk).kelly_criterion(win_probability, avg_win, avg_loss)— Kelly Criterion with quarter-Kelly safety factor.volatility_based_size(account_balance, target_volatility?, current_volatility)— Size inversely proportional to volatility.risk_parity_size(account_balance, positions, target_risk?)— Equal risk contribution from each position.optimal_f(trade_results)— Find the optimal f value that maximizes terminal wealth ratio.
Stop Loss
Section titled “Stop Loss”atr_stop_loss(entry_price, atr_multiplier?, atr_period?)— ATR-based stop distance.percent_stop_loss(entry_price, stop_percent?)— Fixed percentage stop.trailing_stop(entry_price, current_price, trail_percent?, is_long?)— Trailing stop with locked profit tracking.
Risk Metrics
Section titled “Risk Metrics”sharpe_ratio(returns, risk_free_rate?)— Annualized Sharpe ratio.sortino_ratio(returns, risk_free_rate?, target_return?)— Annualized Sortino ratio (downside risk only).max_drawdown(equity_curve)— Maximum drawdown with start/end indices.historical_var(returns, confidence_level?)— Value at Risk (historical method).cvar(returns, confidence_level?)— Conditional Value at Risk.portfolio_risk(positions, correlation_matrix?)— Portfolio volatility with optional correlation.
Trade Validation
Section titled “Trade Validation”validate_trade_risk(trade, account_balance, open_positions)— Check if a trade meets risk criteria (max risk per trade, portfolio risk limits, position sizing, correlation).calculate_position_limits(account_balance, max_position_size?, max_sector_exposure?)— Compute position and exposure limits.
Indicators
Section titled “Indicators”The std::finance::indicators sub-modules provide technical analysis functions:
- Moving averages:
sma,ema(simple and exponential) - Volatility:
atr(Average True Range) - Oscillators: RSI, Stochastic, and related oscillators
- Trend: Trend-following indicators
- Volume: Volume-based indicators
Traits
Section titled “Traits”Extendable— Data sources that support extending their range backward/forward.TimeSeriesSource— Time-series sources with a known timeframe.
See Also
Section titled “See Also”- Simulation — simulation framework used by backtesting