和软件的macD的值进行对比过,数值是一样的,不过我这个版本小数点后面更精确,软件的是四舍五入的。这个版本支持增量更新,更加方便。
使用需要单例模式,如果多并发请自行修改代码。默认是12,26,9
特别注意,网站上的和软件的MACD有时候并不一致,本文的计算结果以软件为准!!!
废话不多说,直接开干!
import JAVA.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Component;
@Component("macdUtils")
public class MacdUtils {
static int fastSet = 12;
static int slowSet = 26;
static int deaSet = 9;
static int emaFastFast = fastSet - 1;
static int emaFastSlow = 2;
static int emaFastBase = fastSet + 1;
static int emaSlowFast = slowSet - 1;
static int emaSlowSlow = 2;
static int emaSlowBase = slowSet + 1;
static int deaFast = deaSet - 1;
static int deaSlow = 2;
static int deaBase = deaSet + 1;
public void init(int fastSetNum, int slowSetNum, int deaSetNum) {
fastSet = fastSetNum;
slowSet = slowSetNum;
deaSet = deaSetNum;
emaFastFast = fastSet - 1;
emaFastSlow = 2;
emaFastBase = fastSet + 1;
emaSlowFast = slowSet - 1;
emaSlowSlow = 2;
emaSlowBase = slowSet + 1;
deaFast = deaSet - 1;
deaSlow = 2;
deaBase = deaSet + 1;
}
public Map<String, BigDecimal> firstMACD(BigDecimal yestodayClose, BigDecimal nowClose) {
Map<String, BigDecimal> macdInfoMap = new HashMap<>();
BigDecimal EMA12Fast = yestodayClose.multiply(BigDecimal.valueOf(emaFastFast)).divide(BigDecimal.valueOf(emaFastBase), 6, RoundingMode.DOWN);
BigDecimal EMA12Slow = nowClose.multiply(BigDecimal.valueOf(emaFastSlow)).divide(BigDecimal.valueOf(emaFastBase), 6, RoundingMode.DOWN);
BigDecimal EMA12 = EMA12Fast.add(EMA12Slow);
BigDecimal EMA26Fast = yestodayClose.multiply(BigDecimal.valueOf(emaSlowFast)).divide(BigDecimal.valueOf(emaSlowBase), 6, RoundingMode.DOWN);
BigDecimal EMA26Slow = nowClose.multiply(BigDecimal.valueOf(emaSlowSlow)).divide(BigDecimal.valueOf(emaSlowBase), 6, RoundingMode.DOWN);
BigDecimal EMA26 = EMA26Fast.add(EMA26Slow);
BigDecimal DIF = EMA12.subtract(EMA26);
BigDecimal DEA = BigDecimal.ZERO.multiply(BigDecimal.valueOf(deaFast)).divide(BigDecimal.valueOf(deaBase), 6, RoundingMode.DOWN)
.add(DIF.multiply(BigDecimal.valueOf(2)).divide(BigDecimal.valueOf(deaBase), 6, RoundingMode.DOWN));
BigDecimal MACD = DIF.subtract(DEA);
macdInfoMap.put("EMA12", EMA12);
macdInfoMap.put("EMA26", EMA26);
macdInfoMap.put("DIF", DIF);
macdInfoMap.put("DEA", DEA);
macdInfoMap.put("MACD", MACD);
return macdInfoMap;
}
public Map<String, BigDecimal> makeMACD(BigDecimal yesterdayEMA12, BigDecimal yesterdayEMA26, BigDecimal yesterdayDEA, BigDecimal nowClose) {
Map<String, BigDecimal> macdInfoMap = new HashMap<>();
BigDecimal EMA12Fast = yesterdayEMA12.multiply(BigDecimal.valueOf(emaFastFast)).divide(BigDecimal.valueOf(emaFastBase), 6, RoundingMode.DOWN);
BigDecimal EMA12Slow = nowClose.multiply(BigDecimal.valueOf(emaFastSlow)).divide(BigDecimal.valueOf(emaFastBase), 6, RoundingMode.DOWN);
BigDecimal EMA12 = EMA12Fast.add(EMA12Slow);
BigDecimal EMA26Fast = yesterdayEMA26.multiply(BigDecimal.valueOf(emaSlowFast)).divide(BigDecimal.valueOf(emaSlowBase), 6, RoundingMode.DOWN);
BigDecimal EMA26Slow = nowClose.multiply(BigDecimal.valueOf(emaSlowSlow)).divide(BigDecimal.valueOf(emaSlowBase), 6, RoundingMode.DOWN);
BigDecimal EMA26 = EMA26Fast.add(EMA26Slow);
BigDecimal DIF = EMA12.subtract(EMA26);
BigDecimal DEA = yesterdayDEA.multiply(BigDecimal.valueOf(deaFast)).divide(BigDecimal.valueOf(deaBase), 6, RoundingMode.DOWN)
.add(DIF.multiply(BigDecimal.valueOf(2)).divide(BigDecimal.valueOf(deaBase), 6, RoundingMode.DOWN));
BigDecimal MACD = DIF.subtract(DEA);
macdInfoMap.put("EMA12", EMA12);
macdInfoMap.put("EMA26", EMA26);
macdInfoMap.put("DIF", DIF);
macdInfoMap.put("DEA", DEA);
macdInfoMap.put("MACD", MACD);
return macdInfoMap;
}
}