MACD
- 计算移动平均值(EMA)
12日EMA的算式为
EMA(12)=前一日EMA(12)×11/13+今日收盘价×2/13
26日EMA的算式为
EMA(26)=前一日EMA(26)×25/27+今日收盘价×2/27 - 计算离差值(DIF)
DIF=今日EMA(12)-今日EMA(26) - 计算DIF的9日EMA
根据离差值计算其9日的EMA,即离差平均值,是所求的MACD值。为了不与指标原名相混淆,此值又名DEA或DEM。
今日DEA(MACD)=前一日DEA×8/10+今日DIF×2/10
YKLineEntity * lastEntity = timeArray[number-1];
e.sumClosePx = [dic[@"closePrice"]doubleValue] + lastEntity.sumClosePx;
if (timeArray.count ==12)
{
e.EMA12 = lastEntity.sumClosePx/12;
}
else if (timeArray.count>12)
{
YKLineEntity * lastTwoEntity = timeArray[number-1];
e.EMA12 = (2 * [dic[@"closePrice"]doubleValue] + 11 * lastTwoEntity.EMA12)/13;
}
if (timeArray.count ==26)
{
e.EMA26 = lastEntity.sumClosePx/26;
e.DIF = e.EMA12 - e.EMA26;
e.DEA = 0.2*e.DIF;
e.MACD = (e.DIF - e.DEA)*2;
}
else if (timeArray.count>26)
{
YKLineEntity * lastTwoEntity = timeArray[number-1];
e.EMA26 = (2 * [dic[@"closePrice"]doubleValue] + 25 * lastTwoEntity.EMA26)/27;
e.DIF = e.EMA12 - e.EMA26;
e.DEA = lastTwoEntity.DEA*0.8 + e.DIF*0.2;
e.MACD = (e.DIF - e.DEA)*2;
}
附KDJ算法
- RSV(9)=(今日收盘价-9日内最低价)÷(9日内最高价-9日内最低价)×100
- K(3日)=(当日RSV值+前一日K值)÷3
- D(3日)=(当日K值+前一日D值)÷3
- J=3K-2D