From da58e44ddaaffe2faede1ecc394bfcc981eb1817 Mon Sep 17 00:00:00 2001 From: moshferatu Date: Tue, 27 Feb 2024 14:43:14 -0800 Subject: [PATCH] Add some debug logs for tracking entry slippage based on the options chain since that data is coming from Tradestation while the order is submitted on IBKR --- iron_condor.py | 8 ++++++++ options_chain.py | 12 ++++++++++++ 2 files changed, 20 insertions(+) diff --git a/iron_condor.py b/iron_condor.py index fda38cb..ce3705e 100644 --- a/iron_condor.py +++ b/iron_condor.py @@ -119,6 +119,12 @@ def _enter_iron_condor(entry_time: datetime): logging.info(f'Short Call Strike: {short_call_strike}') logging.info(f'Long Call Strike: {long_call_strike}') + put_spread_price = options_chain.spread_mid_price(short_put_strike, long_put_strike, OptionType.PUT) + logging.info(f'Options Chain Put Spread Mid Price: {put_spread_price}') + + call_spread_price = options_chain.spread_mid_price(short_call_strike, long_call_strike, OptionType.CALL) + logging.info(f'Options Chain Call Spread Mid Price: {call_spread_price}') + ibkr_client = Client() trade = IronCondorTrade(symbol, credit_target, entry_time, float(getenv('STOP_MULTIPLE'))) @@ -129,6 +135,7 @@ def _enter_iron_condor(entry_time: datetime): logging.info(f'Call Spread Mid Price: {call_spread_order.mid_price}') logging.info(f'Call Spread Fill Price: {call_spread_order.fill_price}') logging.info(f'Call Spread Slippage: {call_spread_order.mid_price - call_spread_order.fill_price}') + logging.info(f'Options Chain Call Spread Slippage: {call_spread_price - call_spread_order.fill_price}') monitor_spread_price( trade = trade, @@ -145,6 +152,7 @@ def _enter_iron_condor(entry_time: datetime): logging.info(f'Put Spread Mid Price: {put_spread_order.mid_price}') logging.info(f'Put Spread Fill Price: {put_spread_order.fill_price}') logging.info(f'Put Spread Slippage: {put_spread_order.mid_price - put_spread_order.fill_price}') + logging.info(f'Options Chain Put Spread Slippage: {put_spread_price - put_spread_order.fill_price}') monitor_spread_price( trade = trade, diff --git a/options_chain.py b/options_chain.py index 8050713..ecd70e0 100644 --- a/options_chain.py +++ b/options_chain.py @@ -37,6 +37,18 @@ class OptionsChain: options['Strike Distance'] = abs(options['Strike'] - target_strike) return options.loc[options['Strike Distance'].idxmin()] + def spread_mid_price(self, near_strike: float, far_strike: float, option_type: OptionType): + options = self.options_by_type(option_type) + + near_leg = options[options['Strike'] == near_strike] + far_leg = options[options['Strike'] == far_strike] + + near_leg_mid = (near_leg['Ask'] + near_leg['Bid'].clip(lower = 0)) / 2.0 + far_leg_mid = (far_leg['Ask'] + far_leg['Bid'].clip(lower = 0)) / 2.0 + spread_mid = near_leg_mid.iloc[0] - far_leg_mid.iloc[0] + + return spread_mid + def __repr__(self) -> str: return self.options_chain.to_string()