下单体去掉 tradeSide:账户是单向持仓,带它会被 40774 全拒
实盘首批 4 个信号全部下单失败,8 次尝试(4 信号 × 2 腿)都是 [40774] The order type for unilateral position must also be the unilateral position type。投递链路其余部分完全正常:延后 0.0~0.2s、ssh 在线 255 分钟 零重连、去重 0、闸全过。纯粹是下单体语法。 官方文档:单向持仓下要忽略 tradeSide(side 取 buy/sell 即可),平仓用 side 取反 + reduceOnly=YES;而 reduceOnly 也只在单向模式下有效。双向持仓 才需要 tradeSide=open/close。我们发的是双向语法打到单向账户,是整体拒单 而不是部分降级。 三处下单体(entry_with_stop / tp_limit / close_market)都去掉 tradeSide。 reduceOnly 保留——它在单向模式下正是防止反手开出反向仓的那个参数。 另加 setup_position_mode():把模式钉成单向并读回核对,与既有的"每次启动 都设一遍逐仓和杠杆、不假设交易所侧状态"一致。调用点放在 reconcile 之后, 因为有持仓或挂单时交易所不允许切换。读回不是单向时推 Telegram 告警。 空跑不可能验到这个:dry=True 在发请求之前就返回假成功。这类"下单体字段 组合"的问题只有真单会暴露,和之前 oid 里 - 字符那条同一类。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+32
-5
@@ -155,6 +155,25 @@ class Bitget:
|
||||
"marginCoin": MARGIN_COIN,
|
||||
"marginMode": mode})
|
||||
|
||||
async def set_position_mode(self, mode: str = "one_way_mode") -> dict:
|
||||
"""单向 / 双向持仓。**按 productType 生效,不是按 symbol。**
|
||||
|
||||
必须显式设,因为它决定下单体的语法,两者不匹配会被整体拒单:
|
||||
|
||||
单向:side=buy/sell,**不带** tradeSide;平仓用 reduceOnly=YES
|
||||
双向:side + tradeSide=open/close;reduceOnly 在这个模式下无效
|
||||
|
||||
实盘上曾因为带着 tradeSide 打到单向账户,连续 8 次下单全被 40774 拒掉
|
||||
(4 个信号 × 2 条腿),而链路其余部分完全正常。
|
||||
|
||||
我们永不同时持有两个方向,所以单向是对的模式;且 reduceOnly 只在单向
|
||||
下可用,而出场腿依赖它防止反手开出反向仓。
|
||||
|
||||
交易所侧有持仓或挂单时切换会失败——所以调用点放在 reconcile 之后。
|
||||
"""
|
||||
return await self._req("POST", "/api/v2/mix/account/set-position-mode",
|
||||
body={"productType": PRODUCT, "posMode": mode})
|
||||
|
||||
async def entry_with_stop(self, symbol: str, side: str, size: str,
|
||||
stop_px: str, client_oid: str) -> dict:
|
||||
"""市价入场,**同时**把止损挂到服务端。
|
||||
@@ -162,10 +181,12 @@ class Bitget:
|
||||
`presetStopLossPrice` 触发后按市价执行,这正是成本模型要的(止损是
|
||||
taker)。`clientOid` 给交易所级幂等——重发同一个 oid 会被拒,比本地
|
||||
去重可靠,因为「已发出但没收到回复」这种情况本地判不了。
|
||||
|
||||
**不带 `tradeSide`**:账户是单向持仓,带了会被 40774 整体拒单。
|
||||
"""
|
||||
body = {"symbol": symbol, "productType": PRODUCT,
|
||||
"marginMode": "isolated", "marginCoin": MARGIN_COIN,
|
||||
"size": size, "side": side, "tradeSide": "open",
|
||||
"size": size, "side": side,
|
||||
"orderType": "market", "clientOid": client_oid,
|
||||
"presetStopLossPrice": stop_px}
|
||||
if self.dry:
|
||||
@@ -180,11 +201,14 @@ class Bitget:
|
||||
|
||||
`side` 传的是**平仓方向**(多头止盈是 sell)。`post_only` 保证是 maker:
|
||||
成本模型里止盈那 60% 按 maker 费率计且不吃滑点,用 taker 会破坏预算。
|
||||
`reduceOnly` 防止在单向模式下反手开出一个反向仓。
|
||||
|
||||
单向持仓下平仓的写法是 `side` 取反 + `reduceOnly=YES`,**不带**
|
||||
`tradeSide`。`reduceOnly` 也正好只在单向模式下有效,它防止反手开出
|
||||
一个反向仓。
|
||||
"""
|
||||
body = {"symbol": symbol, "productType": PRODUCT,
|
||||
"marginMode": "isolated", "marginCoin": MARGIN_COIN,
|
||||
"size": size, "side": side, "tradeSide": "close",
|
||||
"size": size, "side": side,
|
||||
"orderType": "limit", "price": px, "force": "post_only",
|
||||
"reduceOnly": "YES", "clientOid": client_oid}
|
||||
if self.dry:
|
||||
@@ -195,11 +219,14 @@ class Bitget:
|
||||
|
||||
async def close_market(self, symbol: str, hold_side: str,
|
||||
size: str, client_oid: str) -> dict:
|
||||
"""市价平(超时腿与对账用)。"""
|
||||
"""市价平(超时腿与对账用)。
|
||||
|
||||
同 tp_limit:单向持仓下是 `side` 取反 + `reduceOnly`,不带 `tradeSide`。
|
||||
"""
|
||||
side = "sell" if hold_side == "long" else "buy"
|
||||
body = {"symbol": symbol, "productType": PRODUCT,
|
||||
"marginMode": "isolated", "marginCoin": MARGIN_COIN,
|
||||
"size": size, "side": side, "tradeSide": "close",
|
||||
"size": size, "side": side,
|
||||
"orderType": "market", "reduceOnly": "YES",
|
||||
"clientOid": client_oid}
|
||||
if self.dry:
|
||||
|
||||
Reference in New Issue
Block a user