ybf

ybf


1.15三大交易所平均价抓取代码参考

/**

  • 三大交易所平均值
  • @author **/ @Slf4j @Service public class OtcAvgExchangePrice implements CoinPriceFetchService {

    @Autowired private BaseExchangeRateServiceImpl baseExchangeRateService;

    @Override public void fetchPriceJsonAndSave() { getAndSavePriceInfo(AssetType.USDT); getAndSavePriceInfo(AssetType.BTC); }

    private void getAndSavePriceInfo(AssetType assetType) { BigDecimal buyPrice = null; BigDecimal sellPrice = null; long start = System.currentTimeMillis(); try { buyPrice = calculateAvg(pickBuyApiIdList(assetType)); log.info("三大行{}买1价信息均为{}", assetType.getSymbol(), buyPrice); sellPrice = calculateAvg(pickSellApiIdList(assetType)); log.info("三大行{}卖1价信息均为{}", assetType.getSymbol(), sellPrice); } catch (Exception e) { log.info("计算三大交易所平均值失败:\n{}", e.getMessage()); } long duration = System.currentTimeMillis() - start; parseAndSave(assetType, buyPrice, sellPrice, duration); }

    private BigDecimal calculateAvg(List<Long> apiIdList) { if (CollectionUtil.isEmpty(apiIdList)) { return null; }

    List&lt;BaseExchangeRate&gt; priceList = baseExchangeRateService.list(
            new LambdaQueryWrapper&lt;BaseExchangeRate&gt;().
                    in(BaseExchangeRate::getApiInfoId, apiIdList)
                    .eq(BaseExchangeRate::getStatus, 1)
    );
    if (CollectionUtil.isEmpty(priceList)) {
        return null;
    }
    BigDecimal priceSum = priceList.stream().map(BaseExchangeRate::getPrice)
            .reduce(BigDecimal.ZERO, BigDecimal::add);
    int count = priceList.size();
    if (0 == count) {
        return null;
    }
    return priceSum.divide(BigDecimal.valueOf(count), 8, RoundingMode.CEILING);

    }

    private List<Long> pickSellApiIdList(AssetType assetType) { List<Long> sellApiInfoIdList = null; if (AssetType.USDT.equals(assetType)) { sellApiInfoIdList = Lists.newArrayList( ApiInfoEnum.OKEX_OTC_USDT_FIRST_SELL_PRICE.getId(), ApiInfoEnum.HT_OTC_USDT_FIRST_SELL_PRICE.getId(), ApiInfoEnum.BNB_OTC_USDT_FIRST_SELL_PRICE.getId()); } else if (AssetType.BTC.equals(assetType)) { sellApiInfoIdList = Lists.newArrayList( ApiInfoEnum.OKEX_OTC_BTC_FIRST_SELL_PRICE.getId(), ApiInfoEnum.HT_OTC_BTC_FIRST_SELL_PRICE.getId(), ApiInfoEnum.BNB_OTC_BTC_FIRST_SELL_PRICE.getId()); }

    return sellApiInfoIdList;

    }

    private List<Long> pickBuyApiIdList(AssetType assetType) { List<Long> buyApiInfoIdList = null; if (AssetType.USDT.equals(assetType)) { //取三大交易所USDT买价平均数 buyApiInfoIdList = Lists.newArrayList( ApiInfoEnum.OKEX_OTC_USDT_FIRST_BUY_PRICE.getId(), ApiInfoEnum.HT_OTC_USDT_FIRST_BUY_PRICE.getId(), ApiInfoEnum.BNB_OTC_USDT_FIRST_BUY_PRICE.getId()); } else if (AssetType.BTC.equals(assetType)) { buyApiInfoIdList = Lists.newArrayList( ApiInfoEnum.OKEX_OTC_BTC_FIRST_BUY_PRICE.getId(), ApiInfoEnum.HT_OTC_BTC_FIRST_BUY_PRICE.getId(), ApiInfoEnum.BNB_OTC_BTC_FIRST_BUY_PRICE.getId()); }

    return buyApiInfoIdList;

    }

    private void parseAndSave(AssetType assetType, BigDecimal buyPrice, BigDecimal sellPrice, long duration) { try { BaseExchangeRateHistoryDTO baseExchangeRateHistoryDTO = new BaseExchangeRateHistoryDTO(); baseExchangeRateHistoryDTO.setPriceUnit(AssetType.CNY); baseExchangeRateHistoryDTO.setCoinType(assetType); baseExchangeRateHistoryDTO.setDuration(duration); baseExchangeRateHistoryDTO.setResponse(null); baseExchangeRateHistoryDTO.setPrice(buyPrice); if (assetType.getSymbol().equals(AssetType.USDT.getSymbol())) { baseExchangeRateHistoryDTO.setApiInfo(ApiInfoEnum.OTC_USDT_AVG_FIRST_BUY_PRICE); baseExchangeRateHistoryDTO.setPriceTrading(RateTradingEnum.CNY_USDT); } else if (assetType.getSymbol().equals(AssetType.BTC.getSymbol())) { baseExchangeRateHistoryDTO.setApiInfo(ApiInfoEnum.OTC_BTC_AVG_FIRST_BUY_PRICE); baseExchangeRateHistoryDTO.setPriceTrading(RateTradingEnum.CNY_BTC); } baseExchangeRateHistoryDTO.setDirection(BUY_DIRECTION); baseExchangeRateHistoryDTO.setApiSource(RateSourceEnum.SYSTEM); baseExchangeRateService.mergeBaseExchangeRate(baseExchangeRateHistoryDTO, false); baseExchangeRateHistoryDTO.setPrice(sellPrice); if (assetType.getSymbol().equals(AssetType.USDT.getSymbol())) { baseExchangeRateHistoryDTO.setApiInfo(ApiInfoEnum.OTC_USDT_AVG_FIRST_SELL_PRICE); } else if (assetType.getSymbol().equals(AssetType.BTC.getSymbol())) { baseExchangeRateHistoryDTO.setApiInfo(ApiInfoEnum.OTC_BTC_AVG_FIRST_SELL_PRICE); } baseExchangeRateHistoryDTO.setDirection(SELL_DIRECTION); baseExchangeRateService.mergeBaseExchangeRate(baseExchangeRateHistoryDTO, false); } catch (Exception e) { log.error("三大交易所之{}平均值汇率更新失败:{}", assetType.getSymbol(), e.getMessage()); } }

}

页面列表

ITEM_HTML