Retrieve options chain from TradeStation as opposed to IBKR

This commit is contained in:
moshferatu 2024-01-29 08:41:17 -08:00
parent cd473ded8a
commit 1cc6d303f5

View File

@ -12,6 +12,7 @@ from ibkr.option_type import CALL, PUT
from ibkr.order_action import BUY, SELL from ibkr.order_action import BUY, SELL
from os import getenv from os import getenv
from pytz import timezone from pytz import timezone
from tradestation import TradeStationClient
load_dotenv() load_dotenv()
@ -96,17 +97,9 @@ def _enter_iron_condor():
symbol, sub_symbol = 'SPX', 'SPXW' symbol, sub_symbol = 'SPX', 'SPXW'
expiration = datetime.now() expiration = datetime.now()
underlying_ticker = ibkr_client.get_ticker(symbol, 'CBOE') tradestation_client = TradeStationClient(getenv('TRADESTATION_REFRESH_TOKEN'))
current_price = underlying_ticker.last
# Filtering strikes based on distance from current price speeds up the request. option_chain = tradestation_client.get_options_chain('$SPXW.X', expiration)
max_strike_distance = 100
def contract_filter(contract):
if contract.right == CALL:
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)
option_chain = ibkr_client.get_option_chain(symbol, expiration, sub_symbol = sub_symbol, contract_filter = contract_filter)
logging.info(option_chain) logging.info(option_chain)
target_delta = 0.10 target_delta = 0.10
@ -117,8 +110,8 @@ def _enter_iron_condor():
return options.loc[options['Delta Distance'].idxmin()] return options.loc[options['Delta Distance'].idxmin()]
# Find the strikes that minimize the distance to the target delta. # Find the strikes that minimize the distance to the target delta.
short_put_contract = closest_contract_by_delta(-target_delta, option_chain, PUT) short_put_contract = closest_contract_by_delta(-target_delta, option_chain, 'PUT')
short_call_contract = closest_contract_by_delta(target_delta, option_chain, CALL) short_call_contract = closest_contract_by_delta(target_delta, option_chain, 'CALL')
# When selecting long strikes, minimize the distance to a 50 point spread. # When selecting long strikes, minimize the distance to a 50 point spread.
# TODO: Select long strike based on preferred price. # TODO: Select long strike based on preferred price.
@ -130,8 +123,8 @@ def _enter_iron_condor():
options['Strike Distance'] = abs(options['Strike'] - target_strike) options['Strike Distance'] = abs(options['Strike'] - target_strike)
return options.loc[options['Strike Distance'].idxmin()] return options.loc[options['Strike Distance'].idxmin()]
long_put_contract = closest_contract_by_strike(target_long_put_strike, option_chain, PUT) long_put_contract = closest_contract_by_strike(target_long_put_strike, option_chain, 'PUT')
long_call_contract = closest_contract_by_strike(target_long_call_strike, option_chain, CALL) long_call_contract = closest_contract_by_strike(target_long_call_strike, option_chain, 'CALL')
# Build the iron condor. # Build the iron condor.
short_put_strike = float(short_put_contract['Strike']) short_put_strike = float(short_put_contract['Strike'])