UNPKG

@showr/core

Version:
96 lines (95 loc) 2.93 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Quote = void 0; /** * Creates a quote out of any value. * * create a quote with new Quote<T>. * quote should have value:T * quote might have indicators, attributes. * * new Quote(1) = { value: 1 }, * new Quote({ close: 1 }) = { value: { close: 1 }}, */ class Quote { _value; _indicators; _strategies; /** * Creates a quote after type-casting the given value. * @param value - Any value. * @param symbol - `key` of the value when converted to the object. */ constructor(value) { this._value = this.sanitize(value); this._indicators = {}; this._strategies = {}; } get value() { return this._value; } get indicators() { return this._indicators; } get strategies() { return this._strategies; } sanitize(valueToTransform) { if (typeof valueToTransform === 'number' || typeof valueToTransform === 'bigint' || typeof valueToTransform === 'string' || (valueToTransform && typeof valueToTransform === 'object' && !(valueToTransform instanceof Array))) { return valueToTransform; } else { throw new Error(`Invalid quote value: ${valueToTransform}.`); } } /** * Get value of the given attribute of the quote. * @param attribute - The attribute. * @returns The value of given attribute if exists, `undefined` otherwise. */ getAttribute(attribute) { return this._value[attribute]; } /** * Get quote indicator value by indicator name. * @param indicatorName - Name of the `Indicator`. * @returns Value of indicator if exists, `undefined` otherwise. */ getIndicator(indicatorName) { return this._indicators[indicatorName]; } /** * Set indicator for a quote. * @param indicatorName - Name of the `Indicator`. * @param indicatorValue - Calculated value of the `Indicator`. * @returns self reference. */ setIndicator(indicatorName, indicatorValue) { Object.assign(this._indicators, { [indicatorName]: indicatorValue }); return this; } /** * Get strategy values for the given quote. * @param strategyName - Name of the strategy. * @returns `StrategyPoint` object if strategy exists, `undefined` otherwise. */ getStrategy(strategyName) { return this._strategies[strategyName]; } /** * Set strategy for a quote. * @param strategyName - Name of the `Strategy`. * @param strategyValue - Strategy value. * @returns self reference. */ setStrategy(strategyName, strategyValue) { Object.assign(this._strategies, { [strategyName]: strategyValue }); return this; } } exports.Quote = Quote;