1
zj
2025-05-30 41bc089a838c108a945706096a2b874cbcdd8486
src/main/java/project/contract/internal/ContractOrderServiceImpl.java
@@ -1,5 +1,7 @@
package project.contract.internal;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
@@ -74,7 +76,7 @@
      order.setPips_amount(item.getPips_amount());
      order.setCreate_time(new Date());
        ApplicationUtil.executeInsert(order);
      ApplicationUtil.executeInsert(order);
      redisHandler.setSync(ContractRedisKeys.CONTRACT_ORDERNO + order.getOrder_no(), order);
      Map<String, ContractOrder> map = (Map<String, ContractOrder>) redisHandler.get(ContractRedisKeys.CONTRACT_SUBMITTED_ORDER_PARTY_ID + order.getPartyId().toString());
@@ -511,8 +513,8 @@
      if (ContractOrder.STATE_SUBMITTED.equals(order.getState())) {
         double rate = Arith.div(order.getVolume(), order.getVolume_open());
         map.put("profit",df.format(Arith.sub(
                     Arith.add(Arith.add(order.getAmount_close(),Arith.mul(order.getProfit(),rate)), order.getDeposit()),
                     order.getDeposit_open())));
               Arith.add(Arith.add(order.getAmount_close(),Arith.mul(order.getProfit(),rate)), order.getDeposit()),
               order.getDeposit_open())));
      } else {
         map.put("profit", df.format(order.getProfit()));
      }
@@ -540,24 +542,30 @@
      double totleMoney = wallet.getMoney();
      profit = Arith.add(profit,totleMoney);
      if (map.get("direction") != null) {
         String direction = map.get("direction").toString();
         double liquidationPrice;
         double deposit = depositOpen + profit;
         double deposit =  profit;
         if(leverRate == 1){
            liquidationPrice = 0;
         }else {
            if ("sell".equals(direction)) {
               liquidationPrice = tradeAvgPrice + (deposit / div);
               double lossPercentage = deposit / (Arith.mul(order.getVolume(), order.getUnit_amount()));
               double priceIncrease = lossPercentage * tradeAvgPrice;
               liquidationPrice = tradeAvgPrice + priceIncrease;
            } else {
               liquidationPrice = (mul * tradeAvgPrice) / (deposit + mul);
               double lossPercentage = deposit / (Arith.mul(order.getVolume(), order.getUnit_amount()));
               double priceDrop = lossPercentage * tradeAvgPrice;
               liquidationPrice = tradeAvgPrice - priceDrop;
            }
         }
         DecimalFormat dfs = new DecimalFormat("#.#####");
         String formattedPrice = dfs.format(liquidationPrice);
         // 检查是否为负数
         if (Double.parseDouble(formattedPrice) < 0) {
            formattedPrice = "0";
         }
         map.put("qiangPing", formattedPrice);
      }
@@ -566,48 +574,6 @@
      return map;
   }
//
//
   public static void main(String[] args) {
      Map<String, Object> map = new HashMap<>();
      map.put("direction", "buy");  // "buy" 或 "sell"
      double currentPrice = 59649.7;  // 开仓价格
      double leve = 10;  // 杠杆倍数
      double bzj = 500; // 保证金
      double zjbzj = 500; // 资金账户
      double xj = 70781.28; // 现价
      double mul = Arith.mul(bzj, leve);//仓位
      double div = Arith.div(mul, currentPrice);//持有币的数量
      double amount = Arith.mul(div, Arith.sub(xj, currentPrice));
      System.out.println(amount);
      if (map.get("direction") != null) {
         String direction = map.get("direction").toString();
         double liquidationPrice;
         double bcbzj = bzj + zjbzj;
         if(leve == 1){
            liquidationPrice = 0;
         }else{
            if ("sell".equals(direction)) {
               liquidationPrice = currentPrice + (bcbzj / div);
            } else {
               liquidationPrice = (mul * currentPrice) / (bcbzj + mul);
            }
         }
         DecimalFormat dfs = new DecimalFormat("#.#####");
         String formattedPrice = dfs.format(liquidationPrice);
         map.put("qiangPing", formattedPrice);
      }
      // 打印结果以验证
      System.out.println("Calculated Liquidation Price: " + map.get("qiangPing"));
   }
//
   @Override
   public List<ContractOrder> findSubmitted() {