From d23645e976981bc9b670eea1d469fe8a36be309c Mon Sep 17 00:00:00 2001
From: KKSU <15274802129@163.com>
Date: Wed, 17 Apr 2024 17:19:53 +0800
Subject: [PATCH] 55测试环境

---
 src/main/java/com/xcong/excoin/utils/CalculateUtil.java |   59 +++++++++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 49 insertions(+), 10 deletions(-)

diff --git a/src/main/java/com/xcong/excoin/utils/CalculateUtil.java b/src/main/java/com/xcong/excoin/utils/CalculateUtil.java
index e350857..6ce033e 100644
--- a/src/main/java/com/xcong/excoin/utils/CalculateUtil.java
+++ b/src/main/java/com/xcong/excoin/utils/CalculateUtil.java
@@ -205,17 +205,20 @@
     }
 
     /**
-     * 权益-维持保证金+开仓价*张数*规格-开仓价*费率-(总账户-维持保证金) / 规格*张数
+     * 多:
+     * (开仓价*数量*规格 - 其他币种保证金 - (总账户金额-维持保证金) - (权益 - 维持保证金)) / 数量*规格
+     * 空:
+     * (开仓价*数量*规格 - 其他币种保证金 + (总账户金额-维持保证金) + (权益 - 维持保证金)) / 数量*规格
      *
      * @param memberEntity
      * @param contractHoldOrderEntity
      * @return
      */
-    public static BigDecimal calWholePriceTwo(MemberEntity memberEntity, ContractHoldOrderEntity contractHoldOrderEntity) {
+    public static BigDecimal calWholePriceTwo(MemberEntity memberEntity, ContractHoldOrderEntity contractHoldOrderEntity, int type) {
         ContractHoldOrderDao holdOrderDao = SpringContextHolder.getBean(ContractHoldOrderDao.class);
         MemberWalletContractDao walletContractDao = SpringContextHolder.getBean(MemberWalletContractDao.class);
+        CacheSettingUtils cacheSettingUtils = SpringContextHolder.getBean(CacheSettingUtils.class);
         RedisUtils redisUtils = SpringContextHolder.getBean(RedisUtils.class);
-        BigDecimal newPrice = new BigDecimal(redisUtils.getString(CoinTypeConvert.convertToKey(contractHoldOrderEntity.getSymbol())));
 
         Long memberId = memberEntity.getId();
         MemberWalletContractEntity walletContract = walletContractDao.findWalletContractByMemberIdAndSymbol(memberId, CoinTypeEnum.USDT.name());
@@ -230,7 +233,32 @@
                 } else {
                     totalBondAmount = totalBondAmount.add(holdOrderEntity.getBondAmount());
                 }
-                totalProfitOrLoss = totalProfitOrLoss.add(calProfitOrLoss(holdOrderEntity, memberEntity));
+
+
+                BigDecimal newPrice = new BigDecimal(redisUtils.getString(CoinTypeConvert.convertToKey(holdOrderEntity.getSymbol())));
+                if (type == 2) {
+                    log.info("newPrice : {}", newPrice);
+                }
+                // 盈亏
+                BigDecimal rewardRatio = BigDecimal.ZERO;
+                // 开多
+                if (ContractHoldOrderEntity.OPENING_TYPE_MORE == holdOrderEntity.getOpeningType()) {
+                    // (最新价-开仓价)*规格*张数
+                    rewardRatio = newPrice.subtract(holdOrderEntity.getOpeningPrice()).multiply(holdOrderEntity.getSymbolSku()).multiply(new BigDecimal(holdOrderEntity.getSymbolCntSale()));
+                    // 开空
+                } else {
+                    // (开仓价-最新价)*规格*张数
+                    rewardRatio = holdOrderEntity.getOpeningPrice().subtract(newPrice).multiply(holdOrderEntity.getSymbolSku()).multiply(new BigDecimal(holdOrderEntity.getSymbolCntSale()));
+                }
+
+                if (memberEntity.getIsProfit() == MemberEntity.IS_PROFIT_Y) {
+                    PlatformTradeSettingEntity tradeSettingEntity = cacheSettingUtils.getTradeSetting();
+                    if (rewardRatio.compareTo(BigDecimal.ZERO) > -1) {
+                        rewardRatio = rewardRatio.multiply(BigDecimal.ONE.subtract(tradeSettingEntity.getProfitParam()));
+                    }
+                }
+
+                totalProfitOrLoss = totalProfitOrLoss.add(rewardRatio);
             }
 
             // 维持保证金
@@ -238,23 +266,34 @@
             // 成本 数量*面值*开仓价
             BigDecimal cost = contractHoldOrderEntity.getSymbolSku().multiply(contractHoldOrderEntity.getOpeningPrice()).multiply(new BigDecimal(contractHoldOrderEntity.getSymbolCntSale()));
             // 费率
-            BigDecimal ratio = newPrice.multiply(new BigDecimal("0.005"));
+            BigDecimal ratio = contractHoldOrderEntity.getOpeningPrice().multiply(new BigDecimal("0.005"));
             // 总账户 - 维持保证金
             BigDecimal orderProfitOrLoss = walletContract.getTotalBalance().subtract(holdBond);
-            // 权益
-            BigDecimal qy = walletContract.getTotalBalance().add(totalProfitOrLoss);
+            // 权益 - 维持保证金
+            BigDecimal qy = walletContract.getTotalBalance().add(totalProfitOrLoss).subtract(holdBond);
+//            BigDecimal qy = BigDecimal.ZERO;
 
-            BigDecimal prefix = qy.add(cost).subtract(ratio);
+            BigDecimal prefix = cost.subtract(totalBondAmount);
             BigDecimal divideChild;
             if (ContractHoldOrderEntity.OPENING_TYPE_MORE == contractHoldOrderEntity.getOpeningType()) {
-                divideChild = prefix.subtract(orderProfitOrLoss);
+                divideChild = prefix.subtract(orderProfitOrLoss).subtract(qy);
             } else {
-                divideChild = prefix.add(orderProfitOrLoss);
+                divideChild = prefix.add(orderProfitOrLoss).add(qy);
             }
 //            BigDecimal divideChild = walletContract.getTotalBalance().add(totalProfitOrLoss).subtract(holdBond).subtract(cost).add(newPrice.multiply(new BigDecimal("0.005")));
             BigDecimal divideParent = contractHoldOrderEntity.getSymbolSku().multiply(new BigDecimal(contractHoldOrderEntity.getSymbolCntSale()));
 
             forcePrice = divideChild.divide(divideParent, 8, BigDecimal.ROUND_DOWN);
+            if (type == 2) {
+                log.info("=======全仓爆仓=======");
+                log.info("holdBond : {}", holdBond);
+                log.info("cost : {}", cost);
+                log.info("ratio : {}", ratio);
+                log.info("orderProfitOrLoss : {}", orderProfitOrLoss);
+                log.info("total : {}", walletContract.getTotalBalance());
+                log.info("totalProfitOrLoss : {}", totalProfitOrLoss);
+                log.info("qy : {}", qy);
+            }
         }
         return forcePrice;
     }

--
Gitblit v1.9.1