Filter trading days according to the current volatility regime
This commit is contained in:
parent
9b6405dc30
commit
786364a8af
@ -1,6 +1,7 @@
|
|||||||
import schedule
|
import schedule
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
from backtesting.filter import VolatilityRegimeFilter
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
from ibkr import Client
|
from ibkr import Client
|
||||||
@ -13,6 +14,8 @@ load_dotenv()
|
|||||||
|
|
||||||
eastern_timezone = 'America/New_York'
|
eastern_timezone = 'America/New_York'
|
||||||
|
|
||||||
|
trade_filters = [VolatilityRegimeFilter()]
|
||||||
|
|
||||||
def enter_trade():
|
def enter_trade():
|
||||||
job_process = Process(target = enter_iron_condor)
|
job_process = Process(target = enter_iron_condor)
|
||||||
job_process.start()
|
job_process.start()
|
||||||
@ -27,27 +30,32 @@ def connection_successful():
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
if connection_successful():
|
now = datetime.now(timezone(eastern_timezone))
|
||||||
print('Connected to IBKR.')
|
|
||||||
|
|
||||||
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(',')
|
if connection_successful():
|
||||||
for entry_time in entry_times:
|
print('Connected to IBKR.')
|
||||||
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.
|
entry_times = getenv('ENTRY_TIMES').split(',')
|
||||||
if schedule_time > now:
|
for entry_time in entry_times:
|
||||||
print(f'Scheduling for {entry_time}.')
|
schedule_time = datetime.strptime(entry_time, '%H:%M:%S').replace(
|
||||||
schedule.every().day.at(entry_time, eastern_timezone).do(enter_trade, schedule_time)
|
year = now.year,
|
||||||
|
month = now.month,
|
||||||
|
day = now.day,
|
||||||
|
tzinfo = now.tzinfo
|
||||||
|
)
|
||||||
|
|
||||||
while True:
|
# Prevent scheduling for times that have already elapsed.
|
||||||
schedule.run_pending()
|
if schedule_time > now:
|
||||||
time.sleep(1)
|
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:
|
else:
|
||||||
print('ERROR: Cannot connect to IBKR. Ensure that TWS / Gateway is running.')
|
print('Trade filters prevent trading today.')
|
Loading…
Reference in New Issue
Block a user