diff --git a/README.md b/README.md index 632181f..e570273 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,7 @@ The following list of indicators are currently supported by this package: - [Bollinger Band Width](src/indicator/volatility/index.md#bollinger-band-width) - [Bollinger Bands](src/indicator/volatility/index.md#bollinger-bands) - [Chandelier Exit](src/indicator/volatility/index.md#chandelier-exit) +- [Keltner Channel (KC)](src/indicator/volatility/index.md#keltner-channel-kc) - [Moving Standard Deviation (Std)](src/indicator/volatility/index.md#moving-standard-deviation-std) - [Projection Oscillator (PO)](src/indicator/volatility/index.md#projection-oscillator-po) - [Ulcer Index (UI)](src/indicator/volatility/index.md#ulcer-index-ui) diff --git a/src/indicator/volatility/atr.ts b/src/indicator/volatility/atr.ts index 8b36682..64200d8 100644 --- a/src/indicator/volatility/atr.ts +++ b/src/indicator/volatility/atr.ts @@ -25,7 +25,6 @@ export interface AtrResult { * @param lows low values. * @param closings closing values. * @return atr result.const ATR_PERIOD = 14; - */ export function atr( period: number, diff --git a/src/indicator/volatility/index.md b/src/indicator/volatility/index.md index 015351b..f23487d 100644 --- a/src/indicator/volatility/index.md +++ b/src/indicator/volatility/index.md @@ -7,6 +7,7 @@ Volatility indicators measure the rate of movement regardless of its direction. - [Bollinger Band Width](#bollinger-band-width) - [Bollinger Bands](#bollinger-bands) - [Chandelier Exit](#chandelier-exit) +- [Keltner Channel (KC)](#keltner-channel-kc) - [Moving Standard Deviation (Std)](#moving-standard-deviation-std) - [Projection Oscillator (PO)](#projection-oscillator-po) - [Ulcer Index (UI)](#ulcer-index-ui) @@ -89,6 +90,30 @@ import {chandelierExit} from 'indicatorts'; const result = chandelierExit(highs, lows, closings); ``` +#### Keltner Channel (KC) + +The [keltnerChannel](./keltnerChannel.ts) provides volatility-based bands that are placed on either side of an asset's price and can aid in determining the direction of a trend. + +``` +Middle Line = EMA(period, closings) +Upper Band = EMA(period, closings) + 2 * ATR(period, highs, lows, closings) +Lower Band = EMA(period, closings) - 2 * ATR(period, highs, lows, closings) +``` + +```TypeScript +import {keltnerChannel} from 'indicatorts'; + +const result = keltnerChannel(period, highs, lows, closings); +``` + +The [defaultKeltnerChannel](./keltnerChannel.ts) calculates it with the default period of 20. + +```TypeScript +import {defaultKeltnerChannel} from 'indicatorts'; + +const result = defaultKeltnerChannel(highs, lows, closings); +``` + #### Moving Standard Deviation (Std) The [mstd](./mstd.ts) function calculates the moving standard deviation for a given period. diff --git a/src/indicator/volatility/keltnerChannel.test.ts b/src/indicator/volatility/keltnerChannel.test.ts new file mode 100644 index 0000000..be8efd4 --- /dev/null +++ b/src/indicator/volatility/keltnerChannel.test.ts @@ -0,0 +1,27 @@ +// Copyright (c) 2022 Onur Cinar. All Rights Reserved. +// https://github.com/cinar/indicatorts + +import { roundDigitsAll } from '../../helper/numArray'; +import { defaultKeltnerChannel } from './keltnerChannel'; + +describe('Keltner Channel', () => { + it('should be able to compute KC', () => { + const highs = [10, 9, 12, 14, 12]; + const lows = [6, 7, 9, 12, 10]; + const closings = [9, 11, 7, 10, 8]; + const expectedMiddleLine = [9, 9.19, 8.98, 9.08, 8.98]; + const expectedUpperBand = [17, 17.19, 17.65, 17.58, 17.38]; + const expectedLowerBand = [1, 1.19, 0.32, 0.58, 0.58]; + + const actual = defaultKeltnerChannel(highs, lows, closings); + expect(roundDigitsAll(2, actual.middleLine)).toStrictEqual( + expectedMiddleLine + ); + expect(roundDigitsAll(2, actual.upperBand)).toStrictEqual( + expectedUpperBand + ); + expect(roundDigitsAll(2, actual.lowerBand)).toStrictEqual( + expectedLowerBand + ); + }); +}); diff --git a/src/indicator/volatility/keltnerChannel.ts b/src/indicator/volatility/keltnerChannel.ts new file mode 100644 index 0000000..c315eca --- /dev/null +++ b/src/indicator/volatility/keltnerChannel.ts @@ -0,0 +1,71 @@ +// Copyright (c) 2022 Onur Cinar. All Rights Reserved. +// https://github.com/cinar/indicatorts + +import { add, multiplyBy, substract } from '../../helper/numArray'; +import { ema } from '../trend/ema'; +import { atr } from './atr'; + +/** + * Default period for KC. + */ +export const KC_PERIOD = 20; + +/** + * Keltner channel result object. + */ +export interface KeltnerChannelResult { + middleLine: number[]; + upperBand: number[]; + lowerBand: number[]; +} + +/** + * The Keltner Channel (KC) provides volatility-based bands that are placed + * on either side of an asset's price and can aid in determining the + * direction of a trend. + * + * Middle Line = EMA(period, closings) + * Upper Band = EMA(period, closings) + 2 * ATR(period, highs, lows, closings) + * Lower Band = EMA(period, closings) - 2 * ATR(period, highs, lows, closings) + * + * @param period window period. + * @param highs high values. + * @param lows low values. + * @param closings closing values. + * @returns kc result. + */ +export function keltnerChannel( + period: number, + highs: number[], + lows: number[], + closings: number[] +): KeltnerChannelResult { + const atrResult = atr(period, highs, lows, closings); + const atr2 = multiplyBy(2, atrResult.atrLine); + + const middleLine = ema(period, closings); + const upperBand = add(middleLine, atr2); + const lowerBand = substract(middleLine, atr2); + + return { + middleLine, + upperBand, + lowerBand, + }; +} + +/** + * The default keltner channel with the default period of 20. + * + * @param highs high values. + * @param lows low values. + * @param closings closing values. + * @returns kc result. + */ +export function defaultKeltnerChannel( + highs: number[], + lows: number[], + closings: number[] +): KeltnerChannelResult { + return keltnerChannel(KC_PERIOD, highs, lows, closings); +}