Skip to content

Commit

Permalink
Pull request #5292: Feature/DXCF-5587 dxc 289 switching to using cand…
Browse files Browse the repository at this point in the history
…le identifiers replace binary search by search by

Merge in DXCHARTS/dxchart5 from feature/DXCF-5587-dxc-289-switching-to-using-candle-identifiers-replace-binary-search-by-search-by to master

* commit 'c78e9346a440f9a85a5031d5f28ef5e6427da241':
  [DXCF-5587] [DXC-289] Switching to using candle identifiers - replace binary search by search by id using different data structure // pr fix
  [DXCF-5587] [DXC-289] Switching to using candle identifiers - replace binary search by search by id using different data structure // pr fix
  [DXCF-5587] [DXC-289] Switching to using candle identifiers - replace binary search by search by id using different data structure // init
  [DXCF-5587] [DXC-289] Switching to using candle identifiers - replace binary search by search by id using different data structure // init
  [DXCF-5587] [DXC-289] Switching to using candle identifiers - replace binary search by search by id using different data structure // init

GitOrigin-RevId: 51c45dc61536f79765e276952f1c454672d613c7
  • Loading branch information
Keelaro1 authored and dxcity committed Nov 19, 2024
1 parent 10070c8 commit 5d22720
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/chart/chart.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { DrawerType } from './drawers/drawing-manager';
import { DateTimeFormatter, TimeFormatterConfig } from './model/date-time.formatter';
import { DEFAULT_MERGE_OPTIONS, merge, MergeOptions } from './utils/merge.utils';
import { DeepPartial } from './utils/object.utils';
import { Candle, defaultSortCandles } from './model/candle.model';

export const MAIN_FONT = 'Open Sans Semibold, sans-serif';

Expand Down Expand Up @@ -109,6 +110,7 @@ export const getDefaultConfig = (): FullChartConfig => ({
histogram: {
barCapSize: 1,
},
sortCandles: defaultSortCandles,
},
yAxis: {
type: 'regular',
Expand Down Expand Up @@ -977,6 +979,8 @@ export interface ChartConfigComponentsChart {
selectedWidth: number;
minCandlesOffset: number;
histogram: ChartConfigComponentsHistogram;
// optional because backward compability
sortCandles?: (candles: Candle[]) => Candle[];
}

export interface ChartConfigComponentsEvents {
Expand Down
20 changes: 11 additions & 9 deletions src/chart/components/chart/chart.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ export class ChartModel extends ChartBaseElement {
instrument: ChartInstrument = this.mainCandleSeries.instrument,
recalculateAndUpdate = true,
): CandleSeriesModel | undefined {
const preparedCandles = prepareCandles(candles);
const preparedCandles = this.prepareCandles(candles);
// set correct indexes based on main candles timestamp
const reindexCandles = this.reindexCandlesBasedOnSeries(this.mainCandleSeries.dataPoints, preparedCandles);
// ensure there are no gaps in new candles
Expand Down Expand Up @@ -270,7 +270,7 @@ export class ChartModel extends ChartBaseElement {
this.mainInstrumentChangedSubject.next(mainSeries.instrument);
}
this.rememberCurrentTimeframe();
const preparedCandles = prepareCandles(mainSeries.candles);
const preparedCandles = this.prepareCandles(mainSeries.candles);
this.mainCandleSeries.clearData();
reindexCandles(preparedCandles);
this.mainCandleSeries.dataPoints = preparedCandles;
Expand Down Expand Up @@ -364,15 +364,15 @@ export class ChartModel extends ChartBaseElement {
return;
}

const preparedCandles = prepareCandles(mainSeries.candles);
const preparedCandles = this.prepareCandles(mainSeries.candles);
const updateResult = updateCandles(this.mainCandleSeries.dataPoints, preparedCandles);
const updatedCandles = updateResult.candles;
reindexCandles(updatedCandles);
this.mainCandleSeries.dataPoints = updatedCandles;

// re-create series
secondarySeries.map(series => {
const preparedCandles = prepareCandles(series.candles);
const preparedCandles = this.prepareCandles(series.candles);
const updatedCandles = updateCandles(
this.findSecondarySeriesBySymbol(series.instrument?.symbol ?? '')?.dataPoints ?? [],
preparedCandles,
Expand Down Expand Up @@ -1170,6 +1170,13 @@ export class ChartModel extends ChartBaseElement {
this.candlesUpdatedSubject.next();
this.canvasModel.fireDraw();
}

private prepareCandles = (candles: PartialCandle[]): Candle[] => {
const prepared = candles.map(prepareCandle).filter(isCandle);
const { sortCandles } = this.config.components.chart;

return sortCandles ? sortCandles(prepared) : prepared;
};
}

export interface UpdateCandlesResult {
Expand All @@ -1178,11 +1185,6 @@ export interface UpdateCandlesResult {
candles: Candle[];
}

const sortCandles = (candles: Candle[]): Candle[] =>
candles.slice().sort((a, b) => (a.timestamp === b.timestamp ? 0 : a.timestamp > b.timestamp ? 1 : -1));

const prepareCandles = (candles: PartialCandle[]): Candle[] => sortCandles(candles.map(prepareCandle).filter(isCandle));

const findFirstNotEmptyCandle = (candles: Array<Candle>, startIdx: number, iterateStep: number): Candle | undefined => {
if (startIdx >= candles.length) {
return candles[candles.length - 1];
Expand Down
3 changes: 3 additions & 0 deletions src/chart/model/candle.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ export interface Candle {
readonly vwap?: number;
}

export const defaultSortCandles = (candles: Candle[]): Candle[] =>
candles.slice().sort((a, b) => (a.timestamp === b.timestamp ? 0 : a.timestamp > b.timestamp ? 1 : -1));

export const generateCandleId = (timestamp: number, hashValue: number | string): string => {
return `${timestamp}_${hashCode(hashValue.toString())}`;
};
Expand Down

0 comments on commit 5d22720

Please sign in to comment.