ybf

ybf


1.5各交易所的各币种的平均价代码参考

/**

  • 各交易所的各币种的平均价
  • @autho **/ @Slf4j @Service public class CoinAvgExchangePrice implements CoinPriceFetchService {

    @Autowired private BaseExchangeRateServiceImpl baseExchangeRateService; @Autowired private CoinDisposeExchangePrice coinDisposeExchangePrice;

    @Override public void fetchPriceJsonAndSave() { try { long startTime = System.currentTimeMillis(); / 从数据库获取基础汇率数据/ List<BaseExchangeRate> list = getBaseExchangeRateList(); /key为apiInfoId/ Map<Long, BaseExchangeRate> sourceMap = listTomap(list); if (CollectionUtil.isEmpty(sourceMap)) { return; } log.info("sourceMap:\n{}", JacksonUtils.toJson(sourceMap)); /key为apiInfoId,value为计算后的平均值/ List<BaseExchangeRate> avgList = getAvgPrice(sourceMap, startTime); log.info("各交易所的各币种的平均价:{}", avgList); save(avgList); / 三大交易所USD的平均价处理及转换CNY汇率/ disposeUsdAvg();

    } catch (Exception e) {
        log.error(&quot;计算各交易所的各币种的平均价失败:\n{}&quot;, e.getMessage());
    }

    }

    private void disposeUsdAvg() { List<BaseExchangeRate> list = baseExchangeRateService.list(new LambdaQueryWrapper<BaseExchangeRate>() .ge(BaseExchangeRate::getApiInfoId, ApiInfoEnum.BNB_USDT_USD_FIRST_SELL_PRICE.getId()) .le(BaseExchangeRate::getApiInfoId, ApiInfoEnum.OKEX_USD_USDT_FIRST_BUY_PRICE.getId()) .eq(BaseExchangeRate::getStatus, 1) ); if(list==null||list.isEmpty()){ return; } BigDecimal buySumPrice = new BigDecimal("0"); BigDecimal sellSumPrice = new BigDecimal("0"); //计算平均值 for (BaseExchangeRate b : list) { if(TradeDirectionEnum.BUY_FROM_USER.getCode().equals(b.getDirection())){ buySumPrice = NumberUtil.add(b.getPrice(),buySumPrice); continue; } if(TradeDirectionEnum.SELL_TO_USER.getCode().equals(b.getDirection())){ sellSumPrice = NumberUtil.add(b.getPrice(),sellSumPrice); } } BigDecimal buyAvgPrice = buySumPrice.divide(new BigDecimal("3"), 8, RoundingMode.CEILING); BigDecimal sellAvgPrice = sellSumPrice.divide(new BigDecimal("3"), 8, RoundingMode.CEILING); newInstanceAndToSave(TradeDirectionEnum.BUY_FROM_USER,buyAvgPrice); newInstanceAndToSave(TradeDirectionEnum.SELL_TO_USER,sellAvgPrice); }

    private void newInstanceAndToSave(TradeDirectionEnum buyOrSell, BigDecimal avgPrice) { BaseExchangeRateHistoryDTO baseExchangeRateHistoryDTO = new BaseExchangeRateHistoryDTO(); baseExchangeRateHistoryDTO.setCoinType(AssetType.USDT); baseExchangeRateHistoryDTO.setPriceTrading(RateTradingEnum.USD_USDT); baseExchangeRateHistoryDTO.setPriceUnit(AssetType.USD); baseExchangeRateHistoryDTO.setDuration(0L); baseExchangeRateHistoryDTO.setResponse(""); baseExchangeRateHistoryDTO.setPrice(avgPrice); if(TradeDirectionEnum.BUY_FROM_USER.equals(buyOrSell)){ baseExchangeRateHistoryDTO.setDirection(TradeDirectionEnum.BUY_FROM_USER.getCode()); baseExchangeRateHistoryDTO.setApiInfo(ApiInfoEnum.OTC_CNY_USDT_AVG_FIRST_BUY_PRICE); }else{ baseExchangeRateHistoryDTO.setDirection(TradeDirectionEnum.SELL_TO_USER.getCode()); baseExchangeRateHistoryDTO.setApiInfo(ApiInfoEnum.OTC_CNY_USDT_AVG_FIRST_SELL_PRICE); } baseExchangeRateHistoryDTO.setApiSource(RateSourceEnum.SYSTEM); log.info("disposeUsdAvg-baseExchangeRateHistoryDTO: {}", JacksonUtils.toJson(baseExchangeRateHistoryDTO)); baseExchangeRateService.mergeBaseExchangeRate(baseExchangeRateHistoryDTO, true);

    //汇率转换及入库
    coinDisposeExchangePrice.fetchPriceJsonAndSave(AssetType.USD.getSymbol(), RateSourceEnum.SYSTEM.getMsg());

    }

    private void save(List<BaseExchangeRate> avgList) { if (CollectionUtil.isEmpty(avgList)) { return; }

    LocalDateTime updAt = LocalDateTime.now();
    List&lt;Long&gt; ids = new ArrayList&lt;&gt;();
    
    for (BaseExchangeRate item : avgList) {
        item.setUpdatedAt(updAt);
        ids.add(item.getId());
    }
    List&lt;BaseExchangeRate&gt; historyList = this.baseExchangeRateService.listByIds(ids);
    baseExchangeRateService.saveAvgBaseExchangeRateToHistory(historyList);
    
    baseExchangeRateService.updateBatchById(avgList);

    }

    private List<BaseExchangeRate> getBaseExchangeRateList() { /ApiInfoId 12到27 为币汇 币安 火币 okex 的usdt,btc的buy一价和sell一价; ApiInfoId 28和29 对本业务无用,要排除掉 ApiInfoId 30到37 为币汇 币安 火币 okex 的usdt,btc的平均价/ return baseExchangeRateService.list( new LambdaQueryWrapper<BaseExchangeRate>() .ge(BaseExchangeRate::getApiInfoId, 12L) .le(BaseExchangeRate::getApiInfoId, 37L) //.eq(BaseExchangeRate::getStatus, 1) ); }

    private List<BaseExchangeRate> getAvgPrice(Map<Long, BaseExchangeRate> sourceMap, long startTime) { long duration = System.currentTimeMillis() - startTime; List<BaseExchangeRate> avgList = new ArrayList<>();

    /* 币汇 usdt*/
    fillAvgList(sourceMap, duration, ApiInfoEnum.BH_OTC_USDT_FIRST_BUY_PRICE, ApiInfoEnum.BH_OTC_USDT_FIRST_SELL_PRICE, ApiInfoEnum.BH_OTC_USDT_AVG_PRICE, avgList);
    
    /* 币汇 btc*/
    fillAvgList(sourceMap, duration, ApiInfoEnum.BH_OTC_BTC_FIRST_BUY_PRICE, ApiInfoEnum.BH_OTC_BTC_FIRST_SELL_PRICE, ApiInfoEnum.BH_OTC_BTC_AVG_PRICE, avgList);
    
    /* 币安 usdt */
    fillAvgList(sourceMap, duration, ApiInfoEnum.BNB_OTC_USDT_FIRST_BUY_PRICE, ApiInfoEnum.BNB_OTC_USDT_FIRST_SELL_PRICE, ApiInfoEnum.BNB_OTC_USDT_AVG_PRICE, avgList);
    
    /* 币安 btc */
    fillAvgList(sourceMap, duration, ApiInfoEnum.BNB_OTC_BTC_FIRST_BUY_PRICE, ApiInfoEnum.BNB_OTC_BTC_FIRST_SELL_PRICE, ApiInfoEnum.BNB_OTC_BTC_AVG_PRICE, avgList);
    /* 火币 usdt*/
    fillAvgList(sourceMap, duration, ApiInfoEnum.HT_OTC_USDT_FIRST_BUY_PRICE, ApiInfoEnum.HT_OTC_USDT_FIRST_SELL_PRICE, ApiInfoEnum.HT_OTC_USDT_AVG_PRICE, avgList);
    
    /* 火币 btc*/
    fillAvgList(sourceMap, duration, ApiInfoEnum.HT_OTC_BTC_FIRST_BUY_PRICE, ApiInfoEnum.HT_OTC_BTC_FIRST_SELL_PRICE, ApiInfoEnum.HT_OTC_BTC_AVG_PRICE, avgList);
    
    /* okex usdt*/
    fillAvgList(sourceMap, duration, ApiInfoEnum.OKEX_OTC_USDT_FIRST_BUY_PRICE, ApiInfoEnum.OKEX_OTC_USDT_FIRST_SELL_PRICE, ApiInfoEnum.OKEX_OTC_USDT_AVG_PRICE, avgList);
    
    /* okex btc*/
    fillAvgList(sourceMap, duration, ApiInfoEnum.OKEX_OTC_BTC_FIRST_BUY_PRICE, ApiInfoEnum.OKEX_OTC_BTC_FIRST_SELL_PRICE, ApiInfoEnum.OKEX_OTC_BTC_AVG_PRICE, avgList);
    
    return avgList;

    }

    private void fillAvgList(Map<Long, BaseExchangeRate> sourceMap, long duration, ApiInfoEnum buy, ApiInfoEnum sell, ApiInfoEnum avg, List<BaseExchangeRate> avgList) { log.info("{}", avg); BaseExchangeRate b = sourceMap.get(avg.getId()); if (null == b) { log.warn("api_info_id:{}对应的记录不存在", avg); return; } b.setStatus(Integer.parseInt(BaseRateEnum.OK.getCode())); if (sourceMap.containsKey(buy.getId()) && sourceMap.containsKey(sell.getId())) { BigDecimal buyPrice = sourceMap.get(buy.getId()).getPrice(); BigDecimal sellPrice = sourceMap.get(sell.getId()).getPrice(); BigDecimal sum = buyPrice.add(sellPrice); BigDecimal avgPrice = sum.divide(new BigDecimal("2"), 8, RoundingMode.CEILING);

        b.setPrice(avgPrice);
        b.setDuration(duration);
    
        BaseExchangeRate rate=sourceMap.get(buy.getId());
        b.setPriceTrading(rate.getCoinType().equals(AssetType.USDT.getCode()) ?
                RateTradingEnum.CNY_USDT.getCode():RateTradingEnum.CNY_BTC.getCode());
        b.setApiSource(RateSourceEnum.SYSTEM.getMsg());
        //买一价或卖一价为不可用,则平均价不可用
        if(StatusEnum.NO.getCode().compareTo(sourceMap.get(buy.getId()).getStatus())==0||
                StatusEnum.NO.getCode().compareTo(sourceMap.get(sell.getId()).getStatus())==0||
                baseExchangeRateService.isOverRateLimitThreshold(b)){
            b.setStatus(Integer.parseInt(BaseRateEnum.NO.getCode()));
        }
        avgList.add(b);
    }else{
        if(baseExchangeRateService.isOverRateLimitThreshold(b)){
            b.setStatus(Integer.parseInt(BaseRateEnum.NO.getCode()));
        }
        avgList.add(b);
    }

    }

    private Map<Long, BaseExchangeRate> listTomap(List<BaseExchangeRate> priceList) { if (CollectionUtil.isEmpty(priceList)) { return null; } Map<Long, BaseExchangeRate> map = new HashMap<>(); for (BaseExchangeRate ber : priceList) { if (ber.getApiInfoId().equals(28L) || ber.getApiInfoId().equals(29L)) { continue; } map.put(ber.getApiInfoId(), ber); } return map; }

}

页面列表

ITEM_HTML