| | |
| | | import com.yami.trading.common.util.ThreadUtils; |
| | | import com.yami.trading.security.common.util.SecurityUtils; |
| | | import com.yami.trading.service.SessionTokenService; |
| | | import com.yami.trading.service.StrongLevelCalculationService; |
| | | import com.yami.trading.service.WalletService; |
| | | import com.yami.trading.service.contract.ContractApplyOrderService; |
| | | import com.yami.trading.service.contract.ContractLockService; |
| | |
| | | String partyId = SecurityUtils.getUser().getUserId(); |
| | | RLock rLock = redissonClient.getLock("contract_open_" + partyId); |
| | | boolean lockAcquired = false; |
| | | |
| | | double faceValue = 0.01;//面值 |
| | | double minAmount = 0.01;//最低张数 |
| | | try { |
| | | // 尝试获取锁,最多等待5秒 |
| | | lockAcquired = rLock.tryLock(5, TimeUnit.SECONDS); |
| | | if (!lockAcquired) { |
| | | log.warn("无法获取锁: contract_open_{}", partyId); |
| | | throw new YamiShopBindException("请稍后再试"); |
| | | } |
| | | //判断下单金额是否符合最低金额 最低下单张数:0.01 |
| | | //合约张数 张数=保证金*杠杆倍数/(面值*最新成交价) |
| | | double v = openAction.getAmount().doubleValue() * openAction.getLever_rate().doubleValue() / (faceValue * openAction.getPrice().doubleValue()); |
| | | BigDecimal amount = BigDecimal.valueOf(v).setScale(4, RoundingMode.DOWN); |
| | | if (amount.compareTo(new BigDecimal(faceValue)) < 0) { |
| | | double minimumAmount = minAmount * faceValue * openAction.getPrice().doubleValue() / openAction.getLever_rate().doubleValue(); |
| | | double roundedAmount = Math.ceil(minimumAmount * 10000) / 10000; |
| | | throw new YamiShopBindException("最低下单金额:"+roundedAmount); |
| | | } |
| | | |
| | | // 校验当前用户订单状态 |
| | |
| | | private void checkUserStatus(String partyId) { |
| | | User user = userService.getById(partyId); |
| | | if (!user.isEnabled()) { |
| | | throw new YamiShopBindException("用户已锁定"); |
| | | throw new YamiShopBindException("User is locked"); |
| | | } |
| | | } |
| | | |
| | |
| | | order.setSymbol(openAction.getSymbol()); |
| | | order.setDirection(openAction.getDirection()); |
| | | order.setOffset(ContractApplyOrder.OFFSET_OPEN); |
| | | order.setVolume(openAction.getAmount()); |
| | | order.setVolumeOpen(openAction.getAmount()); |
| | | // order.setVolume(openAction.getAmount()); |
| | | // order.setVolumeOpen(openAction.getAmount()); |
| | | order.setLeverRate(openAction.getLever_rate()); |
| | | order.setPrice(openAction.getPrice()); |
| | | order.setStopPriceProfit(openAction.getStop_price_profit()); |
| | | order.setStopPriceLoss(openAction.getStop_price_loss()); |
| | | order.setOrderPriceType(openAction.getPrice_type()); |
| | | order.setState(ContractApplyOrder.STATE_SUBMITTED); |
| | | order.setMoney(openAction.getAmount()); |
| | | order.setLocationType(openAction.getLocationType()); |
| | | |
| | | contractApplyOrderService.saveCreate(order); |
| | | } |
| | |
| | | try { |
| | | User user = userService.getById(partyId); |
| | | if (!user.isEnabled()) { |
| | | throw new YamiShopBindException("用户已锁定"); |
| | | throw new YamiShopBindException("User is locked"); |
| | | } |
| | | |
| | | Syspara syspara = sysparaService.find("stop_user_internet"); |
| | |
| | | map.put("price", order.getPrice()); |
| | | map.put("stop_price_profit", order.getStopPriceProfit()); |
| | | if( order.getStopPriceLoss() !=null) { |
| | | map.put("stop_price_loss", order.getStopPriceLoss().setScale(4, RoundingMode.HALF_UP)); |
| | | map.put("stop_price_loss", order.getStopPriceLoss().setScale(4, RoundingMode.DOWN)); |
| | | }else{ |
| | | map.put("stop_price_loss", null); |
| | | |
| | | } |
| | | map.put("price_type", order.getOrderPriceType()); |
| | | map.put("state", order.getState()); |
| | | map.put("amount", order.getVolume().multiply(order.getUnitAmount()).setScale(4, RoundingMode.HALF_UP)); |
| | | map.put("amount", order.getVolume().multiply(order.getUnitAmount()).setScale(4, RoundingMode.DOWN)); |
| | | map.put("amount_open", order.getVolumeOpen().multiply(order.getUnitAmount())); |
| | | map.put("fee", order.getFee()); |
| | | map.put("deposit", order.getDeposit()); |
| | |
| | | Realtime realtime = realtimeMap.get(data.getSymbol()); |
| | | BigDecimal mark_price = realtime.getClose(); |
| | | if(data.getDirection().equalsIgnoreCase("buy")){ |
| | | expectedProfitAndLoss += data.getVolume().multiply(data.getLeverRate()).multiply(data.getUnitAmount()).multiply(mark_price.subtract(data.getPrice()).divide(data.getPrice(), 4, RoundingMode.HALF_UP)).doubleValue(); |
| | | expectedProfitAndLoss += data.getVolume().multiply(data.getLeverRate()).multiply(data.getUnitAmount()).multiply(mark_price.subtract(data.getPrice()).divide(data.getPrice(), 4, RoundingMode.DOWN)).doubleValue(); |
| | | }else{ |
| | | expectedProfitAndLoss -= data.getVolume().multiply(data.getLeverRate()).multiply(data.getUnitAmount()).multiply(mark_price.subtract(data.getPrice()).divide(data.getPrice(), 4, RoundingMode.HALF_UP)).doubleValue(); |
| | | expectedProfitAndLoss -= data.getVolume().multiply(data.getLeverRate()).multiply(data.getUnitAmount()).multiply(mark_price.subtract(data.getPrice()).divide(data.getPrice(), 4, RoundingMode.DOWN)).doubleValue(); |
| | | } |
| | | //手续费 |
| | | double fee = Double.parseDouble(data.getFee().toString()); |