股票K线图指标之ADX-java源码 android源码示例

ADX:

N:=14;
TR1:=SMA(MAX(MAX(HIGH-LOW,ABS(HIGH-REF(CLOSE,1))),ABS(LOW-REF(CLOSE,1))),N,1);
HD:=HIGH-REF(HIGH,1);
LD:=REF(LOW,1)-LOW;
DMP:=SMA(IF(HD>0 AND HD>LD,HD,0),N,1);
DMM:=SMA(IF(LD>0 AND LD>HD,LD,0),N,1);
PDI:DMP*100/TR1,COLORRED,DOTLINE;
MDI:DMM*100/TR1,COLORGREEN,DOTLINE;
ADX:SMA(ABS(MDI-PDI)/(MDI+PDI)*100,N,1),COLORYELLOW,LINETHICK2;

   /**
     * @param list  数据集合
     * @param cycle adx参数
     * @return
     */
    public static List<KLineObj> getAdxLineDatas(List list, int cycle) {
        List<KLineObj> lineDatas = new ArrayList<KLineObj>();

        List pdiValue = new ArrayList();
        List mdiValue = new ArrayList();
        List adxValue = new ArrayList();
        double TR1 = 0;
        double DMP = 0;
        double DMM = 0;
        double ADX = 0;
        if (cycle >= list.size())
            return null;
        for (int i = cycle - 1; i < list.size(); i++) {
            //前一个数据
            int ref = i - 1;
            if (ref < 0)
                continue;
            KCandleObj item = list.get(i);
            KCandleObj refItem = list.get(ref);
            int m = 1;
            TR1 = countSMA(Math.max(Math.max(item.getHigh() - item.getLow(),
                    Math.abs(item.getHigh() - refItem.getClose())),
                    Math.abs(item.getLow() - refItem.getClose())), cycle, m, TR1);
            double HD = item.getHigh() - refItem.getHigh();
            double LD = refItem.getLow() - item.getLow();
            DMP = countSMA((HD > 0 && HD > LD) ? HD: 0, cycle, 1, DMP);
            DMM = countSMA((LD > 0 && LD > HD) ? LD: 0, cycle, 1, DMM);

            double PDI = 0;
            double MDI = 0;
            if (TR1 != 0) {
                PDI = DMP * 100D / TR1;
                MDI = DMM * 100D / TR1;
            }
            if (MDI + PDI == 0) {
                ADX = 0;
            } else {
                ADX = countSMA(Math.abs(MDI - PDI) / (MDI + PDI) * 100D, cycle, m, ADX);
            }
            pdiValue.add(new KCandleObj(PDI));
            mdiValue.add(new KCandleObj(MDI));
            adxValue.add(new KCandleObj(ADX));

            Log.v(TAG, "PDI="+PDI +" MDI="+MDI+" ADX="+ADX);
        }
        //确保和 传入的list size一致,
        int size = list.size() - pdiValue.size();
        for (int i = 0; i < size; i++) {
            pdiValue.add(0, new KCandleObj());
            mdiValue.add(0, new KCandleObj());
            adxValue.add(0, new KCandleObj());
        }

//        KLineObj line = new KLineObj();
//        line.setLineData(pdiValue);
//        line.setTitle("PDI");
//        line.setLineColor(KParamConfig.COLOR_ADX_01);
//        lineDatas.add(line);
//
//        KLineObj line2 = new KLineObj();
//        line2.setLineData(mdiValue);
//        line2.setTitle("MDI");
//        line2.setLineColor(KParamConfig.COLOR_ADX_02);
//        lineDatas.add(line2);

        KLineObj line3 = new KLineObj();
        line3.setLineData(adxValue);
        line3.setTitle("ADX");
        line3.setLineColor(KParamConfig.COLOR_ADX_03);
        lineDatas.add(line3);


        //加两条固定线 20  50
        KLineObj line20 = new KLineObj();
        line20.setLineColor(KParamConfig.COLOR_ADX_20);
        line20.setLineValue(KParamConfig.VALUE_ADX_01);
        lineDatas.add(line20);


        KLineObj line50 = new KLineObj();
        line50.setLineColor(KParamConfig.COLOR_ADX_50);
        line50.setLineValue(KParamConfig.VALUE_ADX_02);
        lineDatas.add(line50);


        return lineDatas;
    }
分享到:

评论已关闭

  1. dangfm

    算法写的很好