Skip to content

Commit

Permalink
Add histoMinute()
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanZim committed Feb 25, 2017
1 parent fa1f546 commit aaa5e13
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
------------------
Expand Down
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
14 changes: 13 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -87,5 +98,6 @@ module.exports = {
topPairs,
topExchanges,
histoDay,
histoHour
histoHour,
histoMinute
}
39 changes: 39 additions & 0 deletions test/histo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

0 comments on commit aaa5e13

Please sign in to comment.