Skip to content

add SNAP MID order type and usage example #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions examples/place-order-snap-mid.py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 2 additions & 0 deletions ib_async/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
OrderStatus,
PercentChangeCondition,
PriceCondition,
SnapMidOrder,
StopLimitOrder,
StopOrder,
TimeCondition,
Expand Down Expand Up @@ -194,6 +195,7 @@
"OrderStatus",
"PercentChangeCondition",
"PriceCondition",
"SnapMidOrder",
"StopLimitOrder",
"StopOrder",
"TimeCondition",
Expand Down
7 changes: 7 additions & 0 deletions ib_async/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__(
Expand Down