From cd473ded8a54517c09e08192e6fa3bed42de1777 Mon Sep 17 00:00:00 2001 From: moshferatu Date: Mon, 29 Jan 2024 08:39:58 -0800 Subject: [PATCH] Prevent scheduling trades for entry time that have already elapsed --- iron_condor_scheduler.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/iron_condor_scheduler.py b/iron_condor_scheduler.py index f8012d7..ed72a41 100644 --- a/iron_condor_scheduler.py +++ b/iron_condor_scheduler.py @@ -8,10 +8,12 @@ from ibkr import Client from iron_condor import enter_iron_condor from multiprocessing import Process from os import getenv +from pytz import timezone load_dotenv() entry_time_format = '%H:%M:%S' +eastern_timezone = 'America/New_York' def available_entry_times(): start = datetime.strptime('09:35:00', entry_time_format) @@ -57,11 +59,22 @@ if __name__ == '__main__': if connection_successful(): print('Connected to IBKR.') + now = datetime.now(timezone(eastern_timezone)) + entry_times = best_entry_times() print(entry_times) for entry_time in entry_times: - print(f'Scheduling for {entry_time}.') - schedule.every().day.at(entry_time, 'America/New_York').do(enter_trade) + 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) while True: schedule.run_pending()