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

BOLL:
(1)计算MA 
MA=N日内的收盘价之和÷N
(2)计算标准差MD
 MD=平方根N日的(C-MA)的两次方之和除以N
;
     MD =(((c-MA)^2 +前一天的(c-MA)^2+...+n-1)天的(c-MA)^2)/n) 再开平方
     就是Math.sqrt(sum((C-MA)*(C-MA))/  n);
(3)计算MB、UP、DN线
MB=(N-1)日的MA
UP=MB+k×MD 
DN=MB-k×MD

java源码:
/**
 * @param datas 传入的数据集合,要求是升序排列
 * @param T     boll T 一般默认为20
 * @param K     boll K 一般默认为2
 * @return
 */
public static List<KLineObj> getBollData(List datas, int T, int K) {
    if (T < 1 || K < 1)
        return null;
    if (datas == null || datas.isEmpty())
        return null;
    if (T > datas.size())
        return null;
    //使用KLineObj 集合保存画线的对象
    List<KLineObj> linesDatas = new ArrayList<KLineObj>();
    //存储上轨数据
    List zhongList = new ArrayList();
    //存储中轨数据
    List shangList = new ArrayList();
    //存储下轨数据
    List xiaList = new ArrayList();
    //上轨
    KCandleObj shangEntity;
    //中轨
    KCandleObj zhongEntity;
    //下轨
    KCandleObj xiaEntity;

    double standtard = 0;
    double squarSum = 0;

    int cycle = T;

    List sma = countMA(datas, T);
    if (sma == null || sma.size() == 0)
        return null;
    for (int i = cycle - 1; i < datas.size(); i++) {
        double smaValue = sma.get(i - T + 1).getNormValue();

        standtard = 0;

        for (int j = i - T + 1; j <= i; j++) {
            standtard += (datas.get(j).getClose() - smaValue)
                    * (datas.get(j).getClose() - smaValue);
        }
        squarSum = Math.sqrt(standtard / T);

        zhongEntity = new KCandleObj(smaValue);
        shangEntity = new KCandleObj(smaValue + squarSum * K);
        xiaEntity = new KCandleObj(smaValue - squarSum * K);
        zhongList.add(zhongEntity);
        xiaList.add(xiaEntity);
        shangList.add(shangEntity);
    }
    Log.v(TAG, "datas =" + datas.size());
    Log.v(TAG, "zhongList 11=" + zhongList.size());
    //确保和 传入的list size一致,
    int size = datas.size() - shangList.size();
    for (int i = 0; i < size; i++) {
        shangList.add(0, new KCandleObj());
        zhongList.add(0, new KCandleObj());
        xiaList.add(0, new KCandleObj());
    }
    Log.v(TAG, "zhongList 22=" + zhongList.size());
    KLineObj shangLine = new KLineObj();
    shangLine.setDisplay(true);
    shangLine.setLineColor(KParamConfig.COLOR_BOLL_UPPER);
    shangLine.setLineData(shangList);
    shangLine.setTitle("UPPER");//Boll 上轨
    KLineObj zhongLine = new KLineObj();
    zhongLine.setDisplay(true);
    zhongLine.setLineColor(KParamConfig.COLOR_BOLL_MID);
    zhongLine.setLineData(zhongList);
    zhongLine.setTitle("MID");//Boll 中轨
    KLineObj xiaLine = new KLineObj();
    xiaLine.setDisplay(true);
    xiaLine.setLineColor(KParamConfig.COLOR_BOLL_LOWER);
    xiaLine.setLineData(xiaList);
    xiaLine.setTitle("LOWER");//Boll 下轨
    linesDatas.add(shangLine);
    linesDatas.add(zhongLine);
    linesDatas.add(xiaLine);
    return linesDatas;
}
/**
 * 算出ma的值 需要确保和传入的list size一致
 * @param list 数据集合
 * @param days 周期
 * @return   集合数据 
MA=N日内的收盘价之和÷N
 */
public static List countMA(List list, int days) {
    if (days < 1) {
        return null;
    }
    if (list == null || list.size() == 0)
        return null;
    int cycle = days;

    if (cycle > list.size()) {
        //设置的指标参数大于数据集合 不用计算
        return null;
    }

    double sum = 0;

    List ma5Values = new ArrayList();

    for (int i = cycle - 1; i < list.size(); i++) {
        if (i == cycle - 1) {
            for (int j = i - days + 1; j <= i; j++) {
                sum += list.get(j).getClose();
            }
        } else {
            sum = sum + list.get(i).getClose()
                    - list.get(i - days).getClose();
        }
        ma5Values.add(new KCandleObj(sum / days));
    }
    return ma5Values;
}

					
分享到:

评论已关闭