From 2e26d408849070c324c2db84d16c8891c83540fa Mon Sep 17 00:00:00 2001 From: moshferatu Date: Tue, 28 Nov 2023 13:20:24 -0800 Subject: [PATCH] Add stop loss order submission logic to client --- ibkr/client.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/ibkr/client.py b/ibkr/client.py index c2f07d7..45a8034 100644 --- a/ibkr/client.py +++ b/ibkr/client.py @@ -1,7 +1,7 @@ import pandas as pd 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 typing import Callable, List @@ -9,6 +9,7 @@ from .currency import USD from .exchange import SMART from .market_data_type import LIVE from .option_leg import OptionLeg +from .order_action import BUY, SELL class Client: @@ -77,7 +78,7 @@ class Client: self.ib.qualifyContracts(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 = [] for leg in legs: option_contract = self.get_option_contract(leg) @@ -96,9 +97,18 @@ class Client: combo_contract.comboLegs = combo_legs 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.totalQuantity = quantity combo_order.transmit = True - self.ib.placeOrder(combo_contract, combo_order) \ No newline at end of file + 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) \ No newline at end of file