Provide the entry time to the iron condor entry script in order to be explicit about the expected entry time of each trade

This commit is contained in:
moshferatu 2024-02-13 10:50:50 -08:00
parent b5168bd92d
commit 9ce9067e5d
2 changed files with 7 additions and 12 deletions

View File

@ -13,7 +13,6 @@ from ibkr.order_action import BUY, SELL
from options_chain import OptionsChain from options_chain import OptionsChain
from option_type import OptionType from option_type import OptionType
from os import getenv from os import getenv
from pytz import timezone
load_dotenv() load_dotenv()
@ -77,19 +76,19 @@ def monitor_spread_price(short_leg: OptionLeg, long_leg: OptionLeg, stop_price:
market_data[leg] = leg_market_data market_data[leg] = leg_market_data
leg_market_data.updateEvent += on_market_data_update leg_market_data.updateEvent += on_market_data_update
def enter_iron_condor(): def enter_iron_condor(entry_time: datetime):
logging.basicConfig( logging.basicConfig(
filename=f'iron_condor_{datetime.now().strftime("%H%M")}.log', filename=f'iron_condor_{entry_time.strftime("%H%M")}.log',
level=logging.INFO, level=logging.INFO,
format='%(asctime)s : %(levelname)s : %(message)s', format='%(asctime)s : %(levelname)s : %(message)s',
datefmt='%Y-%m-%d %H:%M:%S' datefmt='%Y-%m-%d %H:%M:%S'
) )
try: try:
_enter_iron_condor() _enter_iron_condor(entry_time)
except Exception as e: except Exception as e:
logging.error("Error: %s", traceback.format_exc()) logging.error("Error: %s", traceback.format_exc())
def _enter_iron_condor(): def _enter_iron_condor(entry_time: datetime):
ibkr_host = getenv('IBKR_HOST') ibkr_host = getenv('IBKR_HOST')
ibkr_port = getenv('IBKR_PORT') ibkr_port = getenv('IBKR_PORT')
ibkr_client = Client(host = ibkr_host, port = ibkr_port) ibkr_client = Client(host = ibkr_host, port = ibkr_port)
@ -168,15 +167,11 @@ def _enter_iron_condor():
'Open': fill_price 'Open': fill_price
} }
# Convert timezone and replace seconds and microseconds to facilitate joins with backtest table.
now = datetime.now().astimezone(timezone('US/Eastern'))
now = now.replace(second = 0, microsecond = 0, tzinfo = None)
trade_records.append({ trade_records.append({
'Date': now.date(), 'Date': datetime.now().date(),
'Symbol': symbol, 'Symbol': symbol,
'Strategy': f'${credit_target:.2f} Iron Condor', 'Strategy': f'${credit_target:.2f} Iron Condor',
'Entry Time': now, 'Entry Time': entry_time.replace(tzinfo = None),
'Exit Time': None, 'Exit Time': None,
'Spreads': [call_spread_details, put_spread_details], 'Spreads': [call_spread_details, put_spread_details],
'Profit': None 'Profit': None

View File

@ -44,7 +44,7 @@ if __name__ == '__main__':
# Prevent scheduling for times that have already elapsed. # Prevent scheduling for times that have already elapsed.
if schedule_time > now: if schedule_time > now:
print(f'Scheduling for {entry_time}.') print(f'Scheduling for {entry_time}.')
schedule.every().day.at(entry_time, eastern_timezone).do(enter_trade) schedule.every().day.at(entry_time, eastern_timezone).do(enter_trade, schedule_time)
while True: while True:
schedule.run_pending() schedule.run_pending()