Files
Hermes-Skills/okx-auto-position/references/okx-trigger-orders.md
T

3.2 KiB

OKX 条件单API详解

Trigger订单(做T用)

基本用法

okx_post('/api/v5/trade/order-algo', {
    "instId": "ETH-USDT-SWAP",
    "tdMode": "cross",
    "side": "buy",           # buy=买入, sell=卖出
    "ordType": "trigger",    # 用trigger不是conditional
    "sz": "4",               # 数量
    "triggerPx": "1770",     # 触发价
    "triggerPxType": "last", # last=最新价, index=指数, mark=标记
    "orderPx": "-1"          # -1=市价单, 或指定限价
})

关键Pitfalls

  1. 参数名是orderPx不是ordPx

    • "ordPx": "-1" → 报错50014: Parameter orderPx can not be empty
    • "orderPx": "-1"
  2. reduceOnly不支持trigger订单

    • "reduceOnly": "true" → 报错51205: Reduce Only is not available
    • 不传reduceOnly,直接sell即可
  3. conditional订单的限制

    • conditional的SL触发价不能低于当前价(用于止损)
    • conditional的TP触发价不能高于当前价(用于止盈)
    • 做T低吸(价格下跌触发买入)必须用trigger类型
  4. Trigger vs Conditional vs OCO

    类型 用途 触发方向
    trigger 价格到任意方向触发 任意
    conditional 止损/止盈 SL不能低于现价, TP不能高于现价
    oco 同时设TP+SL 双腿
  5. 触发后自动市价成交

    • 不是纯提醒,会自动下单
    • 如果只想提醒不想下单,需要自己写监控脚本

查询pending条件单

# 查trigger类型的pending订单
resp = okx_get('/api/v5/trade/orders-algo-pending', 'ordType=trigger')
for order in resp.get('data', []):
    print(f"algoId={order['algoId']} triggerPx={order['triggerPx']} side={order['side']} sz={order['sz']}")

取消条件单

okx_post('/api/v5/trade/cancel-algos', [{
    "algoId": "3718137391157260288",
    "instId": "ETH-USDT-SWAP"
}])

实战案例

ETH做T条件单设置

# 低吸1: 价格跌到$1770时买入4张
okx_post('/api/v5/trade/order-algo', {
    "instId": "ETH-USDT-SWAP", "tdMode": "cross",
    "side": "buy", "ordType": "trigger",
    "sz": "4", "triggerPx": "1770", "triggerPxType": "last",
    "orderPx": "-1"
})
# 返回: {"code":"0","data":[{"algoId":"3718137391157260288"}]}

# 低吸2: 价格跌到$1764时买入4张
okx_post('/api/v5/trade/order-algo', {
    "instId": "ETH-USDT-SWAP", "tdMode": "cross",
    "side": "buy", "ordType": "trigger",
    "sz": "4", "triggerPx": "1764", "triggerPxType": "last",
    "orderPx": "-1"
})
# 返回: {"code":"0","data":[{"algoId":"3718137438267682816"}]}

# 高抛: 价格涨到$1787时卖出4张
okx_post('/api/v5/trade/order-algo', {
    "instId": "ETH-USDT-SWAP", "tdMode": "cross",
    "side": "sell", "ordType": "trigger",
    "sz": "4", "triggerPx": "1787", "triggerPxType": "last",
    "orderPx": "-1"
})
# 返回: {"code":"0","data":[{"algoId":"3718137842229489664"}]}

错误排查

错误码 含义 解决
50014 orderPx为空 orderPx不是ordPx
51205 reduceOnly不支持 去掉reduceOnly参数
51278 SL触发价低于现价 用trigger类型代替conditional
51280 SL触发价必须低于现价 用trigger类型代替conditional