From 0854f968f2b696ea4c39ef175b083c0a55116418 Mon Sep 17 00:00:00 2001 From: xiaoyong931011 <15274802129@163.com> Date: Tue, 30 Mar 2021 21:53:23 +0800 Subject: [PATCH] Merge branch 'activity' of http://120.27.238.55:7000/r/exchange into activity --- src/test/java/com/xcong/excoin/WholeTest.java | 20 ++------------------ src/main/java/com/xcong/excoin/modules/contract/service/impl/ContractHoldOrderServiceImpl.java | 2 +- src/main/java/com/xcong/excoin/utils/CalculateUtil.java | 36 +++++++++++++++++++++++++++++++++++- 3 files changed, 38 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/xcong/excoin/modules/contract/service/impl/ContractHoldOrderServiceImpl.java b/src/main/java/com/xcong/excoin/modules/contract/service/impl/ContractHoldOrderServiceImpl.java index a64317f..0532ea6 100644 --- a/src/main/java/com/xcong/excoin/modules/contract/service/impl/ContractHoldOrderServiceImpl.java +++ b/src/main/java/com/xcong/excoin/modules/contract/service/impl/ContractHoldOrderServiceImpl.java @@ -531,7 +531,7 @@ holdOrderListVo.setReturnRate(returnRate); holdOrderListVo.setProfitOrLoss(rewardRatio); if (ContractEntrustOrderEntity.POSITION_TYPE_ALL == memberEntity.getContractPositionType()) { - BigDecimal forcePrice = CalculateUtil.getForceSetPriceForWhole(holdOrderEntity.getSymbol(), memberEntity); + BigDecimal forcePrice = CalculateUtil.calForcePriceForWhole(memberEntity, holdOrderEntity); holdOrderListVo.setForceClosingPrice(forcePrice); } resultList.add(holdOrderListVo); diff --git a/src/main/java/com/xcong/excoin/utils/CalculateUtil.java b/src/main/java/com/xcong/excoin/utils/CalculateUtil.java index 935f1f3..ba1125e 100644 --- a/src/main/java/com/xcong/excoin/utils/CalculateUtil.java +++ b/src/main/java/com/xcong/excoin/utils/CalculateUtil.java @@ -163,6 +163,40 @@ return result; } + public static BigDecimal calForcePriceForWhole(MemberEntity memberEntity, ContractHoldOrderEntity contractHoldOrderEntity) { + ContractHoldOrderDao holdOrderDao = SpringContextHolder.getBean(ContractHoldOrderDao.class); + MemberWalletContractDao walletContractDao = SpringContextHolder.getBean(MemberWalletContractDao.class); + + Long memberId = memberEntity.getId(); + MemberWalletContractEntity walletContract = walletContractDao.findWalletContractByMemberIdAndSymbol(memberId, CoinTypeEnum.USDT.name()); + List<ContractHoldOrderEntity> holdOrderEntities = holdOrderDao.selectHoldOrderListForWholeByMemberIdAndSymbol(memberId, null); + BigDecimal forcePrice = BigDecimal.ZERO; + if (CollUtil.isNotEmpty(holdOrderEntities)) { + BigDecimal totalBondAmount = BigDecimal.ZERO; + BigDecimal totalProfitOrLoss = BigDecimal.ZERO; + for (ContractHoldOrderEntity holdOrderEntity : holdOrderEntities) { + if (holdOrderEntity.getId().equals(contractHoldOrderEntity.getId())) { + totalBondAmount = totalBondAmount.add(holdOrderEntity.getHoldBond()); + } else { + totalBondAmount = totalBondAmount.add(holdOrderEntity.getBondAmount()); + } + totalProfitOrLoss = totalProfitOrLoss.add(calProfitOrLoss(holdOrderEntity, memberEntity)); + } + + BigDecimal divideChild = walletContract.getTotalBalance().subtract(totalBondAmount).add(totalProfitOrLoss); + BigDecimal divideParent = contractHoldOrderEntity.getSymbolSku().multiply(new BigDecimal(contractHoldOrderEntity.getSymbolCntSale())); + + BigDecimal divide = divideChild.divide(divideParent, 8, BigDecimal.ROUND_DOWN); + + if (ContractHoldOrderEntity.OPENING_TYPE_MORE == contractHoldOrderEntity.getOpeningType()) { + forcePrice = contractHoldOrderEntity.getOpeningPrice().subtract(divide); + } else { + forcePrice = contractHoldOrderEntity.getOpeningPrice().add(divide); + } + } + return forcePrice; + } + public static BigDecimal calProfitOrLoss(ContractHoldOrderEntity holdOrderEntity, MemberEntity memberEntity) { CacheSettingUtils cacheSettingUtils = SpringContextHolder.getBean(CacheSettingUtils.class); RedisUtils redisUtils = SpringContextHolder.getBean(RedisUtils.class); @@ -286,6 +320,6 @@ redisUtils.set(AppContants.HOLD_BOND_RATIO, tradeSetting.getHoldBondRatio()); } - return contractHoldOrder.getOpeningPrice().multiply(new BigDecimal(contractHoldOrder.getSymbolCntSale())).multiply(holdBondRatio).multiply(contractHoldOrder.getSymbolSku()); + return contractHoldOrder.getOpeningPrice().multiply(new BigDecimal(contractHoldOrder.getSymbolCntSale())).multiply(holdBondRatio.multiply(new BigDecimal(100))).multiply(contractHoldOrder.getSymbolSku()).divide(new BigDecimal(contractHoldOrder.getLeverRatio()), 8, BigDecimal.ROUND_DOWN); } } diff --git a/src/test/java/com/xcong/excoin/WholeTest.java b/src/test/java/com/xcong/excoin/WholeTest.java index 36b5d9d..83ee582 100644 --- a/src/test/java/com/xcong/excoin/WholeTest.java +++ b/src/test/java/com/xcong/excoin/WholeTest.java @@ -200,26 +200,10 @@ @Test public void wholeForceNewTest() { - String symbol = "BTC/USDT"; MemberEntity memberEntity = memberDao.selectById(15L); - ContractHoldOrderEntity holdOrder = contractHoldOrderDao.selectById(144L); - List<ContractHoldOrderEntity> wholeOrders = contractHoldOrderDao.selectHoldOrderListForWholeByMemberIdAndSymbol(memberEntity.getId(), null); - MemberWalletContractEntity wallet = memberWalletContractDao.findWalletContractByMemberIdAndSymbol(memberEntity.getId(), CoinTypeEnum.USDT.name()); - if (CollUtil.isNotEmpty(wholeOrders)) { - BigDecimal totalProfitOrLoss = BigDecimal.ZERO; - BigDecimal otherSymbolBond = BigDecimal.ZERO; - for (ContractHoldOrderEntity wholeOrder : wholeOrders) { - BigDecimal profitOrLoss = CalculateUtil.calProfitOrLoss(wholeOrder, memberEntity); - totalProfitOrLoss = totalProfitOrLoss.add(profitOrLoss); - if (!wholeOrder.getId().equals(holdOrder.getId())) { - otherSymbolBond = otherSymbolBond.add(wholeOrder.getBondAmount()); - } - } + ContractHoldOrderEntity holdOrder = contractHoldOrderDao.selectById(300L); - BigDecimal bond = wallet.getTotalBalance().add(totalProfitOrLoss).subtract(otherSymbolBond); - System.out.println(bond); - System.out.println(CalculateUtil.getForceSetPrice(bond, holdOrder.getOpeningPrice(), holdOrder.getSymbolCntSale(), holdOrder.getSymbolSku(), holdOrder.getOpeningType(), memberEntity)); - } + System.out.println(CalculateUtil.calForcePriceForWhole(memberEntity, holdOrder)); } } -- Gitblit v1.9.1