package com.nq.utils.stock.sina; import com.alibaba.fastjson2.JSONObject; import com.nq.common.ServerResponse; import com.nq.pojo.*; import com.nq.utils.http.HttpClientRequest; import com.nq.utils.PropertiesUtil; import com.nq.utils.redis.JsonUtil; import com.nq.utils.redis.RedisKeyUtil; import com.nq.utils.stock.sina.vo.SinaStockMinData; import com.nq.vo.stock.StockListVO; import com.nq.vo.stock.StockUsVO; import com.nq.vo.stock.StockVO; import com.nq.vo.stock.k.MinDataVO; import com.nq.vo.stock.k.echarts.EchartsDataVO; import java.math.BigDecimal; import java.util.*; import org.apache.commons.lang3.StringUtils; import org.codehaus.jackson.type.TypeReference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class StockApi { public static final String sina_url = PropertiesUtil.getProperty("sina.single.stock.url"); private static final Logger log = LoggerFactory.getLogger(StockApi.class); public static List getStockReailTimes(List stocks) { if (stocks.size() == 0) { return null; } List stockListVOs = new ArrayList<>(); try { for (int i = 0; i < stocks.size(); i++) { Stock stock = stocks.get(i); StockRealTimeBean realTimeStock = RedisKeyUtil.getCacheRealTimeStock(stock); StockListVO stockListVO = new StockListVO(); stockListVO.setHcrate(new BigDecimal(realTimeStock.getPcp())); if(realTimeStock.getPcp().contains("-")){ stockListVO.setColor(0); }else{ stockListVO.setColor(1); } stockListVO.setNowPrice(realTimeStock.getLast()); stockListVO.setToday_max(realTimeStock.getHigh()); stockListVO.setToday_min(realTimeStock.getLow()); stockListVO.setOpen_px(realTimeStock.getBid()); stockListVO.setPreclose_px(realTimeStock.getPc()); stockListVO.setBusiness_amount(realTimeStock.getAsk()); stockListVO.setBusiness_balance(""); stockListVO.setName(stock.getStockName()); stockListVO.setCode(stock.getStockCode()); stockListVO.setSpell(stock.getStockSpell()); stockListVO.setGid(stock.getStockGid().toUpperCase()); stockListVO.setStock_plate(stock.getStockPlate()); stockListVO.setStock_type(stock.getStockType()); stockListVOs.add(stockListVO); } } catch (Exception e) { e.printStackTrace(); return null; } return stockListVOs; } /** * 对接印度股票 */ public static StockListVO getStockRealTime(Stock stock) { if (stock == null) { return null; } StockListVO stockListVO = new StockListVO(); try { StockRealTimeBean realTimeStock = RedisKeyUtil.getCacheRealTimeStock(stock); stockListVO.setHcrate(new BigDecimal(realTimeStock.getPcp().replace("%", ""))); stockListVO.setNowPrice(realTimeStock.getLast()); stockListVO.setToday_max(realTimeStock.getHigh()); stockListVO.setToday_min(realTimeStock.getLow()); stockListVO.setOpen_px(realTimeStock.getBid()); stockListVO.setPreclose_px(realTimeStock.getPc()); stockListVO.setBusiness_amount(realTimeStock.getAsk()); stockListVO.setBusiness_balance(""); stockListVO.setName(stock.getStockName()); stockListVO.setCode(stock.getStockCode()); stockListVO.setSpell(stock.getStockSpell()); stockListVO.setGid(stock.getStockGid().toUpperCase()); stockListVO.setStock_plate(stock.getStockPlate()); stockListVO.setStock_type(stock.getStockType()); } catch (Exception e) { e.printStackTrace(); return null; } return stockListVO; } public static StockVO assembleInStockVO(Stock stock) { StockVO stockListVO = new StockVO(); if (stock == null) { return null; } try { StockRealTimeBean qhBean = RedisKeyUtil.getCacheRealTimeStock(stock); if(!qhBean.getPcp().endsWith("%")){ stockListVO.setHcrate(qhBean.getPcp()+"%"); }else{ stockListVO.setHcrate(qhBean.getPcp()); } if(qhBean.getPcp().startsWith("-")){ stockListVO.setColor("rgb(208, 75, 100)"); }else{ stockListVO.setColor("rgb(3, 173, 143)"); } stockListVO.setNowPrice(qhBean.getLast()); stockListVO.setToday_max(qhBean.getHigh()); stockListVO.setToday_min(qhBean.getLow()); stockListVO.setOpen_px(qhBean.getBid()); stockListVO.setPreclose_px(qhBean.getLast_close()); stockListVO.setBusiness_amount(qhBean.getAsk()); stockListVO.setBusiness_balance(""); } catch (Exception e) { e.printStackTrace(); return null; } return stockListVO; } public static StockListVO otherStockListVO(String Result) { StockListVO stockListVO = new StockListVO(); JSONObject jsonObject = JSONObject.parseObject(Result); stockListVO.setName(jsonObject.getString("f14")); stockListVO.setNowPrice(!Objects.equals(jsonObject.getString("f2"), "-") ? jsonObject.getString("f2") : "0"); stockListVO.setHcrate(new BigDecimal(!Objects.equals(jsonObject.getString("f3"), "-") ? jsonObject.getString("f3") : "0")); stockListVO.setToday_max(!Objects.equals(jsonObject.getString("f15"), "-") ? jsonObject.getString("f15") : "0"); stockListVO.setToday_min(!Objects.equals(jsonObject.getString("f16"), "-") ? jsonObject.getString("f16") : "0"); stockListVO.setBusiness_amount(!Objects.equals(jsonObject.getString("f6"), "-") ? jsonObject.getString("f6") : "0"); stockListVO.setBusiness_balance(!Objects.equals(jsonObject.getString("f5"), "-") ? jsonObject.getString("f5") : "0"); stockListVO.setPreclose_px(!Objects.equals(jsonObject.getString("f18"), "-") ? jsonObject.getString("f18") : "0"); stockListVO.setOpen_px(!Objects.equals(jsonObject.getString("f17"), "-") ? jsonObject.getString("f17") : "0"); stockListVO.setType(jsonObject.getString("f13")); return stockListVO; } //us 数据转换 public static StockUsVO otherUsStockVO(String Result) { StockUsVO stockVO = new StockUsVO(); //判断是否是 JSONObject jsonObject = JSONObject.parseObject(Result); stockVO.setName(jsonObject.getString("f14")); stockVO.setNowPrice(jsonObject.getString("f2") != "-" ? jsonObject.getString("f2") : "0"); stockVO.setHcrate(new BigDecimal(!Objects.equals(jsonObject.getString("f3"), "-") ? jsonObject.getString("f3") : "0")); stockVO.setToday_max(jsonObject.getString("f15") != "-" ? jsonObject.getString("f15") : "0"); stockVO.setToday_min(jsonObject.getString("f16") != "-" ? jsonObject.getString("f16") : "0"); stockVO.setBusiness_amount(jsonObject.getString("f6") != "-" ? jsonObject.getString("f6") : "0"); stockVO.setBusiness_balance(jsonObject.getString("f5") != "-" ? jsonObject.getString("f5") : "0"); stockVO.setPreclose_px(jsonObject.getString("f18") != "-" ? jsonObject.getString("f18") : "0"); stockVO.setOpen_px(jsonObject.getString("f17") != "-" ? jsonObject.getString("f17") : "0"); stockVO.setType(jsonObject.getString("f13") != "-" ? jsonObject.getString("f13") : "0"); return stockVO; } public static ServerResponse getStockMinK(Stock stock, int time, int ma, int size) { int maxSize = Integer.parseInt(PropertiesUtil.getProperty("sina.k.min.max.size")); if (size > maxSize) { size = maxSize; } String minUrl = PropertiesUtil.getProperty("sina.k.min.url"); minUrl = minUrl + "?symbol=" + stock.getStockGid() + "&scale=" + time + "&ma=" + ma + "&datalen=" + size; String hqstr = ""; try { hqstr = HttpClientRequest.doGet(minUrl); } catch (Exception e) { log.error("获取股票K线分时图出错,错误信息 = {}", e); } log.info(" time = {} ma = {} size = {}", new Object[]{Integer.valueOf(time), Integer.valueOf(ma), Integer.valueOf(size)}); hqstr = hqstr.replace("day", "\"day\"").replace("open", "\"open\"").replace("high", "\"high\"").replace("low", "\"low\"").replace("close", "\"close\""); if (ma == 5) { hqstr = hqstr.replace("ma_volume5", "\"ma_volume\"").replace(",volume", ",\"volume\"").replace("ma_price5", "\"ma_price\""); } else if (ma == 10) { hqstr = hqstr.replace("ma_volume10", "\"ma_volume\"").replace(",volume", ",\"volume\"").replace("ma_price10", "\"ma_price\""); } else if (ma == 15) { hqstr = hqstr.replace("ma_volume15", "\"ma_volume\"").replace(",volume", ",\"volume\"").replace("ma_price15", "\"ma_price\""); } else { return ServerResponse.createByErrorMsg("ma 取值 5,10,15"); } if (StringUtils.isBlank(hqstr)) { return ServerResponse.createByErrorMsg("没有查询到行情数据"); } MinDataVO minDataVO = new MinDataVO(); minDataVO.setStockName(stock.getStockName()); minDataVO.setStockCode(stock.getStockCode()); minDataVO.setGid(stock.getStockGid()); hqstr = hqstr.replaceAll("\"\"", "\""); List list = (List) JsonUtil.string2Obj(hqstr, new TypeReference>() { }); log.info("需要查询的行情size为: {}", Integer.valueOf(size)); minDataVO.setData(list); return ServerResponse.createBySuccess(minDataVO); } /*期货分时-k线 * stock:期货代码 * time:5、15、30、60,单位分钟 * */ public static ServerResponse getFuturesMinK(StockFutures stock, int time, int size) { String minUrl = PropertiesUtil.getProperty("sina.futures.k.min.url").replace("{code}", stock.getFuturesCode()).replace("{time}", String.valueOf(time)); String stamp = String.valueOf(new Date().getTime());// new Date()为获取当前系统时间 minUrl = minUrl.replace("{stamp}", stamp); String hqstr = ""; try { hqstr = HttpClientRequest.doGet(minUrl); } catch (Exception e) { log.error("获取股票K线分时图出错,错误信息 = {}", e); } log.info("期货分时 - time = {} ", time); hqstr = hqstr.split("\\[")[1].replace("]);", ""); hqstr = hqstr.replace("d", "\"day\""); hqstr = hqstr.replace("o", "\"open\""); hqstr = hqstr.replace("h", "\"high\""); hqstr = hqstr.replace("l", "\"low\""); hqstr = hqstr.replace("c", "\"close\""); hqstr = hqstr.replace("v", "\"volume\""); if (StringUtils.isBlank(hqstr)) { return ServerResponse.createByErrorMsg("没有查询到行情数据"); } MinDataVO minDataVO = new MinDataVO(); minDataVO.setStockName(stock.getFuturesName()); minDataVO.setStockCode(stock.getFuturesCode()); minDataVO.setGid(stock.getFuturesGid()); hqstr = hqstr.replaceAll("\"\"", "\""); hqstr = "[" + hqstr + "]"; List list = (List) JsonUtil.string2Obj(hqstr, new TypeReference>() { }); //int size = Integer.valueOf(PropertiesUtil.getProperty("sina.futures.k.min.max.size")); if (list.size() > size) { list = list.subList((list.size() - size - 1), list.size()); } minDataVO.setData(list); return ServerResponse.createBySuccess(minDataVO); } public static EchartsDataVO assembleEchartsDataVO(MinDataVO minDataVO) { EchartsDataVO echartsDataVO = new EchartsDataVO(); echartsDataVO.setStockName(minDataVO.getStockName()); echartsDataVO.setStockCode(minDataVO.getStockCode()); List minDataList = minDataVO.getData(); double[][] values = (double[][]) null; Object[][] volumes = (Object[][]) null; String[] date = null; if (minDataList.size() > 0) { values = new double[minDataList.size()][5]; volumes = new Object[minDataList.size()][3]; date = new String[minDataList.size()]; for (int i = 0; i < minDataList.size(); i++) { SinaStockMinData sinaStockMinData = (SinaStockMinData) minDataList.get(i); for (int j = 0; j < values[i].length; j++) { values[i][0] = Double.valueOf(sinaStockMinData.getOpen()).doubleValue(); values[i][1] = Double.valueOf(sinaStockMinData.getClose()).doubleValue(); values[i][2] = Double.valueOf(sinaStockMinData.getLow()).doubleValue(); values[i][3] = Double.valueOf(sinaStockMinData.getHigh()).doubleValue(); values[i][4] = Double.valueOf(sinaStockMinData.getVolume()).doubleValue(); } for (int k = 0; k < volumes[i].length; k++) { volumes[i][0] = Integer.valueOf(i); volumes[i][1] = Double.valueOf(sinaStockMinData.getVolume()); if ((new BigDecimal(sinaStockMinData.getClose())) .compareTo(new BigDecimal(sinaStockMinData.getOpen())) == 1) { volumes[i][2] = Integer.valueOf(1); } else { volumes[i][2] = Integer.valueOf(-1); } } date[i] = sinaStockMinData.getDay(); } } echartsDataVO.setValues(values); echartsDataVO.setVolumes(volumes); echartsDataVO.setDate(date); return echartsDataVO; } /*股票日线*/ public static ServerResponse getStockDayK(Stock stock, int time, int ma, int size) { int maxSize = Integer.parseInt(PropertiesUtil.getProperty("sina.k.min.max.size")); if (size > maxSize) { size = maxSize; } String minUrl = PropertiesUtil.getProperty("sina.k.min.url"); minUrl = minUrl + "?symbol=" + stock.getStockGid() + "&scale=" + time + "&ma=" + ma + "&datalen=" + size; String hqstr = ""; try { hqstr = HttpClientRequest.doGet(minUrl); } catch (Exception e) { log.error("获取股票K线分时图出错,错误信息 = {}", e); } log.info(" time = {} ma = {} size = {}", new Object[]{Integer.valueOf(time), Integer.valueOf(ma), Integer.valueOf(size)}); hqstr = hqstr.replace("day", "\"day\"").replace("open", "\"open\"").replace("high", "\"high\"").replace("low", "\"low\"").replace("close", "\"close\""); if (ma == 5) { hqstr = hqstr.replace("ma_volume5", "\"ma_volume\"").replace(",volume", ",\"volume\"").replace("ma_price5", "\"ma_price\""); } else if (ma == 10) { hqstr = hqstr.replace("ma_volume10", "\"ma_volume\"").replace(",volume", ",\"volume\"").replace("ma_price10", "\"ma_price\""); } else if (ma == 15) { hqstr = hqstr.replace("ma_volume15", "\"ma_volume\"").replace(",volume", ",\"volume\"").replace("ma_price15", "\"ma_price\""); } else { return ServerResponse.createByErrorMsg("ma 取值 5,10,15"); } if (StringUtils.isBlank(hqstr)) { return ServerResponse.createByErrorMsg("没有查询到行情数据"); } MinDataVO minDataVO = new MinDataVO(); minDataVO.setStockName(stock.getStockName()); minDataVO.setStockCode(stock.getStockCode()); minDataVO.setGid(stock.getStockGid()); hqstr = hqstr.replaceAll("\"\"", "\""); List list = (List) JsonUtil.string2Obj(hqstr, new TypeReference>() { }); log.info("需要查询的行情size为: {}", Integer.valueOf(size)); minDataVO.setData(list); return ServerResponse.createBySuccess(minDataVO); } /*指数分时-k线 * stock:指数代码 * time:5、15、30、60,单位分钟 * */ public static ServerResponse getIndexMinK(StockIndex stock, int time, int size) { String minUrl = PropertiesUtil.getProperty("sina.index.k.min.url").replace("{code}", stock.getIndexGid()).replace("{time}", String.valueOf(time)); String stamp = String.valueOf(new Date().getTime());// new Date()为获取当前系统时间 minUrl = minUrl.replace("{stamp}", stamp); String hqstr = ""; try { hqstr = HttpClientRequest.doGet(minUrl); } catch (Exception e) { log.error("获取股票K线分时图出错,错误信息 = {}", e); } log.info("期货分时 - time = {} ", time); hqstr = hqstr.split("\\[")[1].replace("]);", ""); /*hqstr = hqstr.replace("d","\"day\""); hqstr = hqstr.replace("o","\"open\""); hqstr = hqstr.replace("h","\"high\""); hqstr = hqstr.replace("l","\"low\""); hqstr = hqstr.replace("c","\"close\""); hqstr = hqstr.replace("v","\"volume\"");*/ if (StringUtils.isBlank(hqstr)) { return ServerResponse.createByErrorMsg("没有查询到行情数据"); } MinDataVO minDataVO = new MinDataVO(); minDataVO.setStockName(stock.getIndexName()); minDataVO.setStockCode(stock.getIndexCode()); minDataVO.setGid(stock.getIndexGid()); hqstr = hqstr.replaceAll("\"\"", "\""); hqstr = "[" + hqstr + "]"; List list = (List) JsonUtil.string2Obj(hqstr, new TypeReference>() { }); //int size = Integer.valueOf(PropertiesUtil.getProperty("sina.index.k.min.max.size")); if (list.size() > size) { list = list.subList((list.size() - size - 1), list.size()); } minDataVO.setData(list); return ServerResponse.createBySuccess(minDataVO); } public static void main(String[] args) { // List list = Lists.newArrayList(); // list.add(Integer.valueOf(1)); // list.add(Integer.valueOf(2)); // list.add(Integer.valueOf(3)); // System.out.println(list.size()); // // String[][] values = new String[list.size()][5]; // // System.out.println("[]" + values.length); // System.out.println("[][]" + values[1].length); // // System.out.println(getSinaStock("sh601318")); // // String sss = "[\n{\n\"day\": \"2019-03-05 14:50:00\",\n\"open\": \"13.020\",\n\"high\": \"13.040\",\n\"low\": \"13.000\",\n\"close\": \"13.040\",\n\"volume\": \"2611513\",\n\"ma_price5\": 13.01,\n\"ma_volume5\": 3216535\n},\n{\n\"day\": \"2019-03-05 14:55:00\",\n\"open\": \"13.040\",\n\"high\": \"13.040\",\n\"low\": \"13.010\",\n\"close\": \"13.030\",\n\"volume\": \"2296000\",\n\"ma_price5\": 13.016,\n\"ma_volume5\": 3044839\n}\n]"; // // sss = sss.substring(1, sss.length() - 1); // // sss = "{" + sss + "}"; // getSinaStock("s_sh600090"); } }