
Cryptocurrency trading thrives on timely and accurate data. By leveraging AI-driven insights and technical indicators, we can automate the process of generating and sending trading signals.
In this article, we’ll explore how to build a Crypto Signal Bot that:
We fetch daily (1d) candlestick data using the Binance API:
import axios from 'axios';
const BINANCE_API_URL = 'https://api.binance.com/api/v3/klines';
async function getTokenData(token: string) {
  try {
    const response = await axios.get(BINANCE_API_URL, {
      params: {
        symbol: `${token.toUpperCase()}USDT`,
        interval: '1d',
        limit: 100,
      },
    });    return response.data.map((candle: any[]) => ({
      open: parseFloat(candle[1]),
      high: parseFloat(candle[2]),
      low: parseFloat(candle[3]),
      close: parseFloat(candle[4]),
      volume: parseFloat(candle[5]),
      timestamp: candle[0],
    }));
  } catch (error) {
    console.error(`Error fetching token data for ${token}:`, error.message);
    return null;
  }
}We calculate key indicators like SMA, EMA, RSI, MACD, Bollinger Bands, OBV, and Stochastic:
import technicalIndicators from 'technicalindicators';
function analyzeIndicators(candles: any[]) {
  const closePrices = candles.map(c => c.close);
  const volumes = candles.map(c => c.volume);
  
  const sma20 = technicalIndicators.SMA.calculate({ period: 20, values: closePrices });
  const ema20 = technicalIndicators.EMA.calculate({ period: 20, values: closePrices });
  const rsi = technicalIndicators.RSI.calculate({ period: 14, values: closePrices });
  const macd = technicalIndicators.MACD.calculate({
    values: closePrices,
    fastPeriod: 12,
    slowPeriod: 26,
    signalPeriod: 9,
  });  const bb = technicalIndicators.BollingerBands.calculate({
    period: 20,
    values: closePrices,
    stdDev: 2,
  });  const obv = technicalIndicators.OBV.calculate({ close: closePrices, volume: volumes });  return { sma20, ema20, rsi, macd, bb, obv };
}We implement functions to detect Hammer, Bullish/Bearish Engulfing, and Three White Soldiers:
function detectHammer(candles: any[]): boolean {
  const candle = candles[candles.length - 1];
  return Math.abs(candle.open - candle.close) <= (candle.close - candle.low) / 2;
}function detectBullishEngulfing(candles: any[]): boolean {
  if (candles.length < 2) return false;
  const prev = candles[candles.length - 2];
  const current = candles[candles.length - 1];
  return prev.close < prev.open && current.open < prev.close && current.close > prev.open;
}We use Google’s Custom Search API to fetch recent news and OpenAI to process sentiment:
import OpenAI from 'openai';
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
async function getTokenSentiment(token: string) {
  const searchQuery = `${token} cryptocurrency latest news and sentiment`;
  const searchUrl = `https://www.googleapis.com/customsearch/v1?q=${encodeURIComponent(searchQuery)}&key=${process.env.GOOGLE_SEARCH_API_KEY}&cx=${process.env.GOOGLE_CSE_ID}`;  try {
    const response = await axios.get(searchUrl);
    const articles = response.data.items.map((item: any) => item.snippet).join('\n');    const sentimentAnalysis = await openai.chat.completions.create({
      model: 'gpt-4o-mini',
      messages: [{ role: 'system', content: `Analyze this crypto sentiment: ${articles}` }],
    });return sentimentAnalysis.choices[0].message.content;
} catch (error) {
console.error('Error fetching sentiment:', error);
return null;
}
}
We use Grammy.js to send buy/sell alerts to users via Telegram:
import { Composer } from 'grammy';
import { getPricePrediction } from '../../utils/binance.js';export const bot = new Composer();
bot.command('token', async (ctx) => {
  const name = ctx.message?.text.split(' ')[1];
  if (!name) return ctx.reply('Please provide a token name.');  const loading = await ctx.reply('⌛ Fetching data...');
  const message = await getPricePrediction(name);  if (message) {
    await ctx.reply(message, { parse_mode: 'HTML' });
  } else {
    await ctx.reply('❌ Failed to fetch data.');
  }await ctx.api.deleteMessage(ctx.chat.id, loading.message_id);
});
📊 Technical Analysis of BSW
🔍 Current Price: $0.05
📈 Investment Strategy: Hold 📍 Entry Price: ~$0.045 🎯 Price Target: $0.065 ⚠ Market Summary: Neutral sentiment, growing interest.
With this bot, traders can receive real-time buy/sell alerts based on both technical indicators and market sentiment. By combining Binance API data, Google Search insights, and OpenAI’s analysis, we ensure a data-driven approach to crypto trading.