| | |
| | | package project.contract.internal; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.text.DecimalFormat; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | | |
| | | import org.apache.commons.collections.CollectionUtils; |
| | | import org.apache.commons.lang3.ObjectUtils; |
| | | |
| | | import kernel.util.Arith; |
| | | import kernel.util.DateUtils; |
| | |
| | | import kernel.web.ApplicationUtil; |
| | | import kernel.web.Page; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.util.ObjectUtils; |
| | | import project.Constants; |
| | | import project.contract.ContractApplyOrder; |
| | | import project.contract.ContractApplyOrderService; |
| | |
| | | double profit = this.settle(order, volume); |
| | | update(order); |
| | | // if (profit > 0) { |
| | | |
| | | Wallet wallet = this.walletService.saveWalletByPartyId(order.getPartyId()); |
| | | double amount_before = wallet.getMoney(); |
| | | Object money = redisHandler.get("PARTY_ID_MONEY_" + order.getPartyId().toString()); |
| | | if(ObjectUtils.isEmpty(money)){ |
| | | wallet.setMoney(wallet.getMoney()); |
| | | }else{ |
| | | wallet.setMoney(Double.parseDouble(money.toString())); |
| | | } |
| | | |
| | | if (Arith.add(wallet.getMoney(), profit) < 0) { |
| | | profit = Arith.sub(0, wallet.getMoney()); |
| | | } |
| | | |
| | | // wallet.setMoney(Arith.add(wallet.getMoney(), profit));/ |
| | | if (Arith.add(wallet.getMoney(), profit) < 0) { |
| | |
| | | |
| | | Wallet wallet = this.walletService.saveWalletByPartyId(order.getPartyId()); |
| | | double amount_before = wallet.getMoney(); |
| | | |
| | | Object money = redisHandler.get("PARTY_ID_MONEY_" + order.getPartyId().toString()); |
| | | if(ObjectUtils.isEmpty(money)){ |
| | | wallet.setMoney(wallet.getMoney()); |
| | | }else{ |
| | | wallet.setMoney(Double.parseDouble(money.toString())); |
| | | } |
| | | |
| | | if (Arith.add(wallet.getMoney(), profit) < 0) { |
| | | profit = Arith.sub(0, wallet.getMoney()); |
| | |
| | | moneylog.setContent_type(Constants.MONEYLOG_CONTENT_CONTRACT_CLOSE); |
| | | |
| | | moneyLogService.save(moneylog); |
| | | |
| | | order.setState(ContractOrder.STATE_CREATED); |
| | | order.setVolume(0D); |
| | | order.setDeposit(0); |
| | | order.setClose_time(new Date()); |
| | | update(order); |
| | | |
| | | /** |
| | | * 合约产品平仓后添加当前流水 |
| | |
| | | */ |
| | | double rate = Arith.div(volume, order.getVolume_open()); |
| | | |
| | | //profit = Arith.mul(Arith.add(order.getDeposit(), order.getProfit()), rate); |
| | | //平仓收益 |
| | | // double mul = Arith.mul(order.getDeposit_open(), order.getLever_rate());//仓位 |
| | | // double div = Arith.div(mul, order.getTrade_avg_price());//持有币的数量 |
| | | // double amount = Arith.mul(div, Arith.sub(order.getClose_avg_price(), order.getTrade_avg_price())); |
| | | |
| | | profit = Arith.add(Arith.mul(order.getDeposit_open(),rate),Arith.mul(order.getProfit(),rate)); |
| | | order.setAmount_close(Arith.add(order.getAmount_close(), profit)); |
| | | order.setVolume(Arith.sub(order.getVolume(), volume)); |
| | |
| | | order.setState(ContractOrder.STATE_CREATED); |
| | | order.setClose_time(new Date()); |
| | | } |
| | | |
| | | update(order); |
| | | return profit; |
| | | |
| | | } |
| | |
| | | Arith.add(Arith.add(order.getAmount_close(),Arith.mul(order.getProfit(),rate)), order.getDeposit()), |
| | | order.getDeposit_open()))); |
| | | } else { |
| | | map.put("profit", df.format(Arith.sub(Arith.add(order.getAmount_close(), order.getDeposit()), order.getDeposit_open()))); |
| | | map.put("profit", df.format(order.getProfit())); |
| | | } |
| | | // 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 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); |
| | | map.put("qiangPing", formattedPrice); |
| | |
| | | |
| | | 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() { |