Last active
June 6, 2026 19:35
-
-
Save jweinst1/ffe90e3e4e3e625975df45a8823998c8 to your computer and use it in GitHub Desktop.
get options quotes from alpaca py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| >>> from alpaca.data.requests import OptionBarsRequest | |
| >>> from alpaca.data.timeframe import TimeFrame | |
| >>> bars_request = OptionBarsRequest(symbol_or_symbols='QQQ260702C00750000', timeframe=TimeFrame.Day) | |
| >>> bars = client.get_option_bars(bars_request) | |
| >>> bars | |
| {'data': {}} | |
| >>> from datetime import datetime | |
| >>> bars_request = OptionBarsRequest(symbol_or_symbols='QQQ260702C00750000', timeframe=TimeFrame.Day, start=datetime(2026, 6, 1), end=datetime(2026, 6, 5)) | |
| >>> bars = client.get_option_bars(bars_request) | |
| >>> bars | |
| { 'data': { 'QQQ260702C00750000': [ { 'close': 15.83, | |
| 'high': 16.71, | |
| 'low': 12.68, | |
| 'open': 12.72, | |
| 'symbol': 'QQQ260702C00750000', | |
| 'timestamp': datetime.datetime(2026, 6, 1, 4, 0, tzinfo=TzInfo(0)), | |
| 'trade_count': 38.0, | |
| 'volume': 144.0, | |
| 'vwap': 13.477778}, | |
| { 'close': 16.65, | |
| 'high': 16.95, | |
| 'low': 13.88, | |
| 'open': 13.88, | |
| 'symbol': 'QQQ260702C00750000', | |
| 'timestamp': datetime.datetime(2026, 6, 2, 4, 0, tzinfo=TzInfo(0)), | |
| 'trade_count': 42.0, | |
| 'volume': 133.0, | |
| 'vwap': 16.063835}, | |
| { 'close': 15.53, | |
| 'high': 18.31, | |
| 'low': 14.62, | |
| 'open': 18.31, | |
| 'symbol': 'QQQ260702C00750000', | |
| 'timestamp': datetime.datetime(2026, 6, 3, 4, 0, tzinfo=TzInfo(0)), | |
| 'trade_count': 70.0, | |
| 'volume': 235.0, | |
| 'vwap': 15.869532}, | |
| { 'close': 12.79, | |
| 'high': 14.52, | |
| 'low': 10.3, | |
| 'open': 11.23, | |
| 'symbol': 'QQQ260702C00750000', | |
| 'timestamp': datetime.datetime(2026, 6, 4, 4, 0, tzinfo=TzInfo(0)), | |
| 'trade_count': 55.0, | |
| 'volume': 151.0, | |
| 'vwap': 11.865828}]}} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| from alpaca.data.historical import OptionHistoricalDataClient | |
| from alpaca.data.requests import OptionChainRequest | |
| from alpaca.data.enums import OptionsFeed | |
| # ====================== CONFIG ====================== | |
| API_KEY = "*****************" | |
| SECRET_KEY = "*********************************" | |
| MAX_CONTRACTS_TO_SHOW = 15 # Change this to see more/less | |
| # =================================================== | |
| if not API_KEY or not SECRET_KEY: | |
| print("β Please set your ALPACA_API_KEY and ALPACA_SECRET_KEY environment variables.") | |
| exit(1) | |
| client = OptionHistoricalDataClient(API_KEY, SECRET_KEY) | |
| request = OptionChainRequest( | |
| underlying_symbol="XSP", | |
| feed=OptionsFeed.INDICATIVE, # Change to OPRA if you have the subscription | |
| ) | |
| print("π Fetching XSP option chain...\n") | |
| try: | |
| chain = client.get_option_chain(request) | |
| if not chain: | |
| print("β No contracts returned.") | |
| else: | |
| print(f"β Retrieved {len(chain)} option contracts for XSP\n") | |
| for i, (symbol, snapshot) in enumerate(chain.items()): | |
| if i >= MAX_CONTRACTS_TO_SHOW: | |
| break | |
| # Parse contract details from symbol (e.g. XSP260619C00700000) | |
| # Format: Root + YYMMDD + C/P + Strike (padded) | |
| try: | |
| root = symbol[:3] # XSP | |
| exp_date = f"20{symbol[3:9]}" # e.g. 20260619 | |
| option_type = "Call" if symbol[9] == "C" else "Put" | |
| strike = int(symbol[10:]) / 1000 # Strike is usually padded with zeros | |
| except: | |
| option_type = "Unknown" | |
| exp_date = "Unknown" | |
| strike = "Unknown" | |
| print(f"{symbol}") | |
| print(f" Type : {option_type}") | |
| print(f" Strike : {strike}") | |
| print(f" Expiration : {exp_date}") | |
| quote = snapshot.latest_quote | |
| trade = snapshot.latest_trade | |
| if quote: | |
| print(f" Bid : {quote.bid_price} x {quote.bid_size}") | |
| print(f" Ask : {quote.ask_price} x {quote.ask_size}") | |
| else: | |
| print(" Bid/Ask : No quote available") | |
| if trade and trade.price is not None: | |
| print(f" Last Trade : {trade.price} @ {trade.timestamp}") | |
| else: | |
| print(" Last Trade : No trade data") | |
| print("-" * 70) | |
| if len(chain) > MAX_CONTRACTS_TO_SHOW: | |
| print(f"\n... and {len(chain) - MAX_CONTRACTS_TO_SHOW:,} more contracts (total: {len(chain):,})") | |
| except Exception as e: | |
| print(f"β Error: {e}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # get an options chain request | |
| """ | |
| 'implied_volatility': 0.2353, | |
| 'latest_quote': { 'ask_exchange': 'A', | |
| 'ask_price': 0.35, | |
| 'ask_size': 244.0, | |
| 'bid_exchange': 'A', | |
| 'bid_price': 0.02, | |
| 'bid_size': 206.0, | |
| 'conditions': 'A', | |
| 'symbol': 'QQQ260702C00810000', | |
| 'tape': None, | |
| 'timestamp': datetime.datetime(2026, 6, 5, 19, 59, 59, 663412, tzinfo=TzInfo(0))}, | |
| 'latest_trade': { 'conditions': 'f', | |
| 'exchange': 'M', | |
| 'id': None, | |
| 'price': 0.19, | |
| 'size': 1.0, | |
| 'symbol': 'QQQ260702C00810000', | |
| 'tape': None, | |
| 'timestamp': datetime.datetime(2026, 6, 5, 19, 8, 59, 561737, tzinfo=TzInfo(0))}, | |
| 'symbol': 'QQQ260702C00810000'}, 'QQQ260702C00750000': { 'greeks': { 'delta': 0.1677, | |
| 'gamma': 0.0054, | |
| 'rho': 0.0808, | |
| 'theta': -0.235, | |
| 'vega': 0.4689}, | |
| 'implied_volatility': 0.2482, | |
| 'latest_quote': { 'ask_exchange': 'A', | |
| 'ask_price': 4.14, | |
| 'ask_size': 320.0, | |
| 'bid_exchange': 'N', | |
| 'bid_price': 3.9, | |
| 'bid_size': 1.0, | |
| 'conditions': 'B', | |
| 'symbol': 'QQQ260702C00750000', | |
| 'tape': None, | |
| 'timestamp': datetime.datetime(2026, 6, 5, 19, 59, 59, 195176, tzinfo=TzInfo(0))}, | |
| 'latest_trade': { 'conditions': 'a', | |
| 'exchange': 'X', | |
| 'id': None, | |
| 'price': 3.76, | |
| 'size': 10.0, | |
| 'symbol': 'QQQ260702C00750000', | |
| 'tape': None, | |
| 'timestamp': datetime.datetime(2026, 6, 5, 20, 9, 51, 242377, tzinfo=TzInfo(0))}, | |
| resp = client.get_option_chain(req) | |
| client = OptionHistoricalDataClient(API_KEY, SECRET_KEY) | |
| """ | |
| req = OptionChainRequest(underlying_symbol="QQQ", expiration_date_gte="2026-06-29", expiration_date_lte="2026-07-14", type=ContractType.CALL, strike_price_lte=810.0, strike_price_gte=750.0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment