Skip to content

finance

The finance module provides financial data types, technical indicators, risk management, and backtesting primitives. It is organized into several sub-modules.

use std::finance
from 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 }

Canonical OHLCV candle for market data:

FieldTypeDescription
timestamptimestampBar timestamp
opennumberOpening price
highnumberHighest price
lownumberLowest price
closenumberClosing price
volumenumberTraded volume

Trade record emitted by backtests:

FieldTypeDescription
idstringTrade identifier
symbolstringInstrument symbol
sidestring"buy" or "sell"
entry_pricenumberEntry fill price
exit_pricenumberExit fill price
quantitynumberFilled quantity
entry_timetimestampEntry timestamp
exit_timetimestampExit timestamp
pnlnumberProfit/loss in quote currency
pnl_pctnumberProfit/loss as fraction of entry
  • 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.
  • 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.
  • 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.
  • 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.

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
  • Extendable — Data sources that support extending their range backward/forward.
  • TimeSeriesSource — Time-series sources with a known timeframe.
  • Simulation — simulation framework used by backtesting