Filter trading days according to the current volatility regime

This commit is contained in:
moshferatu 2024-02-13 14:30:22 -08:00
parent 9b6405dc30
commit 786364a8af

View File

@ -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.')
print('Trade filters prevent trading today.')