Insert records into trades table once orders have been filled

This commit is contained in:
moshferatu 2024-01-25 07:18:53 -08:00
parent 32744f80cc
commit 5766a49164
2 changed files with 37 additions and 1 deletions

View File

@ -1,7 +1,9 @@
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
@ -9,6 +11,7 @@ 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 os import getenv from os import getenv
from pytz import timezone
load_dotenv() load_dotenv()
@ -141,6 +144,10 @@ def _enter_iron_condor():
logging.info(f'Short Call Strike: {short_call_strike}') logging.info(f'Short Call Strike: {short_call_strike}')
logging.info(f'Long Call Strike: {long_call_strike}') logging.info(f'Long Call Strike: {long_call_strike}')
trade_records = []
call_spread_details = {}
put_spread_details = {}
short_call_leg = OptionLeg(symbol, expiration, short_call_strike, CALL, SELL, sub_symbol) short_call_leg = OptionLeg(symbol, expiration, short_call_strike, CALL, SELL, sub_symbol)
long_call_leg = OptionLeg(symbol, expiration, long_call_strike, CALL, BUY, sub_symbol) long_call_leg = OptionLeg(symbol, expiration, long_call_strike, CALL, BUY, sub_symbol)
@ -153,6 +160,14 @@ def _enter_iron_condor():
logging.info(f'Call Spread Fill Price: {fill_price}') logging.info(f'Call Spread Fill Price: {fill_price}')
monitor_spread_price(short_call_leg, long_call_leg, fill_price * 2, ibkr_client) monitor_spread_price(short_call_leg, long_call_leg, fill_price * 2, ibkr_client)
call_spread_details = {
'Legs': [
{'Action': 'SELL', 'Strike': short_call_strike, 'Type': 'CALL'},
{'Action': 'BUY', 'Strike': long_call_strike, 'Type': 'CALL'}
],
'Open': fill_price
}
short_put_leg = OptionLeg(symbol, expiration, short_put_strike, PUT, SELL, sub_symbol) short_put_leg = OptionLeg(symbol, expiration, short_put_strike, PUT, SELL, sub_symbol)
long_put_leg = OptionLeg(symbol, expiration, long_put_strike, PUT, BUY, sub_symbol) long_put_leg = OptionLeg(symbol, expiration, long_put_strike, PUT, BUY, sub_symbol)
@ -165,5 +180,25 @@ def _enter_iron_condor():
logging.info(f'Put Spread Fill Price: {fill_price}') logging.info(f'Put Spread Fill Price: {fill_price}')
monitor_spread_price(short_put_leg, long_put_leg, fill_price * 2, ibkr_client) monitor_spread_price(short_put_leg, long_put_leg, fill_price * 2, ibkr_client)
put_spread_details = {
'Legs': [
{'Action': 'SELL', 'Strike': short_put_strike, 'Type': 'PUT'},
{'Action': 'BUY', 'Strike': long_put_strike, 'Type': 'PUT'}
],
'Open': fill_price
}
now = datetime.now().astimezone(timezone('US/Eastern'))
trade_records.append({
'Date': now.date(),
'Symbol': symbol,
'Strategy': f'{int(target_delta * 100)} Delta Iron Condor @ {now.strftime("%H:%M:00")}',
'Entry Time': now,
'Exit Time': None,
'Spreads': [call_spread_details, put_spread_details],
'Profit': None
})
upsert(pd.DataFrame(trade_records))
# TODO: Add a shutdown hook. # TODO: Add a shutdown hook.
ibkr_client.run_event_loop() ibkr_client.run_event_loop()

View File

@ -1,3 +1,4 @@
ibkr ibkr
python-dotenv python-dotenv
pytz
tastytrade tastytrade