metrics¶
athena.metrics
¶
Portfolio risk and performance metrics.
Provides functions for calculating Sharpe ratio, Sortino ratio, maximum drawdown, Value at Risk, volatility, win rate, and alpha/beta against market benchmarks. Sub-modules for options-implied analytics (forward curves, theta decay) are available but not re-exported here.
AlphaBetaResult
dataclass
¶
Result container for alpha/beta calculations.
Source code in src/athena/metrics/alpha_beta.py
ClosedPosition
dataclass
¶
Represents a closed position with its realized gain/loss.
Source code in src/athena/metrics/win_rate.py
WinRateResult
dataclass
¶
Result of a win rate calculation.
Source code in src/athena/metrics/win_rate.py
align_strategy_with_benchmark(strategy_returns, benchmark_returns, rf_manager=None, trading_days_per_year=252)
¶
Align strategy returns with benchmark returns on trading days.
Only includes dates where both strategy and benchmark have data. Optionally includes risk-free rates for each date.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strategy_returns
|
dict[datetime, float]
|
Dictionary mapping dates to strategy returns. |
required |
benchmark_returns
|
dict[datetime, float]
|
Dictionary mapping dates to benchmark returns. |
required |
rf_manager
|
RiskFreeRateManager | None
|
Optional risk-free rate manager. If None, uses 0.0 for all dates. |
None
|
trading_days_per_year
|
int
|
Number of trading days per year for daily rate calc. |
252
|
Returns:
| Type | Description |
|---|---|
Tuple[list[float], list[float], list[float], list[datetime]]
|
Tuple of (aligned_strategy_returns, aligned_benchmark_returns, aligned_rf_rates, aligned_dates). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no overlapping dates are found. |
Source code in src/athena/metrics/alpha_beta.py
calculate_alpha_beta(strategy_returns, benchmark_returns, risk_free_rates=None, trading_days_per_year=252)
¶
Calculate alpha and beta using OLS regression.
Uses the CAPM model: excess_strategy = alpha + beta * excess_benchmark
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strategy_returns
|
list[float]
|
List of daily strategy returns. |
required |
benchmark_returns
|
list[float]
|
List of daily benchmark returns (same length). |
required |
risk_free_rates
|
list[float] | None
|
Optional list of daily risk-free rates. If None, uses 0.0 (raw returns instead of excess). |
None
|
trading_days_per_year
|
int
|
Trading days per year for annualization (default 252). |
252
|
Returns:
| Type | Description |
|---|---|
AlphaBetaResult
|
AlphaBetaResult with alpha, beta, r_squared, and correlation. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If inputs have different lengths or fewer than 2 observations. |
Source code in src/athena/metrics/alpha_beta.py
calculate_alpha_beta_by_day_cumulative(portfolio, target_currency, benchmark_ticker=BENCHMARK_SP500, rf_manager=None, start_date=None, end_date=None, trading_days_per_year=252, min_observations=20, price_manager=None)
¶
Calculate cumulative alpha/beta for each trading day in a date range.
For each trading day, calculates alpha/beta using all returns from start_date up to that day (cumulative/expanding window).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
portfolio
|
Portfolio
|
Portfolio object containing transactions and settings. |
required |
target_currency
|
Currency
|
The currency to convert all values into. |
required |
benchmark_ticker
|
str
|
Yahoo Finance ticker for benchmark (default: ^GSPC). |
BENCHMARK_SP500
|
rf_manager
|
RiskFreeRateManager | None
|
Risk-free rate manager. If None, uses FRED DTB3. |
None
|
start_date
|
datetime | None
|
The start date for the calculation (inclusive). |
None
|
end_date
|
datetime | None
|
The end date for the calculation (inclusive). |
None
|
trading_days_per_year
|
int
|
Trading days per year for annualization (default 252). |
252
|
min_observations
|
int
|
Minimum observations before calculating (default 20). |
20
|
price_manager
|
PricingDataManager | None
|
PricingDataManager for portfolio valuation. |
None
|
Returns:
| Type | Description |
|---|---|
dict[datetime, AlphaBetaResult]
|
Dictionary mapping each trading day to AlphaBetaResult. |
dict[datetime, AlphaBetaResult]
|
Days with insufficient observations are excluded. |
Source code in src/athena/metrics/alpha_beta.py
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 | |
calculate_alpha_beta_by_day_rolling_window(portfolio, target_currency, window_size, benchmark_ticker=BENCHMARK_SP500, rf_manager=None, start_date=None, end_date=None, trading_days_per_year=252, price_manager=None)
¶
Calculate rolling window alpha/beta for each trading day.
For each trading day, calculates alpha/beta using the trailing window_size trading days of returns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
portfolio
|
Portfolio
|
Portfolio object containing transactions and settings. |
required |
target_currency
|
Currency
|
The currency to convert all values into. |
required |
window_size
|
int
|
Number of trailing trading days to use for each calculation. Common values: 21 (month), 63 (quarter), 252 (year). |
required |
benchmark_ticker
|
str
|
Yahoo Finance ticker for benchmark (default: ^GSPC). |
BENCHMARK_SP500
|
rf_manager
|
RiskFreeRateManager | None
|
Risk-free rate manager. If None, uses FRED DTB3. |
None
|
start_date
|
datetime | None
|
The start date for the calculation (inclusive). |
None
|
end_date
|
datetime | None
|
The end date for the calculation (inclusive). |
None
|
trading_days_per_year
|
int
|
Trading days per year for annualization (default 252). |
252
|
price_manager
|
PricingDataManager | None
|
PricingDataManager for portfolio valuation. |
None
|
Returns:
| Type | Description |
|---|---|
dict[datetime, AlphaBetaResult]
|
Dictionary mapping each trading day to AlphaBetaResult. |
dict[datetime, AlphaBetaResult]
|
Days with insufficient observations are excluded. |
Source code in src/athena/metrics/alpha_beta.py
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 | |
calculate_alpha_beta_cumulative(portfolio, target_currency, benchmark_ticker=BENCHMARK_SP500, rf_manager=None, start_date=None, end_date=None, trading_days_per_year=252, price_manager=None)
¶
Calculate cumulative alpha and beta for a portfolio vs benchmark.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
portfolio
|
Portfolio
|
Portfolio object containing transactions and settings. |
required |
target_currency
|
Currency
|
The currency to convert all values into. |
required |
benchmark_ticker
|
str
|
Yahoo Finance ticker for benchmark (default: ^GSPC). |
BENCHMARK_SP500
|
rf_manager
|
RiskFreeRateManager | None
|
Risk-free rate manager. If None, uses FRED DTB3. |
None
|
start_date
|
datetime | None
|
The start date for the calculation (inclusive). If None, uses the earliest transaction date. |
None
|
end_date
|
datetime | None
|
The end date for the calculation (inclusive). If None, uses the latest transaction date. |
None
|
trading_days_per_year
|
int
|
Trading days per year for annualization (default 252). |
252
|
price_manager
|
PricingDataManager | None
|
PricingDataManager for portfolio valuation. If None, uses YFinancePricingDataManager. |
None
|
Returns:
| Type | Description |
|---|---|
AlphaBetaResult
|
AlphaBetaResult with alpha, beta, r_squared, and correlation. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If insufficient data or calculation error. |
Source code in src/athena/metrics/alpha_beta.py
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | |
calculate_alpha_beta_from_values(portfolio_values, benchmark_ticker=BENCHMARK_SP500, rf_manager=None, trading_days_per_year=252)
¶
Calculate alpha/beta directly from portfolio values dictionary.
Convenience function when you already have portfolio values and don't need to calculate them from a Portfolio object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
portfolio_values
|
dict[datetime, Decimal]
|
Dictionary mapping dates to portfolio values. |
required |
benchmark_ticker
|
str
|
Yahoo Finance ticker for benchmark (default: ^GSPC). |
BENCHMARK_SP500
|
rf_manager
|
RiskFreeRateManager | None
|
Risk-free rate manager. If None, uses FRED DTB3. |
None
|
trading_days_per_year
|
int
|
Trading days per year for annualization (default 252). |
252
|
Returns:
| Type | Description |
|---|---|
AlphaBetaResult
|
AlphaBetaResult with alpha, beta, r_squared, and correlation. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If insufficient data or calculation error. |
Source code in src/athena/metrics/alpha_beta.py
fetch_benchmark_returns(benchmark_ticker, min_date, max_date, force_cache_refresh=False)
¶
Fetch benchmark price data and calculate daily returns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
benchmark_ticker
|
str
|
Yahoo Finance ticker (e.g., "^GSPC" for S&P 500). |
required |
min_date
|
date
|
Start date for data. |
required |
max_date
|
date
|
End date for data. |
required |
force_cache_refresh
|
bool
|
Whether to force refresh cached data. |
False
|
Returns:
| Type | Description |
|---|---|
dict[datetime, float]
|
Dictionary mapping dates to daily returns. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no benchmark data is available. |
Source code in src/athena/metrics/alpha_beta.py
calculate_drawdown_series(portfolio_values)
¶
Calculate the drawdown at each point in time from a time series of portfolio values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
portfolio_values
|
dict[datetime, Decimal]
|
Dictionary mapping dates to portfolio values. |
required |
Returns:
| Type | Description |
|---|---|
dict[datetime, float]
|
Dictionary mapping dates to drawdown values (negative floats). |
dict[datetime, float]
|
A drawdown of -0.10 means the portfolio is 10% below its peak. |
Source code in src/athena/metrics/max_drawdown.py
calculate_max_drawdown(portfolio_values)
¶
Calculate the maximum drawdown from a time series of portfolio values.
Maximum drawdown measures the largest peak-to-trough decline in portfolio value, expressed as a percentage of the peak value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
portfolio_values
|
dict[datetime, Decimal]
|
Dictionary mapping dates to portfolio values. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Tuple of (max_drawdown, peak_date, trough_date). |
datetime | None
|
max_drawdown is a negative float (e.g., -0.20 = 20% drawdown). |
datetime | None
|
peak_date and trough_date are None if insufficient data. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than two observations. |
Source code in src/athena/metrics/max_drawdown.py
calculate_max_drawdown_by_day_cumulative(portfolio, target_currency, start_date=None, end_date=None, min_observations=2)
¶
Calculate cumulative maximum drawdown for each day in a date range.
For each day, calculates the maximum drawdown using all portfolio values from start_date up to that day (cumulative/expanding window).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
portfolio
|
Portfolio
|
Portfolio object containing transactions and settings. |
required |
target_currency
|
Currency
|
The currency to convert all values into. |
required |
start_date
|
datetime | None
|
The start date for the calculation (inclusive). If None, uses the earliest transaction date. |
None
|
end_date
|
datetime | None
|
The end date for the calculation (inclusive). If None, uses the latest transaction date. |
None
|
min_observations
|
int
|
Minimum number of observations required before calculating max drawdown. Defaults to 2. |
2
|
Returns:
| Type | Description |
|---|---|
dict[datetime, float]
|
Dictionary mapping each date to the cumulative max drawdown up to that date. |
dict[datetime, float]
|
Days with insufficient observations are excluded. |
Source code in src/athena/metrics/max_drawdown.py
calculate_max_drawdown_by_day_rolling_window(portfolio, target_currency, window_size, start_date=None, end_date=None)
¶
Calculate rolling window maximum drawdown for each day in a date range.
For each day, calculates the maximum drawdown using the trailing window_size days of portfolio values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
portfolio
|
Portfolio
|
Portfolio object containing transactions and settings. |
required |
target_currency
|
Currency
|
The currency to convert all values into. |
required |
window_size
|
int
|
Number of trailing days to use for each calculation. Common values: 30, 60, 90, 252 (trading year). |
required |
start_date
|
datetime | None
|
The start date for the calculation (inclusive). If None, uses the earliest transaction date. |
None
|
end_date
|
datetime | None
|
The end date for the calculation (inclusive). If None, uses the latest transaction date. |
None
|
Returns:
| Type | Description |
|---|---|
dict[datetime, float]
|
Dictionary mapping each date to the rolling max drawdown. |
dict[datetime, float]
|
Days with insufficient observations (fewer than window_size) are excluded. |
Source code in src/athena/metrics/max_drawdown.py
calculate_max_drawdown_cumulative(portfolio, target_currency, start_date=None, end_date=None)
¶
Calculate the maximum drawdown for a portfolio over a date range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
portfolio
|
Portfolio
|
Portfolio object containing transactions and settings. |
required |
target_currency
|
Currency
|
The currency to convert all values into. |
required |
start_date
|
datetime | None
|
The start date for the calculation (inclusive). If None, uses the earliest transaction date. |
None
|
end_date
|
datetime | None
|
The end date for the calculation (inclusive). If None, uses the latest transaction date. |
None
|
Returns:
| Type | Description |
|---|---|
float
|
Tuple of (max_drawdown, peak_date, trough_date). |
datetime | None
|
max_drawdown is a negative float (e.g., -0.20 = 20% drawdown). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than two observations. |
Source code in src/athena/metrics/max_drawdown.py
calculate_max_drawdown_from_values(portfolio_values)
¶
Calculate maximum drawdown directly from a dictionary of portfolio values.
This is a convenience function when you already have portfolio values and don't need to calculate them from a Portfolio object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
portfolio_values
|
dict[datetime, Decimal]
|
Dictionary mapping dates to portfolio values. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Tuple of (max_drawdown, peak_date, trough_date). |
datetime | None
|
max_drawdown is a negative float (e.g., -0.20 = 20% drawdown). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than two observations. |
Source code in src/athena/metrics/max_drawdown.py
calculate_daily_returns(portfolio_values)
¶
Calculate daily returns from a time series of portfolio values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
portfolio_values
|
dict[datetime, Decimal]
|
Dictionary mapping dates to portfolio values. |
required |
Returns:
| Type | Description |
|---|---|
dict[datetime, float]
|
Dictionary mapping dates to daily returns (as floats). |
dict[datetime, float]
|
The first date will not have a return (needs previous day). |
Source code in src/athena/metrics/sharpe.py
calculate_sharpe_ratio(returns, annual_risk_free_rate, periods_in_year=365)
¶
Calculate daily and annualized Sharpe ratios from a list of returns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
list[float]
|
List of daily returns as decimals (e.g., 0.002 = 0.2%). |
required |
annual_risk_free_rate
|
float
|
Annual nominal risk-free rate (e.g., 0.03 for 3%). |
required |
periods_in_year
|
int
|
Trading periods in a year (365 for daily). |
365
|
Returns:
| Type | Description |
|---|---|
Tuple[float, float]
|
Tuple of (daily_sharpe, annual_sharpe). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than two observations or zero standard deviation. |
Source code in src/athena/metrics/sharpe.py
calculate_sharpe_ratio_by_day_cumulative(portfolio, target_currency, annual_risk_free_rate, start_date=None, end_date=None, periods_in_year=365, min_observations=2)
¶
Calculate cumulative Sharpe ratios for each day in a date range.
For each day, calculates the Sharpe ratio using all returns from start_date up to that day (cumulative/expanding window).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
portfolio
|
Portfolio
|
Portfolio object containing transactions and settings. |
required |
target_currency
|
Currency
|
The currency to convert all values into. |
required |
annual_risk_free_rate
|
float
|
Annual nominal risk-free rate (e.g., 0.03 for 3%). |
required |
start_date
|
datetime | None
|
The start date for the calculation (inclusive). If None, uses the earliest transaction date. |
None
|
end_date
|
datetime | None
|
The end date for the calculation (inclusive). If None, uses the latest transaction date. |
None
|
periods_in_year
|
int
|
Trading periods in a year (365 for daily, 252 for US equities trading days). |
365
|
min_observations
|
int
|
Minimum number of return observations required before calculating Sharpe. Defaults to 2. |
2
|
Returns:
| Type | Description |
|---|---|
dict[datetime, Tuple[float, float]]
|
Dictionary mapping each date to a tuple of (daily_sharpe, annual_sharpe). |
dict[datetime, Tuple[float, float]]
|
Days with insufficient observations are excluded. |
Source code in src/athena/metrics/sharpe.py
calculate_sharpe_ratio_by_day_rolling_window(portfolio, target_currency, annual_risk_free_rate, window_size, start_date=None, end_date=None, periods_in_year=365)
¶
Calculate rolling window Sharpe ratios for each day in a date range.
For each day, calculates the Sharpe ratio using the trailing window_size days of returns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
portfolio
|
Portfolio
|
Portfolio object containing transactions and settings. |
required |
target_currency
|
Currency
|
The currency to convert all values into. |
required |
annual_risk_free_rate
|
float
|
Annual nominal risk-free rate (e.g., 0.03 for 3%). |
required |
window_size
|
int
|
Number of trailing days to use for each calculation. Common values: 30, 60, 90, 252 (trading year). |
required |
start_date
|
datetime | None
|
The start date for the calculation (inclusive). If None, uses the earliest transaction date. |
None
|
end_date
|
datetime | None
|
The end date for the calculation (inclusive). If None, uses the latest transaction date. |
None
|
periods_in_year
|
int
|
Trading periods in a year (365 for daily, 252 for US equities trading days). |
365
|
Returns:
| Type | Description |
|---|---|
dict[datetime, Tuple[float, float]]
|
Dictionary mapping each date to a tuple of (daily_sharpe, annual_sharpe). |
dict[datetime, Tuple[float, float]]
|
Days with insufficient observations (fewer than window_size) are excluded. |
Source code in src/athena/metrics/sharpe.py
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | |
calculate_sharpe_ratio_cumulative(portfolio, target_currency, annual_risk_free_rate, start_date=None, end_date=None, periods_in_year=365)
¶
Calculate the cumulative Sharpe ratio for a portfolio over a date range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
portfolio
|
Portfolio
|
Portfolio object containing transactions and settings. |
required |
target_currency
|
Currency
|
The currency to convert all values into. |
required |
annual_risk_free_rate
|
float
|
Annual nominal risk-free rate (e.g., 0.03 for 3%). |
required |
start_date
|
datetime | None
|
The start date for the calculation (inclusive). If None, uses the earliest transaction date. |
None
|
end_date
|
datetime | None
|
The end date for the calculation (inclusive). If None, uses the latest transaction date. |
None
|
periods_in_year
|
int
|
Trading periods in a year (365 for daily, 252 for US equities trading days). |
365
|
Returns:
| Type | Description |
|---|---|
Tuple[float, float]
|
Tuple of (daily_sharpe, annual_sharpe). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than two observations or zero standard deviation. |
Source code in src/athena/metrics/sharpe.py
calculate_sharpe_ratio_from_values(portfolio_values, annual_risk_free_rate, periods_in_year=365)
¶
Calculate Sharpe ratios directly from a dictionary of portfolio values.
This is a convenience function when you already have portfolio values and don't need to calculate them from a Portfolio object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
portfolio_values
|
dict[datetime, Decimal]
|
Dictionary mapping dates to portfolio values. |
required |
annual_risk_free_rate
|
float
|
Annual nominal risk-free rate (e.g., 0.03 for 3%). |
required |
periods_in_year
|
int
|
Trading periods in a year (365 for daily). |
365
|
Returns:
| Type | Description |
|---|---|
Tuple[float, float]
|
Tuple of (daily_sharpe, annual_sharpe). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than two observations or zero standard deviation. |
Source code in src/athena/metrics/sharpe.py
align_returns_with_risk_free_rates(portfolio_returns, rf_manager, trading_days_per_year=252)
¶
Align portfolio returns with risk-free rates on trading days only.
Includes dates where portfolio returns exist and either: - Actual risk-free rate data is available for that date, OR - It's a recent weekday where FRED data may not be published yet (uses most recent available rate as fallback)
Weekends are always excluded.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
portfolio_returns
|
dict[datetime, float]
|
Dictionary mapping dates to portfolio returns. |
required |
rf_manager
|
RiskFreeRateManager
|
Risk-free rate manager to get daily rates from. |
required |
trading_days_per_year
|
int
|
Number of trading days per year for daily rate calc. |
252
|
Returns:
| Type | Description |
|---|---|
list[float]
|
Tuple of (aligned_portfolio_returns, aligned_rf_rates, aligned_dates). |
list[float]
|
All three lists have the same length and correspond to each other. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no overlapping dates are found. |
Source code in src/athena/metrics/sharpe_advanced.py
calculate_sharpe_ratio_advanced(returns, daily_risk_free_rates, trading_days_per_year=252)
¶
Calculate Sharpe ratio using variable daily risk-free rates.
Uses the standard (non-geometric) approach: excess_return = portfolio_return - risk_free_rate
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
list[float]
|
List of daily portfolio returns (aligned with rf rates). |
required |
daily_risk_free_rates
|
list[float]
|
List of daily risk-free rates (same length as returns). |
required |
trading_days_per_year
|
int
|
Trading days per year for annualization (default 252). |
252
|
Returns:
| Type | Description |
|---|---|
Tuple[float, float]
|
Tuple of (daily_sharpe, annual_sharpe). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If inputs have different lengths, fewer than 2 observations, or zero standard deviation. |
Source code in src/athena/metrics/sharpe_advanced.py
calculate_sharpe_ratio_by_day_cumulative_advanced(portfolio, target_currency, rf_manager=None, start_date=None, end_date=None, trading_days_per_year=252, min_observations=2)
¶
Calculate cumulative Sharpe ratios for each trading day in a date range.
For each trading day, calculates the Sharpe ratio using all returns from start_date up to that day (cumulative/expanding window).
Only includes dates where risk-free rate data is available.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
portfolio
|
Portfolio
|
Portfolio object containing transactions and settings. |
required |
target_currency
|
Currency
|
The currency to convert all values into. |
required |
rf_manager
|
RiskFreeRateManager | None
|
Risk-free rate manager. If None, uses FRED DTB3. |
None
|
start_date
|
datetime | None
|
The start date for the calculation (inclusive). If None, uses the earliest transaction date. |
None
|
end_date
|
datetime | None
|
The end date for the calculation (inclusive). If None, uses the latest transaction date. |
None
|
trading_days_per_year
|
int
|
Trading days per year for annualization (default 252). |
252
|
min_observations
|
int
|
Minimum number of observations required before calculating Sharpe. Defaults to 2. |
2
|
Returns:
| Type | Description |
|---|---|
dict[datetime, Tuple[float, float]]
|
Dictionary mapping each trading day to (daily_sharpe, annual_sharpe). |
dict[datetime, Tuple[float, float]]
|
Days with insufficient observations are excluded. |
Source code in src/athena/metrics/sharpe_advanced.py
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | |
calculate_sharpe_ratio_by_day_rolling_window_advanced(portfolio, target_currency, window_size, rf_manager=None, start_date=None, end_date=None, trading_days_per_year=252)
¶
Calculate rolling window Sharpe ratios for each trading day.
For each trading day, calculates the Sharpe ratio using the trailing window_size trading days of returns.
Only includes dates where risk-free rate data is available.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
portfolio
|
Portfolio
|
Portfolio object containing transactions and settings. |
required |
target_currency
|
Currency
|
The currency to convert all values into. |
required |
window_size
|
int
|
Number of trailing trading days to use for each calculation. Common values: 21 (month), 63 (quarter), 252 (year). |
required |
rf_manager
|
RiskFreeRateManager | None
|
Risk-free rate manager. If None, uses FRED DTB3. |
None
|
start_date
|
datetime | None
|
The start date for the calculation (inclusive). If None, uses the earliest transaction date. |
None
|
end_date
|
datetime | None
|
The end date for the calculation (inclusive). If None, uses the latest transaction date. |
None
|
trading_days_per_year
|
int
|
Trading days per year for annualization (default 252). |
252
|
Returns:
| Type | Description |
|---|---|
dict[datetime, Tuple[float, float]]
|
Dictionary mapping each trading day to (daily_sharpe, annual_sharpe). |
dict[datetime, Tuple[float, float]]
|
Days with insufficient observations (fewer than window_size) are excluded. |
Source code in src/athena/metrics/sharpe_advanced.py
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 | |
calculate_sharpe_ratio_cumulative_advanced(portfolio, target_currency, rf_manager=None, start_date=None, end_date=None, trading_days_per_year=252)
¶
Calculate the cumulative Sharpe ratio using dynamic risk-free rates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
portfolio
|
Portfolio
|
Portfolio object containing transactions and settings. |
required |
target_currency
|
Currency
|
The currency to convert all values into. |
required |
rf_manager
|
RiskFreeRateManager | None
|
Risk-free rate manager. If None, uses FRED DTB3. |
None
|
start_date
|
datetime | None
|
The start date for the calculation (inclusive). If None, uses the earliest transaction date. |
None
|
end_date
|
datetime | None
|
The end date for the calculation (inclusive). If None, uses the latest transaction date. |
None
|
trading_days_per_year
|
int
|
Trading days per year for annualization (default 252). |
252
|
Returns:
| Type | Description |
|---|---|
Tuple[float, float]
|
Tuple of (daily_sharpe, annual_sharpe). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If insufficient data or calculation error. |
Source code in src/athena/metrics/sharpe_advanced.py
calculate_sharpe_ratio_from_values_advanced(portfolio_values, rf_manager=None, trading_days_per_year=252)
¶
Calculate Sharpe ratios directly from portfolio values using dynamic RF rates.
Convenience function when you already have portfolio values and don't need to calculate them from a Portfolio object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
portfolio_values
|
dict[datetime, Decimal]
|
Dictionary mapping dates to portfolio values. |
required |
rf_manager
|
RiskFreeRateManager | None
|
Risk-free rate manager. If None, uses FRED DTB3. |
None
|
trading_days_per_year
|
int
|
Trading days per year for annualization (default 252). |
252
|
Returns:
| Type | Description |
|---|---|
Tuple[float, float]
|
Tuple of (daily_sharpe, annual_sharpe). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If insufficient data or calculation error. |
Source code in src/athena/metrics/sharpe_advanced.py
calculate_downside_deviation(returns, target_return=0.0)
¶
Calculate downside deviation from a list of returns.
Downside deviation measures the volatility of returns that fall below a target return (typically the risk-free rate or zero).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
list[float]
|
List of returns as decimals (e.g., 0.002 = 0.2%). |
required |
target_return
|
float
|
The minimum acceptable return (default 0.0). |
0.0
|
Returns:
| Type | Description |
|---|---|
float
|
The downside deviation as a float. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than two observations. |
Source code in src/athena/metrics/sortino.py
calculate_sortino_ratio(returns, annual_risk_free_rate, periods_in_year=365)
¶
Calculate daily and annualized Sortino ratios from a list of returns.
The Sortino ratio is similar to the Sharpe ratio but only considers downside volatility, making it a better measure for investors who are primarily concerned with downside risk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
list[float]
|
List of daily returns as decimals (e.g., 0.002 = 0.2%). |
required |
annual_risk_free_rate
|
float
|
Annual nominal risk-free rate (e.g., 0.03 for 3%). |
required |
periods_in_year
|
int
|
Trading periods in a year (365 for daily). |
365
|
Returns:
| Type | Description |
|---|---|
Tuple[float, float]
|
Tuple of (daily_sortino, annual_sortino). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than two observations or zero downside deviation. |
Source code in src/athena/metrics/sortino.py
calculate_sortino_ratio_by_day_cumulative(portfolio, target_currency, annual_risk_free_rate, start_date=None, end_date=None, periods_in_year=365, min_observations=2)
¶
Calculate cumulative Sortino ratios for each day in a date range.
For each day, calculates the Sortino ratio using all returns from start_date up to that day (cumulative/expanding window).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
portfolio
|
Portfolio
|
Portfolio object containing transactions and settings. |
required |
target_currency
|
Currency
|
The currency to convert all values into. |
required |
annual_risk_free_rate
|
float
|
Annual nominal risk-free rate (e.g., 0.03 for 3%). |
required |
start_date
|
datetime | None
|
The start date for the calculation (inclusive). If None, uses the earliest transaction date. |
None
|
end_date
|
datetime | None
|
The end date for the calculation (inclusive). If None, uses the latest transaction date. |
None
|
periods_in_year
|
int
|
Trading periods in a year (365 for daily, 252 for US equities trading days). |
365
|
min_observations
|
int
|
Minimum number of return observations required before calculating Sortino. Defaults to 2. |
2
|
Returns:
| Type | Description |
|---|---|
dict[datetime, Tuple[float, float]]
|
Dictionary mapping each date to a tuple of (daily_sortino, annual_sortino). |
dict[datetime, Tuple[float, float]]
|
Days with insufficient observations are excluded. |
Source code in src/athena/metrics/sortino.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | |
calculate_sortino_ratio_by_day_rolling_window(portfolio, target_currency, annual_risk_free_rate, window_size, start_date=None, end_date=None, periods_in_year=365)
¶
Calculate rolling window Sortino ratios for each day in a date range.
For each day, calculates the Sortino ratio using the trailing window_size days of returns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
portfolio
|
Portfolio
|
Portfolio object containing transactions and settings. |
required |
target_currency
|
Currency
|
The currency to convert all values into. |
required |
annual_risk_free_rate
|
float
|
Annual nominal risk-free rate (e.g., 0.03 for 3%). |
required |
window_size
|
int
|
Number of trailing days to use for each calculation. Common values: 30, 60, 90, 252 (trading year). |
required |
start_date
|
datetime | None
|
The start date for the calculation (inclusive). If None, uses the earliest transaction date. |
None
|
end_date
|
datetime | None
|
The end date for the calculation (inclusive). If None, uses the latest transaction date. |
None
|
periods_in_year
|
int
|
Trading periods in a year (365 for daily, 252 for US equities trading days). |
365
|
Returns:
| Type | Description |
|---|---|
dict[datetime, Tuple[float, float]]
|
Dictionary mapping each date to a tuple of (daily_sortino, annual_sortino). |
dict[datetime, Tuple[float, float]]
|
Days with insufficient observations (fewer than window_size) are excluded. |
Source code in src/athena/metrics/sortino.py
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | |
calculate_sortino_ratio_cumulative(portfolio, target_currency, annual_risk_free_rate, start_date=None, end_date=None, periods_in_year=365)
¶
Calculate the cumulative Sortino ratio for a portfolio over a date range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
portfolio
|
Portfolio
|
Portfolio object containing transactions and settings. |
required |
target_currency
|
Currency
|
The currency to convert all values into. |
required |
annual_risk_free_rate
|
float
|
Annual nominal risk-free rate (e.g., 0.03 for 3%). |
required |
start_date
|
datetime | None
|
The start date for the calculation (inclusive). If None, uses the earliest transaction date. |
None
|
end_date
|
datetime | None
|
The end date for the calculation (inclusive). If None, uses the latest transaction date. |
None
|
periods_in_year
|
int
|
Trading periods in a year (365 for daily, 252 for US equities trading days). |
365
|
Returns:
| Type | Description |
|---|---|
Tuple[float, float]
|
Tuple of (daily_sortino, annual_sortino). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than two observations or zero downside deviation. |
Source code in src/athena/metrics/sortino.py
calculate_sortino_ratio_from_values(portfolio_values, annual_risk_free_rate, periods_in_year=365)
¶
Calculate Sortino ratios directly from a dictionary of portfolio values.
This is a convenience function when you already have portfolio values and don't need to calculate them from a Portfolio object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
portfolio_values
|
dict[datetime, Decimal]
|
Dictionary mapping dates to portfolio values. |
required |
annual_risk_free_rate
|
float
|
Annual nominal risk-free rate (e.g., 0.03 for 3%). |
required |
periods_in_year
|
int
|
Trading periods in a year (365 for daily). |
365
|
Returns:
| Type | Description |
|---|---|
Tuple[float, float]
|
Tuple of (daily_sortino, annual_sortino). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than two observations or zero downside deviation. |
Source code in src/athena/metrics/sortino.py
calculate_cvar(returns, confidence_level=0.95)
¶
Calculate Conditional Value at Risk (Expected Shortfall).
CVaR represents the expected loss given that the loss exceeds the VaR threshold. It provides a more complete picture of tail risk than VaR alone.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
list[float]
|
List of daily returns as decimals (e.g., 0.002 = 0.2%). |
required |
confidence_level
|
float
|
Confidence level for CVaR (e.g., 0.95 for 95%). |
0.95
|
Returns:
| Type | Description |
|---|---|
float
|
CVaR as a negative float representing the expected loss beyond VaR |
float
|
(e.g., -0.08 means 8% expected loss in worst cases). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than two observations or invalid confidence level. |
Source code in src/athena/metrics/value_at_risk.py
calculate_var_by_day_cumulative(portfolio, target_currency, confidence_level=0.95, method='historical', start_date=None, end_date=None, min_observations=30)
¶
Calculate cumulative Value at Risk for each day in a date range.
For each day, calculates VaR using all returns from start_date up to that day (cumulative/expanding window).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
portfolio
|
Portfolio
|
Portfolio object containing transactions and settings. |
required |
target_currency
|
Currency
|
The currency to convert all values into. |
required |
confidence_level
|
float
|
Confidence level for VaR (e.g., 0.95 for 95%). |
0.95
|
method
|
str
|
VaR calculation method ("historical" or "parametric"). |
'historical'
|
start_date
|
datetime | None
|
The start date for the calculation (inclusive). If None, uses the earliest transaction date. |
None
|
end_date
|
datetime | None
|
The end date for the calculation (inclusive). If None, uses the latest transaction date. |
None
|
min_observations
|
int
|
Minimum number of return observations required before calculating VaR. Defaults to 30. |
30
|
Returns:
| Type | Description |
|---|---|
dict[datetime, float]
|
Dictionary mapping each date to the cumulative VaR up to that date. |
dict[datetime, float]
|
Days with insufficient observations are excluded. |
Source code in src/athena/metrics/value_at_risk.py
calculate_var_by_day_rolling_window(portfolio, target_currency, window_size, confidence_level=0.95, method='historical', start_date=None, end_date=None)
¶
Calculate rolling window Value at Risk for each day in a date range.
For each day, calculates VaR using the trailing window_size days of returns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
portfolio
|
Portfolio
|
Portfolio object containing transactions and settings. |
required |
target_currency
|
Currency
|
The currency to convert all values into. |
required |
window_size
|
int
|
Number of trailing days to use for each calculation. Common values: 30, 60, 90, 252 (trading year). |
required |
confidence_level
|
float
|
Confidence level for VaR (e.g., 0.95 for 95%). |
0.95
|
method
|
str
|
VaR calculation method ("historical" or "parametric"). |
'historical'
|
start_date
|
datetime | None
|
The start date for the calculation (inclusive). If None, uses the earliest transaction date. |
None
|
end_date
|
datetime | None
|
The end date for the calculation (inclusive). If None, uses the latest transaction date. |
None
|
Returns:
| Type | Description |
|---|---|
dict[datetime, float]
|
Dictionary mapping each date to the rolling VaR. |
dict[datetime, float]
|
Days with insufficient observations (fewer than window_size) are excluded. |
Source code in src/athena/metrics/value_at_risk.py
calculate_var_cumulative(portfolio, target_currency, confidence_level=0.95, method='historical', start_date=None, end_date=None)
¶
Calculate Value at Risk and CVaR for a portfolio over a date range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
portfolio
|
Portfolio
|
Portfolio object containing transactions and settings. |
required |
target_currency
|
Currency
|
The currency to convert all values into. |
required |
confidence_level
|
float
|
Confidence level for VaR (e.g., 0.95 for 95%). |
0.95
|
method
|
str
|
VaR calculation method ("historical" or "parametric"). |
'historical'
|
start_date
|
datetime | None
|
The start date for the calculation (inclusive). If None, uses the earliest transaction date. |
None
|
end_date
|
datetime | None
|
The end date for the calculation (inclusive). If None, uses the latest transaction date. |
None
|
Returns:
| Type | Description |
|---|---|
Tuple[float, float]
|
Tuple of (var, cvar) as negative floats. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than two observations or invalid parameters. |
Source code in src/athena/metrics/value_at_risk.py
calculate_var_from_values(portfolio_values, confidence_level=0.95, method='historical')
¶
Calculate VaR and CVaR directly from a dictionary of portfolio values.
This is a convenience function when you already have portfolio values and don't need to calculate them from a Portfolio object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
portfolio_values
|
dict[datetime, Decimal]
|
Dictionary mapping dates to portfolio values. |
required |
confidence_level
|
float
|
Confidence level for VaR (e.g., 0.95 for 95%). |
0.95
|
method
|
str
|
VaR calculation method ("historical" or "parametric"). |
'historical'
|
Returns:
| Type | Description |
|---|---|
Tuple[float, float]
|
Tuple of (var, cvar) as negative floats. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than two observations or invalid parameters. |
Source code in src/athena/metrics/value_at_risk.py
calculate_var_historical(returns, confidence_level=0.95)
¶
Calculate Value at Risk using the historical simulation method.
Historical VaR uses the actual distribution of past returns to estimate potential losses at a given confidence level.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
list[float]
|
List of daily returns as decimals (e.g., 0.002 = 0.2%). |
required |
confidence_level
|
float
|
Confidence level for VaR (e.g., 0.95 for 95%). |
0.95
|
Returns:
| Type | Description |
|---|---|
float
|
VaR as a negative float representing the potential loss |
float
|
(e.g., -0.05 means 5% potential loss at the given confidence level). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than two observations or invalid confidence level. |
Source code in src/athena/metrics/value_at_risk.py
calculate_var_parametric(returns, confidence_level=0.95)
¶
Calculate Value at Risk using the parametric (variance-covariance) method.
Parametric VaR assumes returns are normally distributed and uses the mean and standard deviation to estimate potential losses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
list[float]
|
List of daily returns as decimals (e.g., 0.002 = 0.2%). |
required |
confidence_level
|
float
|
Confidence level for VaR (e.g., 0.95 for 95%). |
0.95
|
Returns:
| Type | Description |
|---|---|
float
|
VaR as a negative float representing the potential loss |
float
|
(e.g., -0.05 means 5% potential loss at the given confidence level). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than two observations or invalid confidence level. |
Source code in src/athena/metrics/value_at_risk.py
calculate_downside_volatility(returns, periods_in_year=365)
¶
Calculate downside volatility (volatility of negative returns only).
Downside volatility measures the dispersion of returns below zero, capturing the variability of losses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
list[float]
|
List of daily returns as decimals (e.g., 0.002 = 0.2%). |
required |
periods_in_year
|
int
|
Trading periods in a year (365 for daily). |
365
|
Returns:
| Type | Description |
|---|---|
Tuple[float, float]
|
Tuple of (daily_downside_volatility, annual_downside_volatility). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than two negative return observations. |
Source code in src/athena/metrics/volatility.py
calculate_upside_volatility(returns, periods_in_year=365)
¶
Calculate upside volatility (volatility of positive returns only).
Upside volatility measures the dispersion of returns above zero, capturing the variability of gains.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
list[float]
|
List of daily returns as decimals (e.g., 0.002 = 0.2%). |
required |
periods_in_year
|
int
|
Trading periods in a year (365 for daily). |
365
|
Returns:
| Type | Description |
|---|---|
Tuple[float, float]
|
Tuple of (daily_upside_volatility, annual_upside_volatility). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than two positive return observations. |
Source code in src/athena/metrics/volatility.py
calculate_volatility(returns, periods_in_year=365)
¶
Calculate daily and annualized volatility from a list of returns.
Volatility is measured as the standard deviation of returns, which quantifies the dispersion of returns around their mean.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
list[float]
|
List of daily returns as decimals (e.g., 0.002 = 0.2%). |
required |
periods_in_year
|
int
|
Trading periods in a year (365 for daily). |
365
|
Returns:
| Type | Description |
|---|---|
Tuple[float, float]
|
Tuple of (daily_volatility, annual_volatility). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than two observations. |
Source code in src/athena/metrics/volatility.py
calculate_volatility_by_day_cumulative(portfolio, target_currency, start_date=None, end_date=None, periods_in_year=365, min_observations=2)
¶
Calculate cumulative volatility for each day in a date range.
For each day, calculates the volatility using all returns from start_date up to that day (cumulative/expanding window).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
portfolio
|
Portfolio
|
Portfolio object containing transactions and settings. |
required |
target_currency
|
Currency
|
The currency to convert all values into. |
required |
start_date
|
datetime | None
|
The start date for the calculation (inclusive). If None, uses the earliest transaction date. |
None
|
end_date
|
datetime | None
|
The end date for the calculation (inclusive). If None, uses the latest transaction date. |
None
|
periods_in_year
|
int
|
Trading periods in a year (365 for daily, 252 for US equities trading days). |
365
|
min_observations
|
int
|
Minimum number of return observations required before calculating volatility. Defaults to 2. |
2
|
Returns:
| Type | Description |
|---|---|
dict[datetime, Tuple[float, float]]
|
Dictionary mapping each date to a tuple of (daily_volatility, annual_volatility). |
dict[datetime, Tuple[float, float]]
|
Days with insufficient observations are excluded. |
Source code in src/athena/metrics/volatility.py
calculate_volatility_by_day_rolling_window(portfolio, target_currency, window_size, start_date=None, end_date=None, periods_in_year=365)
¶
Calculate rolling window volatility for each day in a date range.
For each day, calculates the volatility using the trailing window_size days of returns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
portfolio
|
Portfolio
|
Portfolio object containing transactions and settings. |
required |
target_currency
|
Currency
|
The currency to convert all values into. |
required |
window_size
|
int
|
Number of trailing days to use for each calculation. Common values: 20, 30, 60, 90. |
required |
start_date
|
datetime | None
|
The start date for the calculation (inclusive). If None, uses the earliest transaction date. |
None
|
end_date
|
datetime | None
|
The end date for the calculation (inclusive). If None, uses the latest transaction date. |
None
|
periods_in_year
|
int
|
Trading periods in a year (365 for daily, 252 for US equities trading days). |
365
|
Returns:
| Type | Description |
|---|---|
dict[datetime, Tuple[float, float]]
|
Dictionary mapping each date to a tuple of (daily_volatility, annual_volatility). |
dict[datetime, Tuple[float, float]]
|
Days with insufficient observations (fewer than window_size) are excluded. |
Source code in src/athena/metrics/volatility.py
calculate_volatility_cumulative(portfolio, target_currency, start_date=None, end_date=None, periods_in_year=365)
¶
Calculate the volatility for a portfolio over a date range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
portfolio
|
Portfolio
|
Portfolio object containing transactions and settings. |
required |
target_currency
|
Currency
|
The currency to convert all values into. |
required |
start_date
|
datetime | None
|
The start date for the calculation (inclusive). If None, uses the earliest transaction date. |
None
|
end_date
|
datetime | None
|
The end date for the calculation (inclusive). If None, uses the latest transaction date. |
None
|
periods_in_year
|
int
|
Trading periods in a year (365 for daily, 252 for US equities trading days). |
365
|
Returns:
| Type | Description |
|---|---|
Tuple[float, float]
|
Tuple of (daily_volatility, annual_volatility). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than two observations. |
Source code in src/athena/metrics/volatility.py
calculate_volatility_from_values(portfolio_values, periods_in_year=365)
¶
Calculate volatility directly from a dictionary of portfolio values.
This is a convenience function when you already have portfolio values and don't need to calculate them from a Portfolio object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
portfolio_values
|
dict[datetime, Decimal]
|
Dictionary mapping dates to portfolio values. |
required |
periods_in_year
|
int
|
Trading periods in a year (365 for daily). |
365
|
Returns:
| Type | Description |
|---|---|
Tuple[float, float]
|
Tuple of (daily_volatility, annual_volatility). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If fewer than two observations. |
Source code in src/athena/metrics/volatility.py
calculate_volatility_ratio(returns)
¶
Calculate the ratio of downside to upside volatility.
A ratio greater than 1 indicates more variability in losses than gains. A ratio less than 1 indicates more variability in gains than losses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
list[float]
|
List of daily returns as decimals (e.g., 0.002 = 0.2%). |
required |
Returns:
| Type | Description |
|---|---|
float | None
|
The volatility ratio (downside/upside), or None if insufficient data. |
Source code in src/athena/metrics/volatility.py
calculate_volatility_statistics(portfolio, target_currency, start_date=None, end_date=None, periods_in_year=365)
¶
Calculate comprehensive volatility statistics for a portfolio.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
portfolio
|
Portfolio
|
Portfolio object containing transactions and settings. |
required |
target_currency
|
Currency
|
The currency to convert all values into. |
required |
start_date
|
datetime | None
|
The start date for the calculation (inclusive). If None, uses the earliest transaction date. |
None
|
end_date
|
datetime | None
|
The end date for the calculation (inclusive). If None, uses the latest transaction date. |
None
|
periods_in_year
|
int
|
Trading periods in a year (365 for daily). |
365
|
Returns:
| Type | Description |
|---|---|
dict[str, float | None]
|
Dictionary containing: |
dict[str, float | None]
|
|
dict[str, float | None]
|
|
dict[str, float | None]
|
|
dict[str, float | None]
|
|
dict[str, float | None]
|
|
dict[str, float | None]
|
|
dict[str, float | None]
|
|
Source code in src/athena/metrics/volatility.py
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 | |
calculate_win_rate_all_positions(portfolio, as_of=None)
¶
Calculate win rate based on all positions (open and closed).
For closed positions, uses realized gain/loss. For open positions, uses unrealized gain/loss based on current market value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
portfolio
|
Portfolio
|
Portfolio object containing transactions. |
required |
as_of
|
datetime | None
|
The datetime to evaluate positions at. If None, uses the latest transaction date. |
None
|
Returns:
| Type | Description |
|---|---|
WinRateResult
|
WinRateResult containing win rate statistics for all positions. |
Source code in src/athena/metrics/win_rate.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | |
calculate_win_rate_by_symbol(portfolio, as_of=None, include_open=False)
¶
Calculate win rate statistics grouped by symbol.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
portfolio
|
Portfolio
|
Portfolio object containing transactions. |
required |
as_of
|
datetime | None
|
Only consider transactions up to this datetime. If None, considers all transactions. |
None
|
include_open
|
bool
|
If True, includes unrealized gains from open positions. |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, WinRateResult]
|
Dictionary mapping symbol to WinRateResult. |
Source code in src/athena/metrics/win_rate.py
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 | |
calculate_win_rate_closed(portfolio, as_of=None)
¶
Calculate win rate based on closed positions only.
A winning position is one where the sell price is higher than the buy price. Uses FIFO matching to pair buys with sells.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
portfolio
|
Portfolio
|
Portfolio object containing transactions. |
required |
as_of
|
datetime | None
|
Only consider transactions up to this datetime. If None, considers all transactions. |
None
|
Returns:
| Type | Description |
|---|---|
WinRateResult
|
WinRateResult containing win rate statistics for closed positions. |
Source code in src/athena/metrics/win_rate.py
get_closed_positions(portfolio, as_of=None)
¶
Get all closed positions from a portfolio.
A closed position is created when shares are sold. This uses FIFO (First In, First Out) matching to pair buys with sells.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
portfolio
|
Portfolio
|
Portfolio object containing transactions. |
required |
as_of
|
datetime | None
|
Only consider transactions up to this datetime. If None, considers all transactions. |
None
|
Returns:
| Type | Description |
|---|---|
list[ClosedPosition]
|
List of ClosedPosition objects representing realized trades. |
Source code in src/athena/metrics/win_rate.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | |