Skip to content
This repository was archived by the owner on Feb 22, 2024. It is now read-only.

Commit 2495237

Browse files
committed
Merge pull request #47 from qingweibinary/qingwei/ohlc
Qingwei/ohlc
2 parents 8ef95c1 + 5fd3fb8 commit 2495237

File tree

2 files changed

+46
-16
lines changed

2 files changed

+46
-16
lines changed

src/__tests__/custom-test.js

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,51 @@ describe('custom', () => {
1616
it('should get more extra ticks for non-tick-contract', async () => {
1717
await liveApi.authorize(token);
1818
const nonTickContractID = '8686424368';
19-
const ticks = await liveApi.getDataForContract(() => liveApi.getContractInfo(nonTickContractID));
19+
const ticks = await liveApi
20+
.getDataForContract(() => liveApi.getContractInfo(nonTickContractID).then(r => r.proposal_open_contract));
2021
expect(ticks).to.have.lengthOf(451);
2122
});
2223

2324
it('should get exact number of ticks for tick-contract', async () => {
2425
await liveApi.authorize(token);
2526
const tickContractID = '8818581808';
26-
const ticks = await liveApi.getDataForContract(() => liveApi.getContractInfo(tickContractID));
27+
const ticks = await liveApi
28+
.getDataForContract(() => liveApi.getContractInfo(tickContractID).then(r => r.proposal_open_contract));
2729
expect(ticks).to.have.lengthOf(8);
2830
});
31+
32+
it('should return candles if user request candles', async () => {
33+
await liveApi.authorize(token);
34+
const nonTickContractID = '8686424368';
35+
const candles = await liveApi
36+
.getDataForContract(
37+
() => liveApi.getContractInfo(nonTickContractID).then(r => r.proposal_open_contract),
38+
1,
39+
'all',
40+
'candles',
41+
);
42+
expect(candles).to.have.lengthOf(16);
43+
expect(candles[0]).to.have.keys('open', 'close', 'epoch', 'high', 'low');
44+
});
2945
});
3046

31-
it('getDataForSymbol', async () => {
32-
await liveApi.authorize(token);
33-
const ticks = await liveApi.getDataForSymbol('R_100');
34-
expect(ticks).to.have.length.above(1000);
47+
describe('getDataForSymbol', () => {
48+
it('should get data for specified market', async () => {
49+
await liveApi.authorize(token);
50+
const ticks = await liveApi.getDataForSymbol('R_100');
51+
expect(ticks).to.have.length.above(1000);
52+
});
53+
54+
it('should get data for specified market using given duration params', async () => {
55+
await liveApi.authorize(token);
56+
const ticks = await liveApi.getDataForSymbol('R_100', 1, 'minute');
57+
expect(ticks).to.have.length.above(29);
58+
});
59+
60+
it('should get candles for specified market if requested candles', async () => {
61+
await liveApi.authorize(token);
62+
const ticks = await liveApi.getDataForSymbol('R_100', 1, 'hour', 'candles');
63+
expect(ticks).to.have.length.above(59);
64+
});
3565
});
3666
});

src/custom.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ const hcUnitConverter = type => {
1616
const autoAdjustGetData = (api, symbol, start, end, style = 'ticks', granularity = 60) => {
1717
const secs = end - start;
1818
const ticksCount = secs / 2;
19-
if (ticksCount >= 5000) {
20-
style = 'candles';
19+
if (ticksCount >= 5000 || style === 'candles') {
2120
const idealGranularity = secs / 4999;
2221
granularities.forEach((g, i) => {
2322
if (idealGranularity > g && idealGranularity <= granularities[i + 1]) {
@@ -31,18 +30,18 @@ const autoAdjustGetData = (api, symbol, start, end, style = 'ticks', granularity
3130
end,
3231
adjust_start_time: 1,
3332
count: 4999,
34-
style,
33+
style: 'candles',
3534
granularity,
3635
}
37-
).then(r => ohlcDataToTicks(r.candles));
36+
).then(r => style === 'ticks' ? ohlcDataToTicks(r.candles) : r.candles);
3837
}
3938
return api.getTickHistory(symbol,
4039
{
4140
start,
4241
end,
4342
adjust_start_time: 1,
4443
count: 4999,
45-
style,
44+
style: 'ticks',
4645
}
4746
).then(r => {
4847
const ticks = r.history.times.map((t, idx) => {
@@ -60,11 +59,11 @@ const autoAdjustGetData = (api, symbol, start, end, style = 'ticks', granularity
6059
* @param durationCount
6160
* @param durationType
6261
*/
63-
export function getDataForSymbol(api, symbol, durationCount = 1, durationType = 'all') {
62+
export function getDataForSymbol(api, symbol, durationCount = 1, durationType = 'all', style = 'ticks') {
6463
const durationUnit = hcUnitConverter(durationType);
6564
const end = nowEpoch();
6665
const start = end - durationToSecs(durationCount, durationUnit);
67-
return autoAdjustGetData(api, symbol, start, end);
66+
return autoAdjustGetData(api, symbol, start, end, style);
6867
}
6968

7069
/**
@@ -73,7 +72,9 @@ export function getDataForSymbol(api, symbol, durationCount = 1, durationType =
7372
* @param getContract - function that accept nothing and return a Promise containing contract
7473
* @param durationCount - number of duration
7574
* @param durationType - type of duration, check http://api.highcharts.com/highstock#rangeSelector.buttons
76-
* @param style - one of ['ticks', 'candles'], check https://developers.binary.com/api/#ticks_history
75+
* @param style - one of ['ticks', 'candles'], this will affect the return data shape,
76+
* internally library might not always use this param when requesting, eg. when data is too large,
77+
* library will use `candles` instead of `ticks`, this is handle by library so user do not need to worry
7778
* @param granularity - default to 60, check https://developers.binary.com/api/#ticks_history
7879
* @returns {*|Promise.<TResult>}
7980
*/
@@ -87,8 +88,7 @@ export function getDataForContract(
8788
) {
8889
const getAllData = () =>
8990
getContract()
90-
.then(r => {
91-
const contract = r.proposal_open_contract;
91+
.then(contract => {
9292
const symbol = contract.underlying;
9393
if (contract.tick_count) {
9494
const start = contract.purchase_time;

0 commit comments

Comments
 (0)