-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
5 changed files
with
124 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |