- nodejs
npm i
node main
const api = require('kucoin-node-api'); | |
const TelegramBot = require('node-telegram-bot-api'); | |
// Telegram token | |
// replace the value below with the Telegram token you receive from @BotFather | |
const token = 'XXX'; | |
// Patterns to spot | |
const toSpot = [1000, 5000, 15000]; | |
// The chat id of the room/user to send the messages | |
const chatId = -530146638; | |
const symbol = 'LUNA-USDT'; | |
// KuCoin API info | |
const config = { | |
apiKey: 'XXX', | |
secretKey: 'XXX', | |
passphrase: 'XXX', | |
environment: 'live' | |
}; | |
// Create a bot that uses 'polling' to fetch new updates | |
const bot = new TelegramBot(token, {polling: true}); | |
api.init(config) | |
// To get the chat id you can uncomment this | |
// bot.on('message', (msg) => { | |
// const chatId = msg.chat.id; | |
// console.log(chatId); | |
// }); | |
let currentPrice = 0; | |
/** | |
* @return {string[][]} | |
*/ | |
const spotWhale = (orders) => { | |
const delta = .3; // we want to list stuff close to the current price | |
return orders.filter( | |
([price, total]) => toSpot.includes(+total) && (+price > currentPrice - delta && +price < currentPrice + delta) | |
); | |
}; | |
/** | |
* @param {string} symbol | |
* @return {number} | |
*/ | |
const getPrice = async (symbol) => +(await api.getTicker(symbol)).data.price; | |
const messageFactory = (type, price, total) => | |
`${type.toUpperCase()} DETECTED\nprice: ${price} | total: ${total}\n`; | |
const cache = { | |
ask: {}, | |
bid: {}, | |
}; | |
const params = { | |
topic: 'orderbook', | |
symbols: [symbol], | |
}; | |
api.initSocket(params, async (msg) => { | |
const data = JSON.parse(msg); | |
const order = data.data; | |
if (order) { | |
// Update the price from time to time | |
if (currentPrice === 0 || Math.random() > 0.9996) { | |
const priceCached = currentPrice; | |
currentPrice = -1; | |
console.log(`Fetch new price...`); | |
const newPrice = await getPrice(symbol); | |
if (Math.abs(priceCached - newPrice) > 0.01) { | |
currentPrice = newPrice; | |
bot.sendMessage(chatId, `${symbol} price updated: ${currentPrice}`); | |
console.log(`Current price updated (${currentPrice})`); | |
} | |
} | |
const asksRes = spotWhale(order.changes.asks); | |
const bidsRes = spotWhale(order.changes.bids); | |
let message = ''; | |
const createMessage = (type, data) => { | |
const [first] = data; | |
const price = +first[0]; | |
const ts = +first[2]; | |
if (cache[type][price]) { | |
if (Math.abs(cache[type][price] - ts) > 5000) { | |
cache[type][price] = ts; | |
message += messageFactory(type, price, first[1]); | |
} | |
} else { | |
cache[type][price] = ts; | |
message += messageFactory(type, price, first[1]); | |
} | |
}; | |
if (asksRes.length) { | |
createMessage('ask', asksRes); | |
} | |
if (bidsRes.length) { | |
createMessage('bid', bidsRes); | |
} | |
if (message) { | |
console.log(message); | |
bot.sendMessage(chatId, message); | |
message = ''; | |
} | |
} | |
}); |
{ | |
"name": "luna-whale-spotter", | |
"version": "1.0.0", | |
"description": "Spot whales from KuCoin", | |
"main": "main.js", | |
"dependencies": { | |
"kucoin-node-api": "^1.2.2", | |
"node-telegram-bot-api": "^0.52.0" | |
}, | |
"devDependencies": {} | |
} |