package com.yami.trading.service.impl;
|
|
import com.yami.trading.service.StrongLevelCalculationService;
|
import org.springframework.stereotype.Service;
|
|
import java.math.BigDecimal;
|
import java.math.RoundingMode;
|
|
/**
|
* @program: trading-order-master
|
* @description:
|
* @create: 2025-01-15 16:12
|
**/
|
@Service
|
public class StrongLevelCalculationServiceImpl implements StrongLevelCalculationService {
|
|
/**
|
* 多仓强平价格计算 多仓预估强平价 =(保证金余额-面值 *|张数|*开仓均价)/(面值*张数|*(维持保证金率+手续费率 -1));
|
* @param marginBalance 保证金余额
|
* @param faceValue 合约面值
|
* @param contractQuantity 合约张数
|
* @param openingPrice 开仓均价
|
* @param maintenanceMarginRate 维持保证金率
|
* @param feeRate 手续费率
|
* @return
|
*/
|
@Override
|
public 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);
|
|
// 计算强平价
|
double result = numerator / denominator;
|
if (result < 0) {
|
return 0;
|
}
|
return new BigDecimal(result).setScale(2, RoundingMode.HALF_UP).doubleValue();
|
}
|
|
|
/**
|
* 空仓强平价格计算 空仓预估强平价 =(保证金余额+面值 *|张数|*开仓均价)/(面值*|张数|*(维持金率+王续费率 +1))
|
* @param marginBalance 保证金余额
|
* @param faceValue 合约面值
|
* @param contractQuantity 合约张数
|
* @param openingPrice 开仓均价
|
* @param maintenanceMarginRate 维持保证金率
|
* @param feeRate 手续费率
|
* @return
|
*/
|
@Override
|
public 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);
|
|
// 计算空仓预估强平价
|
double result = numerator / denominator;
|
if (result < 0) {
|
return 0;
|
}
|
return new BigDecimal(result).setScale(2, RoundingMode.HALF_UP).doubleValue();
|
}
|
|
|
|
/**
|
*
|
* @param earnestMoney 保证金
|
* @param level 杠杆
|
* @param faceValue 面值
|
* @param recentQuotation 最新价格
|
* @return 合约张数 张数=可用保证金*杠杆倍数/(面值*最新成交价)
|
*/
|
@Override
|
public double countSheets(double earnestMoney, int level, double faceValue, double recentQuotation){
|
double result = earnestMoney * level / (faceValue * recentQuotation);
|
|
BigDecimal bd = new BigDecimal(result).setScale(2, RoundingMode.DOWN);
|
|
return bd.doubleValue();
|
}
|
|
|
|
|
public static void main(String[] args) {
|
// 给定参数
|
double marginBalance = 10; // 保证金余额
|
double faceValue = 0.01; // 合约面值(固定面值不能调整)
|
double contractQuantity = 0.5; // 合约张数 张数=可用保证金*杠杆倍数/(面值*最新成交价)
|
double openingPrice = 97016.4; // 开仓均价
|
double maintenanceMarginRate = 0.004; // 维持保证金率(固定不变)
|
double feeRate = 0.0005; // 手续费率 根据实际设置
|
|
// 计算强平价
|
double liquidationPrice = demoA(marginBalance, faceValue, contractQuantity,
|
openingPrice, maintenanceMarginRate, feeRate);
|
|
// 输出结果
|
System.out.println("多仓预估强平价: " + liquidationPrice);
|
|
// 计算空仓预估强平价
|
double liquidationPrice2 = demoB(marginBalance, faceValue, contractQuantity,
|
openingPrice, maintenanceMarginRate, feeRate);
|
|
// 输出结果
|
System.out.println("空仓预估强平价: " + liquidationPrice2);
|
}
|
|
public static double demoA (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);
|
|
// 计算强平价
|
double result = numerator / denominator;
|
if (result < 0) {
|
return 0;
|
}
|
return new BigDecimal(result).setScale(2, RoundingMode.HALF_UP).doubleValue();
|
}
|
|
|
/**
|
* 空仓强平价格计算 空仓预估强平价 =(保证金余额+面值 *|张数|*开仓均价)/(面值*|张数|*(维持金率+王续费率 +1))
|
* @param marginBalance 保证金余额
|
* @param faceValue 合约面值
|
* @param contractQuantity 合约张数
|
* @param openingPrice 开仓均价
|
* @param maintenanceMarginRate 维持保证金率
|
* @param feeRate 手续费率
|
* @return
|
*/
|
public static double demoB (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);
|
|
// 计算空仓预估强平价
|
double result = numerator / denominator;
|
if (result < 0) {
|
return 0;
|
}
|
return new BigDecimal(result).setScale(2, RoundingMode.HALF_UP).doubleValue();
|
}
|
|
}
|