-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathchandeForecastOscillator.ts
94 lines (83 loc) · 2.61 KB
/
chandeForecastOscillator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright (c) 2022 Onur Cinar. All Rights Reserved.
// https://github.com/cinar/indicatorts
import {
divide,
generateNumbers,
multiplyBy,
subtract,
} from '../../helper/numArray';
import {
linearRegressionUsingLeastSquare,
movingLinearRegressionUsingLeastSquare,
} from '../../helper/regression';
/**
* Optional configuration of Chande forecast oscillator parameters.
*/
export interface CFOConfig {
period?: number;
}
/**
* The default configuration of Chande forecast oscillator.
*/
export const CFODefaultConfig: Required<CFOConfig> = {
period: 4,
};
/**
* The Chande Forecast Oscillator developed by Tushar Chande The Forecast
* Oscillator plots the percentage difference between the closing price and
* the n-period linear regression forecasted price. The oscillator is above
* zero when the forecast price is greater than the closing price and less
* than zero if it is below.
*
* R = Linreg(Closing)
* CFO = ((Closing - R) / Closing) * 100
*
* @param closings closing values.
* @return cfo values.
*/
export function cfo(closings: number[]): number[] {
const x = generateNumbers(0, closings.length, 1);
const r = linearRegressionUsingLeastSquare(x, closings);
const result = multiplyBy(100, divide(subtract(closings, r), closings));
return result;
}
// Export full name
export { cfo as chandeForecastOscillator };
/**
* Optional configuration of moving Chande forecast oscillator parameters.
*/
export interface MCFOConfig {
period?: number;
}
/**
* The default configuration of moving Chande forecast oscillator.
*/
export const MCFODefaultConfig: Required<MCFOConfig> = {
period: 4,
};
/**
* Moving Chande Forecast Oscillator calculates based on
* the given period.
*
* The Chande Forecast Oscillator developed by Tushar Chande The Forecast
* Oscillator plots the percentage difference between the closing price and
* the n-period linear regression forecasted price. The oscillator is above
* zero when the forecast price is greater than the closing price and less
* than zero if it is below.
*
* R = Linreg(Closing)
* CFO = ((Closing - R) / Closing) * 100
*
* @param closings closing values.
* @param config configuration.
* @return moving cfo.
*/
export function mcfo(closings: number[], config: MCFOConfig = {}): number[] {
const { period } = { ...MCFODefaultConfig, ...config };
const xVal = generateNumbers(0, closings.length, 1);
const rVal = movingLinearRegressionUsingLeastSquare(period, xVal, closings);
const result = multiplyBy(100, divide(subtract(closings, rVal), closings));
return result;
}
// Export full name
export { mcfo as movingChandeForecastOscillator };