qqq_today_options.py•2.47 kB
import os
from datetime import datetime, timezone
from typing import List, Dict, Any
from ib_mcp.ib import ib_manager
def main() -> None:
symbol = "QQQ"
# Use UTC date; IB expiries are in YYYYMMDD. If today has weeklies, it will match.
today_str = datetime.now(timezone.utc).strftime("%Y%m%d")
# Enumerate option chain (using reqSecDefOptParams under the hood)
items: List[Dict[str, Any]] = ib_manager.list_option_contracts(
symbol=symbol,
months_ahead=1,
right="BOTH",
strikes_around=12,
include_weeklies=True,
exchange="SMART",
currency="USD",
)
items = [it for it in items if it.get("expiry") == today_str]
if not items:
print(f"No QQQ options found for today ({today_str}). If this is not an expiration date, there may be no same-day options.")
return
# Limit the number just for demo readability
items = items[:200]
# Build contracts and fetch batched snapshots
contracts = [
ib_manager.option(
it["symbol"], it["expiry"], it["strike"], it["right"], it["exchange"], it["currency"]
)
for it in items
]
snapshots = ib_manager.req_snapshots_batch(contracts, timeout_sec=8.0, chunk_size=80)
rows = []
for it, snap in zip(items, snapshots):
right = it.get("right", "").upper()
type_str = "Call" if right == "C" else ("Put" if right == "P" else right)
contract_str = f"{it['symbol']} {it['expiry']} {it['strike']} {right}"
rows.append(
(
contract_str,
type_str,
snap.get("bid"),
snap.get("ask"),
snap.get("last"),
)
)
# Sort by strike then type for readability
def parse_strike(contract: str) -> float:
try:
return float(contract.split()[2])
except Exception:
return 0.0
rows.sort(key=lambda r: (parse_strike(r[0]), r[1]))
# Print table
print(f"QQQ Options for {today_str}\n")
print(f"{'Contract':<28} {'Type':<9} {'Bid':>8} {'Ask':>8} {'Last':>8}")
print("-" * 70)
for contract, type_str, bid, ask, last in rows:
def f(v):
return f"{v:.2f}" if isinstance(v, (int, float)) and v == v else ("" if v is None else str(v))
print(f"{contract:<28} {type_str:<9} {f(bid):>8} {f(ask):>8} {f(last):>8}")
if __name__ == "__main__":
main()