diff --git a/examples/place-order-snap-mid.py b/examples/place-order-snap-mid.py new file mode 100644 index 0000000..9f8c9ea --- /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 8e21627..76fe64a 100644 --- a/ib_async/__init__.py +++ b/ib_async/__init__.py @@ -97,6 +97,7 @@ OrderStatus, PercentChangeCondition, PriceCondition, + SnapMidOrder, StopLimitOrder, StopOrder, TimeCondition, @@ -198,6 +199,7 @@ "OrderStatus", "PercentChangeCondition", "PriceCondition", + "SnapMidOrder", "StopLimitOrder", "StopOrder", "TimeCondition", diff --git a/ib_async/order.py b/ib_async/order.py index 33b866a..1b28a76 100644 --- a/ib_async/order.py +++ b/ib_async/order.py @@ -203,6 +203,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__(