股票dmi指标的算法用java实现
时间: 2023-11-22 10:57:03 浏览: 175
DMI指标全称为Directional Movement Index,是一种技术分析工具,用于衡量股票趋势的方向和强度。其算法可以用Java实现,具体步骤如下:
1. 计算真实波幅(True Range):TR = max(H-L, abs(H-PDC), abs(L-PDC),其中H为当日最高价,L为当日最低价,PDC为前一日收盘价。
2. 计算方向线(Directional Line,DI):DI+ = 100 * EMA(+DM)/TR,DI- = 100 * EMA(-DM)/TR,其中+DM为当日上涨的价格变化,-DM为当日下跌的价格变化,EMA为指数移动平均。
3. 计算动向平均数(Average Directional Movement,ADX):ADX = 100 * EMA(DI)/TR,其中DI = abs(DI+ - DI-) / (DI+ + DI-)。
完整的Java代码如下:
```java
public class DMI {
private double[] high, low, close;
public DMI(double[] high, double[] low, double[] close) {
this.high = high;
this.low = low;
this.close = close;
}
public double[] getDIPlus(int n) {
double[] tr = new double[close.length];
double[] dmPlus = new double[close.length];
double[] diPlus = new double[close.length];
for (int i = 1; i < close.length; i++) {
tr[i] = Math.max(high[i] - low[i], Math.abs(high[i] - close[i - 1]));
tr[i] = Math.max(tr[i], Math.abs(low[i] - close[i - 1]));
double moveUp = high[i] - high[i - 1];
double moveDown = low[i - 1] - low[i];
if (moveUp > moveDown && moveUp > 0) {
dmPlus[i] = moveUp;
}
diPlus[i] = 100 * ema(dmPlus, i, n) / tr[i];
}
return diPlus;
}
public double[] getDIMinus(int n) {
double[] tr = new double[close.length];
double[] dmMinus = new double[close.length];
double[] diMinus = new double[close.length];
for (int i = 1; i < close.length; i++) {
tr[i] = Math.max(high[i] - low[i], Math.abs(high[i] - close[i - 1]));
tr[i] = Math.max(tr[i], Math.abs(low[i] - close[i - 1]));
double moveUp = high[i] - high[i - 1];
double moveDown = low[i - 1] - low[i];
if (moveDown > moveUp && moveDown > 0) {
dmMinus[i] = moveDown;
}
diMinus[i] = 100 * ema(dmMinus, i, n) / tr[i];
}
return diMinus;
}
public double[] getADX(int n) {
double[] tr = new double[close.length];
double[] dmPlus = new double[close.length];
double[] dmMinus = new double[close.length];
double[] diPlus = new double[close.length];
double[] diMinus = new double[close.length];
double[] dx = new double[close.length];
double[] adx = new double[close.length];
for (int i = 1; i < close.length; i++) {
tr[i] = Math.max(high[i] - low[i], Math.abs(high[i] - close[i - 1]));
tr[i] = Math.max(tr[i], Math.abs(low[i] - close[i - 1]));
double moveUp = high[i] - high[i - 1];
double moveDown = low[i - 1] - low[i];
if (moveUp > moveDown && moveUp > 0) {
dmPlus[i] = moveUp;
}
if (moveDown > moveUp && moveDown > 0) {
dmMinus[i] = moveDown;
}
diPlus[i] = 100 * ema(dmPlus, i, n) / tr[i];
diMinus[i] = 100 * ema(dmMinus, i, n) / tr[i];
if (diPlus[i] + diMinus[i] != 0) {
dx[i] = 100 * Math.abs(diPlus[i] - diMinus[i]) / (diPlus[i] + diMinus[i]);
}
adx[i] = ema(dx, i, n);
}
return adx;
}
private double ema(double[] values, int day, int n) {
double k = 2.0 / (n + 1);
double ema = values[day];
for (int i = day - 1; i > day - n && i >= 0; i--) {
ema = values[i] * k + ema * (1 - k);
}
return ema;
}
}
```
使用示例:
```java
double[] high = {10, 11, 12, 13, 14, 15};
double[] low = {9, 10, 11, 12, 13, 14};
double[] close = {10.5, 11.5, 12.5, 13.5, 14.5, 15.5};
DMI dmi = new DMI(high, low, close);
double[] diPlus = dmi.getDIPlus(14);
double[] diMinus = dmi.getDIMinus(14);
double[] adx = dmi.getADX(14);
```
其中n为计算EMA的天数,可根据需要进行调整。
阅读全文