Skip to content

Instantly share code, notes, and snippets.

@ngruychev
Last active January 7, 2022 22:55
Show Gist options
  • Save ngruychev/e9319da0daaeb39c39a87d26d1fbd74a to your computer and use it in GitHub Desktop.
Save ngruychev/e9319da0daaeb39c39a87d26d1fbd74a to your computer and use it in GitHub Desktop.
binance-cli-watcher
import { plot } from "https://deno.land/x/[email protected]/mod.ts";
import { parse } from "https://deno.land/[email protected]/flags/mod.ts";
import { sleep } from "https://deno.land/x/[email protected]/mod.ts";
import {
blue,
green,
red,
yellow,
} from "https://deno.land/[email protected]/fmt/colors.ts";
import { writeAll } from "https://deno.land/[email protected]/streams/conversion.ts";
function colorize(n: string, c = 0): string {
return +n > c ? green(`${n}`) : +n < c ? red(`${n}`) : `${n}`;
}
const winSymbols = ["+", "+", "-", "-", "-", "\\", "/", "\\", "/", "|"];
function format(padding: string) {
return (num: number) => (padding + num.toFixed(flags.p)).slice(-padding.length);
}
const flags = parse(Deno.args, {
default: { t: 5, s: "XLMUSDT", p: 6 },
alias: {
"h": "help",
},
});
if (flags.h) {
console.log(`\
Usage: binance-cli-watcher [-tmxswhp]
-h, --help
this message
-t
update interval in seconds
default: 5
-m
min value
-x
max value
-s
symbol
default: XLMUSDT (to the moon!)
-w
windows-compatible characters - for use in cmd
powered by deno, deno.land/x/chart, and binance's api
-p
decimal precision
default: 4
`);
Deno.exit();
}
async function getData(s: string): Promise<number> {
let tries = 10;
let err: Error | undefined;
while (tries-- > 0) {
try {
return fetch(`https://api.binance.com/api/v1/ticker/price?symbol=${s}`)
.then((r) => r.json())
.then((r) => +r.price);
} catch (e) {
err = e;
}
await sleep(1);
}
throw err;
}
const priceData: number[] = [];
async function print(txt: string) {
await writeAll(Deno.stdout, new TextEncoder().encode(txt));
}
console.clear();
while (true) {
const { rows, columns } = Deno.consoleSize(Deno.stdout.rid);
const price = await getData(flags.s);
const padding = price.toFixed(flags.p).replace(/./g, ' ');
const [prevPrice] = priceData.slice(-1);
priceData.push(price);
while (priceData.length > columns - padding.length - 3) priceData.shift();
if (priceData.length === 0) continue;
if ([...new Set(priceData)].length == 1) console.clear();
await print("\n");
await print(
`${yellow(flags.s)}${
blue("".padStart(columns - flags.s.length - price.toFixed(flags.p).length, "="))
}${colorize(price.toFixed(flags.p), prevPrice)}\n`,
);
await print(
plot(priceData, {
min: flags.m,
max: flags.x,
height: rows - 2,
format: format(padding),
padding,
symbols: flags.w && winSymbols,
colors: (price > prevPrice
? ["green"]
: price < prevPrice
? ["red"]
: ["blue"]),
}),
);
await sleep(flags.t);
}
@ngruychev
Copy link
Author

ngruychev commented Apr 29, 2021

how to run:
deno run --allow-net --unstable https://gist.githubusercontent.com/ngruychev/e9319da0daaeb39c39a87d26d1fbd74a/raw/a72bcf39cf3333e275cb99cc6635859929ad788d/binance-cli-watcher.ts -s ETHUSDT -t 30
how to install:
deno install -qf --allow-net --unstable -n binance-cli-watcher https://gist.github.com/ngruychev/e9319da0daaeb39c39a87d26d1fbd74a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment