Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from cinar/issue-5
Ulcer Index (UI) added. Fixed #5.
- Loading branch information
Showing
5 changed files
with
90 additions
and
2 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,16 @@ | ||
// Copyright (c) 2022 Onur Cinar. All Rights Reserved. | ||
// https://github.com/cinar/indicatorts | ||
|
||
import {roundDigitsAll} from '../../index'; | ||
import {defaultUlcerIndex} from './ulcerIndex'; | ||
|
||
describe('Ulcer Index', () => { | ||
it('should be able to compute UI', () => { | ||
const closings = [9, 11, 7, 10, 8, 7, 7, 8, 10, 9, 5, 4, 6, 7]; | ||
const expected = [0, 0, 20.99, 18.74, 20.73, 24.05, 26.17, 26.31, | ||
24.99, 24.39, 28.49, 32.88, 34.02, 34.19]; | ||
|
||
const actual = defaultUlcerIndex(closings); | ||
expect(roundDigitsAll(2, actual)).toStrictEqual(expected); | ||
}); | ||
}); |
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,45 @@ | ||
// Copyright (c) 2022 Onur Cinar. All Rights Reserved. | ||
// https://github.com/cinar/indicatorts | ||
|
||
import {divide, multiply, multiplyBy, sqrt, substract} from '../../helper/numArray'; | ||
import {mmax} from '../trend/mmax'; | ||
import {sma} from '../trend/sma'; | ||
|
||
/** | ||
* Default period for UI. | ||
*/ | ||
export const UI_DEFAULT_PERIOD = 14; | ||
|
||
/** | ||
* The Ulcer Index (UI) measures downside risk. The index increases in value | ||
* as the price moves farther away from a recent high and falls as the price | ||
* rises to new highs. | ||
* | ||
* High Closings = Max(period, Closings) | ||
* Percentage Drawdown = 100 * ((Closings - High Closings) / High Closings) | ||
* Squared Average = Sma(period, Percent Drawdown * Percent Drawdown) | ||
* Ulcer Index = Sqrt(Squared Average) | ||
* | ||
* @param period window period. | ||
* @param closings closing values. | ||
* @returns ui values. | ||
*/ | ||
export function ulcerIndex(period: number, closings: number[]): number[] { | ||
const highClosings = mmax(period, closings); | ||
const percentageDrawdown = multiplyBy(100, divide(substract(closings, highClosings), highClosings)); | ||
const squaredAverage = sma(period, multiply(percentageDrawdown, percentageDrawdown)); | ||
const ui = sqrt(squaredAverage); | ||
|
||
return ui; | ||
} | ||
|
||
/** | ||
* The default ulcer index with the default period of 14. | ||
* | ||
* @param closings closing values. | ||
* @returns ui values. | ||
*/ | ||
export function defaultUlcerIndex(closings: number[]): number[] { | ||
return ulcerIndex(UI_DEFAULT_PERIOD, closings); | ||
} | ||
|