js k线图kdj的计算方法
时间: 2023-12-07 20:04:23 浏览: 311
android原生 K线图demo
根据提供的引用内容,以下是JavaScript中KDJ指标的计算方法:
```javascript
// 获取KDJ指标
function getKDJ(highs, lows, closes, n = 9, m1 = 3, m2 = 3) {
let k = [];
let d = [];
let j = [];
let rsv = [];
let len = highs.length;
for (let i = 0; i < len; i++) {
let h = highs[i];
let l = lows[i];
let c = closes[i];
let hh = Math.max(...highs.slice(i - n + 1, i + 1));
let ll = Math.min(...lows.slice(i - n + 1, i + 1));
let r = (c - ll) / (hh - ll) * 100;
rsv.push(r);
if (i < n - 1) {
k.push(null);
d.push(null);
j.push(null);
} else if (i === n - 1) {
let sum = rsv.slice(0, n).reduce((a, b) => a + b);
let avg = sum / n;
k.push(avg);
d.push(avg);
j.push(3 * avg - 2 * avg);
} else {
let kValue = (m1 - 1) / m1 * k[i - 1] + 1 / m1 * r;
let dValue = (m2 - 1) / m2 * d[i - 1] + 1 / m2 * kValue;
let jValue = 3 * kValue - 2 * dValue;
k.push(kValue);
d.push(dValue);
j.push(jValue);
}
}
return { k, d, j };
}
```
其中,`highs`、`lows`和`closes`分别为股票的最高价、最低价和收盘价数组,`n`为KDJ指标中的N值,`m1`和`m2`分别为KDJ指标中的M1和M2值。函数返回一个对象,包含K、D、J三个数组,分别为KDJ指标中的K值、D值和J值。
阅读全文