From 1cc6d303f5a6683e65c18a9479992f18177a9866 Mon Sep 17 00:00:00 2001 From: moshferatu Date: Mon, 29 Jan 2024 08:41:17 -0800 Subject: [PATCH] Retrieve options chain from TradeStation as opposed to IBKR --- iron_condor.py | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/iron_condor.py b/iron_condor.py index 89f82e3..b34f518 100644 --- a/iron_condor.py +++ b/iron_condor.py @@ -12,6 +12,7 @@ from ibkr.option_type import CALL, PUT from ibkr.order_action import BUY, SELL from os import getenv from pytz import timezone +from tradestation import TradeStationClient load_dotenv() @@ -95,18 +96,10 @@ def _enter_iron_condor(): # The weekly symbol for SPX (SPXW) is required in order to distinguish from monthly options. symbol, sub_symbol = 'SPX', 'SPXW' expiration = datetime.now() + + tradestation_client = TradeStationClient(getenv('TRADESTATION_REFRESH_TOKEN')) - underlying_ticker = ibkr_client.get_ticker(symbol, '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 == 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) + option_chain = tradestation_client.get_options_chain('$SPXW.X', expiration) logging.info(option_chain) target_delta = 0.10 @@ -117,8 +110,8 @@ def _enter_iron_condor(): return options.loc[options['Delta Distance'].idxmin()] # Find the strikes that minimize the distance to the target delta. - 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_put_contract = closest_contract_by_delta(-target_delta, option_chain, 'PUT') + short_call_contract = closest_contract_by_delta(target_delta, option_chain, 'CALL') # When selecting long strikes, minimize the distance to a 50 point spread. # TODO: Select long strike based on preferred price. @@ -130,8 +123,8 @@ def _enter_iron_condor(): options['Strike Distance'] = abs(options['Strike'] - target_strike) return options.loc[options['Strike Distance'].idxmin()] - 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_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') # Build the iron condor. short_put_strike = float(short_put_contract['Strike'])