Skip to content
This repository was archived by the owner on Feb 22, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 36 additions & 6 deletions src/__tests__/custom-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,51 @@ 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);
});

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);
});
});
});
20 changes: 10 additions & 10 deletions src/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand All @@ -31,18 +30,18 @@ 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,
{
start,
end,
adjust_start_time: 1,
count: 4999,
style,
style: 'ticks',
}
).then(r => {
const ticks = r.history.times.map((t, idx) => {
Expand All @@ -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);
}

/**
Expand All @@ -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.<TResult>}
*/
Expand All @@ -87,8 +88,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;
Expand Down