| | |
| | | package com.yami.trading.service.contract; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.yami.trading.bean.contract.domain.ContractOrder; |
| | | import com.yami.trading.bean.data.domain.Realtime; |
| | | import com.yami.trading.bean.item.domain.Item; |
| | | import com.yami.trading.bean.model.Wallet; |
| | | import com.yami.trading.common.util.ThreadUtils; |
| | | import com.yami.trading.service.StrongLevelCalculationService; |
| | | import com.yami.trading.service.WalletService; |
| | | import com.yami.trading.service.data.DataService; |
| | | import com.yami.trading.service.item.ItemService; |
| | |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | @Slf4j |
| | | @Service |
| | |
| | | private DataService dataService; |
| | | @Autowired |
| | | private WalletService walletService; |
| | | |
| | | @Autowired |
| | | private StrongLevelCalculationService strongLevelCalculationService; |
| | | |
| | | private SysparaService sysparaService; |
| | | |
| | | public void saveCalculation(String order_no, List<ContractOrder> partyContractOrders) { |
| | |
| | | } |
| | | |
| | | /** |
| | | * 盈亏计算 |
| | | * 盈亏计算 收益=(平仓均价-开仓均价)*面值*张数 |
| | | * |
| | | * USDT保证金合约做多:收益=(平仓均价-开仓均价)*面值*张数 |
| | | * USDT保证金合约做空:收益=(开仓均价-平仓均价)*面值*张数 |
| | | * 以BTC为例,BTC一张合约面值为0.01BTC,在价格19000的时候,开了10张多单。当价格涨到20000的时候,小明的收益=(20000-19000)*0.01*10=100USDT |
| | | * 币本位合约: |
| | | * 做多:收益=面值*张数/开仓价-面值*张数/平仓价 |
| | | * 做空:收益=面值*张数/平仓价-面值*张数/开仓价 |
| | | * 以BTC为例,BTC一张合约面值为100美元,小明在价格20000的时候,开了5张空单。当价格下跌到19000的时候,小明的收益=100*5/19000-100*5/20000=0. |
| | | * |
| | | * @param profit_loss profit 盈 loss亏 |
| | | * @param currentPrice 当前点位 |
| | |
| | | /** |
| | | * 偏差点位 |
| | | */ |
| | | BigDecimal point = currentPrice.subtract(order.getTradeAvgPrice()).abs().divide(order.getPips(), 10, RoundingMode.HALF_UP); |
| | | BigDecimal point = currentPrice.subtract(order.getTradeAvgPrice()); |
| | | /* |
| | | * 根据偏 差点数和手数算出盈亏金额 |
| | | */ |
| | | BigDecimal amount = order.getPipsAmount().multiply(point).multiply(order.getVolume()); |
| | | BigDecimal amount = point.multiply(new BigDecimal("0.01")).multiply(order.getVolumeOpen()).setScale(4, BigDecimal.ROUND_DOWN);; |
| | | |
| | | if ("profit".equals(profit_loss)) { |
| | | /** |
| | | * 盈 正数 |
| | | */ |
| | | Item item = itemService.findBySymbol(order.getSymbol()); |
| | | |
| | | // if ("profit".equals(profit_loss)) { |
| | | // /** |
| | | // * 盈 正数 |
| | | // */ |
| | | // order.setProfit(amount); |
| | | // } else if ("loss".equals(profit_loss)) { |
| | | // order.setProfit(amount); |
| | | // |
| | | // } |
| | | |
| | | order.setProfit(amount); |
| | | } else if ("loss".equals(profit_loss)) { |
| | | order.setProfit(amount.negate()); |
| | | |
| | | double faceValue = 0.01; // 合约面值(固定面值不能调整) |
| | | double maintenanceMarginRate = 0.004; // 维持保证金率(固定不变) |
| | | |
| | | |
| | | /** |
| | | * 全仓收益加入保证金计算 |
| | | */ |
| | | BigDecimal earnings; |
| | | |
| | | if (order.getLocationType() == 1) { |
| | | earnings = BigDecimal.ZERO; |
| | | |
| | | // 统计非当前订单的其他收益 |
| | | List<ContractOrder> list = contractOrderService.list(new LambdaQueryWrapper<>(ContractOrder.class) |
| | | .eq(ContractOrder::getState, ContractOrder.STATE_SUBMITTED) |
| | | .eq(ContractOrder::getPartyId, order.getPartyId()) |
| | | .ne(ContractOrder::getOrderNo, order.getOrderNo()) |
| | | ); |
| | | |
| | | // 提前计算 currentPrice 与 order.getTradeAvgPrice() 的差值,避免重复计算 |
| | | BigDecimal priceDifference = currentPrice.subtract(order.getTradeAvgPrice()); |
| | | |
| | | // 计算所有订单的收益 |
| | | for (ContractOrder contractOrder : list) { |
| | | BigDecimal profit = priceDifference |
| | | .multiply(new BigDecimal("0.01")) |
| | | .multiply(contractOrder.getVolumeOpen()) |
| | | .setScale(4, RoundingMode.DOWN); |
| | | |
| | | earnings = earnings.add(profit); // 累加收益 |
| | | } |
| | | |
| | | // 获取当前账户余额并加到收益中 |
| | | Map<String, Object> moneyAll = walletService.getMoneyAll(order.getPartyId()); |
| | | earnings = earnings.add(new BigDecimal(moneyAll.get("money_all_coin").toString())); |
| | | |
| | | } else { |
| | | // 如果不符合条件,直接使用 order.getDepositOpen() 作为收益 |
| | | earnings = order.getDepositOpen(); |
| | | } |
| | | |
| | | if(ContractOrder.DIRECTION_BUY.equals(order.getDirection())){ |
| | | double forceClosePrice = strongLevelCalculationService.calculateLiquidationPrice(earnings.doubleValue(), |
| | | faceValue, order.getVolumeOpen().doubleValue(), order.getTradeAvgPrice().doubleValue() |
| | | , maintenanceMarginRate, item.getUnitFee().doubleValue()); |
| | | order.setForceClosePrice(BigDecimal.valueOf(forceClosePrice).toString()); |
| | | }else{ |
| | | double forceClosePrice = strongLevelCalculationService.calculateLiquidationPrice(earnings.doubleValue(), |
| | | faceValue, order.getVolumeOpen().doubleValue(), order.getTradeAvgPrice().doubleValue() |
| | | , maintenanceMarginRate, item.getUnitFee().doubleValue()); |
| | | order.setForceClosePrice(BigDecimal.valueOf(forceClosePrice).toString()); |
| | | } |
| | | /** |
| | | * 多次平仓价格不对,后续修 |
| | |
| | | * 买涨 |
| | | */ |
| | | if (currentPrice.compareTo(profitStop) >= 0) { |
| | | this.contractOrderService.saveClose(order.getPartyId().toString(), order.getOrderNo()); |
| | | this.contractOrderService.saveClose(order.getPartyId().toString(), order.getOrderNo(),null); |
| | | return; |
| | | } |
| | | } else if (profitStop != null && profitStop.compareTo(BigDecimal.ZERO) > 0 |
| | |
| | | * 买跌 |
| | | */ |
| | | if (currentPrice.compareTo(profitStop) <= 0) { |
| | | this.contractOrderService.saveClose(order.getPartyId().toString(), order.getOrderNo()); |
| | | this.contractOrderService.saveClose(order.getPartyId().toString(), order.getOrderNo(),null); |
| | | return; |
| | | } |
| | | } |
| | |
| | | * 买涨 |
| | | */ |
| | | if (currentPrice.compareTo(loss_stop) <= 0) { |
| | | this.contractOrderService.saveClose(order.getPartyId().toString(), order.getOrderNo()); |
| | | this.contractOrderService.saveClose(order.getPartyId().toString(), order.getOrderNo(),null); |
| | | return; |
| | | |
| | | } |
| | |
| | | */ |
| | | |
| | | if (currentPrice.compareTo(loss_stop) >= 0) { |
| | | this.contractOrderService.saveClose(order.getPartyId().toString(), order.getOrderNo()); |
| | | this.contractOrderService.saveClose(order.getPartyId().toString(), order.getOrderNo(),null); |
| | | return; |
| | | } |
| | | } |
| | | if (order_close_line_type == 1) { |
| | | /** |
| | | * 收益 |
| | | * 强平计算 |
| | | */ |
| | | BigDecimal profit = BigDecimal.ZERO; |
| | | |
| | | List<ContractOrder> list = partyContractOrders; |
| | | for (int i = 0; i < list.size(); i++) { |
| | | ContractOrder close_line = list.get(i); |
| | | profit = profit.add(close_line.getProfit()).add(close_line.getDeposit()); |
| | | } |
| | | //判断是全仓还是逐仓 |
| | | if (order.getLocationType() == 1) { |
| | | // /** |
| | | // * 收益 |
| | | // */ |
| | | // BigDecimal profit = BigDecimal.ZERO; |
| | | // |
| | | // List<ContractOrder> list = partyContractOrders; |
| | | // for (int i = 0; i < list.size(); i++) { |
| | | // ContractOrder close_line = list.get(i); |
| | | // profit = profit.add(close_line.getProfit()).add(close_line.getDeposit()); |
| | | // } |
| | | |
| | | |
| | | Wallet wallet = this.walletService.findByUserId(order.getPartyId().toString()); |
| | | // 计算所有除自己以外的profit |
| | | BigDecimal profitExptThis = profit.subtract(order.getProfit()).subtract(order.getDeposit()); |
| | | /** |
| | | * profitAll+wallet<=0 |
| | | * profitAll<=wallet 强平 |
| | | * p1 +E (p2~pn) <=wallet |
| | | * (currentPrice-tradavg)*pipAmount*volume/pips + depost1 <=wallet-E(p2~pn) |
| | | */ |
| | | BigDecimal left = wallet.getMoney().negate().subtract(profitExptThis).subtract(order.getDeposit()); |
| | | BigDecimal overLine = (left.multiply(order.getPips()).divide(order.getPipsAmount(), 10, RoundingMode.HALF_UP) |
| | | .divide(order.getVolume(), 10, RoundingMode.HALF_UP)); |
| | | Integer decimal = itemService.getDecimal(order.getSymbol()); |
| | | BigDecimal forceClose = BigDecimal.ZERO; |
| | | // 买多,从买价跌多少 |
| | | if (order.getDirection().equalsIgnoreCase(ContractOrder.DIRECTION_BUY)) { |
| | | forceClose = order.getTradeAvgPrice().add(overLine).setScale(decimal, RoundingMode.HALF_UP); |
| | | //买跌,涨到多少 |
| | | } else { |
| | | forceClose = order.getTradeAvgPrice().subtract(overLine).setScale(decimal, RoundingMode.HALF_UP); |
| | | } |
| | | if (forceClose.compareTo(BigDecimal.ZERO) < 0) { |
| | | forceClose = BigDecimal.ZERO; |
| | | } |
| | | order.setForceClosePrice(forceClose.toPlainString()); |
| | | // Wallet wallet = this.walletService.findByUserId(order.getPartyId().toString()); |
| | | this.contractOrderService.updateByIdBuffer(order); |
| | | |
| | | if (profit.add(wallet.getMoney()).compareTo(BigDecimal.ZERO) <= 0) { |
| | | // if (profit.add(wallet.getMoney()).compareTo(BigDecimal.ZERO) <= 0) { |
| | | if (currentPrice.toString().compareTo(order.getForceClosePrice()) <= 0) {//达到强平价 |
| | | /** |
| | | * 触发全仓强平 |
| | | */ |
| | | this.contractOrderService.saveClose(order.getPartyId().toString(), order.getOrderNo()); |
| | | ThreadUtils.sleep(100); |
| | | for (int i = 0; i < list.size(); i++) { |
| | | ContractOrder close_line = list.get(i); |
| | | if (!order.getOrderNo().equals(close_line.getOrderNo())) { |
| | | try { |
| | | if (ContractLock.add(close_line.getOrderNo())) { |
| | | this.contractOrderService.saveClose(close_line.getPartyId().toString(), |
| | | close_line.getOrderNo()); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("error:", e); |
| | | } finally { |
| | | ContractLock.remove(close_line.getOrderNo()); |
| | | } |
| | | |
| | | } |
| | | } |
| | | this.contractOrderService.allClose(order.getPartyId()); |
| | | |
| | | } |
| | | } else { |
| | | BigDecimal divide = order.getDeposit().divide(order.getProfit().abs(), 10, RoundingMode.HALF_UP); |
| | | if (order.getProfit().compareTo(BigDecimal.ZERO) < 0 && divide.compareTo(order_close_line.divide(new BigDecimal(100), 10, RoundingMode.HALF_UP)) <= 0) { |
| | | /** |
| | | * 低于系统默认平仓线,进行强平 |
| | | */ |
| | | this.contractOrderService.saveClose(order.getPartyId().toString(), order.getOrderNo()); |
| | | return; |
| | | if (currentPrice.toString().compareTo(order.getForceClosePrice()) <= 0) {//达到强平价 |
| | | this.contractOrderService.saveClose(order.getPartyId().toString(), order.getOrderNo(),"强平"); |
| | | } |
| | | } |
| | | |
| | | } |
| | | |
| | | // @Override |
| | | // public void afterPropertiesSet() throws Exception { |
| | | // order_close_line = this.sysparaService.find("order_close_line").getDouble(); |
| | | // order_close_line_type = this.sysparaService.find("order_close_line_type").getInteger(); |
| | | // |
| | | // } |
| | | |
| | | public void setDataService(DataService dataService) { |
| | | this.dataService = dataService; |