Skip to content

Commit ec3063e

Browse files
committed
Add histoHour()
1 parent 8c7da7c commit ec3063e

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Switched to https://min-api.cryptocompare.com/
1111
- added: `topPairs()`
1212
- added: `topExchanges()`
1313
- added: `histoDay()`
14+
- added: `histoHour()`
1415

1516
0.0.2 / 2016-11-21
1617
------------------

README.md

+30
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,36 @@ cc.histoDay('BTC', 'USD')
257257
.catch(console.error)
258258
```
259259

260+
### `histoHour()`
261+
262+
Get open, high, low, close, volumefrom and volumeto from the hourly historical data.
263+
264+
`histoHour(fsym, tsym[, options])`
265+
266+
- `fsym` (String) From Symbol
267+
- `tsym` (String) To Symbol
268+
- `options` (Object)
269+
- `aggregate` (Number) Number of data points to aggregate.
270+
- `limit` (Number) Limit the number of hours to lookup. Default is 168.
271+
- `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.
272+
- `timestamp` (Date) By default, `histoHour()` gets historical data for the past several hours. Use the `timestamp` option to set a historical start point.
273+
274+
```js
275+
cc.histoHour('BTC', 'USD')
276+
.then(data => {
277+
console.log(data)
278+
// -> [ { time: 1487448000,
279+
// close: 1060.34,
280+
// high: 1061.44,
281+
// low: 1058.85,
282+
// open: 1059.24,
283+
// volumefrom: 739.6,
284+
// volumeto: 790019.22 },
285+
// ... ]
286+
})
287+
.catch(console.error)
288+
```
289+
260290
## License
261291

262292
[MIT](LICENSE.md)

index.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,27 @@ function histoDay (fsym, tsym, options) {
6767
return fetchJSON(url).then(result => result.Data)
6868
}
6969

70+
function histoHour (fsym, tsym, options) {
71+
options = options || {}
72+
if (options.timestamp) {
73+
if (!(options.timestamp instanceof Date)) throw new Error('options.timestamp must be an instance of Date.')
74+
options.timestamp = Math.floor(options.timestamp.getTime() / 1000)
75+
}
76+
let url = `${baseUrl}histohour?fsym=${fsym}&tsym=${tsym}`
77+
if (options.limit) url += `&limit=${options.limit}`
78+
if (options.tryConversion === false) url += '&tryConversion=false'
79+
if (options.aggregate) url += `&aggregate=${options.aggregate}`
80+
if (options.timestamp) url += `&toTs=${options.timestamp}`
81+
return fetchJSON(url).then(result => result.Data)
82+
}
83+
7084
module.exports = {
7185
price,
7286
priceMulti,
7387
priceFull,
7488
priceHistorical,
7589
topPairs,
7690
topExchanges,
77-
histoDay
91+
histoDay,
92+
histoHour
7893
}

test/histo.test.js

+39
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,42 @@ test("histoDay()'s timestamp option works", t => {
4343
test("histoDay()'s tryConversion=false works", t => {
4444
helpers.testTryConversion(cc.histoDay(helpers.NOT_USD_TRADABLE, 'USD', { tryConversion: false }), t)
4545
})
46+
47+
test('histoHour()', t => {
48+
t.plan(8)
49+
cc.histoHour('BTC', 'USD').then(data => {
50+
t.is(data.length, 169, 'returns 169 items by default')
51+
const item = data[0]
52+
t.is(typeof item.time, 'number', 'item.time is a number')
53+
t.is(typeof item.close, 'number', 'item.close is a number')
54+
t.is(typeof item.high, 'number', 'item.high is a number')
55+
t.is(typeof item.low, 'number', 'item.low is a number')
56+
t.is(typeof item.open, 'number', 'item.open is a number')
57+
t.is(typeof item.volumefrom, 'number', 'item.volumefrom is a number')
58+
t.is(typeof item.volumeto, 'number', 'item.volumeto is a number')
59+
t.end()
60+
}).catch(t.end)
61+
})
62+
63+
test("histoHour()'s limit option works", t => {
64+
t.plan(1)
65+
cc.histoHour('BTC', 'USD', { limit: 2 }).then(data => {
66+
t.is(data.length, 3, 'returns limit plus timestamped data')
67+
t.end()
68+
}).catch(t.end)
69+
})
70+
71+
test("histoHour()'s timestamp option works", t => {
72+
t.plan(1)
73+
let data = []
74+
data.push(cc.histoHour('BTC', 'USD', { timestamp: new Date('2017-01-01') }))
75+
data.push(cc.histoHour('BTC', 'USD', { timestamp: new Date('2017-01-02') }))
76+
Promise.all(data).then(data => {
77+
t.notDeepEqual(data[0], data[1], 'data from different days should not be equivalent')
78+
t.end()
79+
}).catch(t.end)
80+
})
81+
82+
test("histoHour()'s tryConversion=false works", t => {
83+
helpers.testTryConversion(cc.histoHour(helpers.NOT_USD_TRADABLE, 'USD', { tryConversion: false }), t)
84+
})

0 commit comments

Comments
 (0)