如何通过MEXC API Key下达市价单?参数疑问与卡单排查
关于MEXC API的订单问题与代码排查
核心问题解答
1. 如何通过MEXC API Key下达市价单?
- 调用
/api/v1/private/order/submit接口时,将type参数设为MARKET - 市价单无需传入
price参数(部分场景传0也可,以MEXC官方文档为准) - 确保
openType匹配你的杠杆模式(如CROSS或ISOLATED) - 严格按照要求生成签名,同时在请求头传入有效的
ApiKey和服务器时间
2. 仅传takeProfitPrice时,是否需要同时传stopLossPrice?
不需要。止盈和止损是独立的可选参数,支持单独设置其中一个,无需强制成对传入。
代码卡住问题排查
你的代码卡住大概率和漏传止损价格无关,建议从以下方向排查:
1. 参数问题
- 拼写错误:函数参数里的
reduceOnlt是笔误,MEXC API的正确参数名是reduceOnly,错误参数名会导致接口无法识别,可能引发异常 - 参数遗漏:代码未处理
stopLossPrice参数的传入(虽然它是可选的,但如果业务需要却没处理会不符合预期) - 市价单冗余参数:市价单不需要
price参数,你当前代码无论订单类型都传入了该参数,可能触发接口校验问题
2. 签名与认证问题
- 检查
_sign_v1函数实现:MEXC V1签名要求对JSON格式的请求体做HMAC-SHA256加密,确保签名逻辑完全符合官方规范 - 确认
_get_server_time获取的是MEXC服务器的当前时间,本地时间与服务器时间差超过5分钟会导致签名验证失败
3. 请求基础配置问题
- 检查
BASE_URL是否正确:MEXC现货和合约API地址不同(现货:https://api.mexc.com,合约:https://contract.mexc.com),地址错误会直接导致连接失败 - 网络环境:如果服务器无法访问MEXC API域名,会触发连接超时或卡住,可尝试ping对应域名验证连通性
修正后的关键代码片段
def post_place(symbol, price, vol, side, type, openType, leverage=None, positionId=None, externalOid=None, stopLossPrice=None, takeProfitPrice=None, positionMode=None, reduceOnly=None): # 修正reduceOnlt拼写错误 """Place new order""" method = 'POST' path = '/api/v1/private/order/submit' # 确认BASE_URL对应交易品类(现货/合约) url = '{}{}'.format(BASE_URL, path) print(f"Preparing to place trade with the following details:\n" f"Symbol: {symbol}, Price: {price}, Volume: {vol}, Side: {side}, Type: {type}, OpenType: {openType}, " f"Leverage: {leverage}, PositionId: {positionId}, ExternalOid: {externalOid}, " f"TakeProfitPrice: {takeProfitPrice}, StopLossPrice: {stopLossPrice}, PositionMode: {positionMode}, ReduceOnly: {reduceOnly}") data_original = { 'symbol': symbol, 'vol': vol, 'side': side, 'type': type, 'openType': openType } # 市价单跳过price参数 if type != 'MARKET': data_original['price'] = price # 补充stopLossPrice参数处理 if leverage: data_original.update({"leverage": leverage}) if positionId: data_original.update({"positionId": positionId}) if externalOid: data_original.update({"externalOid": externalOid}) if takeProfitPrice: data_original.update({"takeProfitPrice": takeProfitPrice}) if stopLossPrice: data_original.update({"stopLossPrice": stopLossPrice}) if positionMode: data_original.update({"positionMode": positionMode}) if reduceOnly: data_original.update({"reduceOnly": reduceOnly}) data = json.dumps(data_original) print(f"Data to be sent: {data}") params = _sign_v1(sign_params=data) headers = { "ApiKey": API_KEY, "Request-Time": str(_get_server_time()), "Signature": params, "Content-Type": "application/json" } print(f"Headers: {headers}") try: response = requests.request(method, url, data=data, headers=headers, timeout=10) print(f"Response received: Status Code: {response.status_code}, Response Text: {response.text}") response_json = response.json() print(f"Response JSON: {response_json}") return response_json except requests.exceptions.ConnectionError as e: print(f"Connection error occurred: {str(e)}") return {"error": "connection_error"} except requests.exceptions.Timeout: print("Error: The request timed out.") return {"error": "timeout"} except requests.exceptions.RequestException as e: print(f"Error occurred during the API request: {str(e)}") return {"error": str(e)}
额外建议
- 先用Postman等工具直接调用API验证参数和签名正确性,再迁移到代码
- 对照MEXC API错误码文档,根据接口返回的错误信息精准定位问题
- 确保你的API Key已开启对应交易权限(如合约交易权限)
内容的提问来源于stack exchange,提问作者Jamjam
相关产品推荐
相关产品推荐

