From 8a0355cc984fd48734fb49d572b75fceeb7feada Mon Sep 17 00:00:00 2001 From: moshferatu Date: Sun, 3 Mar 2024 05:35:42 -0800 Subject: [PATCH] Replace condition to check for successful connection to IBKR with assertion --- iron_condor_scheduler.py | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/iron_condor_scheduler.py b/iron_condor_scheduler.py index 426a01f..0ba3bfa 100644 --- a/iron_condor_scheduler.py +++ b/iron_condor_scheduler.py @@ -28,32 +28,28 @@ def connection_successful(): return False if __name__ == '__main__': - now = datetime.now(timezone(eastern_timezone)) + assert connection_successful(), 'Cannot connect to IBKR. Ensure that TWS or Gateway is running.' + now = datetime.now(timezone(eastern_timezone)) if all(filter.trade_allowed(now) for filter in trade_filters): print('Trade filters allow for trading today.') - if connection_successful(): - print('Connected to IBKR.') + entry_times = getenv('ENTRY_TIMES').split(',') + for entry_time in entry_times: + schedule_time = datetime.strptime(entry_time, '%H:%M:%S').replace( + year = now.year, + month = now.month, + day = now.day, + tzinfo = now.tzinfo + ) - entry_times = getenv('ENTRY_TIMES').split(',') - for entry_time in entry_times: - schedule_time = datetime.strptime(entry_time, '%H:%M:%S').replace( - year = now.year, - month = now.month, - day = now.day, - tzinfo = now.tzinfo - ) + # 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_time) - # 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_time) - - while True: - schedule.run_pending() - time.sleep(1) - else: - print('ERROR: Cannot connect to IBKR. Ensure that TWS / Gateway is running.') + while True: + schedule.run_pending() + time.sleep(1) else: print('Trade filters prevent trading today.') \ No newline at end of file