Skip to content

Commit b42ead1

Browse files
authored
Merge pull request #14 from Polymarket/harley/python-batch-orders
feat: add batch order helpers
2 parents c81835d + a407ed7 commit b42ead1

7 files changed

Lines changed: 421 additions & 3 deletions

File tree

‎README.md‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,61 @@ Handle `None` for book `stats` and `transactTime`, and BBO `bestBid`, `bestAsk`,
298298
| `orders.cancel_all(params?)` | Cancel all open orders |
299299
| `orders.preview(params)` | Preview an order |
300300
| `orders.close_position(params)` | Close a position |
301+
| `orders.create_many(params)` | Create up to 20 orders |
302+
| `orders.cancel_many(params)` | Submit up to 20 cancellations |
303+
| `orders.modify_many(params)` | Submit up to 20 modifications |
304+
305+
#### Batch orders
306+
307+
Use `orders.create_many(params)`, `orders.cancel_many(params)`, and
308+
`orders.modify_many(params)` for up to 20 orders per request. Each accepts an
309+
`orders` list; cancel and modify entries include `orderId` and `marketSlug`.
310+
311+
```python
312+
created = client.orders.create_many(
313+
{
314+
"orders": [
315+
{
316+
"marketSlug": "btc-100k",
317+
"intent": "ORDER_INTENT_BUY_LONG",
318+
"type": "ORDER_TYPE_LIMIT",
319+
"price": {"value": "0.55", "currency": "USD"},
320+
"quantity": 10,
321+
},
322+
]
323+
}
324+
)
325+
# For existing open orders:
326+
modified = client.orders.modify_many(
327+
{
328+
"orders": [
329+
{
330+
"orderId": "existing-order-id",
331+
"marketSlug": "btc-100k",
332+
"price": {"value": "0.50", "currency": "USD"},
333+
"quantity": 10,
334+
},
335+
]
336+
}
337+
)
338+
canceled = client.orders.cancel_many(
339+
{
340+
"orders": [
341+
{"orderId": "existing-order-id", "marketSlug": "btc-100k"},
342+
]
343+
}
344+
)
345+
```
346+
347+
With `AsyncPolymarketUS`, await each call. Batches are sent as one request and
348+
are never automatically retried or split. Invalid or oversized batches are
349+
reported through the existing API exceptions.
350+
351+
`createdOrderIds` contains exchange-assigned IDs. `canceledOrderIds` and
352+
`modifiedOrderIds` echo the IDs submitted, and do **not** confirm each order
353+
was canceled or modified; unknown IDs can be ignored by the exchange. Follow
354+
`order_update` events for actual acceptance, fills, cancellations, replacements
355+
and rejections.
301356

302357
### RFQ trades (Authenticated)
303358

‎polymarket_us/resources/orders.py‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@
44
from polymarket_us.types import (
55
CancelAllOrdersParams,
66
CancelAllOrdersResponse,
7+
CancelOrderListParams,
8+
CancelOrderListResponse,
79
CancelOrderParams,
810
ClosePositionParams,
911
ClosePositionResponse,
12+
CreateOrderListParams,
13+
CreateOrderListResponse,
1014
CreateOrderParams,
1115
CreateOrderResponse,
1216
GetOpenOrdersParams,
1317
GetOpenOrdersResponse,
1418
GetOrderResponse,
19+
ModifyOrderListParams,
20+
ModifyOrderListResponse,
1521
ModifyOrderParams,
1622
PreviewOrderParams,
1723
PreviewOrderResponse,
@@ -29,6 +35,30 @@ def create(self, params: CreateOrderParams) -> CreateOrderResponse:
2935
authenticated=True,
3036
)
3137

38+
def create_many(self, params: CreateOrderListParams) -> CreateOrderListResponse:
39+
"""Create up to 20 orders in one request."""
40+
return self._client.post(
41+
"/v1/orders/batched",
42+
body=dict(params),
43+
authenticated=True,
44+
)
45+
46+
def cancel_many(self, params: CancelOrderListParams) -> CancelOrderListResponse:
47+
"""Submit up to 20 cancellations; returned IDs do not confirm success."""
48+
return self._client.post(
49+
"/v1/orders/batched/cancel",
50+
body=dict(params),
51+
authenticated=True,
52+
)
53+
54+
def modify_many(self, params: ModifyOrderListParams) -> ModifyOrderListResponse:
55+
"""Submit up to 20 modifications; returned IDs do not confirm success."""
56+
return self._client.post(
57+
"/v1/orders/batched/modify",
58+
body=dict(params),
59+
authenticated=True,
60+
)
61+
3262
def list(self, params: GetOpenOrdersParams | None = None) -> GetOpenOrdersResponse:
3363
"""List open orders."""
3464
return self._client.get(
@@ -93,6 +123,30 @@ async def create(self, params: CreateOrderParams) -> CreateOrderResponse:
93123
authenticated=True,
94124
)
95125

