package cn.tsxxdw.service.base; import cn.tsxxdw.dto.SharesCciDto; import cn.tsxxdw.dto.SharesQueryDto; import cn.tsxxdw.entity.SharesCciEntity; import cn.tsxxdw.entity.SharesEntity; import cn.tsxxdw.mapper.SharesMapper; import cn.tsxxdw.mybese.service.BaseService; import cn.tsxxdw.other.Where; import cn.tsxxdw.service.mydate.MyDateUtil; import cn.tsxxdw.service.mylog.MyLogUtil; import com.alibaba.fastjson.JSONObject; import com.google.common.collect.Lists; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.LinkedList; import java.util.List; @Slf4j @Service public class SharesCciService extends BaseService<SharesEntity, SharesMapper> { @Autowired private SharesService sharesService; private double calculationCoefficien = 0.015;// 计算系数 private int calculationPeriod = 14; // 初始化数据 public void initCciData() { try { initDataByCode("600238"); } catch (Exception e) { MyLogUtil.logError(e); e.printStackTrace(); } } private List<SharesEntity> getList(String code) throws Exception { SharesQueryDto sharesQueryDto = new SharesQueryDto(); sharesQueryDto.setSharesCode(code); sharesQueryDto.setOrderByAsc("shares_date"); List<SharesEntity> sharesEntityList = sharesService.queryList(sharesQueryDto, Where.useNullSafe(SharesEntity.class).ge("shares_date", "2022-04-01"), false).getData(); return sharesEntityList; } private LinkedList<Double> addLink_14(LinkedList<Double> list, Double e) { if (list.size() == 14) { list.removeFirst(); } list.add(e); return list; } //初始化某一只股票的数据 private void initDataByCode(String code) throws Exception { LinkedList<Double> end_list = new LinkedList<>();// 收盘价 LinkedList<Double> ma_list = new LinkedList<>();//ma LinkedList<Double> tp_list = new LinkedList<>();//tp List<SharesEntity> sharesEntityList = getList(code);// 1 获取某一只股票的历史数据 (2022-04-01 至 2022-06-02) // 2 for循环下,分别获取 每个日期下的 ma,md,tp,cci for (int i = 0; i < sharesEntityList.size(); i++) { SharesEntity sharesEntity = sharesEntityList.get(i); // 获取最小,最大,收盘 三个价格 Double min = sharesEntity.getMinPrice(), max = sharesEntity.getMaxPrice(), end = sharesEntity.getEndPrice(); // TP=(最高价+最低价+收盘价)÷3 double tp = (min + max + end) / 3; addLink_14(tp_list,tp); // 存储最近14天的收盘价 addLink_14(end_list, end); //MA=近N日收盘价的累计之和÷N if (tp_list.size() == 14) { double ma = tp_list.stream().mapToDouble(o -> o).average().getAsDouble(); addLink_14(ma_list, ma); //MD=近N日(MA-tp(n))的绝对值累计之和÷N if (ma_list.size() == 14) { double tempSum = 0; for (int y = 0; y < 14; y++) { double cj = ma - tp_list.get(y); if (cj < 0) cj = -cj; tempSum = tempSum + cj; } double md = tempSum / 14; double cci = (tp - ma) / md / 0.015; //CCI(N日)=(TP-MA)÷MD÷0.015 System.out.println(MyDateUtil.getDateString(sharesEntityList.get(i).getSharesDate()) + "," + cci); } } } System.out.println("cci执行完毕"); } }
股票cci的计算方式(直接上代码,和通信达和东方财富的cci相同的值)
于 2022-06-03 22:58:09 首次发布