Skip to content

Instantly share code, notes, and snippets.

@alobato
Last active November 3, 2021 16:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alobato/3f1e470e78d3aee7881ecfbaaa52e3da to your computer and use it in GitHub Desktop.
Save alobato/3f1e470e78d3aee7881ecfbaaa52e3da to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
const fsp = require('fs').promises;
const { program } = require('commander');
const got = require('got');
const AVERAGE_QTY = 10;
const LOOP_DELAY = 100;
async function read(tempFile) {
try {
const data = await fsp.readFile(tempFile);
return JSON.parse(data);
} catch (err) {
return null;
}
}
async function write(data, tempFile) {
try {
await fsp.writeFile(tempFile, JSON.stringify(data));
} catch(err) {}
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
program
.option('-t, --tg-token <token>', 'Telegram Token')
.option('-c, --tg-chat-id <chatId>', 'Telegram chat id')
.option('-p, --percent <number>', 'Percent alert')
.option('-f, --temp-file <symbols>', 'Temp file')
.option('-s, --symbols <symbols>', 'Symbols list');
program.parse(process.argv);
(async () => {
const symbols = program.symbols.split(',');
const lastSentSymbols = [];
for (const symbol of symbols) {
await sleep(LOOP_DELAY);
const result = await got.get(`https://www.binance.com/api/v3/klines?symbol=${symbol.replace('_', '')}&interval=5m`).json();
const lastItems = result.reduce((acc, item, index) => {
const qty = result.length;
if (index >= (qty - (AVERAGE_QTY + 1)) && index < (qty - 1)) {
acc.push(Number(item[5]));
}
return acc
}, []);
const lastItemsAverage = lastItems.reduce((acc, item) => acc + item, 0) / lastItems.length;
const lastVolume = Number(result[result.length - 1][5]);
const diff = lastVolume - lastItemsAverage;
const percent = (diff * 100) / lastItemsAverage;
const lastOpen = Number(result[result.length - 1][1]);
const lastClose = Number(result[result.length - 1][4]);
const isGreen = lastOpen < lastClose;
const color = isGreen ? '🟢' : '🔴';
if (percent > Number(program.percent)) {
let lastSymbols;
const data = await read(program.tempFile);
if (data) {
lastSymbols = data.lastSentSymbols;
}
if (!lastSymbols || lastSymbols.length === 0 || !lastSymbols.includes(symbol)) {
try { await got.post(`https://api.telegram.org/bot${program.tgToken}/sendMessage`, { json: { disable_web_page_preview: true, parse_mode: 'html', chat_id: program.tgChatId, text: `<a href="https://www.binance.com/pt-BR/trade/${symbol}">${symbol} ${color} ${percent.toFixed(2)}%</a>` } }).json() } catch (err) { console.error(err) }
lastSentSymbols.push(symbol);
}
}
// [
// [
// 1499040000000, // Open time
// "0.01634790", // Open
// "0.80000000", // High
// "0.01575800", // Low
// "0.01577100", // Close
// "148976.11427815", // Volume
// 1499644799999, // Close time
// "2434.19055334", // Quote asset volume
// 308, // Number of trades
// "1756.87402397", // Taker buy base asset volume
// "28.46694368", // Taker buy quote asset volume
// "17928899.62484339" // Ignore.
// ]
// ]
}
if (lastSentSymbols.length > 0) {
await write({ lastSentSymbols }, program.tempFile);
}
})()
{
"name": "binance",
"version": "1.0.0",
"description": "binance",
"bin": "./index.js",
"dependencies": {
"got": "11.8.2",
"commander": "5.1.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment