From 2a79d0aec2932723fb314a5c392c05f64c2ea4ed Mon Sep 17 00:00:00 2001 From: moshferatu Date: Thu, 22 Feb 2024 12:23:59 -0800 Subject: [PATCH] Utilize the new trades table utility to insert records upon iron condor trade entry --- iron_condor.py | 35 +++++------------------------------ 1 file changed, 5 insertions(+), 30 deletions(-) diff --git a/iron_condor.py b/iron_condor.py index 9464e72..f6c9ad3 100644 --- a/iron_condor.py +++ b/iron_condor.py @@ -1,18 +1,19 @@ import logging import nest_asyncio -import pandas as pd import traceback -from database.trades import upsert from dataclasses import replace from datetime import datetime from dotenv import load_dotenv +from os import getenv + from ibkr import Client, OptionLeg from ibkr.option_type import CALL, PUT from ibkr.order_action import BUY, SELL + from options_chain import OptionsChain from option_type import OptionType -from os import getenv +from trades_table import insert_trade load_dotenv() @@ -159,33 +160,7 @@ def _enter_iron_condor(entry_time: datetime): client = ibkr_client ) - call_spread_details = { - 'Legs': [ - {'Action': 'SELL', 'Strike': short_call_strike, 'Type': 'CALL'}, - {'Action': 'BUY', 'Strike': long_call_strike, 'Type': 'CALL'} - ], - 'Open': call_spread_fill, - 'Entry Slippage': round(call_spread_mid - call_spread_fill, 3) - } - - put_spread_details = { - 'Legs': [ - {'Action': 'SELL', 'Strike': short_put_strike, 'Type': 'PUT'}, - {'Action': 'BUY', 'Strike': long_put_strike, 'Type': 'PUT'} - ], - 'Open': put_spread_fill, - 'Entry Slippage': round(put_spread_mid - put_spread_fill, 3) - } - - upsert(pd.DataFrame([{ - 'Date': datetime.now().date(), - 'Symbol': symbol, - 'Strategy': f'${credit_target:.2f} Iron Condor', - 'Entry Time': entry_time.replace(tzinfo = None), - 'Exit Time': None, - 'Spreads': [call_spread_details, put_spread_details], - 'Profit': None - }])) + insert_trade(symbol, credit_target, entry_time, call_spread_order, put_spread_order) # TODO: Add a shutdown hook. ibkr_client.run_event_loop() \ No newline at end of file