From 9ce9067e5d0d477604aa98fec2a133e4b146f481 Mon Sep 17 00:00:00 2001 From: moshferatu Date: Tue, 13 Feb 2024 10:50:50 -0800 Subject: [PATCH] Provide the entry time to the iron condor entry script in order to be explicit about the expected entry time of each trade --- iron_condor.py | 17 ++++++----------- iron_condor_scheduler.py | 2 +- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/iron_condor.py b/iron_condor.py index 799d705..9de8724 100644 --- a/iron_condor.py +++ b/iron_condor.py @@ -13,7 +13,6 @@ from ibkr.order_action import BUY, SELL from options_chain import OptionsChain from option_type import OptionType from os import getenv -from pytz import timezone load_dotenv() @@ -77,19 +76,19 @@ def monitor_spread_price(short_leg: OptionLeg, long_leg: OptionLeg, stop_price: market_data[leg] = leg_market_data leg_market_data.updateEvent += on_market_data_update -def enter_iron_condor(): +def enter_iron_condor(entry_time: datetime): logging.basicConfig( - filename=f'iron_condor_{datetime.now().strftime("%H%M")}.log', + filename=f'iron_condor_{entry_time.strftime("%H%M")}.log', level=logging.INFO, format='%(asctime)s : %(levelname)s : %(message)s', datefmt='%Y-%m-%d %H:%M:%S' ) try: - _enter_iron_condor() + _enter_iron_condor(entry_time) except Exception as e: logging.error("Error: %s", traceback.format_exc()) -def _enter_iron_condor(): +def _enter_iron_condor(entry_time: datetime): ibkr_host = getenv('IBKR_HOST') ibkr_port = getenv('IBKR_PORT') ibkr_client = Client(host = ibkr_host, port = ibkr_port) @@ -168,15 +167,11 @@ def _enter_iron_condor(): 'Open': fill_price } - # Convert timezone and replace seconds and microseconds to facilitate joins with backtest table. - now = datetime.now().astimezone(timezone('US/Eastern')) - now = now.replace(second = 0, microsecond = 0, tzinfo = None) - trade_records.append({ - 'Date': now.date(), + 'Date': datetime.now().date(), 'Symbol': symbol, 'Strategy': f'${credit_target:.2f} Iron Condor', - 'Entry Time': now, + 'Entry Time': entry_time.replace(tzinfo = None), 'Exit Time': None, 'Spreads': [call_spread_details, put_spread_details], 'Profit': None diff --git a/iron_condor_scheduler.py b/iron_condor_scheduler.py index 3f069dd..6ae4892 100644 --- a/iron_condor_scheduler.py +++ b/iron_condor_scheduler.py @@ -44,7 +44,7 @@ if __name__ == '__main__': # Prevent scheduling for times that have already elapsed. if schedule_time > now: print(f'Scheduling for {entry_time}.') - schedule.every().day.at(entry_time, eastern_timezone).do(enter_trade) + schedule.every().day.at(entry_time, eastern_timezone).do(enter_trade, schedule_time) while True: schedule.run_pending()