name: 触发器
description: “当价格阈值满足时自动执行的有条件订单”
emoji: “⚡”
触发器 - 完整API参考
设置在价格条件满足时自动执行交易的有条件订单。适用于预测市场、期货和加密货币现货。
聊天命令
创建触发订单
/trigger buy poly "特朗普 2028" YES below 0.40 size 100
/trigger buy poly "联邦利率" NO above 0.60 size 50
/trigger sell poly "特朗普 2028" YES above 0.55 size all
期货触发器
/trigger long binance BTCUSDT below 95000 size 0.1 leverage 10x
/trigger short binance ETHUSDT above 4000 size 1 leverage 20x
/trigger close binance BTCUSDT above 105000
加密货币现货触发器
/trigger buy sol SOL below 180 size 100usdc
/trigger sell eth ETH above 4000 size 0.5
/trigger swap arb USDC to ARB below 1.50 size 500
管理触发器
/triggers 列出所有活跃的触发器
/triggers pending 显示仅待处理的
/triggers history 触发订单历史
/trigger cancel <id> 取消触发器
/trigger cancel all 取消所有触发器
止损 & 止盈
/sl poly "特朗普" at 0.35 止损在位置
/tp poly "特朗普" at 0.65 止盈在位置
/trailing-stop poly "特朗普" 10% 跟踪止损(从高点的百分比)
TypeScript API参考
创建触发服务
import { createTriggerService } from 'clodds/triggers';
const triggers = createTriggerService({
// 价格监控
checkIntervalMs: 5000, // 每5秒检查一次
// 执行
maxSlippagePercent: 2,
retryAttempts: 3,
// 存储
storage: 'sqlite',
dbPath: './triggers.db',
});
// 开始监控
await triggers.start();
创建预测市场触发器
// 当价格跌破阈值时买入YES
const trigger = await triggers.create({
type: 'entry',
platform: 'polymarket',
market: 'will-trump-win-2028',
side: 'YES',
direction: 'below',
triggerPrice: 0.40,
size: 100, // $100
orderType: 'limit', // 'market' | 'limit'
limitPrice: 0.41, // 可选:限价的最大价格
});
console.log(`触发器ID: ${trigger.id}`);
console.log(`状态: ${trigger.status}`); // 'pending'
// 当价格上涨超过阈值时卖出
await triggers.create({
type: 'exit',
platform: 'polymarket',
market: 'will-trump-win-2028',
side: 'YES',
direction: 'above',
triggerPrice: 0.55,
size: 'all', // 卖出整个位置
});
创建期货触发器
// 当BTC跌破支撑位时长线进入
await triggers.create({
type: 'entry',
platform: 'binance',
symbol: 'BTCUSDT',
side: 'long',
direction: 'below',
triggerPrice: 95000,
size: 0.1,
leverage: 10,
// 填充时自动设置SL/TP
stopLoss: 93000,
takeProfit: 105000,
});
// 当ETH突破阻力位时短线进入
await triggers.create({
type: 'entry',
platform: 'bybit',
symbol: 'ETHUSDT',
side: 'short',
direction: 'above',
triggerPrice: 4000,
size: 1,
leverage: 20,
});
// 当价格目标击中时关闭位置
await triggers.create({
type: 'exit',
platform: 'binance',
symbol: 'BTCUSDT',
direction: 'above',
triggerPrice: 105000,
size: 'all',
});
创建加密货币现货触发器
// 当价格下跌时买入SOL
await triggers.create({
type: 'entry',
platform: 'jupiter', // Solana DEX
tokenIn: 'USDC',
tokenOut: 'SOL',
direction: 'below',
triggerPrice: 180,
size: 100, // 100 USDC
slippagePercent: 1,
});
// 当价格上涨时卖出ETH
await triggers.create({
type: 'exit',
platform: 'uniswap', // EVM DEX
chain: 'ethereum',
tokenIn: 'ETH',
tokenOut: 'USDC',
direction: 'above',
triggerPrice: 4000,
size: 0.5,
});
止损 & 止盈
// 在现有位置设置止损
await triggers.setStopLoss({
platform: 'polymarket',
market: 'will-trump-win-2028',
side: 'YES',
triggerPrice: 0.35,
size: 'all',
});
// 设置止盈
await triggers.setTakeProfit({
platform: 'polymarket',
market: 'will-trump-win-2028',
side: 'YES',
triggerPrice: 0.65,
size: 'all',
});
// 跟踪止损(跟随价格上涨,回调时触发)
await triggers.setTrailingStop({
platform: 'polymarket',
market: 'will-trump-win-2028',
side: 'YES',
trailPercent: 10, // 如果从高点下跌10%则触发
size: 'all',
});
多条件触发器
// 仅当满足多个条件时触发
await triggers.create({
type: 'entry',
platform: 'polymarket',
market: 'will-trump-win-2028',
side: 'YES',
conditions: [
{ type: 'price', direction: 'below', value: 0.40 },
{ type: 'volume24h', direction: 'above', value: 100000 },
{ type: 'spread', direction: 'below', value: 0.02 },
],
// 所有条件必须为真
conditionLogic: 'AND', // 'AND' | 'OR'
size: 100,
});
一对取消另一对(OCO)
// OCO: 触发SL或TP,另一对取消
const oco = await triggers.createOCO({
platform: 'binance',
symbol: 'BTCUSDT',
stopLoss: {
direction: 'below',
triggerPrice: 93000,
size: 'all',
},
takeProfit: {
direction: 'above',
triggerPrice: 105000,
size: 'all',
},
});
列出和管理触发器
// 列出所有触发器
const all = await triggers.list();
for (const t of all) {
console.log(`${t.id}: ${t.platform} ${t.market || t.symbol}`);
console.log(` ${t.direction} ${t.triggerPrice}`);
console.log(` 状态: ${t.status}`);
console.log(` 创建: ${t.createdAt}`);
}
// 获取待处理的
const pending = await triggers.list({ status: 'pending' });
// 获取历史(已触发)
const history = await triggers.list({ status: 'triggered' });
// 取消触发器
await triggers.cancel(triggerId);
// 取消全部
await triggers.cancelAll();
事件处理程序
// 触发器激活
triggers.on('triggered', async (trigger, result) => {
console.log(`触发器 ${trigger.id} 激活了!`);
console.log(`订单: ${result.orderId}`);
console.log(`成交价格: ${result.fillPrice}`);
console.log(`大小: ${result.filledSize}`);
});
// 触发器失败
triggers.on('failed', (trigger, error) => {
console.error(`触发器 ${trigger.id} 失败: ${error.message}`);
});
// 价格接近触发器
triggers.on('approaching', (trigger, currentPrice) => {
console.log(`价格 ${currentPrice} 接近触发器在 ${trigger.triggerPrice}`);
});
支持的平台
预测市场
| 平台 |
进入触发器 |
退出触发器 |
SL/TP |
| Polymarket |
✓ |
✓ |
✓ |
| Kalshi |
✓ |
✓ |
✓ |
| Manifold |
✓ |
✓ |
✓ |
期货
| 平台 |
进入触发器 |
本地触发器 |
SL/TP |
| Binance |
✓ |
✓ |
✓ |
| Bybit |
✓ |
✓ |
✓ |
| MEXC |
✓ |
✓ 本地 |
✓ |
| Hyperliquid |
✓ |
✓ |
✓ |
加密货币现货
| 平台 |
进入触发器 |
退出触发器 |
| Jupiter (Solana) |
✓ |
✓ |
| Raydium |
✓ |
✓ |
| Uniswap (EVM) |
✓ |
✓ |
| 1inch (EVM) |
✓ |
✓ |
触发器类型
| 类型 |
描述 |
| entry |
当触发时打开新位置 |
| exit |
当触发时关闭位置 |
| stop-loss |
限制损失 |
| take-profit |
锁定收益 |
| trailing-stop |
动态止损,跟随价格 |
| oco |
一对取消另一对(SL + TP对) |
价格源
| 平台 |
价格源 |
| Polymarket |
WebSocket中间价 |
| Kalshi |
REST API最佳买卖价 |
| Binance |
WebSocket标记价 |
| Bybit |
WebSocket最后价格 |
| Jupiter |
链上预言机 |
示例
预测市场策略
// 逢低买入,逢高卖出
await triggers.create({
platform: 'polymarket',
market: 'trump-2028',
side: 'YES',
direction: 'below',
triggerPrice: 0.40,
size: 200,
});
await triggers.create({
platform: 'polymarket',
market: 'trump-2028',
side: 'YES',
direction: 'above',
triggerPrice: 0.55,
size: 'all',
});
期货突破策略
// 在阻力位上方突破时进入长线
await triggers.create({
platform: 'binance',
symbol: 'BTCUSDT',
side: 'long',
direction: 'above',
triggerPrice: 100000,
size: 0.5,
leverage: 10,
stopLoss: 98000,
takeProfit: 110000,
});
DCA在下跌时
// 随着价格下跌买入更多
const levels = [180, 170, 160, 150];
for (const price of levels) {
await triggers.create({
platform: 'jupiter',
tokenOut: 'SOL',
tokenIn: 'USDC',
direction: 'below',
triggerPrice: price,
size: 50, // 每个级别$50
});
}
最佳实践
- 使用限价单 —— 避免触发时的滑点
- 设置过期时间 —— 不要让触发器永远存在
- 监控执行 —— 检查成交价格
- 使用OCO进行退出 —— SL + TP一起
- 用小规模测试 —— 验证触发器工作
- 考虑费用 —— 包含在触发价格中