Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(quantlib): enforce implied volatility price tolerance

Rudy Celekli971a170e0f77
diff --git a/README.md b/README.md
index 9da849c1c..821338e2b 100644
--- a/README.md
+++ b/README.md
@@ -1593,6 +1593,8 @@ with `--host` / `--port`.
**MCP tools exposed (74):** `list_skills`, `load_skill`, `start_research_goal`, `get_research_goal`, `add_goal_evidence`, `update_research_goal_status`, `backtest`, `factor_analysis`, `alpha_zoo`, `alpha_bench`, `analyze_options`, `analyze_options_payoff`, `pattern_recognition`, `read_url`, `read_document`, `web_search`, `write_file`, `read_file`, `list_strategies`, `query_strategies`, `get_strategy_evidence`, `refresh_strategy_evidence`, `trading_connections`, `trading_select_connection`, `trading_check`, `trading_account`, `trading_positions`, `trading_orders`, `trading_quote`, `trading_history`, `list_swarm_presets`, `run_swarm`, `get_market_data`, `get_fund_flow`, `get_dragon_tiger`, `get_northbound_flow`, `get_margin_trading`, `get_block_trades`, `get_shareholder_count`, `get_lockup_expiry`, `get_sector_info`, `get_research_reports`, `get_stock_news`, `get_sec_filings`, `get_financial_statements`, `get_options_chain`, `get_stock_profile`, `screen_market`, `search_symbol`, `get_macro_series`, `iwencai_search`, `qveris_search`, `qveris_inspect`, `qveris_execute`, `get_institutional_holdings`, `etf_holdings`, `prediction_market`, `research_papers`, `get_swarm_status`, `get_run_result`, `list_runs`, `reap_stale_runs`, `retry_run`, `analyze_trade_journal`, `extract_shadow_strategy`, `run_shadow_backtest`, `render_shadow_report`, `scan_shadow_signals`, `quantlib_call`, `cashflow_performance`, `orderbook_depth`, `sentiment`, `technical_indicators`, `get_fundamentals`.
+`quantlib_call` option analytics use an absolute **price** tolerance for implied-volatility inversion. A finite result must reprice the quote within that tolerance; the underlying analytics function returns `NaN` for an unconverged or volatility-unidentifiable quote rather than an asserted volatility.
+
### SWARM external MCP tools
`run_swarm` workers can call operator-approved tools from external MCP servers. Configure the server-side allowlist in `VIBE_TRADING_SWARM_AGENT_CONFIG`, `~/.vibe-trading/swarm-agent.json`, or the fallback `~/.vibe-trading/agent.json`; then list remote tools in a swarm preset using the local MCP wrapper name, such as `mcp_internal_kb_search`. Caller-provided `variables` stay template data only and cannot inject MCP URLs, commands, environment variables, or allowlist overrides.
diff --git a/agent/src/quantlib/options.py b/agent/src/quantlib/options.py
index 24d12656c..67f20486d 100644
--- a/agent/src/quantlib/options.py
+++ b/agent/src/quantlib/options.py
@@ -491,10 +491,21 @@ def implied_volatility(market_price: float, S: float, K: float, T: float,
sigma = min(max(sigma - diff / vega, MIN_SIGMA), MAX_SIGMA)
try:
- return identified(float(brentq(
- lambda v: bs_price(S, K, T, r, v, option_type, q) - market_price,
- MIN_SIGMA, MAX_SIGMA, xtol=tol, maxiter=max_iter,
- )))
+ # brentq's xtol is in volatility units, whereas tol is an absolute
+ # price tolerance. Solve the root tightly, then check the same price
+ # residual Newton uses before reporting a converged volatility.
+ candidate = float(
+ brentq(
+ lambda v: bs_price(S, K, T, r, v, option_type, q) - market_price,
+ MIN_SIGMA,
+ MAX_SIGMA,
+ xtol=np.finfo(float).eps,
+ maxiter=max_iter,
+ )
+ )
+ if abs(bs_price(S, K, T, r, candidate, option_type, q) - market_price) >= tol:
+ return float("nan")
+ return identified(candidate)
except (ValueError, RuntimeError):
# ValueError: the bracket does not straddle a root. RuntimeError: brentq
# ran out of its own iterations. Both mean "no volatility found", and
diff --git a/agent/tests/quantlib/test_options_iv_price_tolerance.py b/agent/tests/quantlib/test_options_iv_price_tolerance.py
new file mode 100644
index 000000000..4a0784028
--- /dev/null
+++ b/agent/tests/quantlib/test_options_iv_price_tolerance.py
@@ -0,0 +1,25 @@
+"""Bracket fallback must honor the same price tolerance as Newton."""
+
+import math
+
+import pytest
+
+from src.quantlib.options import bs_price, implied_volatility
+
+
[email protected]("scale", [1.0, 100.0, 10000.0])
+def test_implied_volatility_fallback_reprices_within_absolute_tolerance(
+ scale: float,
+) -> None:
+ spot, strike, maturity, rate, volatility = (
+ 100.0 * scale,
+ 150.0 * scale,
+ 1.0,
+ 0.05,
+ 0.2,
+ )
+ quote = bs_price(spot, strike, maturity, rate, volatility)
+ tolerance = 1e-6
+ implied = implied_volatility(quote, spot, strike, maturity, rate, tol=tolerance)
+ assert math.isfinite(implied)
+ assert abs(bs_price(spot, strike, maturity, rate, implied) - quote) < tolerance