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

/**
     * 获取sar指标参数值
     * @param list 数据集合
     * @param step 参数step 0.02
     * @param maxStep 参数max 0.2
     * @return
     */
    public static List<KLineObj> getSARLineDatas(List list, float step, float maxStep) {
        List<KLineObj> lineDatas = new ArrayList<KLineObj>();

        List sarValue = new ArrayList();
        //记录是否初始化过
        double INIT_VALUE = -100;
        //加速因子
        double af = 0;
        //极值
        double ep = INIT_VALUE;
        //判断是上涨还是下跌  false:下跌
        boolean lasttrend = false;
        double SAR = 0;

        for (int i = 0; i < list.size() - 1; i++) {
            //上一个周期的sar
            double priorSAR = SAR;
            KCandleObj item = list.get(i);
            if (lasttrend) {
                //上涨
                if (ep == INIT_VALUE || ep < item.getHigh()) {
                    //重新初始化值
                    ep = item.getHigh();
                    af = Math.min(af + step, maxStep);
                }
                SAR = priorSAR + af * (ep - priorSAR);
                double lowestPrior2Lows = Math.min(list.get(Math.max(1, i) - 1).getLow(), list.get(i).getLow());
                if (SAR > list.get(i + 1).getLow()) {
                    SAR = ep;
                    //重新初始化值
                    af = 0;
                    ep = INIT_VALUE;
                    lasttrend = !lasttrend;

                } else if (SAR > lowestPrior2Lows) {
                    SAR = lowestPrior2Lows;
                }
            } else {
                if (ep == INIT_VALUE || ep > list.get(i).getLow()) {
                    //重新初始化值
                    ep = list.get(i).getLow();
                    af = Math.min(af + step, maxStep);
                }
                SAR = priorSAR + af * (ep - priorSAR);
                double highestPrior2Highs = Math.max(list.get(Math.max(1, i) - 1).getHigh(), list.get(i).getHigh());
                if (SAR < list.get(i + 1).getHigh()) {
                    SAR = ep;
                    //重新初始化值
                    af = 0;
                    ep = INIT_VALUE;
                    lasttrend = !lasttrend;

                } else if (SAR < highestPrior2Highs) {
                    SAR = highestPrior2Highs;
                }
            }
            sarValue.add(new KCandleObj(SAR));
        }
        //确保和 传入的list size一致,
        int size = list.size() - sarValue.size();
        for (int i = 0; i < size; i++) {
            sarValue.add(0, new KCandleObj());
        }

        KLineObj line = new KLineObj();
        line.setLineData(sarValue);
        line.setTitle("SAR");
        //上涨颜色
        line.setLineColor(KParamConfig.COLOR_SAR_UP);
        //下跌颜色
        line.setLineColor02(KParamConfig.COLOR_SAR_DOWN);
        lineDatas.add(line);

        return lineDatas;
    }
分享到:

评论已关闭

  1. dangfm

    正需要,感谢楼主无私奉献