| | |
| | | package project.project.web.api; |
| | | package project.web.api; |
| | | |
| | | import java.io.IOException; |
| | | import java.text.DecimalFormat; |
| | |
| | | |
| | | @Autowired |
| | | private DataService dataService; |
| | | |
| | | |
| | | @Autowired |
| | | private ContractLockService contractLockService; |
| | | |
| | | |
| | | @Autowired |
| | | private ContractOrderService contractOrderService; |
| | | |
| | |
| | | private WalletService walletService; |
| | | |
| | | private final String action = "/api/contractOrder!"; |
| | | |
| | | |
| | | private static final Logger logger = LoggerFactory.getLogger(ContractOrderController.class); |
| | | |
| | | /** |
| | | * 平仓 |
| | | * |
| | | * |
| | | * order_no 订单号 |
| | | */ |
| | | @RequestMapping(action + "close.action") |
| | |
| | | if (!"0".equals(resultObject.getCode())) { |
| | | return resultObject; |
| | | } |
| | | |
| | | |
| | | try { |
| | | |
| | | CloseDelayThread lockDelayThread = new CloseDelayThread(this.getLoginPartyId(), order_no, this.contractOrderService, false); |
| | |
| | | if (!"0".equals(resultObject.getCode())) { |
| | | return resultObject; |
| | | } |
| | | |
| | | |
| | | try { |
| | | |
| | | CloseDelayThread lockDelayThread = new CloseDelayThread(this.getLoginPartyId(), "", this.contractOrderService, true); |
| | |
| | | |
| | | /** |
| | | * 订单列表 |
| | | * |
| | | * |
| | | * page_no 页码 |
| | | * symbol 币种 |
| | | * type 查询类型:orders 当前持仓单;hisorders 历史持仓单; |
| | |
| | | } |
| | | |
| | | int page_no_int = Integer.valueOf(page_no).intValue(); |
| | | |
| | | |
| | | if ("orders".equals(type)) { |
| | | // // 页条数配成1000达到不分页的效果 |
| | | // data = this.contractOrderService.getPaged(page_no_int, 1000, this.getLoginPartyId(), symbol, type); |
| | |
| | | } else if ("hisorders".equals(type)) { |
| | | data = this.contractOrderService.getPaged(page_no_int, 10, this.getLoginPartyId(), symbol, type,startTime,endTime); |
| | | } |
| | | |
| | | |
| | | String symbolsStr = ""; |
| | | Set<String> symbols = new HashSet<String>(); |
| | | for (int i = 0; i < data.size(); i++) { |
| | |
| | | } else { |
| | | symbolsStr = sym; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | List<Realtime> realtime_all = this.dataService.realtime(symbolsStr); |
| | | if (realtime_all.size() <= 0) { |
| | | realtime_all = new ArrayList<Realtime>(); |
| | | // throw new BusinessException("系统错误,请稍后重试"); |
| | | } |
| | | |
| | | |
| | | Map<String, Realtime> realtimeMap = new HashMap<String, Realtime>(); |
| | | for (int i = 0; i < realtime_all.size(); i++) { |
| | | realtimeMap.put(realtime_all.get(i).getSymbol(), realtime_all.get(i)); |
| | | } |
| | | |
| | | |
| | | for (int i = 0; i < data.size(); i++) { |
| | | Map<String, Object> map = data.get(i); |
| | | |
| | |
| | | profit = Arith.add(profit,totleMoney); |
| | | |
| | | |
| | | if (map.get("direction") != null) { |
| | | if (map.get("direction") != null && map.get("state").equals("submitted")) { |
| | | 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 / Double.parseDouble(map.get("amount").toString()); |
| | | double priceIncrease = lossPercentage * tradeAvgPrice; |
| | | liquidationPrice = tradeAvgPrice + priceIncrease; |
| | | } else { |
| | | liquidationPrice = (mul * tradeAvgPrice) / (deposit + mul); |
| | | double lossPercentage = deposit / Double.parseDouble(map.get("amount").toString()); |
| | | double priceDrop = lossPercentage * tradeAvgPrice; |
| | | liquidationPrice = tradeAvgPrice - priceDrop; |
| | | } |
| | | } |
| | | |
| | | // 检查 liquidationPrice 是否为有效数字 |
| | | if (Double.isInfinite(liquidationPrice) || Double.isNaN(liquidationPrice)) { |
| | | liquidationPrice = 0; // 如果是无效结果,回退为 0 |
| | | } |
| | | DecimalFormat dfs = new DecimalFormat("#.#####"); |
| | | String formattedPrice = dfs.format(liquidationPrice); |
| | | // 检查是否为负数 |
| | | if (Double.parseDouble(formattedPrice) < 0) { |
| | | formattedPrice = "0"; |
| | | } |
| | | map.put("qiangPing", formattedPrice); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | // 标记价格 |
| | | Realtime realtime = realtimeMap.get(map.get("symbol")); |
| | | if (null == realtime) { |
| | |
| | | map.put("mark_price", realtime.getClose()); |
| | | } |
| | | } |
| | | |
| | | |
| | | resultObject.setData(data); |
| | | |
| | | |
| | | } catch (BusinessException e) { |
| | | resultObject.setCode("1"); |
| | | resultObject.setMsg(e.getMessage()); |
| | |
| | | |
| | | /** |
| | | * 订单详情 |
| | | * |
| | | * |
| | | * order_no 订单号 |
| | | */ |
| | | @RequestMapping(action + "get.action") |
| | |
| | | } |
| | | |
| | | try { |
| | | |
| | | |
| | | if (StringUtils.isNullOrEmpty(order_no)) { |
| | | logger.info("contractOrder!get order_no null"); |
| | | throw new BusinessException("订单不存在"); |
| | | } |
| | | |
| | | ContractOrder order = this.contractOrderService.findByOrderNo(order_no); |
| | | |
| | | |
| | | if (null == order) { |
| | | logger.info("contractOrder!get order_no:" + order_no + ", order null"); |
| | | throw new BusinessException("订单不存在"); |
| | | } |
| | | |
| | | |
| | | resultObject.setData(this.contractOrderService.bulidOne(order)); |
| | | |
| | | |
| | | } catch (BusinessException e) { |
| | | resultObject.setCode("1"); |
| | | resultObject.setMsg(e.getMessage()); |
| | |
| | | private boolean all = false; |
| | | |
| | | public void run() { |
| | | |
| | | |
| | | try { |
| | | |
| | | |
| | | while (true) { |
| | | if (true == all) { |
| | | // 一键平仓 |
| | |
| | | // 处理完退出 |
| | | break; |
| | | } |
| | | ThreadUtils.sleep(500); |
| | | ThreadUtils.sleep(500); |
| | | } else { |
| | | // if (ContractLock.add(order_no)) { |
| | | if (contractLockService.getContractLock(order_no)) { |
| | |
| | | // 处理完退出 |
| | | break; |
| | | } |
| | | ThreadUtils.sleep(500); |
| | | ThreadUtils.sleep(500); |
| | | } |
| | | } |
| | | |
| | |
| | | this.all = all; |
| | | } |
| | | } |
| | | |
| | | |
| | | } |