Add utility for interacting with the trades table
This commit is contained in:
parent
ffc281fcc2
commit
c1dfb8f432
58
trades_table.py
Normal file
58
trades_table.py
Normal file
@ -0,0 +1,58 @@
|
||||
from datetime import datetime
|
||||
from pandas import DataFrame
|
||||
|
||||
from backtesting.credit_targeting import iron_condor_strategy
|
||||
from database.trades import get_leg, get_spread, upsert
|
||||
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
|
||||
|
||||
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)
|
||||
)
|
||||
|
||||
def insert_trade(
|
||||
symbol: str,
|
||||
target: float,
|
||||
entry_time: datetime,
|
||||
call_spread_order: OptionOrder,
|
||||
put_spread_order: OptionOrder
|
||||
):
|
||||
|
||||
upsert(
|
||||
DataFrame([{
|
||||
'Date': datetime.now().date(),
|
||||
'Symbol': symbol,
|
||||
'Strategy': iron_condor_strategy(target),
|
||||
'Entry Time': entry_time.replace(tzinfo = None),
|
||||
'Exit Time': None, # Required.
|
||||
'Spreads': [spread(call_spread_order), spread(put_spread_order)],
|
||||
'Profit': None # Required.
|
||||
}])
|
||||
)
|
Loading…
Reference in New Issue
Block a user