@4ex/indicators
Version:
Technical indicators for ohlc charts written in TypeScript
76 lines (75 loc) • 2.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PSAR = void 0;
const fp_1 = require("lodash/fp");
const technicalindicators_1 = require("technicalindicators");
const defaultInput = {
step: 0.02,
max: 0.2,
};
/**
* The parabolic SAR indicator, developed by J. Wells Wilder, is used
* by traders to determine trend direction and potential reversals in price.
* The indicator uses a trailing stop and reverse method called "SAR"
* or stop and reverse, to identify suitable exit and entry points.
* Traders also refer to the indicator as the parabolic stop and reverse,
* parabolic SAR, or PSAR.
* [Investopedia](https://www.investopedia.com/terms/p/parabolicindicator.asp)
*/
class PSAR {
/**
* @param {OHLC[]} series candles series
* @param {number} step step for calculation
* @param {number} max max for calculation
*/
constructor(series = [], step = defaultInput.step, max = defaultInput.max) {
// super();
const high = fp_1.map('high')(series);
const low = fp_1.map('low')(series);
const indicator = new technicalindicators_1.PSAR({ step, max, high, low });
this.indicator = indicator;
this.step = step;
this.max = max;
}
/**
* Calculate PSAR for specified input
* @param {ParabolicStopAndReverseInput} input PSAR input for calculation
* @return {number[]} array representing PSAR values
*/
static calculate(input) {
const { max, step, series } = Object.assign(Object.assign({}, defaultInput), input);
const high = fp_1.map('high')(series);
const low = fp_1.map('low')(series);
return technicalindicators_1.PSAR.calculate({ max, step, high, low });
}
/**
* Generate instance of PSAR indicator
* @param {ParabolicStopAndReverseInput} input PSAR input for generation
* @return {PSAR} instance of indicators
*/
static generator({ max, step, series, }) {
return new PSAR(series, step, max);
}
/**
* get values for PSAR instance data
* @return {number[]} PSAR values
*/
getResults() {
return this.indicator.getResult();
}
/**
* Calc PSAR value for next candle
* @param {OHLC} candle new candle
* @return {number | undefined} next psar value or undefined if period is
* greater than actual series length
*/
next({ low, high }) {
return this.indicator.nextValue({
low: [low],
high: [high],
step: this.step,
max: this.max,
});
}
}
exports.PSAR = PSAR;