| | |
| | | continue; // 无法获取真实数据,跳过 |
| | | } |
| | | |
| | | // 获取真实价格(从Redis获取,不经过StockSetting计算) |
| | | String s = RedisKeyUtil.doPost(stock.getStockCode(), stock.getStockType()); |
| | | if (s == null) { |
| | | continue; // 无法获取真实价格,跳过 |
| | | } |
| | | |
| | | Map<String, Object> stringObjectMap = RedisKeyUtil.jsonToMap(s); |
| | | BigDecimal realPrice = new BigDecimal(stringObjectMap.get("Last").toString()); |
| | | // 获取当前价格(last) |
| | | BigDecimal last = new BigDecimal(realTimeStock.getLast()); |
| | | |
| | | // 计算盘前价格 |
| | | BigDecimal premarketPrice; |
| | |
| | | // type=0: 直接指定价格 |
| | | premarketPrice = new BigDecimal(setting.getPrice()); |
| | | } else { |
| | | // type=1: 百分比调价格,基于真实价格计算 |
| | | premarketPrice = realPrice.multiply(new BigDecimal(setting.getPrice())); |
| | | // type=1: 百分比调价格,基于last计算 |
| | | premarketPrice = last.multiply(new BigDecimal(setting.getPrice())); |
| | | } |
| | | |
| | | // 获取前收盘价 |
| | | BigDecimal prevClose = new BigDecimal(realTimeStock.getPc()); |
| | | // 获取真实涨跌幅(相对于前收盘价的涨跌幅,getPcp()返回的是百分比数值,需要除以100转为小数) |
| | | BigDecimal realHcrate = new BigDecimal(realTimeStock.getPcp()) |
| | | .divide(new BigDecimal("100"), 4, RoundingMode.HALF_UP); |
| | | |
| | | // 基于盘前价格计算涨跌幅 = (盘前价格 - 前收盘价) / 前收盘价 |
| | | BigDecimal hcrate = BigDecimal.ZERO; |
| | | if (prevClose.compareTo(BigDecimal.ZERO) > 0) { |
| | | hcrate = premarketPrice.subtract(prevClose) |
| | | .divide(prevClose, 4, RoundingMode.HALF_UP); |
| | | // 计算盘前价格相对于last的变化率 = (盘前价格 - last) / last |
| | | BigDecimal premarketChangeRate = BigDecimal.ZERO; |
| | | if (last.compareTo(BigDecimal.ZERO) > 0) { |
| | | premarketChangeRate = premarketPrice.subtract(last) |
| | | .divide(last, 4, RoundingMode.HALF_UP); |
| | | } |
| | | |
| | | // 最终涨跌幅 = 真实涨跌幅 + 盘前价格相对于last的变化率 |
| | | BigDecimal hcrate = realHcrate.add(premarketChangeRate); |
| | | |
| | | // 格式化涨跌幅为带百分号的字符串 |
| | | String hcrateP = hcrate.multiply(new BigDecimal("100")) |
| | |
| | | |
| | | PremarketStockVO vo = new PremarketStockVO(); |
| | | vo.setCode(setting.getStockCode()); |
| | | vo.setPrice(premarketPrice); |
| | | vo.setPrice(last); // price对应last |
| | | vo.setHcrate(hcrate); |
| | | vo.setHcrateP(hcrateP); |
| | | resultList.add(vo); |