I’m trying to wet my beak in the Bitquery stream and I’m able to make a connection just fine, and I receive Keep Alive messages, but I have yet to receive any data. Is there something wrong with my config? What could be causing this?
const WebSocket = require("ws");
// === CONFIGURATION ===
const ASSET = "BTC"; // Change this to ETH, etc.
const FIAT = "USD"; // Change to USDT, EUR, etc.
const MIN_SPREAD = 0.50; // Minimum price difference before alerting arbitrage
// WebSocket URLs
const EXCHANGES = {
binance: "wss://stream.binance.com:9443/ws/btcusdt@trade",
kraken: "wss://ws.kraken.com",
bitstamp: "wss://ws.bitstamp.net"
};
// Store live prices
const prices = {
binance: null,
kraken: null,
bitstamp: null
};
// === FUNCTION TO HANDLE MESSAGES ===
function handleMessage(exchange, data) {
let price = null;
try {
const parsedData = JSON.parse(data);
if (exchange === "binance") {
price = parseFloat(parsedData.p);
} else if (exchange === "kraken" && parsedData[1]?.c) {
price = parseFloat(parsedData[1].c[0]);
} else if (exchange === "bitstamp" && parsedData.data?.price) {
price = parseFloat(parsedData.data.price);
}
if (price) {
prices[exchange] = price;
console.log(`💰 ${exchange.toUpperCase()} ${ASSET} Price: $${price.toFixed(2)}`);
checkArbitrage();
}
} catch (error) {
console.error(`❌ Error processing ${exchange} data:`, error.message);
}
}
// === FUNCTION TO CHECK ARBITRAGE ===
function checkArbitrage() {
const exchanges = Object.keys(prices);
for (let i = 0; i < exchanges.length; i++) {
for (let j = i + 1; j < exchanges.length; j++) {
const exchangeA = exchanges[i];
const exchangeB = exchanges[j];
const priceA = prices[exchangeA];
const priceB = prices[exchangeB];
if (priceA && priceB) {
const spread = Math.abs(priceA - priceB);
console.log(`🔄 ${exchangeA.toUpperCase()} vs ${exchangeB.toUpperCase()} Spread: $${spread.toFixed(2)}`);
if (spread >= MIN_SPREAD) {
console.log(`🚀 **Arbitrage Opportunity!** Buy on ${priceA < priceB ? exchangeA.toUpperCase() : exchangeB.toUpperCase()} ($${Math.min(priceA, priceB).toFixed(2)}) and Sell on ${priceA > priceB ? exchangeA.toUpperCase() : exchangeB.toUpperCase()} ($${Math.max(priceA, priceB).toFixed(2)})`);
}
}
}
}
}
// === CONNECT TO WEBSOCKETS ===
function connectToExchange(exchange) {
console.log(`📡 Connecting to ${exchange.toUpperCase()} WebSocket...`);
const ws = new WebSocket(EXCHANGES[exchange]);
ws.on("open", () => {
console.log(`✅ Connected to ${exchange.toUpperCase()}`);
if (exchange === "kraken") {
ws.send(JSON.stringify({ event: "subscribe", pair: [`${ASSET}/${FIAT}`], subscription: { name: "ticker" } }));
} else if (exchange === "bitstamp") {
ws.send(JSON.stringify({ event: "bts:subscribe", data: { channel: `live_trades_${ASSET.toLowerCase()}${FIAT.toLowerCase()}` } }));
}
});
ws.on("message", (data) => handleMessage(exchange, data));
ws.on("error", (err) => console.error(`❌ ${exchange.toUpperCase()} WebSocket error:`, err));
ws.on("close", () => {
console.log(`🔴 ${exchange.toUpperCase()} WebSocket closed. Reconnecting in 5s...`);
setTimeout(() => connectToExchange(exchange), 5000);
});
}
// === START BOT ===
Object.keys(EXCHANGES).forEach(connectToExchange);