2023-09-15 14:17:44 +00:00
|
|
|
from datetime import datetime
|
2023-09-15 15:45:19 +00:00
|
|
|
from dotenv import load_dotenv
|
2023-09-15 14:17:44 +00:00
|
|
|
from ibkr import Client
|
2023-09-15 15:45:19 +00:00
|
|
|
from os import getenv
|
|
|
|
from tastytrade import Tastytrade
|
2023-09-15 14:17:44 +00:00
|
|
|
|
2023-09-15 15:45:19 +00:00
|
|
|
load_dotenv()
|
|
|
|
|
2023-09-18 16:14:39 +00:00
|
|
|
ibkr_host = getenv('IBKR_HOST')
|
|
|
|
ibkr_port = getenv('IBKR_PORT')
|
2023-09-15 15:45:19 +00:00
|
|
|
ibkr_client = Client(host = ibkr_host, port = ibkr_port)
|
|
|
|
|
2023-09-18 16:14:39 +00:00
|
|
|
tastytrade_account = getenv('TASTYTRADE_ACCOUNT')
|
|
|
|
tastytrade_username = getenv('TASTYTRADE_USERNAME')
|
|
|
|
tastytrade_password = getenv('TASTYTRADE_PASSWORD')
|
2023-09-15 15:45:19 +00:00
|
|
|
tastytrade_client = Tastytrade(tastytrade_username, tastytrade_password)
|
|
|
|
tastytrade_client.login()
|
2023-09-15 14:17:44 +00:00
|
|
|
|
|
|
|
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)
|