diff --git a/iron_condor.py b/iron_condor.py index ce3705e..88d6b2e 100644 --- a/iron_condor.py +++ b/iron_condor.py @@ -14,7 +14,7 @@ from ibkr.order_action import BUY, SELL from iron_condor_trade import IronCondorTrade from options_chain import OptionsChain from option_type import OptionType -from trades_table import insert_trade, update_on_stop_loss +from trades_table import insert_trade, update_trade load_dotenv() @@ -64,7 +64,7 @@ def monitor_spread_price(trade: IronCondorTrade, short_leg: OptionLeg, long_leg: exit_slippage = round(exit_order.fill_price - stop_price, 3) logging.info(f'Exit Slippage: {exit_slippage}') - update_on_stop_loss(trade, short_leg.option_type, exit_order.fill_price, exit_slippage) + update_trade(trade, exit_order) # Unsubscribe from market data updates once the trade has exited. for leg in [short_leg, long_leg]: diff --git a/trades_table.py b/trades_table.py index 86b8648..e54e52f 100644 --- a/trades_table.py +++ b/trades_table.py @@ -50,7 +50,7 @@ def insert_trade(iron_condor: IronCondorTrade, call_spread_order: OptionOrder, p }]) ) -def update_on_stop_loss(iron_condor: IronCondorTrade, option_type: str, close_price: float, exit_slippage: float): +def update_trade(iron_condor: IronCondorTrade, stop_order: OptionOrder): date = iron_condor.entry_time.date() trade_record = trade( @@ -61,10 +61,13 @@ def update_on_stop_loss(iron_condor: IronCondorTrade, option_type: str, close_pr ) spreads = trade_record['Spreads'].iloc[0] - spread_index = 0 if option_type == 'C' else 1 # 'C' for call spread, 'P' for put spread. + option_type = translate_option_type(stop_order.legs[0].option_type) + spread_index = 0 if (option_type == OptionType.CALL) else 1 - spreads[spread_index]['Close'] = close_price - spreads[spread_index]['Exit Slippage'] = exit_slippage + spreads[spread_index]['Close'] = stop_order.fill_price + + stop_price = spreads[spread_index]['Open'] * iron_condor.stop_multiple + spreads[spread_index]['Exit Slippage'] = round(stop_order.fill_price - stop_price, 3) upsert( DataFrame([{