name: 持仓
description: “带有止损、止盈和跟踪止损的持仓管理”
emoji: “📍”
持仓 - 完整API参考
管理带有自动止损、止盈和跟踪止损订单的开放持仓。
聊天命令
查看持仓
/positions 列出所有持仓
/positions poly 仅Polymarket持仓
/positions futures 仅期货持仓
/position <id> 持仓详情
止损
/sl <position-id> at 0.35 设置止损价格
/sl <position-id> -10% 止损低于入场价10%
/sl poly "Trump" at 0.35 按市场名称设置
止盈
/tp <position-id> at 0.65 设置止盈价格
/tp <position-id> +20% 止盈高于入场价20%
/tp poly "Trump" at 0.65 按市场名称设置
跟踪止损
/trailing <position-id> 5% 从最高点追踪5%
/trailing <position-id> $0.05 从最高点追踪$0.05
部分退出
/tp <position-id> at 0.55 size 50% 止盈一半
/sl <position-id> at 0.40 size 25% 止损四分之一
TypeScript API参考
创建持仓管理器
import { createPositionManager } from 'clodds/positions';
const positions = createPositionManager({
// 监控
checkIntervalMs: 5000,
// 执行
orderType: 'market', // 'market' | 'limit'
limitBuffer: 0.01, // 限价单的缓冲
// 存储
storage: 'sqlite',
dbPath: './positions.db',
});
// 开始监控
await positions.start();
列出持仓
const all = await positions.list();
for (const pos of all) {
console.log(`${pos.id}: ${pos.platform} ${pos.market}`);
console.log(` 方向: ${pos.side}`);
console.log(` 大小: ${pos.size}`);
console.log(` 入场价: ${pos.entryPrice}`);
console.log(` 当前价: ${pos.currentPrice}`);
console.log(` 盈亏: ${pos.pnl} (${pos.pnlPercent}%)`);
console.log(` 止损: ${pos.stopLoss || '无'}`);
console.log(` 止盈: ${pos.takeProfit || '无'}`);
}
设置止损
// 绝对价格
await positions.setStopLoss({
positionId: 'pos-123',
price: 0.35,
});
// 从入场价百分比
await positions.setStopLoss({
positionId: 'pos-123',
percentFromEntry: 10, // 低于入场价10%
});
// 从当前价百分比
await positions.setStopLoss({
positionId: 'pos-123',
percentFromCurrent: 5, // 低于当前价5%
});
// 部分止损
await positions.setStopLoss({
positionId: 'pos-123',
price: 0.35,
sizePercent: 50, // 退出50%的持仓
});
设置止盈
// 绝对价格
await positions.setTakeProfit({
positionId: 'pos-123',
price: 0.65,
});
// 从入场价百分比
await positions.setTakeProfit({
positionId: 'pos-123',
percentFromEntry: 20, // 高于入场价20%
});
// 多个止盈级别
await positions.setTakeProfit({
positionId: 'pos-123',
levels: [
{ price: 0.55, sizePercent: 25 }, // 25%在0.55
{ price: 0.60, sizePercent: 25 }, // 25%在0.60
{ price: 0.70, sizePercent: 50 }, // 50%在0.70
],
});
设置跟踪止损
// 百分比追踪
await positions.setTrailingStop({
positionId: 'pos-123',
trailPercent: 5, // 低于最高点5%
});
// 绝对追踪
await positions.setTrailingStop({
positionId: 'pos-123',
trailAmount: 0.05, // 低于最高点$0.05
});
// 达到目标后激活
await positions.setTrailingStop({
positionId: 'pos-123',
trailPercent: 5,
activateAt: 0.55, // 达到0.55后开始追踪
});
移除止损
// 移除止损
await positions.removeStopLoss('pos-123');
// 移除止盈
await positions.removeTakeProfit('pos-123');
// 移除跟踪止损
await positions.removeTrailingStop('pos-123');
// 移除全部
await positions.removeAllStops('pos-123');
事件处理器
// 止损触发
positions.on('stopLossTriggered', (position, result) => {
console.log(`🛑 止损触发: ${position.market}`);
console.log(` 入场价: ${position.entryPrice}`);
console.log(` 退出价: ${result.exitPrice}`);
console.log(` 盈亏: ${result.pnl}`);
});
// 止盈触发
positions.on('takeProfitTriggered', (position, result) => {
console.log(`✅ 止盈触发: ${position.market}`);
console.log(` 盈亏: ${result.pnl}`);
});
// 跟踪止损触发
positions.on('trailingStopTriggered', (position, result) => {
console.log(`📉 跟踪止损触发: ${position.market}`);
console.log(` 最高价: ${position.highWaterMark}`);
console.log(` 退出价: ${result.exitPrice}`);
});
// 价格接近止损
positions.on('approaching', (position, type, distance) => {
console.log(`⚠️ ${position.market} ${distance}% 接近${type}`);
});
持仓摘要
const summary = await positions.getSummary();
console.log(`总持仓数: ${summary.count}`);
console.log(`总价值: $${summary.totalValue}`);
console.log(`未实现盈亏: $${summary.unrealizedPnl}`);
console.log(`有止损: ${summary.withStopLoss}`);
console.log(`有止盈: ${summary.withTakeProfit}`);
止损类型
| 类型 |
描述 |
| 止损 |
价格下跌时退出以限制损失 |
| 止盈 |
价格上涨时退出以锁定收益 |
| 跟踪止损 |
动态止损,随着价格上涨 |
| 盈亏平衡 |
利润目标达到后移动止损至入场价 |
订单执行
| 选项 |
描述 |
market |
立即以当前价格执行 |
limit |
以指定价格或更好的价格执行 |
buffer |
限价单增加缓冲以确保成交 |
最佳实践
- 始终设置止损 — 不要让持仓无保护
- 使用跟踪止损 — 随着价格上涨锁定收益
- 部分退出 — 在多个级别逐步退出
- 监控接近止损 — 在触发前获得警报
- 复核已触发的止损 — 检查执行质量