1
zj
2024-08-14 a949bf71283aeabbf4ac26adb7f183b67c02dfc7
src/main/java/project/contract/internal/ContractOrderServiceImpl.java
@@ -14,6 +14,7 @@
import kernel.util.StringUtils;
import kernel.web.ApplicationUtil;
import kernel.web.Page;
import org.springframework.beans.factory.annotation.Autowired;
import project.Constants;
import project.contract.ContractApplyOrder;
import project.contract.ContractApplyOrderService;
@@ -501,12 +502,113 @@
      } else {
         map.put("profit", df.format(Arith.sub(Arith.add(order.getAmount_close(), order.getDeposit()), order.getDeposit_open())));
      }
//      double currentPrice = Double.parseDouble(map.get("trade_avg_price").toString());
//
//      double leve = Double.parseDouble(map.get("lever_rate").toString());
//      //得到强平价格
//      if(null != map.get("direction")){
//         double liquidationPrice;
//         if(map.get("direction").equals("sell")){
//            liquidationPrice = currentPrice * (1 + (1 / leve));
//         }else{
//            liquidationPrice = currentPrice * (1 - 1 / leve);
//         }
//         DecimalFormat dfs = new DecimalFormat("#.#####");
//         String formattedPrice = dfs.format(liquidationPrice);
//         map.put("qiangPing",formattedPrice);
//      }
      double depositOpen =  Double.parseDouble(map.get("deposit_open").toString());
      double leverRate = Double.parseDouble(map.get("lever_rate").toString());
      double tradeAvgPrice =  Double.parseDouble(map.get("trade_avg_price").toString());
      double mul = Arith.mul(depositOpen, leverRate);//仓位
      double div = Arith.div(mul, tradeAvgPrice);//持有币的数量
      /**
       * 计算全仓收益 保证金
       */
      double profit = 0;
      List<ContractOrder> list = findSubmitted(order.getPartyId().toString(), null, null);
      for (int f = 0; f < list.size(); f++) {
         ContractOrder close_line = list.get(f);
         if(close_line.getProfit() > 0){
            profit = Arith.add(profit, Arith.add(close_line.getProfit(), close_line.getDeposit()));
         }else{
            profit = Arith.add(profit, close_line.getDeposit());
         }
      }
      Wallet wallet = this.walletService.saveWalletByPartyId(order.getPartyId().toString());
      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;
         if(leverRate == 1){
            liquidationPrice = 0;
         }else {
            if ("sell".equals(direction)) {
               liquidationPrice = tradeAvgPrice + (deposit / div);
            } else {
               liquidationPrice = (mul * tradeAvgPrice) / (deposit + mul);
            }
         }
         DecimalFormat dfs = new DecimalFormat("#.#####");
         String formattedPrice = dfs.format(liquidationPrice);
         map.put("qiangPing", formattedPrice);
      }
      map.put("volume", order.getVolume());
      map.put("volume_open", order.getVolume_open());
      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() {