From ec3063e9fd304eadb43c4e725fe79c0697308b59 Mon Sep 17 00:00:00 2001 From: RyanZim Date: Sat, 25 Feb 2017 15:30:36 -0500 Subject: [PATCH] Add histoHour() --- CHANGELOG.md | 1 + README.md | 30 ++++++++++++++++++++++++++++++ index.js | 17 ++++++++++++++++- test/histo.test.js | 39 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bf9a820..c458a86 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ Switched to https://min-api.cryptocompare.com/ - added: `topPairs()` - added: `topExchanges()` - added: `histoDay()` +- added: `histoHour()` 0.0.2 / 2016-11-21 ------------------ diff --git a/README.md b/README.md index 96f8b18..45fb49d 100644 --- a/README.md +++ b/README.md @@ -257,6 +257,36 @@ cc.histoDay('BTC', 'USD') .catch(console.error) ``` +### `histoHour()` + +Get open, high, low, close, volumefrom and volumeto from the hourly historical data. + +`histoHour(fsym, tsym[, options])` + +- `fsym` (String) From Symbol +- `tsym` (String) To Symbol +- `options` (Object) + - `aggregate` (Number) Number of data points to aggregate. + - `limit` (Number) Limit the number of hours to lookup. Default is 168. + - `tryConversion` (Boolean) By default, if the crypto does not trade directly into the toSymbol requested, BTC will be used for conversion. Set `tryConversion` to `false` to disable using BTC for conversion. + - `timestamp` (Date) By default, `histoHour()` gets historical data for the past several hours. Use the `timestamp` option to set a historical start point. + +```js +cc.histoHour('BTC', 'USD') +.then(data => { + console.log(data) + // -> [ { time: 1487448000, + // close: 1060.34, + // high: 1061.44, + // low: 1058.85, + // open: 1059.24, + // volumefrom: 739.6, + // volumeto: 790019.22 }, + // ... ] +}) +.catch(console.error) +``` + ## License [MIT](LICENSE.md) diff --git a/index.js b/index.js index 8dbff7a..3697ca2 100644 --- a/index.js +++ b/index.js @@ -67,6 +67,20 @@ function histoDay (fsym, tsym, options) { return fetchJSON(url).then(result => result.Data) } +function histoHour (fsym, tsym, options) { + options = options || {} + if (options.timestamp) { + if (!(options.timestamp instanceof Date)) throw new Error('options.timestamp must be an instance of Date.') + options.timestamp = Math.floor(options.timestamp.getTime() / 1000) + } + let url = `${baseUrl}histohour?fsym=${fsym}&tsym=${tsym}` + if (options.limit) url += `&limit=${options.limit}` + if (options.tryConversion === false) url += '&tryConversion=false' + if (options.aggregate) url += `&aggregate=${options.aggregate}` + if (options.timestamp) url += `&toTs=${options.timestamp}` + return fetchJSON(url).then(result => result.Data) +} + module.exports = { price, priceMulti, @@ -74,5 +88,6 @@ module.exports = { priceHistorical, topPairs, topExchanges, - histoDay + histoDay, + histoHour } diff --git a/test/histo.test.js b/test/histo.test.js index cbca78f..65c12a7 100644 --- a/test/histo.test.js +++ b/test/histo.test.js @@ -43,3 +43,42 @@ test("histoDay()'s timestamp option works", t => { test("histoDay()'s tryConversion=false works", t => { helpers.testTryConversion(cc.histoDay(helpers.NOT_USD_TRADABLE, 'USD', { tryConversion: false }), t) }) + +test('histoHour()', t => { + t.plan(8) + cc.histoHour('BTC', 'USD').then(data => { + t.is(data.length, 169, 'returns 169 items by default') + const item = data[0] + t.is(typeof item.time, 'number', 'item.time is a number') + t.is(typeof item.close, 'number', 'item.close is a number') + t.is(typeof item.high, 'number', 'item.high is a number') + t.is(typeof item.low, 'number', 'item.low is a number') + t.is(typeof item.open, 'number', 'item.open is a number') + t.is(typeof item.volumefrom, 'number', 'item.volumefrom is a number') + t.is(typeof item.volumeto, 'number', 'item.volumeto is a number') + t.end() + }).catch(t.end) +}) + +test("histoHour()'s limit option works", t => { + t.plan(1) + cc.histoHour('BTC', 'USD', { limit: 2 }).then(data => { + t.is(data.length, 3, 'returns limit plus timestamped data') + t.end() + }).catch(t.end) +}) + +test("histoHour()'s timestamp option works", t => { + t.plan(1) + let data = [] + data.push(cc.histoHour('BTC', 'USD', { timestamp: new Date('2017-01-01') })) + data.push(cc.histoHour('BTC', 'USD', { timestamp: new Date('2017-01-02') })) + Promise.all(data).then(data => { + t.notDeepEqual(data[0], data[1], 'data from different days should not be equivalent') + t.end() + }).catch(t.end) +}) + +test("histoHour()'s tryConversion=false works", t => { + helpers.testTryConversion(cc.histoHour(helpers.NOT_USD_TRADABLE, 'USD', { tryConversion: false }), t) +})