Skip to content

Commit

Permalink
feat: split fake-candles function for studies refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
DeltaZN authored and disyakidneyshot committed Jan 19, 2024
1 parent 294f235 commit 4a31d25
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 73 deletions.
2 changes: 1 addition & 1 deletion src/chart/components/chart/chart-base.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Index, Timestamp } from '../../model/scaling/viewport.model';
import VisualCandle from '../../model/visual-candle';
import { autoDetectPeriod } from '../../utils/auto-period-detector.utils';
import { searchCandleIndex } from '../../utils/candles.utils';
import { fakeVisualCandle, fakeVisualPoint } from './fake-candles';
import { fakeVisualCandle, fakeVisualPoint } from './fake-visual-candle';

export type BaseType = 'candle' | 'point';

Expand Down
75 changes: 4 additions & 71 deletions src/chart/components/chart/fake-candles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,80 +3,13 @@
* 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, nameDirection } from '../../model/candle.model';
import { DataSeriesPoint, VisualSeriesPoint } from '../../model/data-series.model';
import { Index, Pixel, Timestamp, Unit } from '../../model/scaling/viewport.model';
import VisualCandle from '../../model/visual-candle';
import { Candle } 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';
import { round } from '../../utils/math.utils';

const DEFAULT_PERIOD = 60; // 1 minute

/**
* Generates fake candle for left and right "out of data range" zones.
* Requires period to calculate the fake timestamp.
* The timestamp won't take session breaks or holidays into account.
*/
export const fakeVisualCandle = (
dataPoints: Candle[],
visualCandles: VisualCandle[],
candleWidth: number,
index: Index,
period: number = DEFAULT_PERIOD,
): VisualCandle => {
const candle = fakeCandle(dataPoints, index, period);
// in case we have equivolume
candle.volume = candleWidth;
let x: Pixel;
if (visualCandles.length === 0) {
x = 0;
} else if (index >= visualCandles.length) {
const lastCandle = visualCandles[visualCandles.length - 1];
const candlesCountInBetween = index - (visualCandles.length - 1);
const offsetFromLast: Unit = candlesCountInBetween * candleWidth;
x = lastCandle.centerUnit + offsetFromLast;
} else {
const firstCandle = visualCandles[0];
const candlesCountInBetween = -index;
const offsetFromFirst: Unit = candlesCountInBetween * candleWidth;
x = firstCandle.centerUnit - offsetFromFirst;
}
return new VisualCandle(
x,
candleWidth,
candle.open,
candle.close,
candle.lo,
candle.hi,
nameDirection(candle.open, candle.close),
candle,
);
};

export const fakeVisualPoint = (
dataPoints: DataSeriesPoint[],
visualPoints: VisualSeriesPoint[],
meanDataWidth: number,
index: Index,
period: number = DEFAULT_PERIOD,
): VisualSeriesPoint => {
const candle = fakeDataPoint(dataPoints, index, period);
let x: Pixel;
if (visualPoints.length === 0) {
x = 0;
} else if (index >= visualPoints.length) {
const lastCandle = visualPoints[visualPoints.length - 1];
const candlesCountInBetween = index - (visualPoints.length - 1);
const offsetFromLast: Unit = candlesCountInBetween * meanDataWidth;
x = lastCandle.centerUnit + offsetFromLast;
} else {
const firstCandle = visualPoints[0];
const candlesCountInBetween = -index;
const offsetFromFirst: Unit = candlesCountInBetween * meanDataWidth;
x = firstCandle.centerUnit - offsetFromFirst;
}
return new VisualSeriesPoint(x, candle.close);
};
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);
Expand Down
71 changes: 71 additions & 0 deletions src/chart/components/chart/fake-visual-candle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { Candle, nameDirection } from "../../model/candle.model";
import { DataSeriesPoint, VisualSeriesPoint } from "../../model/data-series.model";
import { Index, Pixel, Unit } from "../../model/scaling/viewport.model";
import VisualCandle from "../../model/visual-candle";
import { DEFAULT_PERIOD, fakeCandle, fakeDataPoint } from "./fake-candles";

/**
* Generates fake candle for left and right "out of data range" zones.
* Requires period to calculate the fake timestamp.
* The timestamp won't take session breaks or holidays into account.
*/
export const fakeVisualCandle = (
dataPoints: Candle[],
visualCandles: VisualCandle[],
candleWidth: number,
index: Index,
period: number = DEFAULT_PERIOD,
): VisualCandle => {
const candle = fakeCandle(dataPoints, index, period);
// in case we have equivolume
candle.volume = candleWidth;
let x: Pixel;
if (visualCandles.length === 0) {
x = 0;
} else if (index >= visualCandles.length) {
const lastCandle = visualCandles[visualCandles.length - 1];
const candlesCountInBetween = index - (visualCandles.length - 1);
const offsetFromLast: Unit = candlesCountInBetween * candleWidth;
x = lastCandle.centerUnit + offsetFromLast;
} else {
const firstCandle = visualCandles[0];
const candlesCountInBetween = -index;
const offsetFromFirst: Unit = candlesCountInBetween * candleWidth;
x = firstCandle.centerUnit - offsetFromFirst;
}
return new VisualCandle(
x,
candleWidth,
candle.open,
candle.close,
candle.lo,
candle.hi,
nameDirection(candle.open, candle.close),
candle,
);
};

export const fakeVisualPoint = (
dataPoints: DataSeriesPoint[],
visualPoints: VisualSeriesPoint[],
meanDataWidth: number,
index: Index,
period: number = DEFAULT_PERIOD,
): VisualSeriesPoint => {
const candle = fakeDataPoint(dataPoints, index, period);
let x: Pixel;
if (visualPoints.length === 0) {
x = 0;
} else if (index >= visualPoints.length) {
const lastCandle = visualPoints[visualPoints.length - 1];
const candlesCountInBetween = index - (visualPoints.length - 1);
const offsetFromLast: Unit = candlesCountInBetween * meanDataWidth;
x = lastCandle.centerUnit + offsetFromLast;
} else {
const firstCandle = visualPoints[0];
const candlesCountInBetween = -index;
const offsetFromFirst: Unit = candlesCountInBetween * meanDataWidth;
x = firstCandle.centerUnit - offsetFromFirst;
}
return new VisualSeriesPoint(x, candle.close);
};
2 changes: 1 addition & 1 deletion src/chart/components/x_axis/x-axis-labels.generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { TimeZoneModel } from '../../model/time-zone.model';
import VisualCandle from '../../model/visual-candle';
import { cloneUnsafe, typedEntries_UNSAFE } from '../../utils/object.utils';
import { ChartModel } from '../chart/chart.model';
import { fakeVisualCandle } from '../chart/fake-candles';
import { fakeVisualCandle } from '../chart/fake-visual-candle';
import { NumericAxisLabel } from '../labels_generator/numeric-axis-labels.generator';
import { TimeFormatMatcher } from './time/parser/time-formats-matchers.functions';
import { parseTimeFormatsFromKey } from './time/parser/time-formats-parser.functions';
Expand Down

0 comments on commit 4a31d25

Please sign in to comment.