@devexperts/dxcharts-lite
Version:
82 lines (81 loc) • 2.44 kB
JavaScript
/*
* Copyright (C) 2019 - 2025 Devexperts Solutions IE Limited
* 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 { hashCode } from '../utils/string.utils';
// Has no specific rule, just any number to use in units calculation.
// 1 - because why not? Easy to debug.
export const BASIC_CANDLE_WIDTH = 1;
export const defaultSortCandles = (candles) => candles.slice().sort((a, b) => (a.timestamp === b.timestamp ? 0 : a.timestamp > b.timestamp ? 1 : -1));
export const generateCandleId = (timestamp, hashValue) => {
return `${timestamp}_${hashCode(hashValue.toString())}`;
};
/**
* Provides the name of candle difference
* @param {Number} open
* @param {Number} close
* @returns {String}
*/
export function nameDirection(open, close) {
if (close === open) {
return 'none';
}
else if (close > open) {
return 'up';
}
else {
return 'down';
}
}
/**
* Provides the name of candle difference for hollow chart type
* @param {Number} open
* @param {Number} close
* @returns {String}
*/
export function hollowDirection(open, close) {
if (close > open) {
return 'up';
}
else {
return 'down';
}
}
/**
* Creates a new Candle object based on the provided base object, with the option to modify the prices.
* @param {Candle} base - The base object to copy.
* @param {number} idx - The index of the new Candle object.
* @param {boolean} pricesAsClose - If true, the high, low, and open prices will be set to the close price.
* @returns {Candle} A new Candle object with the same properties as the base object, with the option to modify the prices.
*/
export function copyCandle(base, idx, pricesAsClose = false) {
const { id, expansion, impVolatility, vwap, typicalPrice, volume, timestamp } = base;
let hi = base.hi;
let lo = base.lo;
let open = base.open;
const close = base.close;
if (pricesAsClose) {
hi = close;
lo = close;
open = close;
}
let _idx = base.idx;
if (idx !== undefined) {
_idx = idx;
}
return {
id,
hi,
lo,
open,
close,
timestamp,
volume,
expansion,
idx: _idx,
impVolatility,
vwap,
typicalPrice,
};
}