Utilize the new trades table utility to insert records upon iron condor trade entry

This commit is contained in:
moshferatu 2024-02-22 12:23:59 -08:00
parent c1dfb8f432
commit 2a79d0aec2

View File

@ -1,18 +1,19 @@
import logging import logging
import nest_asyncio import nest_asyncio
import pandas as pd
import traceback import traceback
from database.trades import upsert
from dataclasses import replace from dataclasses import replace
from datetime import datetime from datetime import datetime
from dotenv import load_dotenv from dotenv import load_dotenv
from os import getenv
from ibkr import Client, OptionLeg from ibkr import Client, OptionLeg
from ibkr.option_type import CALL, PUT from ibkr.option_type import CALL, PUT
from ibkr.order_action import BUY, SELL 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 trades_table import insert_trade
load_dotenv() load_dotenv()
@ -159,33 +160,7 @@ def _enter_iron_condor(entry_time: datetime):
client = ibkr_client client = ibkr_client
) )
call_spread_details = { insert_trade(symbol, credit_target, entry_time, call_spread_order, put_spread_order)
'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
}]))
# TODO: Add a shutdown hook. # TODO: Add a shutdown hook.
ibkr_client.run_event_loop() ibkr_client.run_event_loop()