Skip to content

Commit

Permalink
bahar/multiple_active-symbols_and_ticks-history_when_starting_a_bot_loop
Browse files Browse the repository at this point in the history
  • Loading branch information
bahar-fs committed Aug 18, 2020
1 parent f751729 commit dee03ce
Showing 1 changed file with 44 additions and 36 deletions.
80 changes: 44 additions & 36 deletions src/botPage/common/TicksService.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,29 @@ export default class TicksService {
this.tickListeners = new Map();
this.ohlcListeners = new Map();
this.subscriptions = new Map();
this.ticks_history_promise = null;
this.active_symbols_promise = null;
this.observe();
}
requestPipSizes() {
if (this.pipSizes) {
return Promise.resolve(this.pipSizes);
}

return new Promise(resolve => {
this.api.getActiveSymbolsBrief().then(r => {
const { active_symbols: symbols } = r;
this.pipSizes = symbols.reduce((accumulator, currSymbol) => {
// eslint-disable-next-line no-param-reassign
accumulator[currSymbol.symbol] = `${currSymbol.pip}`.length - 2;
return accumulator;
}, {});
resolve(this.pipSizes);
if (!this.active_symbols_promise) {
this.active_symbols_promise = new Promise(resolve => {
this.api.getActiveSymbolsBrief().then(r => {
const { active_symbols: symbols } = r;
this.pipSizes = symbols.reduce((accumulator, currSymbol) => {
// eslint-disable-next-line no-param-reassign
accumulator[currSymbol.symbol] = `${currSymbol.pip}`.length - 2;
return accumulator;
}, {});
resolve(this.pipSizes);
});
});
});
}
return this.active_symbols_promise;
}
request(options) {
const { symbol, granularity } = options;
Expand Down Expand Up @@ -208,31 +213,34 @@ export default class TicksService {
requestTicks(options) {
const { symbol, granularity, style } = options;

return new Promise((resolve, reject) => {
doUntilDone(() =>
this.api.getTickHistory(symbol, {
subscribe : 1,
end : 'latest',
count : 1000,
granularity: granularity ? Number(granularity) : undefined,
style,
})
)
.then(r => {
if (style === 'ticks') {
const ticks = historyToTicks(r.history);

this.updateTicksAndCallListeners(symbol, ticks);
resolve(ticks);
} else {
const candles = parseCandles(r.candles);

this.updateCandlesAndCallListeners([symbol, Number(granularity)], candles);

resolve(candles);
}
})
.catch(reject);
});
if (!this.ticks_history_promise) {
this.ticks_history_promise = new Promise((resolve, reject) => {
doUntilDone(() =>
this.api.getTickHistory(symbol, {
subscribe : 1,
end : 'latest',
count : 1000,
granularity: granularity ? Number(granularity) : undefined,
style,
})
)
.then(r => {
if (style === 'ticks') {
const ticks = historyToTicks(r.history);

this.updateTicksAndCallListeners(symbol, ticks);
resolve(ticks);
} else {
const candles = parseCandles(r.candles);

this.updateCandlesAndCallListeners([symbol, Number(granularity)], candles);

resolve(candles);
}
})
.catch(reject);
});
}
return this.ticks_history_promise;
}
}

0 comments on commit dee03ce

Please sign in to comment.