Skip to content

Instantly share code, notes, and snippets.

@mecampbellsoup
Created August 16, 2017 19:56
Show Gist options
  • Save mecampbellsoup/8fb83c5d58425611bcdd43b63b67e0b2 to your computer and use it in GitHub Desktop.
Save mecampbellsoup/8fb83c5d58425611bcdd43b63b67e0b2 to your computer and use it in GitHub Desktop.
// Deps
var yaml = require('js-yaml');
var fs = require('fs');
var moment = require('moment');
var endDate = moment();
var startDate = moment().subtract(1, 'months');
var startDateUnix = startDate.unix();
var endDateUnix = endDate.unix();
console.log("Start date:", startDate);
console.log("End date:", endDate);
console.log("Start date UNIX:", startDateUnix);
console.log("End date UNIX:", endDateUnix);
// Fetch the price from that ticker
var https = require("https");
ticker = 'ETH';
var historicalData = {};
for (var m = startDate; m.isBefore(endDate); m.add(1, 'days')) {
mAsUnix = m.unix();
Promise.resolve(getDayPrice(ticker, mAsUnix)).then(function(data) {
timestamp = data[0];
prices = data[1];
console.log("Data as of " + timestamp + ": ", prices)
});
};
//https://min-api.cryptocompare.com/data/pricehistorical?fsym=ETH&tsyms=BTC,USD&ts=1452680400
function getDayPrice(ticker, timestamp) {
var rawData = '';
const options = {
hostname: 'min-api.cryptocompare.com',
port: 443,
path: '/data/pricehistorical?fsym=' + ticker + '&tsyms=BTC,USD&ts=' + timestamp,
method: 'GET',
headers: { 'Accept': 'application/json' }
};
return new Promise(function(resolve, reject) {
var parsedJson = '';
https.get(options, (priceResponse) => {
priceResponse.on('data', (d) => {
rawData += d;
});
priceResponse.on('end', () => {
try {
parsedJson = JSON.parse(rawData)[ticker];
btcPrice = parsedJson['BTC'];
usdPrice = parsedJson['USD'];
historicalData[timestamp] = {};
historicalData[timestamp].btc = btcPrice;
historicalData[timestamp].usd = usdPrice;
resolve([timestamp, historicalData[timestamp]]);
} catch (e) {
console.error(`Got error: ${e.message}`);
reject(e);
}
});
}).on('error', (e) => {
console.error(`Got error: ${e.message}`);
});
})
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment