From aaa5e13c6fba2f6debf681e8775b477a39443e34 Mon Sep 17 00:00:00 2001 From: RyanZim Date: Sat, 25 Feb 2017 16:17:43 -0500 Subject: [PATCH] Add histoMinute() --- CHANGELOG.md | 1 + README.md | 30 ++++++++++++++++++++++++++++++ index.js | 14 +++++++++++++- test/histo.test.js | 39 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c458a86..a006709 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ Switched to https://min-api.cryptocompare.com/ - added: `topExchanges()` - added: `histoDay()` - added: `histoHour()` +- added: `histoMinute()` 0.0.2 / 2016-11-21 ------------------ diff --git a/README.md b/README.md index 45fb49d..93eec20 100644 --- a/README.md +++ b/README.md @@ -287,6 +287,36 @@ cc.histoHour('BTC', 'USD') .catch(console.error) ``` +### `histoMinute()` + +Get open, high, low, close, volumefrom and volumeto from the minute-by-minute historical data. + +`histoMinute(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 minutes to lookup. Default is 1440. + - `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, `histoMinute()` gets historical data for the past several minutes. Use the `timestamp` option to set a historical start point. + +```js +cc.histoMinute('BTC', 'USD') +.then(data => { + console.log(data) + // -> [ { time: 1487970960, + // close: 1171.97, + // high: 1172.72, + // low: 1171.97, + // open: 1172.37, + // volumefrom: 25.06, + // volumeto: 29324.12 }, + // ... ] +}) +.catch(console.error) +``` + ## License [MIT](LICENSE.md) diff --git a/index.js b/index.js index a2aa23f..40a931a 100644 --- a/index.js +++ b/index.js @@ -74,6 +74,17 @@ function histoHour (fsym, tsym, options) { return fetchJSON(url).then(result => result.Data) } +function histoMinute (fsym, tsym, options) { + options = options || {} + if (options.timestamp) options.timestamp = dateToTimestamp(options.timestamp) + let url = `${baseUrl}histominute?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) +} + function dateToTimestamp (date) { if (!(date instanceof Date)) throw new Error('timestamp must be an instance of Date.') return Math.floor(date.getTime() / 1000) @@ -87,5 +98,6 @@ module.exports = { topPairs, topExchanges, histoDay, - histoHour + histoHour, + histoMinute } diff --git a/test/histo.test.js b/test/histo.test.js index 65c12a7..85c2592 100644 --- a/test/histo.test.js +++ b/test/histo.test.js @@ -82,3 +82,42 @@ test("histoHour()'s timestamp option works", t => { test("histoHour()'s tryConversion=false works", t => { helpers.testTryConversion(cc.histoHour(helpers.NOT_USD_TRADABLE, 'USD', { tryConversion: false }), t) }) + +test('histoMinute()', t => { + t.plan(8) + cc.histoMinute('BTC', 'USD').then(data => { + t.is(data.length, 1441, 'returns 1441 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("histoMinute()'s limit option works", t => { + t.plan(1) + cc.histoMinute('BTC', 'USD', { limit: 2 }).then(data => { + t.is(data.length, 3, 'returns limit plus timestamped data') + t.end() + }).catch(t.end) +}) + +test("histoMinute()'s timestamp option works", t => { + t.plan(1) + let data = [] + data.push(cc.histoMinute('BTC', 'USD', { timestamp: new Date('2017-01-01') })) + data.push(cc.histoMinute('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("histoMinute()'s tryConversion=false works", t => { + helpers.testTryConversion(cc.histoMinute(helpers.NOT_USD_TRADABLE, 'USD', { tryConversion: false }), t) +})