用java写一个 股票dmi指标中的adx ,adxrPDI和MDI
时间: 2024-03-17 22:44:47 浏览: 296
好的,以下是用 Java 编写股票 DMI 指标中的 ADX、PDI 和 MDI 计算的代码:
```java
public class DMIIndicator {
// 计算ADX
public static double[] adx(double[] high, double[] low, double[] close, int period) {
double[] tr = new double[high.length];
double[] atr = new double[high.length];
double[] pdi = new double[high.length];
double[] mdi = new double[high.length];
double[] dx = new double[high.length];
double[] adx = new double[high.length];
// 计算TR和ATR
for (int i = 1; i < high.length; i++) {
double h_l = high[i] - low[i];
double h_pc = Math.abs(high[i] - close[i - 1]);
double l_pc = Math.abs(low[i] - close[i - 1]);
tr[i] = Math.max(h_l, Math.max(h_pc, l_pc));
atr[i] = ((period - 1) * atr[i - 1] + tr[i]) / period;
}
// 计算PDI和MDI
for (int i = period + 1; i < high.length; i++) {
double pdm = high[i] - high[i - 1];
double mdm = low[i - 1] - low[i];
pdi[i] = 100 * ((period - 1) * pdi[i - 1] + pdm) / (period * atr[i]);
mdi[i] = 100 * ((period - 1) * mdi[i - 1] + mdm) / (period * atr[i]);
}
// 计算DX
for (int i = 2 * period; i < high.length; i++) {
double di_diff = Math.abs(pdi[i] - mdi[i]);
double di_sum = pdi[i] + mdi[i];
dx[i] = 100 * (period * dx[i - 1] + di_diff / di_sum) / (period + 1);
}
// 计算ADX
for (int i = 2 * period; i < high.length; i++) {
adx[i] = ((period - 1) * adx[i - 1] + dx[i]) / period;
}
return adx;
}
// 计算PDI
public static double[] pdi(double[] high, double[] low, double[] close, int period) {
double[] tr = new double[high.length];
double[] atr = new double[high.length];
double[] pdi = new double[high.length];
// 计算TR和ATR
for (int i = 1; i < high.length; i++) {
double h_l = high[i] - low[i];
double h_pc = Math.abs(high[i] - close[i - 1]);
double l_pc = Math.abs(low[i] - close[i - 1]);
tr[i] = Math.max(h_l, Math.max(h_pc, l_pc));
atr[i] = ((period - 1) * atr[i - 1] + tr[i]) / period;
}
// 计算PDI
for (int i = period + 1; i < high.length; i++) {
double pdm = high[i] - high[i - 1];
pdi[i] = 100 * ((period - 1) * pdi[i - 1] + pdm) / (period * atr[i]);
}
return pdi;
}
// 计算MDI
public static double[] mdi(double[] high, double[] low, double[] close, int period) {
double[] tr = new double[high.length];
double[] atr = new double[high.length];
double[] mdi = new double[high.length];
// 计算TR和ATR
for (int i = 1; i < high.length; i++) {
double h_l = high[i] - low[i];
double h_pc = Math.abs(high[i] - close[i - 1]);
double l_pc = Math.abs(low[i] - close[i - 1]);
tr[i] = Math.max(h_l, Math.max(h_pc, l_pc));
atr[i] = ((period - 1) * atr[i - 1] + tr[i]) / period;
}
// 计算MDI
for (int i = period + 1; i < high.length; i++) {
double mdm = low[i - 1] - low[i];
mdi[i] = 100 * ((period - 1) * mdi[i - 1] + mdm) / (period * atr[i]);
}
return mdi;
}
}
```
使用方法:
```java
double[] high = {10.0, 11.0, 12.0, 13.0, 14.0};
double[] low = {9.0, 10.0, 11.0, 12.0, 13.0};
double[] close = {9.5, 10.5, 11.5, 12.5, 13.5};
int period = 14;
double[] adx = DMIIndicator.adx(high, low, close, period);
double[] pdi = DMIIndicator.pdi(high, low, close, period);
double[] mdi = DMIIndicator.mdi(high, low, close, period);
```
其中,`high`、`low`、`close` 分别是股票的最高价、最低价和收盘价,`period` 是计算周期。`adx`、`pdi` 和 `mdi` 分别是计算出来的 ADX、PDI 和 MDI 数组。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)