订单
创建订单
在订单簿中创建新订单。
端点: POST /openapi/v1/order
请求体:
| 字段 | 类型 | 描述 |
|---|---|---|
| outcomeId | string | 要交易的结果 |
| side | string | buy 或 sell |
| type | string | limit 或 market |
| price | number | 0 到 1 之间的价格(限价单) |
| quantity | number | 份额数量 |
认证
所有订单操作需要在 x-api-key 头中使用您的 API 密钥进行认证:
x-api-key: YOUR_API_KEY
批量创建订单
端点: POST /openapi/v1/order/batch
在单个请求中创建多个订单以提高性能。
取消订单
取消现有的挂单。
端点: DELETE /openapi/v1/order/{orderId}
取消所有订单
取消特定市场的所有挂单。
端点: DELETE /openapi/v1/order/cancel-all/{marketId}
批量取消订单
在单个请求中通过订单 ID 取消多个订单。
端点: POST /openapi/v1/order/cancel-batch
获取我的订单
检索您的订单,支持分页和筛选选项。
端点: GET /openapi/v1/order/my-orders
获取市场订单
检索特定市场中的所有订单,按状态筛选并分页。
端点: GET /openapi/v1/order/market/{marketId}
订单类型
限价单
- 以特定价格或更优价格执行
- 可能不会立即成交
- 为市场提供流动性
市价单
- 以最佳可用价格立即执行
- 保证成交(如果有流动性)
- 从市场获取流动性
订单状态
订单可以有以下状态:
| 状态 | 描述 |
|---|---|
| open | 订单活跃,等待成交 |
| partially_filled | 部分数量已成交 |
| filled | 订单完全执行 |
| cancelled | 用户取消订单 |
| expired | 订单根据 expiredAt 时间过期 |
代码示例
创建限价单
const API_KEY = 'YOUR_API_KEY';
const BASE_URL = 'https://engine.xmarket.app/openapi/v1';
async function createLimitOrder(outcomeId, side, price, quantity) {
const orderData = {
outcomeId: outcomeId,
side: side, // 'buy' 或 'sell'
type: 'limit',
price: price, // 0 到 1 之间
quantity: quantity
};
const response = await fetch(
`${BASE_URL}/order`,
{
method: 'POST',
headers: {
'x-api-key': API_KEY,
'Content-Type': 'application/json'
},
body: JSON.stringify(orderData)
}
);
if (!response.ok) {
throw new Error(`Order creation failed: ${response.status}`);
}
return await response.json();
}
// 示例:以 0.65 价格买入 100 份额
const order = await createLimitOrder('outcome-uuid', 'buy', 0.65, 100);
console.log(`Order created: ${order.id}`);
批量创建订单
async function batchCreateOrders(orders) {
const response = await fetch(
`${BASE_URL}/order/batch`,
{
method: 'POST',
headers: {
'x-api-key': API_KEY,
'Content-Type': 'application/json'
},
body: JSON.stringify({ orders })
}
);
if (!response.ok) {
throw new Error(`Batch order creation failed: ${response.status}`);
}
return await response.json();
}
// 一次创建多个订单
const orders = [
{ outcomeId: 'outcome-1', side: 'buy', type: 'limit', price: 0.60, quantity: 50 },
{ outcomeId: 'outcome-2', side: 'buy', type: 'limit', price: 0.45, quantity: 75 },
{ outcomeId: 'outcome-3', side: 'sell', type: 'limit', price: 0.80, quantity: 100 }
];
const result = await batchCreateOrders(orders);
console.log(`Created ${result.orders.length} orders`);
取消多个订单
async function batchCancelOrders(orderIds) {
const response = await fetch(
`${BASE_URL}/order/cancel-batch`,
{
method: 'POST',
headers: {
'x-api-key': API_KEY,
'Content-Type': 'application/json'
},
body: JSON.stringify({ orderIds })
}
);
if (!response.ok) {
throw new Error(`Batch cancel failed: ${response.status}`);
}
return await response.json();
}
// 一次取消多个订单
const orderIdsToCancel = ['order-id-1', 'order-id-2', 'order-id-3'];
await batchCancelOrders(orderIdsToCancel);
console.log('Orders cancelled successfully');
获取市场订单
async function getMarketOrders(marketId, status = 'open') {
const response = await fetch(
`${BASE_URL}/order/market/${marketId}?status=${status}&page=1&pageSize=100`,
{
headers: {
'x-api-key': API_KEY,
'Content-Type': 'application/json'
}
}
);
const data = await response.json();
return data.items;
}
// 获取特定市场的所有挂单
const marketOrders = await getMarketOrders('market-uuid', 'open');
console.log(`Found ${marketOrders.length} open orders in market`);
Python 示例 - 创建和监控订单
import requests
import time
API_KEY = 'YOUR_API_KEY'
BASE_URL = 'https://engine.xmarket.app/openapi/v1'
def create_order(outcome_id, side, price, quantity, order_type='limit'):
headers = {
'x-api-key': API_KEY,
'Content-Type': 'application/json'
}
order_data = {
'outcomeId': outcome_id,
'side': side,
'type': order_type,
'price': price,
'quantity': quantity
}
response = requests.post(
f'{BASE_URL}/order',
headers=headers,
json=order_data
)
response.raise_for_status()
return response.json()
def get_my_orders(status='all', page=1, page_size=50):
headers = {
'x-api-key': API_KEY,
'Content-Type': 'application/json'
}
params = {
'status': status,
'page': page,
'pageSize': page_size
}
response = requests.get(
f'{BASE_URL}/order/my-orders',
headers=headers,
params=params
)
response.raise_for_status()
return response.json()
# 创建订单
order = create_order('outcome-uuid', 'buy', 0.65, 100)
print(f"Order created: {order['id']}")
# 监控订单状态
time.sleep(2)
orders = get_my_orders(status='open')
print(f"Open orders: {orders['total']}")
最佳实践
- 订单验证:提交前始终验证订单参数
- 批量操作:创建/取消多个订单时使用批量端点
- 错误处理:为所有 API 调用实现健壮的错误处理
- 速率限制:遵守 API 速率限制以避免被限流
- 订单管理:跟踪您的挂单并取消过时的订单
- 价格检查:验证价格在有效范围内(0 < price < 1)
- 签名安全:永远不要暴露您的私钥;安全地签署订单
- 幂等性:考虑为订单创建实现幂等性以防止重复
性能优化
- 下多个订单时使用批量创建以减少 API 调用
- 使用批量取消高效关闭多个持仓
- 轮询 my-orders 端点而不是单独检查订单状态
- 缓存市场数据以在提交前验证订单