📦 SQA::RiskManager¶
Description
RiskManager - Comprehensive risk management and position sizing
Provides methods for: - Value at Risk (VaR): Historical, Parametric, Monte Carlo - Conditional VaR (CVaR / Expected Shortfall) - Position sizing: Kelly Criterion, Fixed Fractional, Percent Volatility - Risk metrics: Sharpe, Sortino, Calmar, Maximum Drawdown - Stop loss calculations
🏭 Class Methods¶
.var(returns, confidence: = 0.95, method: = :historical, simulations: = 10_000)¶
Calculate Value at Risk (VaR) using historical method
VaR represents the maximum expected loss over a given time period at a specified confidence level.
Parameters
| Name | Type | Description |
|---|---|---|
returns |
Array<Float> |
Array of period returns (e.g., daily returns) |
confidence |
Float |
Confidence level (default: 0.95 for 95%) |
method |
Symbol |
Method to use (:historical, :parametric, :monte_carlo) |
simulations |
Integer |
Number of Monte Carlo simulations (if method is :monte_carlo) |
Returns
Type: Float
Value at Risk as a percentage
Usage Examples
Source Location
.cvar(returns, confidence: = 0.95)¶
Calculate Conditional Value at Risk (CVaR / Expected Shortfall)
CVaR is the expected loss given that the loss exceeds the VaR threshold. It provides a more conservative risk measure than VaR.
Parameters
| Name | Type | Description |
|---|---|---|
returns |
Array<Float> |
Array of period returns |
confidence |
Float |
Confidence level (default: 0.95) |
Returns
Type: Float
CVaR as a percentage
Usage Examples
Source Location
.kelly_criterion(win_rate:, avg_win:, avg_loss:, capital:, max_fraction: = 0.25)¶
Calculate position size using Kelly Criterion
Kelly Criterion calculates the optimal fraction of capital to risk based on win rate and win/loss ratio.
Formula: f = (p * b - q) / b where: f = fraction of capital to bet p = probability of winning q = probability of losing (1 - p) b = win/loss ratio (avg_win / avg_loss)
Parameters
| Name | Type | Description |
|---|---|---|
win_rate |
Float |
Win rate (0.0 to 1.0) |
avg_win |
Float |
Average win size (as percentage) |
avg_loss |
Float |
Average loss size (as percentage) |
capital |
Float |
Total capital available |
max_fraction |
Float |
Maximum fraction to risk (default: 0.25 for 25%) |
Returns
Type: Float
Dollar amount to risk
Usage Examples
Source Location
.fixed_fractional(capital:, risk_fraction: = 0.02)¶
Calculate position size using Fixed Fractional method
Risk a fixed percentage of capital on each trade. Simple and conservative approach.
Parameters
| Name | Type | Description |
|---|---|---|
capital |
Float |
Total capital |
risk_fraction |
Float |
Fraction to risk (e.g., 0.02 for 2%) |
Returns
Type: Float
Dollar amount to risk
Usage Examples
Source Location
.percent_volatility(capital:, returns:, target_volatility: = 0.15, current_price:)¶
Calculate position size using Percent Volatility method
Adjust position size based on recent volatility. Higher volatility = smaller position size.
Parameters
| Name | Type | Description |
|---|---|---|
capital |
Float |
Total capital |
returns |
Array<Float> |
Recent returns |
target_volatility |
Float |
Target portfolio volatility (annualized) |
current_price |
Float |
Current asset price |
Returns
Type: Integer
Number of shares to buy
Usage Examples
Source Location
.atr_stop_loss(current_price:, atr:, multiplier: = 2.0, direction: = :long)¶
Calculate stop loss price based on ATR (Average True Range)
Parameters
| Name | Type | Description |
|---|---|---|
current_price |
Float |
Current asset price |
atr |
Float |
Average True Range |
multiplier |
Float |
ATR multiplier (default: 2.0) |
direction |
Symbol |
:long or :short |
Returns
Type: Float
Stop loss price
Usage Examples
Source Location
.max_drawdown(prices)¶
Calculate maximum drawdown from price series
Drawdown is the peak-to-trough decline in portfolio value.
Parameters
| Name | Type | Description |
|---|---|---|
prices |
Array<Float> |
Array of prices or portfolio values |
Returns
Type: Hash
{ max_drawdown: Float, peak_idx: Integer, trough_idx: Integer }
Usage Examples
Source Location
.sharpe_ratio(returns, risk_free_rate: = 0.02, periods_per_year: = 252)¶
Calculate Sharpe Ratio
Measures risk-adjusted return (excess return per unit of risk).
Parameters
| Name | Type | Description |
|---|---|---|
returns |
Array<Float> |
Array of period returns |
risk_free_rate |
Float |
Risk-free rate (annualized, default: 0.02) |
periods_per_year |
Integer |
Number of periods per year (default: 252 for daily) |
Returns
Type: Float
Sharpe ratio
Source Location
.sortino_ratio(returns, target_return: = 0.0, periods_per_year: = 252)¶
Calculate Sortino Ratio
Like Sharpe ratio but only penalizes downside volatility.
Parameters
| Name | Type | Description |
|---|---|---|
returns |
Array<Float> |
Array of period returns |
target_return |
Float |
Target return (default: 0.0) |
periods_per_year |
Integer |
Number of periods per year (default: 252) |
Returns
Type: Float
Sortino ratio
Source Location
.calmar_ratio(returns, periods_per_year: = 252)¶
Calculate Calmar Ratio
Ratio of annualized return to maximum drawdown.
Parameters
| Name | Type | Description |
|---|---|---|
returns |
Array<Float> |
Array of period returns |
periods_per_year |
Integer |
Number of periods per year (default: 252) |
Returns
Type: Float
Calmar ratio
Source Location
.monte_carlo_simulation(initial_capital:, returns:, periods:, simulations: = 1000)¶
Monte Carlo simulation for portfolio value
Parameters
| Name | Type | Description |
|---|---|---|
initial_capital |
Float |
Starting capital |
returns |
Array<Float> |
Historical returns to sample from |
periods |
Integer |
Number of periods to simulate |
simulations |
Integer |
Number of simulation paths |
Returns
Type: Hash
Simulation results with percentiles
Usage Examples