1.15三大交易所平均价抓取代码参考
<p>/**</p>
<ul>
<li>三大交易所平均值</li>
<li>
</li>
<li>
<p>@author
**/
@Slf4j
@Service
public class OtcAvgExchangePrice implements CoinPriceFetchService {</p>
<p>@Autowired
private BaseExchangeRateServiceImpl baseExchangeRateService;</p>
<p>@Override
public void fetchPriceJsonAndSave() {
getAndSavePriceInfo(AssetType.USDT);
getAndSavePriceInfo(AssetType.BTC);
}</p>
<p>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);
}</p>
<p>private BigDecimal calculateAvg(List<Long> apiIdList) {
if (CollectionUtil.isEmpty(apiIdList)) {
return null;
}</p>
<pre><code>List<BaseExchangeRate> priceList = baseExchangeRateService.list(
new LambdaQueryWrapper<BaseExchangeRate>().
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);</code></pre>
<p>}</p>
<p>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());
}</p>
<pre><code>return sellApiInfoIdList;</code></pre>
<p>}</p>
<p>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());
}</p>
<pre><code>return buyApiInfoIdList;</code></pre>
<p>}</p>
<p>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());
}
}</p>
</li>
</ul>
<p>}</p>