Skip to content

API Reference parrot_tools.quant.stress_testing

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot_tools.quant.stress_testing

View source

Stress Testing Framework for QuantToolkit.

Provides portfolio stress testing capabilities:

  • Apply historical or hypothetical shock scenarios to portfolios
  • Calculate portfolio-level and position-level losses
  • Identify worst/best performing positions under stress
  • Generate custom volatility spike scenarios

Predefined Scenarios:

  • covid_crash_2020: March 2020 COVID market crash
  • rate_hike_shock: Interest rate increase scenario
  • crypto_winter: Major crypto bear market
  • black_swan: Generic severe market stress

Attribute PREDEFINED_SCENARIOS

Type: dict[str, StressScenario]

Alias StressScenario

Re-exported from parrot_tools.quant.models.StressScenario.

Function create_custom_scenario

Create a custom stress scenario.

create_custom_scenario(name: str, asset_shocks: dict[str, float], description: str | None = None) -> StressScenario

Parameter Type Default Description
name str Scenario name.
asset_shocks dict[str, float] {symbol: shock_pct} mapping (e.g., -0.20 = -20%).
description str | None None Optional description.

Returns: StressScenario — StressScenario object.

Function create_sector_rotation_scenario

Create a sector rotation scenario.

create_sector_rotation_scenario(sector_shocks: dict[str, float], sector_mapping: dict[str, str]) -> StressScenario

Parameter Type Default Description
sector_shocks dict[str, float] {sector: shock_pct} mapping. Example: {"tech": -0.15, "energy": 0.10, "utilities": 0.05}
sector_mapping dict[str, str] {symbol: sector} mapping. Example: {"AAPL": "tech", "XOM": "energy", "NEE": "utilities"}

Returns: StressScenario — StressScenario with symbol-level shocks derived from sectors.

Function create_volatility_shock_scenario

Create a scenario where volatility spikes by a multiplier.

Higher vol typically correlates with negative returns. Rule of thumb: 2x vol spike ~ -10% to -20% return for equities.

create_volatility_shock_scenario(current_volatilities: dict[str, float], multiplier: float = 2.0, vol_to_return_factor: float = -0.5) -> StressScenario

Parameter Type Default Description
current_volatilities dict[str, float] {symbol: current_annual_vol} mapping.
multiplier float 2.0 How much vol increases (2.0 = doubles).
vol_to_return_factor float -0.5 Conversion factor (negative = vol up means returns down).

Returns: StressScenario — StressScenario with estimated return shocks.

Function get_concentrated_risk_positions

Identify positions that contribute disproportionately to losses.

get_concentrated_risk_positions(stress_result: dict, threshold_pct: float = 0.1) -> list[dict]

Parameter Type Default Description
stress_result dict Output from stress_test_portfolio().
threshold_pct float 0.1 Loss threshold as fraction of portfolio (default 10%).

Returns: list[dict] — List of positions with high stress impact: [{"symbol": "BTC", "scenario": "covid_crash_2020", "loss_pct": -0.15}, ...]

Function get_predefined_scenario

Get a predefined stress scenario by name.

get_predefined_scenario(name: str) -> StressScenario

Parameter Type Default Description
name str Scenario name (e.g., "covid_crash_2020").

Returns: StressScenario — StressScenario object.

Raises:

  • ValueError — If scenario name is unknown.

Function get_scenario_descriptions

Get descriptions for all predefined scenarios.

get_scenario_descriptions() -> dict[str, str]

Returns: dict[str, str] — {scenario_name: description} mapping.

Function list_predefined_scenarios

List all available predefined scenario names.

list_predefined_scenarios() -> list[str]

Returns: list[str] — List of scenario names.

Function stress_test_portfolio

Apply stress scenarios to a portfolio and estimate losses.

stress_test_portfolio(portfolio_values: dict[str, float], weights: list[float] | None = None, symbols: list[str] | None = None, scenarios: list[StressScenario] | None = None, total_portfolio_value: float | None = None) -> dict

Parameter Type Default Description
portfolio_values dict[str, float] {symbol: current_market_value} mapping.
weights list[float] | None None Position weights (optional, for documentation only).
symbols list[str] | None None Symbol list (optional, for documentation only).
scenarios list[StressScenario] | None None List of stress scenarios to apply. If None, uses all predefined.
total_portfolio_value float | None None Total portfolio value. If None, calculated from positions.

Returns: dict — Dictionary with structure: { "scenario_results": { "covid_crash_2020": { "portfolio_loss_pct": -0.32, "portfolio_loss_usd": -32000.0, "position_impacts": { "SPY": {"shock": -0.34, "loss_usd": -17000.0}, ... }, "worst_position": "BTC", "best_position": "TLT", }, ... }, "worst_scenario": "covid_crash_2020", "max_loss_pct": -0.35, }

Raises:

  • ValueError — If portfolio value is not positive.

Function summarize_stress_results

Generate a human-readable summary of stress test results.

summarize_stress_results(stress_result: dict) -> str

Parameter Type Default Description
stress_result dict Output from stress_test_portfolio().

Returns: str — Formatted summary string.

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally