Skip to content

Instantly share code, notes, and snippets.

@kmckelvin
Created August 14, 2017 09:46
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 kmckelvin/66565734790fe07ee958f3dcc87af8b6 to your computer and use it in GitHub Desktop.
Save kmckelvin/66565734790fe07ee958f3dcc87af8b6 to your computer and use it in GitHub Desktop.
Gives some insights into bitcoin markets from Bittrex stats
const fetch = require('node-fetch');
const chalk = require('chalk');
const term = require('terminal-kit').terminal;
const fs = require('fs'),
path = require('path');
const timestamp = () => new Date().toLocaleTimeString();
const getMarketValue = market =>
fetch(`https://bittrex.com/api/v1.1/public/getmarketsummary?market=${market}`).then(res => res.json());
const getLogColor = (currentValue, lastValue) => {
if (currentValue === lastValue) return chalk.white;
return currentValue > lastValue ? chalk.green : chalk.red;
};
const logValue = (marketResponse, watch, lastValue) => {
const currentValue = marketResponse.result[0].Last;
const color = getLogColor(currentValue, lastValue);
if (currentValue === lastValue) {
process.stdout.write('.');
return;
}
const diff = (currentValue - lastValue).toFixed(8).replace(/^\d+/, m => `+${m}`);
console.log(`\n${timestamp()} ${watch.market} = ${color(currentValue.toFixed(8))} (${diff} ${color(diffGraph(lastValue, currentValue))})`);
};
const nowSecond = () => Math.floor(Date.now() / 1000);
const diffGraph = (prev, curr) => {
const diff = Math.abs(prev - curr);
const dots = Math.round((diff / prev) * 3000);
return '*'.repeat(dots === Infinity ? 0 : dots);
};
const appendStats = (stats, value) => {
stats[nowSecond()] = value;
delete stats[nowSecond() - (181 * 60)];
};
const statusBar = stats => [1, 5, 15, 30].map(m => `${m}m ${statusDiff(stats, m * 60)}`).join(' | ');
const closestStat = (stats, time) =>
stats[time] || stats[time - 1] || stats[time - 2] || stats[time - 3];
const statusDiff = (stats, secondsAgo) => {
const now = nowSecond();
const earliest = closestStat(stats, now - secondsAgo);
const current = stats[now] || Object.keys(stats).sort((a, b) => a < b ? 1 : -1)[0];
const diff = current - earliest;
const percentage = ((current / earliest) * 100) - 100;
return `${diff.toFixed(8)} (${percentage.toFixed(3)})`;
};
const writeStats = (statsPath, stats) => {
fs.writeFile(statsPath, JSON.stringify(stats, null, 2), () => {});
};
const startWatching = watch => {
let lastValue = 0;
const stats = fs.existsSync(statsPath) ?
JSON.parse(fs.readFileSync(statsPath)) :
{};
function lookupMarket() {
const mv = getMarketValue(watch.market);
mv.then(marketResponse => logValue(marketResponse, watch, lastValue));
mv.then(marketResponse => lastValue = marketResponse.result[0].Last)
.then(val => appendStats(stats, val))
.then(() => writeStats(statsPath, stats))
.then(() => {
term.saveCursor();
term.moveTo(1, 1).eraseLine();
term(statusBar(stats));
term.restoreCursor();
});
setTimeout(lookupMarket, 1000);
}
lookupMarket();
};
const appWatch = {
market: process.argv[2],
};
const statsPath = path.resolve(__dirname, '..', `${appWatch.market}.json`);
startWatching(appWatch, statsPath);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment