新版仿ok交易所-后端
zj
2025-02-08 75018b2f492444248d8b476d9703bb312d2befc3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.yami.trading.util;
 
/**
 * @program: trading-order-master
 * @description: 强平价格计算
 * @create: 2025-01-15 15:56
 **/
public class StrongLevelCalculationUtil {
 
    public static void main(String[] args) {
        // 给定参数
        double marginBalance = 968.802; // 保证金余额
        double faceValue = 0.01; // 合约面值(固定面值不能调整)
        double contractQuantity = 1; // 合约张数  张数=保证金/开仓均价*面值/杠杆
        double openingPrice = 96880.2; // 开仓均价
        double maintenanceMarginRate = 0.004; // 维持保证金率(固定不变)
        double feeRate = 0.0005; // 手续费率  根据实际设置
 
        // 计算强平价
        double liquidationPrice = calculateLiquidationPrice(marginBalance, faceValue, contractQuantity,
                openingPrice, maintenanceMarginRate, feeRate);
 
        // 输出结果
        System.out.println("多仓预估强平价: " + liquidationPrice);
 
        // 计算空仓预估强平价
        double liquidationPrice2 = calculateEmptyLiquidationPrice(marginBalance, faceValue, contractQuantity,
                openingPrice, maintenanceMarginRate, feeRate);
 
        // 输出结果
        System.out.println("空仓预估强平价: " + liquidationPrice2);
    }
 
    /**
     * 多仓强平价格计算 多仓预估强平价 =(保证金余额-面值 *|张数|*开仓均价)/(面值*张数|*(维持保证金率+手续费率 -1));
     * @param marginBalance 保证金余额
     * @param faceValue 合约面值
     * @param contractQuantity 合约张数
     * @param openingPrice 开仓均价
     * @param maintenanceMarginRate 维持保证金率
     * @param feeRate 手续费率
     * @return
     */
    public static double calculateLiquidationPrice(double marginBalance, double faceValue, double contractQuantity,
                                                   double openingPrice, double maintenanceMarginRate, double feeRate){
        // 计算分子部分
        double numerator = marginBalance - (faceValue * contractQuantity * openingPrice);
 
        // 计算分母部分
        double denominator = faceValue * contractQuantity * (maintenanceMarginRate + feeRate - 1);
 
        // 计算强平价
        return numerator / denominator;
    }
 
 
    /**
     *  空仓强平价格计算   空仓预估强平价 =(保证金余额+面值 *|张数|*开仓均价)/(面值*|张数|*(维持金率+王续费率 +1))
     * @param marginBalance 保证金余额
     * @param faceValue 合约面值
     * @param contractQuantity 合约张数
     * @param openingPrice 开仓均价
     * @param maintenanceMarginRate 维持保证金率
     * @param feeRate 手续费率
     * @return
     */
    public static double calculateEmptyLiquidationPrice(double marginBalance, double faceValue, double contractQuantity,
                                                        double openingPrice, double maintenanceMarginRate, double feeRate){
        // 计算分子部分
        double numerator = marginBalance + (faceValue * contractQuantity * openingPrice);
 
        // 计算分母部分
        double denominator = faceValue * contractQuantity * (maintenanceMarginRate + feeRate + 1);
 
        // 计算空仓预估强平价
        return numerator / denominator;
    }
}