You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Bybit测试网REST API止盈订单设置失败求助

Bybit测试网REST API设置止盈订单失败问题

我尝试通过Bybit测试网REST API设置止盈订单,按照官方文档操作,但一直失败。试过调整position_idx为0、1、2,把tpsl_mode改成Full,移除stop_loss字段和所有非必填字段,都没用。最后一次请求返回错误:

{"retCode":10001,"retMsg":"empty value: apiTimestamp[] apiKey[] apiSignature[]","result":{},"retExtInfo":{},"time":1690745497593}

相关代码如下:

def set_take_profit_bybit(symbol,profit_price,quantity):
    BASE_URL = 'https://api-testnet.bybit.com'
    SET_ISO_ENDPOINT='/v5/position/trading-stop'
    endpoint = BASE_URL + SET_ISO_ENDPOINT

    timestamp = int(time.time() * 1000)
    stop_loss=0
    tp_trigger_by='MarkPrice'
    sl_trigger_by='IndexPrice'
    category='linear'
    tpsl_mode='Partial'
    tp_order_type='Limit'
    sl_order_type='Limit'
    tp_size=quantity
    sl_size=quantity
    tp_limit_price='0.49'
    sl_limit_price='0.21'
    position_idx=2

    payload = {
        "api_key": API_KEY,
        "category": category,
        "symbol": symbol,
        "takeProfit": profit_price,
        "stopLoss": stop_loss,
        "tpTriggerBy": tp_trigger_by,
        "slTriggerBy": sl_trigger_by,
        "tpslMode": tpsl_mode,
        "tpOrderType": tp_order_type,
        "slOrderType": sl_order_type,
        "tpSize": tp_size,
        "slSize": sl_size,
        "tpLimitPrice": tp_limit_price,
        "slLimitPrice": sl_limit_price,
        "positionIdx": position_idx,
        "timestamp": timestamp,
    }

    signature = create_signature(API_SECRET, payload)
    payload["sign"] = signature

    headers = {
        "Content-Type": "application/x-www-form-urlencoded",
    }

    response = requests.post(endpoint, json=payload, headers=headers)

问题分析与解决

错误提示empty value: apiTimestamp[] apiKey[] apiSignature[]说明服务器没收到核心验证字段,问题出在两个关键地方:

1. 请求格式不匹配

你设置了Content-Type: application/x-www-form-urlencoded,但用json=payload发送请求,导致数据以JSON格式传递,服务器无法解析到表单格式的验证字段。需要改成data=payload发送。

2. 字段命名不符合API要求

Bybit V5 API的验证字段命名是:

  • API key字段名是apiKey,不是api_key
  • 时间戳字段名是apiTimestamp,不是timestamp
    字段名错误会导致服务器识别不到验证信息。

修正后的代码

def set_take_profit_bybit(symbol,profit_price,quantity):
    BASE_URL = 'https://api-testnet.bybit.com'
    SET_ISO_ENDPOINT='/v5/position/trading-stop'
    endpoint = BASE_URL + SET_ISO_ENDPOINT

    api_timestamp = int(time.time() * 1000)
    tp_trigger_by='MarkPrice'
    category='linear'
    tpsl_mode='Partial'
    tp_order_type='Limit'
    tp_size=quantity
    tp_limit_price='0.49'
    position_idx=2

    payload = {
        "apiKey": API_KEY,
        "category": category,
        "symbol": symbol,
        "takeProfit": profit_price,
        "tpTriggerBy": tp_trigger_by,
        "tpslMode": tpsl_mode,
        "tpOrderType": tp_order_type,
        "tpSize": tp_size,
        "tpLimitPrice": tp_limit_price,
        "positionIdx": position_idx,
        "apiTimestamp": api_timestamp,
    }

    signature = create_signature(API_SECRET, payload)
    payload["sign"] = signature

    headers = {
        "Content-Type": "application/x-www-form-urlencoded",
    }

    response = requests.post(endpoint, data=payload, headers=headers)

额外注意事项

  • 如果不需要止损,直接移除所有止损相关字段,不要设stopLoss=0,避免触发不必要的参数校验
  • 确保create_signature函数严格按照Bybit V5签名规则实现:对所有请求参数按key升序排序后,用API密钥进行HMAC-SHA256加密

内容的提问来源于stack exchange,提问作者user1246950

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 09:02:03