Consolidate inputs required to monitor the spread into an option order

This commit is contained in:
moshferatu 2024-02-28 11:19:10 -08:00
parent a9023f8594
commit 2ca34e4004

View File

@ -7,7 +7,7 @@ from datetime import datetime
from dotenv import load_dotenv
from os import getenv
from ibkr import Client, OptionLeg
from ibkr import Client, OptionLeg, OptionOrder
from ibkr.option_type import CALL, PUT
from ibkr.order_action import BUY, SELL
@ -22,13 +22,17 @@ load_dotenv()
# Necessary for monitoring spread prices asynchronously while interacting with the IBKR client.
nest_asyncio.apply()
def monitor_spread_price(trade: IronCondorTrade, short_leg: OptionLeg, long_leg: OptionLeg, stop_price: float, client: Client):
def monitor_spread_price(trade: IronCondorTrade, spread_order: OptionOrder, client: Client):
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Stop loss orders will not execute if trying to sell back a contract with no bid while paper trading.
Therefore, the spread price must be monitored and the spread manually exited if the stop price is reached.
If there is no bid for the long leg, only the short leg will be exited.
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
stop_price = spread_order.fill_price * trade.stop_multiple
stopped_out = False
short_leg = spread_order.legs[0]
long_leg = spread_order.legs[1]
market_data = {} # Stores real-time market data for each leg.
def on_market_data_update(update_event):
@ -137,13 +141,7 @@ def _enter_iron_condor(entry_time: datetime):
logging.info(f'Call Spread Slippage: {call_spread_order.mid_price - call_spread_order.fill_price}')
logging.info(f'Options Chain Call Spread Slippage: {call_spread_price - call_spread_order.fill_price}')
monitor_spread_price(
trade = trade,
short_leg = short_call_leg,
long_leg = long_call_leg,
stop_price = call_spread_order.fill_price * 2,
client = ibkr_client
)
monitor_spread_price(trade, call_spread_order, ibkr_client)
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)
@ -154,13 +152,7 @@ def _enter_iron_condor(entry_time: datetime):
logging.info(f'Put Spread Slippage: {put_spread_order.mid_price - put_spread_order.fill_price}')
logging.info(f'Options Chain Put Spread Slippage: {put_spread_price - put_spread_order.fill_price}')
monitor_spread_price(
trade = trade,
short_leg = short_put_leg,
long_leg = long_put_leg,
stop_price = put_spread_order.fill_price * 2,
client = ibkr_client
)
monitor_spread_price(trade, put_spread_order, ibkr_client)
insert_trade(trade, call_spread_order, put_spread_order)