Skip to content

Commit

Permalink
Add histoHour()
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanZim committed Feb 25, 2017
1 parent 8c7da7c commit ec3063e
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
------------------
Expand Down
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
17 changes: 16 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,27 @@ 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,
priceFull,
priceHistorical,
topPairs,
topExchanges,
histoDay
histoDay,
histoHour
}
39 changes: 39 additions & 0 deletions test/histo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

0 comments on commit ec3063e

Please sign in to comment.