From 786364a8afe206d7cd6ec6e15703694c6bc89dbf Mon Sep 17 00:00:00 2001 From: moshferatu Date: Tue, 13 Feb 2024 14:30:22 -0800 Subject: [PATCH] Filter trading days according to the current volatility regime --- iron_condor_scheduler.py | 46 +++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/iron_condor_scheduler.py b/iron_condor_scheduler.py index 6ae4892..5ebd386 100644 --- a/iron_condor_scheduler.py +++ b/iron_condor_scheduler.py @@ -1,6 +1,7 @@ import schedule import time +from backtesting.filter import VolatilityRegimeFilter from datetime import datetime from dotenv import load_dotenv from ibkr import Client @@ -13,6 +14,8 @@ load_dotenv() eastern_timezone = 'America/New_York' +trade_filters = [VolatilityRegimeFilter()] + def enter_trade(): job_process = Process(target = enter_iron_condor) job_process.start() @@ -27,27 +30,32 @@ def connection_successful(): return False if __name__ == '__main__': - if connection_successful(): - print('Connected to IBKR.') + now = datetime.now(timezone(eastern_timezone)) - now = datetime.now(timezone(eastern_timezone)) + if all(filter.trade_allowed(now) for filter in trade_filters): + print('Trade filters allow for trading today.') - 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 - ) + if connection_successful(): + print('Connected to IBKR.') - # 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) + 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 + ) - while True: - schedule.run_pending() - time.sleep(1) + # 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.') else: - print('ERROR: Cannot connect to IBKR. Ensure that TWS / Gateway is running.') \ No newline at end of file + print('Trade filters prevent trading today.') \ No newline at end of file