126+
async def create_many(self, params: CreateOrderListParams) -> CreateOrderListResponse:
127+
"""Create up to 20 orders in one request."""
128+
return await self._client.post(
129+
"/v1/orders/batched",
130+
body=dict(params),
131+
authenticated=True,
132+
)
133+
134+
async def cancel_many(self, params: CancelOrderListParams) -> CancelOrderListResponse:
135+
"""Submit up to 20 cancellations; returned IDs do not confirm success."""
136+
return await self._client.post(
137+
"/v1/orders/batched/cancel",
138+
body=dict(params),
139+
authenticated=True,
140+
)
141+
142+
async def modify_many(self, params: ModifyOrderListParams) -> ModifyOrderListResponse:
143+
"""Submit up to 20 modifications; returned IDs do not confirm success."""
144+
return await self._client.post(
145+
"/v1/orders/batched/modify",
146+
body=dict(params),
147+
authenticated=True,
148+
)
149+
96150
async def list(self, params: GetOpenOrdersParams | None = None) -> GetOpenOrdersResponse:
97151
"""List open orders."""
98152
return await self._client.get(

‎polymarket_us/types/__init__.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,14 @@
3939
from polymarket_us.types.orders import (
4040
CancelAllOrdersParams,
4141
CancelAllOrdersResponse,
42+
CancelOrderListItem,
43+
CancelOrderListParams,
44+
CancelOrderListResponse,
4245
CancelOrderParams,
4346
ClosePositionParams,
4447
ClosePositionResponse,
48+
CreateOrderListParams,
49+
CreateOrderListResponse,
4550
CreateOrderParams,
4651
CreateOrderResponse,
4752
Execution,
@@ -51,6 +56,9 @@
5156
GetOrderResponse,
5257
ManualOrderIndicator,
5358
MarketMetadata,
59+
ModifyOrderListItem,
60+
ModifyOrderListParams,
61+
ModifyOrderListResponse,
5462
ModifyOrderParams,
5563
Order,
5664
OrderIntent,
@@ -155,6 +163,14 @@
155163
"GetMarketBookResponse",
156164
"GetMarketBBOResponse",
157165
# Orders
166+
"CreateOrderListParams",
167+
"CreateOrderListResponse",
168+
"CancelOrderListItem",
169+
"CancelOrderListParams",
170+
"CancelOrderListResponse",
171+
"ModifyOrderListItem",
172+
"ModifyOrderListParams",
173+
"ModifyOrderListResponse",
158174
"OrderType",
159175
"OrderSide",
160176
"OutcomeSide",

‎polymarket_us/types/orders.py‎

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class CreateOrderParams(TypedDict, total=False):
119119
intent: OrderIntent # Required
120120
type: OrderType
121121
price: Amount
122-
quantity: int
122+
quantity: int | float
123123
tif: TimeInForce
124124
participateDontInitiate: bool
125125
goodTillTime: str
@@ -142,7 +142,7 @@ class ModifyOrderParams(TypedDict, total=False):
142142

143143
marketSlug: str # Required
144144
price: Amount
145-
quantity: int
145+
quantity: int | float
146146
tif: TimeInForce
147147
participateDontInitiate: bool
148148
goodTillTime: str
@@ -211,3 +211,51 @@ class ClosePositionResponse(TypedDict, total=False):
211211

212212
id: str
213213
executions: list[Execution]
214+
215+
216+
class CreateOrderListParams(TypedDict):
217+
"""A batch of up to 20 orders to create."""
218+
219+
orders: list[CreateOrderParams]
220+
221+
222+
class CreateOrderListResponse(TypedDict):
223+
"""Exchange-assigned IDs; follow the order stream for execution outcomes."""
224+
225+
createdOrderIds: list[str]
226+
227+
228+
class CancelOrderListItem(CancelOrderParams):
229+
"""An existing order to cancel in a batch."""
230+
231+
orderId: str
232+
233+
234+
class CancelOrderListParams(TypedDict):
235+
"""A batch of up to 20 orders to cancel."""
236+
237+
orders: list[CancelOrderListItem]
238+
239+
240+
class CancelOrderListResponse(TypedDict):
241+
"""IDs submitted for cancellation, not confirmation of per-order success."""
242+
243+
canceledOrderIds: list[str]
244+
245+
246+
class ModifyOrderListItem(ModifyOrderParams):
247+
"""An existing order to modify; marketSlug is required by the API."""
248+
249+
orderId: str
250+
251+
252+
class ModifyOrderListParams(TypedDict):
253+
"""A batch of up to 20 orders to modify."""
254+
255+
orders: list[ModifyOrderListItem]
256+
257+
258+
class ModifyOrderListResponse(TypedDict):
259+
"""IDs submitted for modification, not confirmation of per-order success."""
260+
261+
modifiedOrderIds: list[str]

‎pyproject.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "polymarket-us"
7-
version = "2.3.0"
7+
version = "2.4.0"
88
description = "Polymarket US Python SDK"
99
readme = "README.md"
1010
license = "MIT"

0 commit comments

Comments
 (0)