From 0af1ea7461a5811801631391a9afcf3645394ac5 Mon Sep 17 00:00:00 2001 From: qingwei Date: Tue, 17 May 2016 09:24:05 +0800 Subject: [PATCH 1/2] Fix --- src/__tests__/custom-test.js | 6 ++++-- src/custom.js | 3 +-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/__tests__/custom-test.js b/src/__tests__/custom-test.js index 4300b21..f7ebf73 100644 --- a/src/__tests__/custom-test.js +++ b/src/__tests__/custom-test.js @@ -16,14 +16,16 @@ describe('custom', () => { it('should get more extra ticks for non-tick-contract', async () => { await liveApi.authorize(token); const nonTickContractID = '8686424368'; - const ticks = await liveApi.getDataForContract(() => liveApi.getContractInfo(nonTickContractID)); + const ticks = await liveApi + .getDataForContract(() => liveApi.getContractInfo(nonTickContractID).then(r => r.proposal_open_contract)); expect(ticks).to.have.lengthOf(451); }); it('should get exact number of ticks for tick-contract', async () => { await liveApi.authorize(token); const tickContractID = '8818581808'; - const ticks = await liveApi.getDataForContract(() => liveApi.getContractInfo(tickContractID)); + const ticks = await liveApi + .getDataForContract(() => liveApi.getContractInfo(tickContractID).then(r => r.proposal_open_contract)); expect(ticks).to.have.lengthOf(8); }); }); diff --git a/src/custom.js b/src/custom.js index 3b894dc..a414abc 100644 --- a/src/custom.js +++ b/src/custom.js @@ -87,8 +87,7 @@ export function getDataForContract( ) { const getAllData = () => getContract() - .then(r => { - const contract = r.proposal_open_contract; + .then(contract => { const symbol = contract.underlying; if (contract.tick_count) { const start = contract.purchase_time; From 5fd3fb85aa8351f83530d9623c0a139ee1569d0c Mon Sep 17 00:00:00 2001 From: qingwei Date: Tue, 17 May 2016 10:19:50 +0800 Subject: [PATCH 2/2] Add candle support on custom api call --- src/__tests__/custom-test.js | 36 ++++++++++++++++++++++++++++++++---- src/custom.js | 17 +++++++++-------- 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/src/__tests__/custom-test.js b/src/__tests__/custom-test.js index f7ebf73..77d10f2 100644 --- a/src/__tests__/custom-test.js +++ b/src/__tests__/custom-test.js @@ -28,11 +28,39 @@ describe('custom', () => { .getDataForContract(() => liveApi.getContractInfo(tickContractID).then(r => r.proposal_open_contract)); expect(ticks).to.have.lengthOf(8); }); + + it('should return candles if user request candles', async () => { + await liveApi.authorize(token); + const nonTickContractID = '8686424368'; + const candles = await liveApi + .getDataForContract( + () => liveApi.getContractInfo(nonTickContractID).then(r => r.proposal_open_contract), + 1, + 'all', + 'candles', + ); + expect(candles).to.have.lengthOf(16); + expect(candles[0]).to.have.keys('open', 'close', 'epoch', 'high', 'low'); + }); }); - it('getDataForSymbol', async () => { - await liveApi.authorize(token); - const ticks = await liveApi.getDataForSymbol('R_100'); - expect(ticks).to.have.length.above(1000); + describe('getDataForSymbol', () => { + it('should get data for specified market', async () => { + await liveApi.authorize(token); + const ticks = await liveApi.getDataForSymbol('R_100'); + expect(ticks).to.have.length.above(1000); + }); + + it('should get data for specified market using given duration params', async () => { + await liveApi.authorize(token); + const ticks = await liveApi.getDataForSymbol('R_100', 1, 'minute'); + expect(ticks).to.have.length.above(29); + }); + + it('should get candles for specified market if requested candles', async () => { + await liveApi.authorize(token); + const ticks = await liveApi.getDataForSymbol('R_100', 1, 'hour', 'candles'); + expect(ticks).to.have.length.above(59); + }); }); }); diff --git a/src/custom.js b/src/custom.js index a414abc..6119356 100644 --- a/src/custom.js +++ b/src/custom.js @@ -16,8 +16,7 @@ const hcUnitConverter = type => { const autoAdjustGetData = (api, symbol, start, end, style = 'ticks', granularity = 60) => { const secs = end - start; const ticksCount = secs / 2; - if (ticksCount >= 5000) { - style = 'candles'; + if (ticksCount >= 5000 || style === 'candles') { const idealGranularity = secs / 4999; granularities.forEach((g, i) => { if (idealGranularity > g && idealGranularity <= granularities[i + 1]) { @@ -31,10 +30,10 @@ const autoAdjustGetData = (api, symbol, start, end, style = 'ticks', granularity end, adjust_start_time: 1, count: 4999, - style, + style: 'candles', granularity, } - ).then(r => ohlcDataToTicks(r.candles)); + ).then(r => style === 'ticks' ? ohlcDataToTicks(r.candles) : r.candles); } return api.getTickHistory(symbol, { @@ -42,7 +41,7 @@ const autoAdjustGetData = (api, symbol, start, end, style = 'ticks', granularity end, adjust_start_time: 1, count: 4999, - style, + style: 'ticks', } ).then(r => { const ticks = r.history.times.map((t, idx) => { @@ -60,11 +59,11 @@ const autoAdjustGetData = (api, symbol, start, end, style = 'ticks', granularity * @param durationCount * @param durationType */ -export function getDataForSymbol(api, symbol, durationCount = 1, durationType = 'all') { +export function getDataForSymbol(api, symbol, durationCount = 1, durationType = 'all', style = 'ticks') { const durationUnit = hcUnitConverter(durationType); const end = nowEpoch(); const start = end - durationToSecs(durationCount, durationUnit); - return autoAdjustGetData(api, symbol, start, end); + return autoAdjustGetData(api, symbol, start, end, style); } /** @@ -73,7 +72,9 @@ export function getDataForSymbol(api, symbol, durationCount = 1, durationType = * @param getContract - function that accept nothing and return a Promise containing contract * @param durationCount - number of duration * @param durationType - type of duration, check http://api.highcharts.com/highstock#rangeSelector.buttons - * @param style - one of ['ticks', 'candles'], check https://developers.binary.com/api/#ticks_history + * @param style - one of ['ticks', 'candles'], this will affect the return data shape, + * internally library might not always use this param when requesting, eg. when data is too large, + * library will use `candles` instead of `ticks`, this is handle by library so user do not need to worry * @param granularity - default to 60, check https://developers.binary.com/api/#ticks_history * @returns {*|Promise.} */