You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现加密货币交易信号Bot的买卖信号交替触发逻辑?

问题需求

我是JavaScript编程新手,正在开发一个向Twitter发布加密货币交易信号的Bot。目前Bot每16秒获取一次ETH价格和RSI指数,RSI低于30发Buy信号、高于70发Sell信号,功能正常,但需要实现同类信号不可重复发送——必须等相反信号触发后,才能再次发送同类型信号。比如RSI连续高于70时,只触发一次Sell,要等触发过Buy后,才能再触发Sell,信号必须在Buy和Sell之间交替。

实现方案

核心思路是记录上一次发送的信号类型,每次判断RSI触发条件时,先检查当前要发送的信号是否和上一次相同,不同才允许发送。

1. 添加状态变量

在全局作用域添加一个变量,用来记录最后一次发送的信号类型,初始值设为null(表示还没发过任何信号):

// 记录最后发送的信号类型:'buy' / 'sell' / null
let lastSentSignal = null;

2. 修改信号触发逻辑

在判断RSI条件时,增加对lastSentSignal的检查:

  • 当RSI>70时,只有最后一次信号不是sell,才发送Sell信号,并更新lastSentSignal为sell
  • 当RSI<30时,只有最后一次信号不是buy,才发送Buy信号,并更新lastSentSignal为buy

3. 优化代码细节

  • 把require('./twitterClient.js')移到全局,避免每次调用函数都重复加载模块
  • 把tweetSell和tweetBuy函数提到全局,避免每次RSI检查都重新定义函数,提升性能
修改后的完整代码
var axios = require('axios');
// 全局加载Twitter客户端,避免重复加载
const rwClient = require("./twitterClient.js");

var price;
// 记录最后发送的信号类型
let lastSentSignal = null;

// 价格更新定时器
let var1 = setInterval(priceUpdater, 16000); 
function priceUpdater() {
    axios.get('https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd')
      .then(function (reply) {
        console.log("ETH Price: " + reply.data.ethereum.usd); 
        price = reply.data.ethereum.usd
      })
      .catch(function (error) {
        console.log(error?.response?.data || error.message);
      }); 
} 

// RSI更新定时器
let var2 = setInterval(rsiUpdater, 16000);

// 全局定义Sell函数
const tweetSell = async () => {
    try {
        await rwClient.v2.tweet("Sell ETH/USD at $" + price)
        console.log("Sell信号已发送");
    } catch (e) {
        console.error("发送Sell信号失败:", e)
    }
}

// 全局定义Buy函数
const tweetBuy = async () => {
    try {
        await rwClient.v2.tweet("Buy ETH/USD at $" + price)
        console.log("Buy信号已发送");
    } catch (e) {
        console.error("发送Buy信号失败:", e)
    }
}

function rsiUpdater() {
    axios.get('https://api.taapi.io/rsi', {
        params: {
          secret: "PLACEHOLDER_FOR_SECRET",
          exchange: "binance",
          symbol: "ETH/USDT",
          interval: "5m",
        }
      })
      .then(function (response) {
        const rsiValue = response.data.value;
        console.log("ETH RSI: " + rsiValue);
        
        if (rsiValue > 70) {
            // 检查最后一次不是Sell信号才发送
            if (lastSentSignal !== 'sell') {
                console.log("Sell ETH/USD at $" + price)
                tweetSell();
                lastSentSignal = 'sell';
            } else {
                console.log("已发送过Sell信号,跳过本次");
            }
        } else if (rsiValue < 30) {
            // 检查最后一次不是Buy信号才发送
            if (lastSentSignal !== 'buy') {
                console.log("Buy ETH/USD at $" + price)
                tweetBuy();
                lastSentSignal = 'buy';
            } else {
                console.log("已发送过Buy信号,跳过本次");
            }
        } else {
            console.log("Hold")
        }
      })
      .catch(function (error) {
        console.log(error?.response?.data || error.message);
      });
}

内容的提问来源于stack exchange,提问作者t.an1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 05:37:08