Skip to content

Commit

Permalink
feat!: add id to candles
Browse files Browse the repository at this point in the history
  • Loading branch information
Keelaro1 committed Aug 12, 2024
1 parent 2cf721b commit 7bbf47f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
20 changes: 17 additions & 3 deletions src/chart/components/chart/chart.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,18 +448,32 @@ export class ChartComponent extends ChartBaseElement {
this.chartModel.removeCandleByIdx(idx, instrumentSymbol);
}

/**
* Remove candles by ids and recalculate indexes, candles should be as a sequence, follow one by one
* Works faster than removeCandlesByIds
*
* @param ids - candles ids to remove
* @param selectedCandleSeries - candle series to remove candles from
*/
public removeCandlesByIdsSequence(
ids: Candle['id'][],
selectedCandleSeries: CandleSeriesModel = this.chartModel.mainCandleSeries,
) {
this.chartModel.removeCandlesByIdsSequence(ids, selectedCandleSeries);
}

/**
* Remove candles by ids and recalculate indexes
*
* @param ids - candles ids to remove
* @param isSequence - true, if candles follow one by one
* @param selectedCandleSeries - candle series to remove candles from
*/
public removeCandlesById(
public removeCandlesByIds(
ids: Candle['id'][],
isSequence: boolean = false,
selectedCandleSeries: CandleSeriesModel = this.chartModel.mainCandleSeries,
) {
this.chartModel.removeCandlesByIds(ids, isSequence, selectedCandleSeries);
this.chartModel.removeCandlesByIds(ids, selectedCandleSeries);
}

/**
Expand Down
24 changes: 15 additions & 9 deletions src/chart/components/chart/chart.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,7 @@ export class ChartModel extends ChartBaseElement {

/**
* Remove candle by idx and recalculate indexes
*
* @param idx
* @param instrumentSymbol - name of the instrument to update
*/
Expand All @@ -1098,17 +1099,13 @@ export class ChartModel extends ChartBaseElement {
}

/**
* Remove candles by ids and recalculate indexes
* Remove candles by ids and recalculate indexes, candles should be as a sequence, follow one by one
* Works faster than removeCandlesByIds
*
* @param ids - candles ids to remove
* @param selectedCandleSeries - candle series to remove candles from
* @param isSequence - true, if candles follow one by one
*/
public removeCandlesByIds(ids: Candle['id'][], isSequence: boolean, selectedCandleSeries: CandleSeriesModel) {
if (!isSequence) {
ids.forEach(id => this.removeCandleByIdx(this.candleIdxFromId(id, selectedCandleSeries)));
return;
}

public removeCandlesByIdsSequence(ids: Candle['id'][], selectedCandleSeries: CandleSeriesModel) {
const firstIdx = this.candleIdxFromId(ids[0], selectedCandleSeries);
const lastIdx = this.candleIdxFromId(ids[ids.length - 1], selectedCandleSeries);

Expand All @@ -1128,6 +1125,15 @@ export class ChartModel extends ChartBaseElement {
this.candlesChangedRedraw();
}

/**
* Remove candles by ids and recalculate indexes
* @param ids - candles ids to remove
* @param selectedCandleSeries - candle series to remove candles from
*/
public removeCandlesByIds(ids: Candle['id'][], selectedCandleSeries: CandleSeriesModel) {
ids.forEach(id => this.removeCandleByIdx(this.candleIdxFromId(id, selectedCandleSeries)));
}

/**
* Add candles by id and recalculate indexes
*
Expand Down Expand Up @@ -1168,7 +1174,7 @@ export interface UpdateCandlesResult {
}

const sortCandles = (candles: Candle[]): Candle[] =>
candles.slice().sort((a, b) => (a.id === b.id ? 0 : a.timestamp > b.timestamp ? 1 : -1));
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));

Expand Down
4 changes: 2 additions & 2 deletions src/chart/components/chart/fake-candles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
import { Candle } from '../../model/candle.model';
import { Candle, generateCandleId } from '../../model/candle.model';
import { DataSeriesPoint } from '../../model/data-series.model';
import { Index, Timestamp } from '../../model/scaling/viewport.model';
import { firstOf, lastOf } from '../../utils/array.utils';
Expand All @@ -14,7 +14,7 @@ export const DEFAULT_PERIOD = 60; // 1 minute
export const fakeCandle = (candles: Candle[], index: Index, period: number = DEFAULT_PERIOD): Candle => {
const t = getTimestampOfIndex(candles, index, period);
return {
id: '',
id: generateCandleId(t, t),
hi: NaN,
lo: NaN,
open: NaN,
Expand Down
4 changes: 3 additions & 1 deletion src/chart/utils/candles-generator.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
/* eslint-disable */
'use strict';

import { generateCandleId } from '../model/candle.model';

export default function generateCandlesData(config) {
// default generation config
config = config || {};
Expand Down Expand Up @@ -77,7 +79,7 @@ function generateRandomCandle(from, to, candleAvgSize, type) {
var high = Math.max(open, close) + Math.random() * candleAvgSize * 0.2;
var low = Math.min(open, close) - Math.random() * candleAvgSize * 0.2;
return {
id: '',
id: generateCandleId(0, high + low + open + close),
hi: high,
lo: low,
open: open,
Expand Down

0 comments on commit 7bbf47f

Please sign in to comment.