@4ex/indicators
Version:
Technical indicators for ohlc charts written in TypeScript
161 lines (160 loc) • 7.12 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PivotPoints = void 0;
const types_1 = require("../types");
const pivot_1 = require("../levels/pivot");
const fp_1 = require("lodash/fp");
const lodash_1 = require("lodash");
const defaultInput = {
type: types_1.PivotPointsEnum.CLASSIC,
};
/**
* Calculate pivot indicator using classic method
* @param {number} P pivot point
* @param {number} high high of period
* @param {number} low low of period
* @param {number} precision number of digits (default: 5)
* @return {PivotPointsOutput} Pivot Points
*/
const classic = (P, high, low, precision = 5) => {
const R1 = lodash_1.round(2 * P - low, precision);
const R2 = lodash_1.round(P + (high - low), precision);
const R3 = lodash_1.round(P + (high - low) * 2, precision);
const R4 = lodash_1.round(P + (high - low) * 3, precision);
const S1 = lodash_1.round(2 * P - high, precision);
const S2 = lodash_1.round(P - (high - low), precision);
const S3 = lodash_1.round(P - (high - low) * 2, precision);
const S4 = lodash_1.round(P - (high - low) * 3, precision);
return { pivot: P, levels: { R1, R2, R3, R4, S1, S2, S3, S4 } };
};
/**
* Calculate pivot indicator using Woodie's method
* @param {number} P pivot point
* @param {number} high high of period
* @param {number} low low of period
* @param {number} precision number of digits (default: 5)
* @return {PivotPointsOutput} Pivot Points
*/
const woodie = (P, high, low, precision = 5) => {
const R1 = lodash_1.round(2 * P - low, precision);
const R2 = lodash_1.round(P + (high - low), precision);
const R3 = lodash_1.round(high + 2 * (P - low), precision);
const R4 = lodash_1.round(P + 3 * (high - low), precision);
const S1 = lodash_1.round(2 * P - high, precision);
const S2 = lodash_1.round(P - (high - low), precision);
const S3 = lodash_1.round(low - 2 * (high - P), precision);
const S4 = lodash_1.round(P - 3 * (high - low), precision);
return { pivot: P, levels: { R1, R2, R3, R4, S1, S2, S3, S4 } };
};
/**
* Calculate pivot indicator using Camarilla's method
* @param {number} P pivot point
* @param {number} high high of period
* @param {number} low low of period
* @param {number} close close of period
* @param {number} precision number of digits (default: 5)
* @return {PivotPointsOutput} Pivot Points
*/
const camarilla = (P, high, low, close, precision = 5) => {
const R1 = lodash_1.round(close + ((high - low) * 1.1) / 12, precision);
const R2 = lodash_1.round(close + ((high - low) * 1.1) / 6, precision);
const R3 = lodash_1.round(close + ((high - low) * 1.1) / 4, precision);
const R4 = lodash_1.round(close + ((high - low) * 1.1) / 2, precision);
const S1 = lodash_1.round(close - ((high - low) * 1.1) / 12, precision);
const S2 = lodash_1.round(close - ((high - low) * 1.1) / 6, precision);
const S3 = lodash_1.round(close - ((high - low) * 1.1) / 4, precision);
const S4 = lodash_1.round(close - ((high - low) * 1.1) / 2, precision);
return { pivot: P, levels: { R1, R2, R3, R4, S1, S2, S3, S4 } };
};
/**
* Calculate pivot indicator using Fibonacci's method
* @param {number} P pivot point
* @param {number} high high of period
* @param {number} low low of period
* @param {number} precision number of digits (default: 5)
* @return {PivotPointsOutput} Pivot Points
*/
const fibonacci = (P, high, low, precision = 5) => {
const R1 = lodash_1.round(P + 0.382 * (high - low), precision);
const R2 = lodash_1.round(P + 0.618 * (high - low), precision);
const R3 = lodash_1.round(P + 1 * (high - low), precision);
const S1 = lodash_1.round(P - 0.382 * (high - low), precision);
const S2 = lodash_1.round(P - 0.618 * (high - low), precision);
const S3 = lodash_1.round(P - 1 * (high - low), precision);
return { pivot: P, levels: { R1, R2, R3, S1, S2, S3 } };
};
/**
* A forex pivot point is an indicator developed by floor traders in the
* commodities markets to determine potential turning points, also known as
* "pivots." Forex pivot points are calculated to determine levels in which
* the sentiment of the market could change from "bullish" to "bearish."
* Currency traders see pivot points as markers of support and resistance.
* Day traders will use pivot points as a way to determine when market
* sentiment has gone from bullish to bearish or vice versa.
*/
class PivotPoints {
/**
* @param {OHLC[]} series candles series
* @param {PivotPointsEnum} type type of pivot points calculation
* @param {PivotEnum} pivot type of pivot calculation
*/
constructor(series, type = defaultInput.type, pivot) {
// super();
const open = series[0][types_1.OHLCEnum.OPEN];
const high = fp_1.flow(fp_1.map(types_1.OHLCEnum.HIGH), fp_1.max)(series);
const low = fp_1.flow(fp_1.map(types_1.OHLCEnum.LOW), fp_1.min)(series);
const close = series[series.length - 1][types_1.OHLCEnum.CLOSE];
this.reference = { open, high, low, close, volume: 0 };
this.type = type;
this.pivot = pivot_1.pivot(high, open, low, close, pivot);
}
/**
* Retrieve PivotPoints values for instance
* @return {PivotPointsOutput} values for instance data
*/
getResults() {
const { high, low, close } = this.reference;
switch (this.type) {
case types_1.PivotPointsEnum.CAMARILLA:
return camarilla(this.pivot, high, low, close);
case types_1.PivotPointsEnum.WOODIE:
return woodie(this.pivot, high, low);
case types_1.PivotPointsEnum.FIBONACCI:
return fibonacci(this.pivot, high, low);
case types_1.PivotPointsEnum.CLASSIC:
return classic(this.pivot, high, low);
}
}
/**
* Calculate PivotPoints to next tick
* @param {OHLC} candle new candle to add to series
* @return {PivotPointsOutput} next PivotPoints value
*/
next(candle) {
const high = fp_1.flow(fp_1.map(types_1.OHLCEnum.HIGH), fp_1.max)([this.reference, candle]);
const low = fp_1.flow(fp_1.map(types_1.OHLCEnum.LOW), fp_1.min)([this.reference, candle]);
const close = candle[types_1.OHLCEnum.CLOSE];
this.reference = Object.assign(Object.assign({}, this.reference), { high,
low,
close });
return this.getResults();
}
/**
* Create instance from data
* @param {PivotPointsInput} input input data
* @return {PivotPoints} PivotPoints instance
*/
static generator({ series, type, pivot, }) {
return new PivotPoints(series, type, pivot);
}
/**
* Get PivotPoints values from input
* @param {PivotPointsInput} input input data
* @return {PivotPointsOutput} PivotPoints values
*/
static calculate(input) {
const { series, type, pivot } = Object.assign(Object.assign({}, defaultInput), input);
return new PivotPoints(series, type, pivot).getResults();
}
}
exports.PivotPoints = PivotPoints;