股票K线图指标之MACD程序开发-java源码 android源码示例

MACD公式:
DIFF : EMA(CLOSE,SHORT) - EMA(CLOSE,LONG); 
DEA : EMA(DIFF,M); 
MACD : 2*(DIFF-DEA);//2 为权重

源代码说明:
1、k线的指标值是使用新的List集合来存储的,逻辑为每个点存一个指标的值,
2、KCandleObj 包含开高低收的字段,getNormValue 表示指标的值
3、对于macd指标来说DIFF和DEA为画线逻辑,macd为画矩形逻辑,其中macd的值以0为界点,大于0或者小于0
   如果大于0,矩形高度为0到high,如果小于0,矩形高度为low到0。

java源代码:

/**
 * macd 算出Dif
 * @param list
 * @param x 12
 * @param y 26
 * @return
 */
private static List getDifDatas(List list, int x,
                                            int y) {
    if (list == null || list.size() == 0)
        return null;
    int cycle = Math.max(x, y);

    if (cycle > list.size()) {
        return null;
    }
    List listX = getEmaValueByIndex(list, x);
    List listY = getEmaValueByIndex(list, y);

    List difs = new ArrayList();
    int start = cycle;
    if (start < 1)
        start = 1;
    start--;

    for (int i = 1; i < list.size(); i++) {
        difs.add(new KCandleObj(listX.get(i).getNormValue()
                - listY.get(i).getNormValue()));
    }
    return difs;
}

/**
 * macd 算Dea
 * @param list
 * @param x 12
 * @param y 26
 * @param z 9
 * @return
 */
private static List getDeaDatas(List list, int x,
                                            int y, int z) {

    List difDatas = getDifDatas(list, x, y);
    if (difDatas == null || difDatas.isEmpty())
        return null;

    List datas = new ArrayList();
    double lastEma = difDatas.get(0).getNormValue();
    double lastDEA = 0;
    for (int i = 1; i < difDatas.size(); i++) {
        if (i < z) {
            double currentEma = 2
                    * (difDatas.get(i).getNormValue() - lastEma) / (z + 1)
                    + lastEma;
            lastEma = currentEma;
            if (i == z - 1)
                lastDEA = lastEma;
            continue;
        } else {
            lastDEA = (lastDEA * (z - 1) / (z + 1))
                    + (difDatas.get(i).getNormValue() * 2 / (z + 1));
            datas.add(new KCandleObj(lastDEA));
        }
    }
    return datas;
}

/**
 * 算出指定索引位置的Ema
 * @param list
 * @param n 周期
 * @return
 */
public static List getEmaValueByIndex(List list,
                                                  int n) {

    List lines = new ArrayList();
    if (list == null || list.size() == 0)
        return lines;
    double lastEma = list.get(0).getClose();
    lines.add(new KCandleObj(lastEma));
    for (int i = 1; i < list.size(); i++) {
        double currentEma = 2 * (list.get(i).getClose() - lastEma)
                / (n + 1) + lastEma;
        lastEma = currentEma;
        lines.add(new KCandleObj(lastEma));
    }
    return lines;
}

/**
 * macd的矩形图集合
 * @param list
 * @param x 12
 * @param y 26
 * @param z 9
 * @return
 */
public static List getMacdStickDatas(List list,
                                                 int x, int y, int z) {
    List difList = getDifDatas(list, x, y);

    List deaList = getDeaDatas(list, x, y, z);

    List stickData = new ArrayList();

    if (difList == null || difList.size() == 0)
        return null;
    if (deaList == null || deaList.size() == 0)
        return null;

    double macd = 0;

    if (deaList != null && !deaList.isEmpty()) {
        for (int i = 0; i < deaList.size(); i++) {
            if (i + z >= difList.size()) {
                break;
            }
            macd = difList.get(i + z).getNormValue()
                    - deaList.get(i).getNormValue();
            macd *= 2;
            if (macd > 0) {
                KCandleObj kCandleObj = new KCandleObj();
                kCandleObj.setHigh(macd);
                stickData.add(kCandleObj);
            } else {
                KCandleObj kCandleObj = new KCandleObj();
                kCandleObj.setLow(macd);
                stickData.add(kCandleObj);
            }
        }
    }

    //确保和 传入的list size一致,
    int size = list.size() - stickData.size();
    for (int i = 0; i < size; i++) {
        stickData.add(0, new KCandleObj());
    }
    return stickData;
}
分享到:

评论已关闭