Spaces:
Runtime error
Runtime error
| from __future__ import annotations | |
| import json | |
| from smolagents import tool | |
| from market_data.providers import get_option_chain, list_option_expirations | |
| from .builder import generate_volatility_strategies | |
| from .payoff import strategy_summary | |
| def build_volatility_strategy( | |
| symbol: str, | |
| volatility_view: str = "neutral", | |
| directional_view: str = "neutral", | |
| near_expiration: str = "", | |
| far_expiration: str = "", | |
| ) -> str: | |
| """Build candidate volatility option strategies from the current option chain. | |
| Args: | |
| symbol: Yahoo Finance ticker. | |
| volatility_view: long_vol, short_vol, vol_expansion, vol_compression, term_structure, or neutral. | |
| directional_view: bullish, bearish, neutral, or range_bound. | |
| near_expiration: Near option expiration in YYYY-MM-DD. Empty uses nearest expiration. | |
| far_expiration: Far option expiration for calendar spreads. Empty uses a later available expiration. | |
| """ | |
| try: | |
| symbol = symbol.strip().upper() | |
| expirations = list_option_expirations(symbol) | |
| if not expirations: | |
| raise ValueError(f"No option expirations found for {symbol}.") | |
| near = near_expiration or expirations[0] | |
| far = far_expiration or (expirations[1] if len(expirations) > 1 else "") | |
| near_chain = get_option_chain(symbol, near) | |
| far_chain = get_option_chain(symbol, far) if far else None | |
| strategies = generate_volatility_strategies( | |
| near_chain=near_chain, | |
| volatility_view=volatility_view, | |
| directional_view=directional_view, | |
| far_chain=far_chain, | |
| ) | |
| return json.dumps( | |
| { | |
| "status": "success", | |
| "symbol": symbol, | |
| "near_expiration": near, | |
| "far_expiration": far or None, | |
| "strategies": [ | |
| { | |
| **strategy.to_dict(), | |
| "payoff_summary": strategy_summary(strategy), | |
| } | |
| for strategy in strategies | |
| ], | |
| "risk_note": ( | |
| "This is research output, not a trade recommendation. " | |
| "Validate quotes, liquidity, margin, assignment risk, and event risk before trading." | |
| ), | |
| }, | |
| ensure_ascii=False, | |
| indent=2, | |
| default=str, | |
| ) | |
| except Exception as exc: | |
| return json.dumps( | |
| {"status": "error", "symbol": symbol, "message": str(exc)}, | |
| ensure_ascii=False, | |
| indent=2, | |
| ) | |