Trade
exchange.Sell()
以限价单方式卖出。现货中为卖出持有的币;合约中配合 SetDirection() 用于开空或平多。
语法
python
order_id = exchange.Sell(price, amount)
order_id = exchange.Sell(price, amount, *arg)
price传-1表示市价单(具体行为以各交易所实现为准);下单失败时返回None,错误详情写入策略日志。
参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| price | float | 是 | 卖出价格;传 -1 表示市价 |
| amount | float | 是 | 卖出数量(现货为币数量,合约为张数/数量,以交易所为准) |
| *arg | any | 否 | 扩展参数(可传多个),用空格拼接后写入该笔下单的交易日志,不参与下单 |
返回值
str: 订单ID,用于后续查询和取消订单None: 下单失败(错误详情写入策略日志)
示例
基础用法
python
# 以51000价格卖出0.1个BTC
order_id = exchange.Sell(51000, 0.1)
Log("卖单已提交,订单ID:", order_id)市价卖出
python
# 直接市价卖出
order_id = exchange.Sell(-1, 0.1, "市价止损")
# 或以买一价模拟市价
def market_sell(amount):
depth = exchange.GetDepth()
price = depth['Bids'][0][0] # 买一价
return exchange.Sell(price, amount)
market_sell(0.1)卖出全部可用币
python
account = exchange.GetAccount()
amount = account['Stocks']
if amount > 0:
ticker = exchange.GetTicker()
exchange.Sell(ticker['Buy'], amount, "清仓")
else:
Log("没有可卖出的币")止盈卖出
python
def take_profit_sell(buy_price, amount, profit_percent=5):
"""价格达到止盈线时卖出"""
target_price = buy_price * (1 + profit_percent / 100)
while True:
ticker = exchange.GetTicker()
if ticker['Last'] >= target_price:
order_id = exchange.Sell(target_price, amount, f"止盈 {profit_percent}%")
Log(f"触发止盈: {amount} @ {target_price}")
return order_id
Sleep(1000)
take_profit_sell(50000, 0.1, profit_percent=5)分批卖出
python
def grid_sell(start_price, end_price, num_orders, total_amount):
"""在价格区间内均匀挂多个卖单"""
price_step = (end_price - start_price) / (num_orders - 1)
amount_per_order = total_amount / num_orders
order_ids = []
for i in range(num_orders):
price = start_price + i * price_step
order_id = exchange.Sell(price, amount_per_order)
if order_id:
order_ids.append(order_id)
Log(f"挂单{i+1}: {amount_per_order:.6f} @ {price:.2f}")
return order_ids
grid_sell(51000, 52000, 5, 0.5)合约:开空与平多
python
exchange.SetContractType("swap")
# 双向持仓:开空
exchange.SetDirection("sell") # 或 "short"
exchange.Sell(-1, 10, "开空")
# 双向持仓:平多
exchange.SetDirection("closebuy") # 或 "close_long"
exchange.Sell(-1, 10, "平多")
# 单向持仓:无需 SetDirection,持有多仓时 Sell 自动平多,否则开空
exchange.Sell(-1, 10)卖出并等待成交
python
def sell_and_wait(price, amount, timeout_ms=60000):
order_id = exchange.Sell(price, amount)
if not order_id:
return None
waited = 0
while waited < timeout_ms:
order = exchange.GetOrder(order_id)
if order and order['DealAmount'] >= order['Amount']:
Log(f"已全部成交: {order['DealAmount']}")
return order
Sleep(1000)
waited += 1000
Log("等待超时,撤单")
exchange.CancelOrder(order_id)
return None
sell_and_wait(51000, 0.1)注意事项
- 限价单:
Sell()是限价单,不保证立即成交 - 精度要求:价格和数量必须符合交易所精度要求,可用
GetCurrenciesPrecision()或GetMarkets()查询 - 持仓检查:现货卖出前确保有足够的币(
GetAccount()["Stocks"]) - 合约方向:双向持仓模式下必须先
SetDirection(),"sell"为开空、"closebuy"为平多 - 订单管理:保存 order_id 用于后续查询和取消
Polymarket 说明
在 Polymarket 中,Sell() 卖出当前标的(某个结果)已持有的份额,提交 GTC 限价单。
| 参数 / 规则 | Polymarket 下的含义 |
|---|---|
| price | USDC / 份,0 < price < 1;精度、范围、tick 取整与 Buy() 相同 |
| amount | 卖出份数,不能超过持有份数 |
price=-1 | 以 0.01 提交限价卖单;未成交部分会以 0.01 继续挂单,请及时撤单 |
| 做空 | 不支持。想看空某结果请买入它的互补结果(看空 Yes 即买入 No) |
| 返回值 | 复合订单 ID "{token_id},{polymarket_order_id}" |
python
symbol = exchange.GetCurrency()
pos = next((p for p in exchange.GetPosition() if p["Symbol"].lower() == symbol.lower()), None)
if pos:
# 止盈挂单:均价的 1.5 倍,不超过 0.99
target = min(round(pos["Price"] * 1.5, 2), 0.99)
exchange.Sell(target, pos["Amount"], f"止盈 @ {target}")接入方式见 Polymarket 总览。
相关方法
- Buy - 买入下单
- CancelOrder - 取消订单
- GetOrder - 查询订单
- GetOrders - 获取未完成订单
- SetDirection - 设置合约开平方向
- GetAccount - 获取账户信息