Requesting Option Instruments and Greeks Through Interactive Brokers
Summary
This example outlines a staged workflow for obtaining option data through an Interactive Brokers connection. It configures an instrument provider for an underlying futures contract and a put option, then checks whether the gateway or trading workstation is reachable before attempting to request the option instrument. The client is configured to use delayed market data.
Running the data request and starting the strategy node are controlled separately by environment settings. After instrument retrieval, the example can launch a strategy intended to subscribe to option Greeks and schedule the node to stop. The code does not show the Greek calculations, subscription handling, or resulting market data, and it is an integration example rather than evidence of a trading strategy or performance.
Key ideas
- The example configures Interactive Brokers to load an underlying futures instrument and a put option.
- It checks endpoint reachability before making an instrument request.
- Delayed market data is selected for the client.
- Separate settings control data requests and strategy-node startup.
- The example does not expose Greek calculations or report trading results.
Tags
Full text
# option_greeks.py
```py
# -------------------------------------------------------------------------------------------------
# Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
# https://nautechsystems.io
#
# Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
# You may not use this file except in compliance with the License.
# You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
# -------------------------------------------------------------------------------------------------
"""
Example of option greeks.
"""
from __future__ import annotations
import asyncio
import os
from _common import add_strategy_from_config
from _common import build_ib_live_node
from _common import default_es_future_instrument_id
from _common import default_es_put_option_instrument_id
from _common import env_bool
from _common import env_int
from _common import instrument_provider_config
from _common import is_ib_endpoint_reachable
from _common import resolve_ib_endpoint
from _common import schedule_node_stop
from nautilus_trader.adapters import interactive_brokers
from nautilus_trader.model import InstrumentId
async def main() -> None:
"""
Run the example.
"""
ib = interactive_brokers
host, port = resolve_ib_endpoint()
provider_config = instrument_provider_config(
load_ids=[
os.getenv(
"IB_V2_OPTION_UNDERLYING_INSTRUMENT_ID",
default_es_future_instrument_id(),
),
os.getenv("IB_V2_OPTION_INSTRUMENT_ID", default_es_put_option_instrument_id()),
],
)
provider = ib.InteractiveBrokersInstrumentProvider(provider_config)
client_config = ib.InteractiveBrokersDataClientConfig(
host=host,
port=port,
client_id=env_int("IB_V2_OPTION_CLIENT_ID", 1401),
connection_timeout=env_int("IB_V2_CONNECTION_TIMEOUT", 10),
request_timeout=env_int("IB_V2_REQUEST_TIMEOUT", 30),
market_data_type=ib.MarketDataType.DELAYED,
instrument_provider=provider_config,
)
if not env_bool("IB_V2_RUN_CLIENT"):
print(
"Built IB option greeks client. Set IB_V2_RUN_CLIENT=1 to request data.",
flush=True,
)
return
if not is_ib_endpoint_reachable(host, port):
print(f"IB Gateway/TWS is not reachable at {host}:{port}", flush=True)
return
try:
client = ib.HistoricalInteractiveBrokersClient(provider, client_config)
except RuntimeError as e:
print(f"Failed to connect to IB Gateway/TWS at {host}:{port}: {e}", flush=True)
return
print("Requesting option instruments...", flush=True)
instruments = await client.request_instruments(
instrument_ids=[
InstrumentId.from_str(
os.getenv("IB_V2_OPTION_INSTRUMENT_ID", default_es_put_option_instrument_id()),
),
],
)
print(f"Loaded {len(instruments)} option instrument(s)", flush=True)
for instrument in instruments:
print(instrument.id, flush=True)
if instruments:
os.environ.setdefault("IB_V2_OPTION_INSTRUMENT_ID", str(instruments[0].id))
if not env_bool("IB_V2_RUN_NODE"):
print(
"Set IB_V2_RUN_NODE=1 to subscribe to option greeks through a v2 strategy.",
flush=True,
)
return
node = build_ib_live_node(
name="IB-V2-OPTION-GREEKS-001",
trader_id="IB-V2-OPTION-GREEKS-001",
host=host,
port=port,
data_client_id=env_int("IB_V2_OPTION_NODE_CLIENT_ID", 1402),
provider_config=provider_config,
)
add_strategy_from_config(
node,
"ib_v2_order_strategies:OptionGreeksStrategy",
)
schedule_node_stop(node, env_int("IB_V2_AUTO_STOP_SECONDS", 30))
node.run()
if __name__ == "__main__":
asyncio.run(main())
```Shown in full with attribution under the source's licence. Licence: LGPL-3.0
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.