2024-02-22 20:23:26 +00:00
|
|
|
from pandas import DataFrame
|
|
|
|
|
2024-02-27 18:52:07 +00:00
|
|
|
from database.trades import get_leg, get_spread, trade, upsert
|
2024-02-22 20:23:26 +00:00
|
|
|
from database.trades.action import Action
|
|
|
|
from database.trades.option_type import OptionType
|
|
|
|
from ibkr import option_type as IBKROptionType
|
|
|
|
from ibkr.option_order import OptionOrder
|
|
|
|
|
2024-02-27 18:52:07 +00:00
|
|
|
from iron_condor_trade import IronCondorTrade
|
|
|
|
|
2024-02-22 20:23:26 +00:00
|
|
|
def translate_action(action_string: str) -> Action:
|
|
|
|
for action in Action:
|
|
|
|
if action_string == action.value:
|
|
|
|
return action
|
|
|
|
|
|
|
|
def translate_option_type(option_type_string: str) -> OptionType:
|
|
|
|
if option_type_string == IBKROptionType.CALL:
|
|
|
|
return OptionType.CALL
|
|
|
|
else:
|
|
|
|
return OptionType.PUT
|
|
|
|
|
|
|
|
def spread(spread_order: OptionOrder) -> dict:
|
|
|
|
near_leg = spread_order.legs[0]
|
|
|
|
far_leg = spread_order.legs[1]
|
|
|
|
return get_spread(
|
|
|
|
near_leg = get_leg(
|
|
|
|
action = translate_action(near_leg.action),
|
|
|
|
strike = near_leg.strike,
|
|
|
|
option_type = translate_option_type(near_leg.option_type)
|
|
|
|
),
|
|
|
|
far_leg = get_leg(
|
|
|
|
action = translate_action(far_leg.action),
|
|
|
|
strike = far_leg.strike,
|
|
|
|
option_type = translate_option_type(far_leg.option_type)
|
|
|
|
),
|
|
|
|
open_price = spread_order.fill_price,
|
|
|
|
entry_slippage = round(spread_order.mid_price - spread_order.fill_price, 3)
|
|
|
|
)
|
|
|
|
|
2024-02-27 18:52:07 +00:00
|
|
|
def insert_trade(iron_condor: IronCondorTrade, call_spread_order: OptionOrder, put_spread_order: OptionOrder):
|
2024-02-22 20:23:26 +00:00
|
|
|
upsert(
|
|
|
|
DataFrame([{
|
2024-02-27 18:52:07 +00:00
|
|
|
'Date': iron_condor.entry_time.date(),
|
|
|
|
'Symbol': iron_condor.symbol,
|
|
|
|
'Strategy': iron_condor.strategy,
|
|
|
|
'Entry Time': iron_condor.entry_time,
|
2024-02-22 20:23:26 +00:00
|
|
|
'Exit Time': None, # Required.
|
|
|
|
'Spreads': [spread(call_spread_order), spread(put_spread_order)],
|
|
|
|
'Profit': None # Required.
|
|
|
|
}])
|
2024-02-27 18:52:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def update_on_stop_loss(iron_condor: IronCondorTrade, option_type: str, close_price: float, exit_slippage: float):
|
|
|
|
date = iron_condor.entry_time.date()
|
|
|
|
|
|
|
|
trade_record = trade(
|
|
|
|
date = date,
|
|
|
|
symbol = iron_condor.symbol,
|
|
|
|
strategy = iron_condor.strategy,
|
|
|
|
entry_time = iron_condor.entry_time
|
|
|
|
)
|
|
|
|
spreads = trade_record['Spreads'].iloc[0]
|
|
|
|
|
|
|
|
spread_index = 0 if option_type == 'C' else 1 # 'C' for call spread, 'P' for put spread.
|
|
|
|
|
|
|
|
spreads[spread_index]['Close'] = close_price
|
|
|
|
spreads[spread_index]['Exit Slippage'] = exit_slippage
|
|
|
|
|
|
|
|
upsert(
|
|
|
|
DataFrame([{
|
|
|
|
'Date': date,
|
|
|
|
'Symbol': iron_condor.symbol,
|
|
|
|
'Strategy': iron_condor.strategy,
|
|
|
|
'Entry Time': iron_condor.entry_time,
|
|
|
|
'Exit Time': None,
|
|
|
|
'Spreads': spreads,
|
|
|
|
'Profit': None # To be updated end of day.
|
|
|
|
}])
|
2024-02-22 20:23:26 +00:00
|
|
|
)
|