@4ex/indicators
Version:
Technical indicators for ohlc charts written in TypeScript
75 lines (74 loc) • 2.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CCI = void 0;
const fp_1 = require("lodash/fp");
const technicalindicators_1 = require("technicalindicators");
const types_1 = require("../types");
const defaultInput = {
period: 20,
};
/**
* The CCI, or Commodity Channel Index, was developed by Donald Lambert,
* a technical analyst who originally published the indicator in Commodities
* magazine (now Futures) in 1980. Despite its name, the CCI can be used
* in any market and is not just for commodities. The CCI was originally
* developed to spot long-term trend changes but has been adapted by traders
* for use on all markets or timeframes. Trading with multiple timeframes
* provides more buy or sell signals for active traders. Traders often use the
* CCI on the longer-term chart to establish the dominant trend and on the
* shorter-term chart to isolate pullbacks and generate trade signals.
*/
class CCI {
/**
* @param {OHLC[]} series candles series
* @param {number} period period for indicator
*/
constructor(series, period = defaultInput.period) {
// super();
const high = fp_1.map(types_1.OHLCEnum.HIGH)(series);
const low = fp_1.map(types_1.OHLCEnum.LOW)(series);
const close = fp_1.map(types_1.OHLCEnum.CLOSE)(series);
this.indicator = new technicalindicators_1.CCI({ high, low, close, period });
}
/**
* Retrieve CCI values for instance
* @return {number[]} values for instance data
*/
getResults() {
return this.indicator.getResult();
}
/**
* Calculate CCI to next tick
* @param {OHLC} candle new candle to add to series
* @return {number | undefined} next CCI value or undefined if period is
* greater than actual series length
*/
next(candle) {
return this.indicator.nextValue(candle);
}
/**
* Create instance from data
* @param {CommodityChannelIndexInput} input input data
* @return {CCI} CCI instance
*/
static generator({ series, period }) {
return new CCI(series, period);
}
/**
* Get CCI values from input
* @param {CommodityChannelIndexInput} input input data
* @return {number[]} CCI values
*/
static calculate({ series, period, }) {
const high = fp_1.map(types_1.OHLCEnum.HIGH)(series);
const low = fp_1.map(types_1.OHLCEnum.LOW)(series);
const close = fp_1.map(types_1.OHLCEnum.CLOSE)(series);
return technicalindicators_1.CCI.calculate({
high,
low,
close,
period: period !== null && period !== void 0 ? period : defaultInput.period,
});
}
}
exports.CCI = CCI;