Contract
exchange.SetDirection()
设置开平仓方向(仅限合约交易)。
语法
python
exchange.SetDirection(direction)参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| direction | str | 是 | 开平仓方向 |
支持的方向
| 方向值 | 别名 | 说明 | 配合的交易方法 |
|---|---|---|---|
"buy" | "long" | 开多仓 | Buy() |
"sell" | "short" | 开空仓 | Sell() |
"closebuy" | "close_long" | 平多仓 | Sell() |
"closesell" | "close_short" | 平空仓 | Buy() |
说明:
- 每个方向都支持两种写法(原值和别名),例如
"buy"和"long"完全等价 - 双向持仓模式:必须使用
SetDirection设置方向,平仓时会设置reduce_only=true确保只减仓 - 单向持仓模式:可选使用
SetDirection,系统会根据 size 正负自动判断开平仓方向
返回值
无返回值
示例
开多仓
python
# 设置方向为开多
exchange.SetDirection("buy")
# 买入开多
order_id = exchange.Buy(50000, 1)
Log("开多仓:", order_id)开空仓
python
# 设置方向为开空
exchange.SetDirection("sell")
# 卖出开空
order_id = exchange.Sell(50000, 1)
Log("开空仓:", order_id)平多仓
python
# 设置方向为平多
exchange.SetDirection("closebuy")
# 卖出平多
order_id = exchange.Sell(51000, 1)
Log("平多仓:", order_id)平空仓
python
# 设置方向为平空
exchange.SetDirection("closesell")
# 买入平空
order_id = exchange.Buy(49000, 1)
Log("平空仓:", order_id)完整的开平仓流程
python
def open_long(price, amount):
"""开多仓"""
exchange.SetDirection("buy")
order_id = exchange.Buy(price, amount)
Log(f"开多仓: {amount} @ {price}")
return order_id
def close_long(price, amount):
"""平多仓"""
exchange.SetDirection("closebuy")
order_id = exchange.Sell(price, amount)
Log(f"平多仓: {amount} @ {price}")
return order_id
def open_short(price, amount):
"""开空仓"""
exchange.SetDirection("sell")
order_id = exchange.Sell(price, amount)
Log(f"开空仓: {amount} @ {price}")
return order_id
def close_short(price, amount):
"""平空仓"""
exchange.SetDirection("closesell")
order_id = exchange.Buy(price, amount)
Log(f"平空仓: {amount} @ {price}")
return order_id
# 使用示例
open_long(50000, 1) # 开多
Sleep(10000)
close_long(51000, 1) # 平多双向持仓示例
python
# 同时持有多空仓位(需要交易所支持双向持仓)
# 开多仓
exchange.SetDirection("buy")
exchange.Buy(50000, 1)
# 开空仓
exchange.SetDirection("sell")
exchange.Sell(50000, 1)
# 查看持仓
positions = exchange.GetPosition()
for pos in positions:
Log(f"{pos['Type']} 持仓: {pos['Amount']}")市价平仓
python
def market_close():
"""市价平掉所有持仓"""
positions = exchange.GetPosition()
for pos in positions:
if pos['Type'] == 'long':
# 平多仓
exchange.SetDirection("closebuy")
exchange.Sell(-1, pos['Amount']) # -1表示市价
Log(f"市价平多: {pos['Amount']}")
elif pos['Type'] == 'short':
# 平空仓
exchange.SetDirection("closesell")
exchange.Buy(-1, pos['Amount'])
Log(f"市价平空: {pos['Amount']}")
market_close()注意事项
- 仅限合约:此方法仅适用于合约交易
- 双向持仓模式:
- 必须设置:每次交易前都需要设置正确的方向
- 平仓时会自动设置
reduce_only=true确保只减仓不开反向仓位 - 方向错误可能导致意外开仓或平仓失败
- 单向持仓模式:
- 可选设置:不设置
SetDirection也能正常工作 - 系统根据 size 正负自动判断:正数开多/平空,负数开空/平多
- 推荐仍然使用
SetDirection以保持代码清晰
- 可选设置:不设置
- 兼容性:支持别名写法,如
"long"等价于"buy",提高代码可读性
python
# 推荐:封装开平仓函数,避免方向设置错误
def safe_open_long(price, amount):
exchange.SetDirection("buy")
return exchange.Buy(price, amount)
def safe_close_long(price, amount):
exchange.SetDirection("closebuy")
return exchange.Sell(price, amount)相关方法
- SetContractType - 设置合约类型
- SetMarginLevel - 设置杠杆倍数
- SetPositionMode - 设置仓位模式(全仓/逐仓)
- GetPosition - 获取持仓信息
- Buy - 买入(开多/平空)
- Sell - 卖出(开空/平多)