Add stop loss order submission logic to client

This commit is contained in:
moshferatu 2023-11-28 13:20:24 -08:00
parent fe99f740a2
commit 2e26d40884

View File

@ -1,7 +1,7 @@
import pandas as pd import pandas as pd
from datetime import datetime from datetime import datetime
from ib_insync import ComboLeg, Contract, IB, Index, Option, Order from ib_insync import ComboLeg, Contract, IB, Index, Option, Order, Trade
from ib_insync.util import isNan from ib_insync.util import isNan
from typing import Callable, List from typing import Callable, List
@ -9,6 +9,7 @@ from .currency import USD
from .exchange import SMART from .exchange import SMART
from .market_data_type import LIVE from .market_data_type import LIVE
from .option_leg import OptionLeg from .option_leg import OptionLeg
from .order_action import BUY, SELL
class Client: class Client:
@ -77,7 +78,7 @@ class Client:
self.ib.qualifyContracts(option_contract) self.ib.qualifyContracts(option_contract)
return option_contract return option_contract
def submit_combo_option_order(self, legs: List[OptionLeg], quantity: int) -> None: def submit_combo_option_order(self, legs: List[OptionLeg], quantity: int) -> Trade:
combo_legs = [] combo_legs = []
for leg in legs: for leg in legs:
option_contract = self.get_option_contract(leg) option_contract = self.get_option_contract(leg)
@ -96,9 +97,18 @@ class Client:
combo_contract.comboLegs = combo_legs combo_contract.comboLegs = combo_legs
combo_order = Order() combo_order = Order()
combo_order.action = 'BUY' # TODO: Document the logic behind this. combo_order.action = BUY # TODO: Document the logic behind this.
combo_order.orderType = 'MKT' # TODO: Limit order support. combo_order.orderType = 'MKT' # TODO: Limit order support.
combo_order.totalQuantity = quantity combo_order.totalQuantity = quantity
combo_order.transmit = True combo_order.transmit = True
self.ib.placeOrder(combo_contract, combo_order) return self.ib.placeOrder(combo_contract, combo_order)
def submit_stop_loss_order(self, trade: Trade, stop_loss_price):
stop_loss_order = Order()
stop_loss_order.action = SELL if trade.order.action == BUY else BUY
stop_loss_order.orderType = 'STP'
stop_loss_order.auxPrice = stop_loss_price
stop_loss_order.totalQuantity = trade.order.totalQuantity
stop_loss_order.transmit = True
return self.ib.placeOrder(trade.contract, stop_loss_order)