Prevent scheduling trades for entry time that have already elapsed

This commit is contained in:
moshferatu 2024-01-29 08:39:58 -08:00
parent 5766a49164
commit cd473ded8a

View File

@ -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:
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, 'America/New_York').do(enter_trade)
schedule.every().day.at(entry_time, eastern_timezone).do(enter_trade)
while True:
schedule.run_pending()