from __future__ import annotations from typing import Any from nicegui import ui from app.components import GreeksTable from app.pages.common import dashboard_page, strategy_catalog from app.services.runtime import get_data_service @ui.page("/options") async def options_page() -> None: data_service = get_data_service() expirations_data = await data_service.get_option_expirations("GLD") expiries = list(expirations_data.get("expirations") or []) default_expiry = expiries[0] if expiries else None chain_data = await data_service.get_options_chain_for_expiry("GLD", default_expiry) chain_state = { "data": chain_data, "rows": list(chain_data.get("rows") or [*chain_data.get("calls", []), *chain_data.get("puts", [])]), } selected_expiry = {"value": chain_data.get("selected_expiry") or default_expiry} selected_strategy = {"value": strategy_catalog()[0]["label"]} chosen_contracts: list[dict[str, Any]] = [] def strike_bounds(rows: list[dict[str, Any]]) -> tuple[float, float]: strike_values = sorted({float(row["strike"]) for row in rows}) if not strike_values: return 0.0, 0.0 return strike_values[0], strike_values[-1] initial_min_strike, initial_max_strike = strike_bounds(chain_state["rows"]) strike_range = {"min": initial_min_strike, "max": initial_max_strike} with dashboard_page( "Options Chain", "Browse GLD contracts, filter by expiry and strike range, inspect Greeks, and attach contracts to hedge workflows.", "options", ): with ui.row().classes("w-full gap-6 max-lg:flex-col"): with ui.card().classes( "w-full rounded-2xl border border-slate-200 bg-white shadow-sm dark:border-slate-800 dark:bg-slate-900" ): ui.label("Filters").classes("text-lg font-semibold text-slate-900 dark:text-slate-100") expiry_select = ui.select(expiries, value=selected_expiry["value"], label="Expiry").classes("w-full") min_strike = ui.number("Min strike", value=strike_range["min"], step=5).classes("w-full") max_strike = ui.number("Max strike", value=strike_range["max"], step=5).classes("w-full") strategy_select = ui.select( [item["label"] for item in strategy_catalog()], value=selected_strategy["value"], label="Add to hedge strategy", ).classes("w-full") source_html = ui.html("").classes("text-xs text-slate-500 dark:text-slate-400") error_html = ui.html("").classes("text-xs text-amber-700 dark:text-amber-300") loading_html = ui.html("").classes("text-xs text-sky-700 dark:text-sky-300") selection_card = ui.card().classes( "w-full rounded-2xl border border-slate-200 bg-white shadow-sm dark:border-slate-800 dark:bg-slate-900" ) chain_table = ui.html("").classes("w-full") greeks = GreeksTable([]) quick_add = ui.card().classes( "w-full rounded-2xl border border-slate-200 bg-white shadow-sm dark:border-slate-800 dark:bg-slate-900" ) def sync_status() -> None: current_data = chain_state["data"] source_label = f"Source: {current_data.get('source', 'unknown')}" if current_data.get("updated_at"): source_label += f" · Updated {current_data['updated_at']}" source_html.content = source_label source_html.update() error_message = current_data.get("error") or expirations_data.get("error") error_html.content = f"Options data unavailable: {error_message}" if error_message else "" error_html.update() def filtered_rows() -> list[dict[str, Any]]: return [ row for row in chain_state["rows"] if strike_range["min"] <= float(row["strike"]) <= strike_range["max"] ] def render_selection() -> None: selection_card.clear() with selection_card: ui.label("Strategy Integration").classes("text-lg font-semibold text-slate-900 dark:text-slate-100") ui.label(f"Target strategy: {selected_strategy['value']}").classes( "text-sm text-slate-500 dark:text-slate-400" ) if not chosen_contracts: ui.label("No contracts added yet.").classes("text-sm text-slate-500 dark:text-slate-400") return for contract in chosen_contracts[-3:]: ui.label( f"{contract['symbol']} · premium ${float(contract['premium']):.2f} · IV {float(contract.get('impliedVolatility', 0.0)):.1%}" ).classes("text-sm text-slate-600 dark:text-slate-300") def add_to_strategy(contract: dict[str, Any]) -> None: chosen_contracts.append(contract) render_selection() greeks.set_options(chosen_contracts[-6:]) ui.notify(f"Added {contract['symbol']} to {selected_strategy['value']}", color="positive") def render_chain() -> None: rows = filtered_rows() chain_table.content = ( """
| Contract | Type | Strike | Bid / Ask | Last | IV | Greeks | Action |
|---|---|---|---|---|---|---|---|
| {row['symbol']} | {row['type'].upper()} | ${float(row['strike']):.2f} | ${float(row['bid']):.2f} / ${float(row['ask']):.2f} | ${float(row.get('lastPrice', row.get('premium', 0.0))):.2f} | {float(row.get('impliedVolatility', 0.0)):.1%} | Δ {float(row.get('delta', 0.0)):+.3f} · Γ {float(row.get('gamma', 0.0)):.3f} · Θ {float(row.get('theta', 0.0)):+.3f} · V {float(row.get('vega', 0.0)):.3f} | Use quick-add buttons below |
| No contracts match the current filter. | |||||||