From 3d4d062d249dc04615e6200de33813f9b0afa47b Mon Sep 17 00:00:00 2001 From: qingwei Date: Tue, 31 May 2016 09:29:34 +0800 Subject: [PATCH 1/3] Fix logic error --- src/__tests__/custom-test.js | 2 +- src/custom.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/__tests__/custom-test.js b/src/__tests__/custom-test.js index 12b5523..d9c3266 100644 --- a/src/__tests__/custom-test.js +++ b/src/__tests__/custom-test.js @@ -26,7 +26,7 @@ describe('custom', () => { const tickContractID = '8818581808'; const ticks = await liveApi .getDataForContract(() => liveApi.getContractInfo(tickContractID).then(r => r.proposal_open_contract)); - expect(ticks).to.have.lengthOf(7); + expect(ticks).to.have.lengthOf(11); }); it('should return candles if user request candles', async () => { diff --git a/src/custom.js b/src/custom.js index c92d5f5..398e167 100644 --- a/src/custom.js +++ b/src/custom.js @@ -93,15 +93,15 @@ export function getDataForContract( .then(contract => { const symbol = contract.underlying; if (contract.tick_count) { - const start = +(contract.purchase_time); - const exitTime = +(contract.exit_tick_time); + const start = +(contract.purchase_time) - 5; + const exitTime = +(contract.exit_tick_time) +5; const end = contract.sell_spot ? exitTime : nowEpoch(); return autoAdjustGetData(api, symbol, start, end, style, granularity); } const bufferSize = 0.05; const contractStart = +(contract.purchase_time); - const contractEnd = +(contract.exit_tick_time); + const contractEnd = +(contract.exit_tick_time) || +(contract.date_expiry); const buffer = Math.round((contractEnd - contractStart) * bufferSize); const start = contractStart - buffer; // add 5 minutes buffer const bufferedExitTime = contractEnd + buffer; From 94c6677fcccf4fa70699a1def24f4b371362d6a8 Mon Sep 17 00:00:00 2001 From: qingwei Date: Tue, 31 May 2016 09:41:51 +0800 Subject: [PATCH 2/3] Add test --- src/__tests__/custom-test.js | 19 +++++++++++++++++++ src/custom.js | 4 ++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/__tests__/custom-test.js b/src/__tests__/custom-test.js index d9c3266..a0b9f0f 100644 --- a/src/__tests__/custom-test.js +++ b/src/__tests__/custom-test.js @@ -42,6 +42,25 @@ describe('custom', () => { expect(candles).to.have.lengthOf(6); expect(candles[0]).to.have.keys('open', 'close', 'epoch', 'high', 'low'); }); + + it('should return even if contract does not have end time', async () => { + await liveApi.authorize(token); + const nonTickContractID = '8686424368'; + const candles = await liveApi + .getDataForContract( + () => liveApi.getContractInfo(nonTickContractID).then(r => { + const cloned = Object.assign({}, r.proposal_open_contract); + delete cloned.exit_tick_time; + delete cloned.date_expiry; + return cloned; + }), + 1, + 'all', + 'candles', + ); + expect(candles).to.have.length.above(1000); + expect(candles[0]).to.have.keys('open', 'close', 'epoch', 'high', 'low'); + }); }); describe('getDataForSymbol', () => { diff --git a/src/custom.js b/src/custom.js index 398e167..3d66cf8 100644 --- a/src/custom.js +++ b/src/custom.js @@ -103,9 +103,9 @@ export function getDataForContract( const contractStart = +(contract.purchase_time); const contractEnd = +(contract.exit_tick_time) || +(contract.date_expiry); const buffer = Math.round((contractEnd - contractStart) * bufferSize); - const start = contractStart - buffer; // add 5 minutes buffer + const start = buffer ? contractStart - buffer : contractStart; // add 5 minutes buffer const bufferedExitTime = contractEnd + buffer; - const end = contract.sell_spot ? bufferedExitTime : nowEpoch(); + const end = contractEnd ? bufferedExitTime : nowEpoch(); return autoAdjustGetData(api, symbol, start, end, style, granularity); }); From 07dfa24301ce029fa829aad1a0c2692c1ae1fa6a Mon Sep 17 00:00:00 2001 From: qingwei Date: Tue, 31 May 2016 10:01:41 +0800 Subject: [PATCH 3/3] Lint fix --- src/__tests__/custom-test.js | 4 +++- src/custom.js | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/__tests__/custom-test.js b/src/__tests__/custom-test.js index a0b9f0f..ca0bc68 100644 --- a/src/__tests__/custom-test.js +++ b/src/__tests__/custom-test.js @@ -17,7 +17,9 @@ describe('custom', () => { await liveApi.authorize(token); const nonTickContractID = '8686424368'; const ticks = await liveApi - .getDataForContract(() => liveApi.getContractInfo(nonTickContractID).then(r => r.proposal_open_contract)); + .getDataForContract(() => + liveApi.getContractInfo(nonTickContractID).then(r => r.proposal_open_contract) + ); expect(ticks).to.have.lengthOf(165); }); diff --git a/src/custom.js b/src/custom.js index 3d66cf8..a9e0bac 100644 --- a/src/custom.js +++ b/src/custom.js @@ -94,7 +94,7 @@ export function getDataForContract( const symbol = contract.underlying; if (contract.tick_count) { const start = +(contract.purchase_time) - 5; - const exitTime = +(contract.exit_tick_time) +5; + const exitTime = +(contract.exit_tick_time) + 5; const end = contract.sell_spot ? exitTime : nowEpoch(); return autoAdjustGetData(api, symbol, start, end, style, granularity); }