18 lines
825 B
Python
18 lines
825 B
Python
|
from datetime import datetime
|
||
|
from ibkr import Client
|
||
|
|
||
|
ibkr_client = Client(host = '127.0.0.1', port = 7497)
|
||
|
|
||
|
underlying_ticker = ibkr_client.get_ticker('SPX', 'CBOE')
|
||
|
current_price = underlying_ticker.last
|
||
|
|
||
|
# Filtering strikes based on distance from current price speeds up the request.
|
||
|
max_strike_distance = 100
|
||
|
def contract_filter(contract):
|
||
|
if contract.right == 'C':
|
||
|
return contract.strike <= (current_price + max_strike_distance) and contract.strike >= current_price
|
||
|
return contract.strike <= current_price and contract.strike >= (current_price - max_strike_distance)
|
||
|
|
||
|
# The weekly symbol for SPX (SPXW) is required in order to distinguish from monthly options.
|
||
|
option_chain = ibkr_client.get_option_chain('SPX', datetime.now(), sub_symbol = 'SPXW', contract_filter = contract_filter)
|
||
|
print(option_chain)
|