From e614e91c4be2c414eb8565b0fb0db75c9f55f5b7 Mon Sep 17 00:00:00 2001 From: "mshaposhnikov@w3-edge.com" Date: Thu, 30 Jan 2025 17:35:43 +0100 Subject: [PATCH 1/2] add SNAP MID order type and usage example --- examples/place-order-snap-mid.py | 48 ++++++++++++++++++++++++++++++++ ib_async/__init__.py | 1 + ib_async/order.py | 7 +++++ 3 files changed, 56 insertions(+) create mode 100644 examples/place-order-snap-mid.py diff --git a/examples/place-order-snap-mid.py b/examples/place-order-snap-mid.py new file mode 100644 index 00000000..9f8c9ea5 --- /dev/null +++ b/examples/place-order-snap-mid.py @@ -0,0 +1,48 @@ +from ib_async import IB, Future, SnapMidOrder, TimeCondition +from datetime import datetime, timedelta, timezone + +ib = IB() +ib.connect( + host = "127.0.0.1", + port = 7497, + clientId = 1 +) + +# Define the ES futures contract (E-mini S&P 500 Futures) +# SNAP MID orders are mostly used to make better-than MKT fill +# for highly liquid instruments, but those which don't support +# more sophisticated IB order types. +# Common example is Futures/CFD +contract = Future( + symbol = "ES", + lastTradeDateOrContractMonth = "20250321", # use actual expiration date + exchange = "CME" +) + +# Define an execution condition for order - some time in future +# to make sure order is not executed immediately (for testing purposes) +future_date = datetime.now(timezone.utc) + timedelta(days = 10) +time_condition = TimeCondition( + time = future_date.strftime("%Y%m%d %H:%M:%S UTC") +) + +# Create order object +order = SnapMidOrder( + action = "BUY", + totalQuantity = 1, + conditions = [ + time_condition + ] +) + +# Place the order with the time condition +print("Placing order...") +trade = ib.placeOrder(contract, order) + +print("waiting a bit...") +ib.sleep(1) + +print("Order placed") +print(f"status: {trade.orderStatus.status}") + +ib.disconnect() diff --git a/ib_async/__init__.py b/ib_async/__init__.py index 8e9a9cb1..7b5b5889 100644 --- a/ib_async/__init__.py +++ b/ib_async/__init__.py @@ -95,6 +95,7 @@ OrderStatus, PercentChangeCondition, PriceCondition, + SnapMidOrder, StopLimitOrder, StopOrder, TimeCondition, diff --git a/ib_async/order.py b/ib_async/order.py index d8d31cb9..12a492ae 100644 --- a/ib_async/order.py +++ b/ib_async/order.py @@ -196,6 +196,13 @@ def __init__(self, action: str, totalQuantity: float, **kwargs): ) +class SnapMidOrder(Order): + def __init__(self, action: str, totalQuantity: float, **kwargs): + Order.__init__( + self, orderType="SNAP MID", action=action, totalQuantity=totalQuantity, **kwargs + ) + + class StopOrder(Order): def __init__(self, action: str, totalQuantity: float, stopPrice: float, **kwargs): Order.__init__( From c0cf156c3525f02308f55e4d362e46f71fcc4484 Mon Sep 17 00:00:00 2001 From: "mshaposhnikov@w3-edge.com" Date: Fri, 31 Jan 2025 11:07:47 +0100 Subject: [PATCH 2/2] add to __all__ needed too --- ib_async/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/ib_async/__init__.py b/ib_async/__init__.py index 7b5b5889..8acbd7ce 100644 --- a/ib_async/__init__.py +++ b/ib_async/__init__.py @@ -195,6 +195,7 @@ "OrderStatus", "PercentChangeCondition", "PriceCondition", + "SnapMidOrder", "StopLimitOrder", "StopOrder", "TimeCondition",