| | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONArray; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.google.common.base.Splitter; |
| | | import com.google.common.collect.Lists; |
| | | import com.yami.trading.bean.cms.Infomation; |
| | | import com.yami.trading.bean.data.domain.*; |
| | | import com.yami.trading.bean.data.respDto.TradeTickResp; |
| | | import com.yami.trading.bean.item.domain.Item; |
| | | import com.yami.trading.common.util.RedisUtil; |
| | | import com.yami.trading.common.util.StringUtils; |
| | | import com.yami.trading.common.util.UTCDateUtils; |
| | | import com.yami.trading.huobi.data.DataCache; |
| | | import com.yami.trading.huobi.data.internal.DepthTimeObject; |
| | | import com.yami.trading.huobi.data.internal.KlineConstant; |
| | | import com.yami.trading.huobi.data.internal.KlineService; |
| | | import com.yami.trading.huobi.data.internal.TradeTimeObject; |
| | | import com.yami.trading.huobi.data.model.AlltickBatchKlineResult; |
| | | import com.yami.trading.huobi.data.model.AlltickNewPriceResult; |
| | | import com.yami.trading.huobi.data.model.AllticktradeResult; |
| | | import com.yami.trading.huobi.hobi.HobiDataService; |
| | | import com.yami.trading.huobi.hobi.constant.AllticktradeMadeOptions; |
| | | import com.yami.trading.huobi.hobi.http.HttpHelper; |
| | | import com.yami.trading.huobi.hobi.http.HttpMethodType; |
| | | import com.yami.trading.service.cms.InfomationService; |
| | |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.net.URLEncoder; |
| | | import java.text.SimpleDateFormat; |
| | | import java.time.LocalDate; |
| | | import java.time.Month; |
| | | import java.time.ZoneId; |
| | | import java.time.ZonedDateTime; |
| | | import java.time.*; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.*; |
| | | import java.util.regex.Matcher; |
| | |
| | | */ |
| | | // public final static String live = "https://hq.sinajs.cn/rn={1}list={2}";//todo 要调整 |
| | | public final static String live = "https://hq.sinajs.cn/etag.php?_={1}&list={2}";//todo 要调整 |
| | | public final static String live_tick = "https://quote.alltick.io/quote-b-api/kline?token={1}&query={2}"; |
| | | |
| | | |
| | | private static Logger logger = LoggerFactory.getLogger(XinLangDataServiceImpl.class); |
| | |
| | | @Autowired |
| | | private ItemService itemService; |
| | | |
| | | @Value("${alltick.trade-tick}") |
| | | private String tradeTick; |
| | | @Value("${alltick.batch-kline}") |
| | | private String batchKline; |
| | | |
| | | |
| | | public static void main(String[] args) { |
| | | XinLangDataServiceImpl service = new XinLangDataServiceImpl(); |
| | | List<Realtime> usdsgd = service.realtimeSingle("USDSGD"); |
| | | List<Realtime> usdsgd = service.realtimeSingle("USDJPY"); |
| | | for (Realtime re : usdsgd) { |
| | | System.out.println(JSONObject.toJSONString(re)); |
| | | } |
| | |
| | | // List<Kline> eurusd = service.getTimeseriesByPeriodOneDay("EURUSD"); |
| | | // System.out.println(JSONObject.toJSONString(eurusd)); |
| | | } |
| | | public Map<String, Object> createRequest(List<String> strings) { |
| | | Map<String, Object> requestData = new HashMap<>(); |
| | | Map<String, Object> data = new HashMap<>(); |
| | | List<Map<String, Object>> dataList = new ArrayList<>(); |
| | | for (String symbolCode : strings) { |
| | | for (int i = 1; i <= 3; i++) { |
| | | Map<String, Object> dataItem = new HashMap<>(); |
| | | dataItem.put("code", symbolCode); |
| | | dataItem.put("kline_type", i); |
| | | dataItem.put("kline_timestamp_end", 0); |
| | | dataItem.put("query_kline_num", 1); |
| | | dataList.add(dataItem); |
| | | } |
| | | } |
| | | data.put("data_list", dataList); |
| | | requestData.put("trace", UUID.randomUUID().toString()); |
| | | requestData.put("data", data); |
| | | return requestData; |
| | | } |
| | | |
| | | public Map<String, Object> createRequestTick(List<String> strings) { |
| | | // 创建 JSON 对象 |
| | | JSONObject response = new JSONObject(); |
| | | response.put("trace", UUID.randomUUID().toString()); |
| | | // 创建数据部分 |
| | | JSONObject dataObject = new JSONObject(); |
| | | JSONArray symbolList = new JSONArray(); |
| | | |
| | | // 构造符号列表 |
| | | for (String code : strings) { |
| | | JSONObject symbol = new JSONObject(); |
| | | symbol.put("code", code); |
| | | symbolList.add(symbol); |
| | | } |
| | | |
| | | dataObject.put("symbol_list", symbolList); |
| | | response.put("data", dataObject); |
| | | return response; |
| | | } |
| | | |
| | | public Map<String, Object> createRequestZdf(String symbolCode) { |
| | | JSONObject json = new JSONObject(); |
| | | json.put("trace", UUID.randomUUID().toString()); |
| | | JSONObject data = new JSONObject(); |
| | | data.put("code", symbolCode); |
| | | data.put("kline_type", 8); |
| | | data.put("kline_timestamp_end", 0); |
| | | data.put("query_kline_num", 2); |
| | | json.put("data", data); |
| | | return json; |
| | | } |
| | | /** |
| | | * 获取原始的K线图数据 |
| | | * |
| | |
| | | return HttpHelper.sendGetHttp(url, null, cookie); |
| | | } |
| | | |
| | | public List<Realtime> realtimeNewPrice(String symbols,List<Realtime> realtimes) { |
| | | //获取最新价 |
| | | try{ |
| | | List<String> strings = Arrays.asList(symbols.split(",")); |
| | | Map<String, Object> requestDataTick = createRequestTick(strings); |
| | | String url = tradeTick.replace("{1}", URLEncoder.encode(requestDataTick.toString(), "UTF-8")); |
| | | HttpGet request = new HttpGet(url); |
| | | HttpResponse response = HttpHelper.getHttpclient().execute(request); |
| | | String resultTickStr = HttpHelper.responseProc(response); |
| | | JSONObject resultJsonTick = JSON.parseObject(resultTickStr); |
| | | if(null == resultJsonTick || 200 != resultJsonTick.getInteger("ret")){ |
| | | logger.error("数据,返回:{} ", resultJsonTick); |
| | | } |
| | | |
| | | JSONObject tempdataTick = resultJsonTick.getJSONObject("data"); |
| | | JSONArray dataArrayTick = tempdataTick.getJSONArray("tick_list"); |
| | | if (dataArrayTick.size() > 0) { |
| | | List<AlltickNewPriceResult> alltickNewPriceResults = JSONObject.parseArray(JSONObject.toJSONString(dataArrayTick), AlltickNewPriceResult.class); |
| | | for (AlltickNewPriceResult tickNewPrice : alltickNewPriceResults) { |
| | | for (Realtime realtime : realtimes){ |
| | | if(tickNewPrice.getCode().equals(realtime.getName())){ |
| | | realtime.setCloseOld(realtime.getClose()); |
| | | // realtime.setClose(new BigDecimal(tickNewPrice.getPrice())); |
| | | // realtime.setVolume(new BigDecimal(tickNewPrice.getVolume())); |
| | | } |
| | | // Item bySymbol = itemService.findBySymbol(realtime.getSymbol()); |
| | | // if(null != bySymbol.getYesterdayLastPrice() && bySymbol.getYesterdayLastPrice().compareTo(BigDecimal.ZERO) != 0){ |
| | | // BigDecimal changePercent = (realtime.getClose().subtract(bySymbol.getYesterdayLastPrice())) |
| | | // .divide(bySymbol.getYesterdayLastPrice(), 5, RoundingMode.HALF_UP) // 精确到4位小数 |
| | | // .multiply(new BigDecimal("100").setScale(2)).setScale(4,RoundingMode.HALF_UP); // 乘以100得到百分比 |
| | | // realtime.setChangeRatioStr(changePercent); |
| | | // } |
| | | } |
| | | } |
| | | } |
| | | // Map<String, Object> requestData = createRequest(strings); |
| | | // String resultStr = HttpHelper.sendPostRequest(batchKline,requestData); |
| | | // JSONObject resultJson = JSON.parseObject(resultStr); |
| | | // if(null == resultJson || 200 != resultJson.getInteger("ret")){ |
| | | // logger.error("数据,返回:{} ", resultJson); |
| | | // } |
| | | // JSONObject tempdata = resultJson.getJSONObject("data"); |
| | | // JSONArray dataArray = tempdata.getJSONArray("kline_list"); |
| | | // if (dataArray.size() > 0) { |
| | | // List<AlltickBatchKlineResult> batchKlines = JSONObject.parseArray(JSONObject.toJSONString(dataArray), AlltickBatchKlineResult.class); |
| | | // for (AlltickBatchKlineResult batchKline : batchKlines) { |
| | | // for (AllticktradeResult result : batchKline.getKline_data()){ |
| | | // for (Realtime realtime : list){ |
| | | // if(realtime.getSymbol().equals(batchKline.getCode())){ |
| | | // int decimal = itemService.getDecimal(realtime.getSymbol()); |
| | | // realtime.setOpen(result.getOpen_price().setScale(decimal, RoundingMode.HALF_UP)); |
| | | // realtime.setHigh(result.getHigh_price().setScale(decimal, RoundingMode.HALF_UP)); |
| | | // realtime.setLow(result.getLow_price().setScale(decimal, RoundingMode.HALF_UP)); |
| | | // realtime.setClose(result.getClose_price().setScale(decimal, RoundingMode.HALF_UP)); |
| | | //// realtime.setAmount(tickNewPrice.getPrice().multiply(tickNewPrice.getVolume()).setScale(item.getDecimals(), RoundingMode.HALF_UP)); |
| | | //// realtime.setVolume(tickNewPrice.getVolume()); |
| | | // } |
| | | // } |
| | | // } |
| | | // } |
| | | // } |
| | | } catch (Exception e){ |
| | | logger.error("error", e); |
| | | } |
| | | return realtimes; |
| | | } |
| | | public List<Realtime> realtimeSingle(String symbols) { |
| | | List<Realtime> list = new ArrayList<Realtime>(); |
| | | try { |
| | | List<String> strings = Arrays.asList(symbols.split(",")); |
| | | for (String symbol: strings) { |
| | | String url = live.replace("{1}",System.currentTimeMillis()+"").replace("{2}", "fx_s"+symbol.toLowerCase()); |
| | | HttpGet request = new HttpGet(url); |
| | | request.addHeader("Referer","https://finance.sina.com.cn"); |
| | | request.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.60 Safari/537.36"); |
| | | HttpResponse response = HttpHelper.getHttpclient().execute(request); |
| | | String result = HttpHelper.responseProc(response); |
| | | int firstQuoteIndex = result.indexOf("\""); |
| | | int secondQuoteIndex = result.indexOf("\"", firstQuoteIndex + 1); |
| | | String extractedString = result.substring(firstQuoteIndex + 1, secondQuoteIndex); |
| | | if(extractedString.split(",").length != 18){ |
| | | return list; |
| | | } |
| | | String[] split = extractedString.split(","); |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | Date date = sdf.parse(split[17]+" " +split[0]); |
| | | long timestamp = date.getTime(); |
| | | Realtime realtime = new Realtime(); |
| | | int decimal = itemService.getDecimal(symbol); |
| | | realtime.setSymbol(symbol); |
| | | realtime.setTs(timestamp); |
| | | realtime.setName(symbol); |
| | | realtime.setOpen(BigDecimal.valueOf(Double.parseDouble(split[5])).setScale(decimal, RoundingMode.HALF_UP)); |
| | | realtime.setHigh(BigDecimal.valueOf(Double.parseDouble(split[6])).setScale(decimal, RoundingMode.HALF_UP)); |
| | | realtime.setLow(BigDecimal.valueOf(Double.parseDouble(split[7])).setScale(decimal, RoundingMode.HALF_UP)); |
| | | if(StringUtils.isNotEmpty(split[1])){ |
| | | realtime.setClose(BigDecimal.valueOf(Double.parseDouble(split[1])).setScale(decimal, RoundingMode.HALF_UP)); |
| | | }else{ |
| | | realtime.setClose(BigDecimal.valueOf(Double.parseDouble(split[7])).setScale(decimal, RoundingMode.HALF_UP)); |
| | | } |
| | | realtime.setMarketCapital(0L); |
| | | realtime.setFloatMarketCapital(0L); |
| | | realtime.setPeForecast(BigDecimal.ZERO); |
| | | realtime.setVolumeRatio(BigDecimal.ZERO); |
| | | realtime.setTurnoverRate(BigDecimal.ZERO); |
| | | |
| | | realtime.setAmount(BigDecimal.ZERO); |
| | | realtime.setVolume(BigDecimal.ZERO); |
| | | list.add(realtime); |
| | | Map<String, Object> requestDataTick = createRequestTick(strings); |
| | | String url = tradeTick.replace("{1}", URLEncoder.encode(requestDataTick.toString(), "UTF-8")); |
| | | HttpGet request = new HttpGet(url); |
| | | HttpResponse response = HttpHelper.getHttpclient().execute(request); |
| | | String resultTickStr = HttpHelper.responseProc(response); |
| | | JSONObject resultJsonTick = JSON.parseObject(resultTickStr); |
| | | if(null == resultJsonTick || 200 != resultJsonTick.getInteger("ret")){ |
| | | logger.error("数据,返回:{} ", resultJsonTick); |
| | | } |
| | | JSONObject tempdataTick = resultJsonTick.getJSONObject("data"); |
| | | JSONArray dataArrayTick = tempdataTick.getJSONArray("tick_list"); |
| | | if (dataArrayTick.size() > 0) { |
| | | List<AlltickNewPriceResult> alltickNewPriceResults = JSONObject.parseArray(JSONObject.toJSONString(dataArrayTick), AlltickNewPriceResult.class); |
| | | for (AlltickNewPriceResult tickNewPrice : alltickNewPriceResults) { |
| | | Realtime realtime = new Realtime(); |
| | | Item item = itemService.findBySymbol(tickNewPrice.getCode()); |
| | | realtime.setSymbol(tickNewPrice.getCode()); |
| | | realtime.setTs(Long.parseLong(tickNewPrice.getTickTime())); |
| | | realtime.setName(tickNewPrice.getCode()); |
| | | realtime.setClose(tickNewPrice.getPrice().setScale(item.getDecimals(), RoundingMode.HALF_UP)); |
| | | // //涨跌幅 = (当天收盘价 - 前一日收盘价) / 前一日收盘价 * 100% |
| | | // if(null != item.getYesterdayLastPrice() && item.getYesterdayLastPrice().compareTo(BigDecimal.ZERO) != 0){ |
| | | // BigDecimal changePercent = (realtime.getClose().subtract(item.getYesterdayLastPrice())) |
| | | // .divide(item.getYesterdayLastPrice(), 5, RoundingMode.HALF_UP) // 精确到4位小数 |
| | | // .multiply(new BigDecimal("100").setScale(2)).setScale(4,RoundingMode.HALF_UP); // 乘以100得到百分比 |
| | | // realtime.setChangeRatioStr(changePercent); |
| | | // } |
| | | realtime.setMarketCapital(0L); |
| | | realtime.setFloatMarketCapital(0L); |
| | | realtime.setPeForecast(BigDecimal.ZERO); |
| | | realtime.setVolumeRatio(BigDecimal.ZERO); |
| | | realtime.setTurnoverRate(BigDecimal.ZERO); |
| | | |
| | | Kline kline = klineService.getLast(tickNewPrice.getCode(),Kline.PERIOD_1DAY); |
| | | if(null != kline){ |
| | | realtime.setOpen(kline.getOpen().setScale(item.getDecimals(), RoundingMode.HALF_UP)); |
| | | realtime.setHigh(kline.getHigh().setScale(item.getDecimals(), RoundingMode.HALF_UP)); |
| | | realtime.setLow(kline.getLow().setScale(item.getDecimals(), RoundingMode.HALF_UP)); |
| | | realtime.setAmount(kline.getAmount().setScale(item.getDecimals(), RoundingMode.HALF_UP)); |
| | | realtime.setVolume(kline.getVolume().setScale(item.getDecimals(), RoundingMode.HALF_UP)); |
| | | }else { |
| | | realtime.setOpen(BigDecimal.ZERO); |
| | | realtime.setHigh(BigDecimal.ZERO); |
| | | realtime.setLow(BigDecimal.ZERO); |
| | | realtime.setAmount(BigDecimal.ZERO); |
| | | realtime.setVolume(BigDecimal.ZERO); |
| | | } |
| | | list.add(realtime); |
| | | } |
| | | } |
| | | |
| | | } catch (Exception e) { |
| | | logger.error("error", e); |
| | | } |