Skip to content

Commit

Permalink
Keltner Channel (KC) fixes issue #3 (#24)
Browse files Browse the repository at this point in the history
* Prettier added.

* Make prettier use .gitignore.

* Prettier added.

* Make prettier use .gitignore.

* Ulcer format.

* Keltner Channel (KC) added.

* FIxed import.
  • Loading branch information
cinar committed Jan 23, 2022
1 parent 7a27ebe commit 09215ff
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion src/indicator/volatility/atr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
25 changes: 25 additions & 0 deletions src/indicator/volatility/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
27 changes: 27 additions & 0 deletions src/indicator/volatility/keltnerChannel.test.ts
Original file line number Diff line number Diff line change
@@ -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
);
});
});
71 changes: 71 additions & 0 deletions src/indicator/volatility/keltnerChannel.ts
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 09215ff

Please sign in to comment.