src/main/java/project/contract/internal/ContractApplyOrderServiceImpl.java
@@ -121,17 +121,20 @@ order.setOrder_no(DateUtil.getToday("yyMMddHHmmss") + RandomUtil.getRandomNum(8)); order.setUnit_amount(item.getUnit_amount()); order.setFee(Arith.mul(item.getUnit_fee(), order.getVolume())); order.setDeposit(Arith.mul(item.getUnit_amount(), order.getVolume())); double fee = calculateFee(order.getLever_rate(), order.getDeposit()); // order.setFee(Arith.mul(item.getUnit_fee(), order.getVolume())); order.setFee(fee); if (order.getLever_rate() != null) { /** * 加上杠杆 */ order.setVolume(Arith.mul(order.getVolume(), order.getLever_rate())); Syspara syspara = sysparaService.find("perpetual_contracts"); if(ObjectUtils.isEmpty(syspara)||"0".equals(syspara.getValue())) { order.setFee(Arith.mul(order.getFee(), order.getLever_rate())); } // Syspara syspara = sysparaService.find("perpetual_contracts"); // if(ObjectUtils.isEmpty(syspara)||"0".equals(syspara.getValue())) { // order.setFee(Arith.mul(order.getFee(), order.getLever_rate())); // } } order.setVolume_open(order.getVolume()); @@ -179,6 +182,21 @@ insertContractApplyOrder(order); } public double calculateFee(double leverRate, double totalCapital) { double feeRate = 0.0; if (leverRate == 25) { feeRate = 0.0375; // 3.75%手续费 } else if (leverRate == 50) { feeRate = 0.075; // 7.5%手续费 } else if (leverRate == 100) { feeRate = 0.15; // 15%手续费 } else if (leverRate == 200) { feeRate = 0.30; // 30%手续费 } return totalCapital * feeRate; // 根据总资金计算手续费 } /** * 平仓委托 */ 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.*; @@ -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() { src/main/java/project/project/web/admin/AdminContractOrderController.java
@@ -134,7 +134,8 @@ 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(map.get("party_id").toString()); double totleMoney = wallet.getMoney(); profit = Arith.add(profit,totleMoney); @@ -143,36 +144,29 @@ 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(Double.parseDouble(map.get("volume").toString()), Double.parseDouble(map.get("unit_amount").toString()))); double priceIncrease = lossPercentage * tradeAvgPrice; liquidationPrice = tradeAvgPrice + priceIncrease; } else { liquidationPrice = (mul * tradeAvgPrice) / (deposit + mul); double lossPercentage = deposit / (Arith.mul(Double.parseDouble(map.get("volume").toString()), Double.parseDouble(map.get("unit_amount").toString()))); 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); } // 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 df = new DecimalFormat("#.#####"); // String formattedPrice = df.format(liquidationPrice); // map.put("qiangPing",formattedPrice); // } } List<Item> items = this.itemService.cacheGetAll(); @@ -305,22 +299,31 @@ 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(Double.parseDouble(map.get("volume").toString()), Double.parseDouble(map.get("unit_amount").toString()))); double priceIncrease = lossPercentage * tradeAvgPrice; liquidationPrice = tradeAvgPrice + priceIncrease; } else { liquidationPrice = (mul * tradeAvgPrice) / (deposit + mul); double lossPercentage = deposit / (Arith.mul(Double.parseDouble(map.get("volume").toString()), Double.parseDouble(map.get("unit_amount").toString()))); 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); } } } catch (BusinessException e) { src/main/java/project/project/web/api/ContractOrderController.java
@@ -216,25 +216,29 @@ 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 / Double.parseDouble(map.get("amount").toString()); double priceIncrease = lossPercentage * tradeAvgPrice; liquidationPrice = tradeAvgPrice + priceIncrease; } else { liquidationPrice = (mul * tradeAvgPrice) / (deposit + mul); double lossPercentage = deposit / Double.parseDouble(map.get("amount").toString()); 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); } // 标记价格 Realtime realtime = realtimeMap.get(map.get("symbol")); if (null == realtime) { src/main/java/project/web/api/RealtimeController.java
@@ -67,8 +67,17 @@ realtime.setOrder(order); list_clone.add(realtime); } data = list_clone.stream().sorted(Comparator.comparing(Realtime::getClose).reversed()).collect(Collectors.toList()); // data = list_clone.stream().sorted(Comparator.comparing(Realtime::getClose).reversed()).collect(Collectors.toList()); // 自定义比较器,确保 BTC 排在第一,ETH 排在第二 } data.sort((r1, r2) -> { if (r1.getSymbol().equals("btc")) return -1; // BTC 应该排在第一 if (r2.getSymbol().equals("btc")) return 1; if (r1.getSymbol().equals("eth")) return -1; // ETH 应该排在第二 if (r2.getSymbol().equals("eth")) return 1; return 0; // 如果都不是 BTC 或 ETH,则保持原始顺序 }); resultObject.setData(this.revise(data)); return JSONObject.toJSONString(resultObject); } catch (BusinessException e) {