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

EMA(c,n) * ema=2 * (c – y’) / (n + 1) + y’; //其中Y’表示上一周期的Y值

 

/**
     * EMA(c,n)
     * ema=2 * (c - y') / (n + 1) + y';
     * 其中Y’表示上一周期的Y值
     *
     * @param list
     * @param n
     * @return
     */
    private static KLineObj getEMAData(
            List list, int n, int lineColor) {
        KLineObj line = new KLineObj();

        if (list == null || list.size() == 0)
            return null;

        int cycle = n;
//        if (cycle < 20)
//            cycle = 20;
        if (n > list.size()) {
            return null;
        }

        List datas = new ArrayList();
        double lastEma = list.get(0).getClose();
        for (int i = 1; i < list.size(); i++) {
            if (i <= cycle - 1) {
                double currentEma = 2 * (list.get(i).getClose() - lastEma)
                        / (n + 1) + lastEma;
                lastEma = currentEma;
                datas.add(new KCandleObj(lastEma));
                continue;
            } else {
                lastEma = 2 * (list.get(i).getClose() - lastEma) / (n + 1)
                        + lastEma;
                datas.add(new KCandleObj(lastEma));
            }
        }
        //确保和 传入的list size一致,
        int size = list.size() - datas.size();
        for (int i = 0; i < size; i++) {
            datas.add(0, new KCandleObj());
        }

        line.setLineColor(lineColor);
        line.setLineData(datas);
        line.setTitle("EMA" + n);
        return line;
    }
分享到:

评论已关闭