| | |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | | |
| | | /** |
| | | * 网格元素,表示网格交易策略中的一个价格层级。 |
| | | * 网格价格层级,策略的最小操作单元。 |
| | | * |
| | | * <p>每个网格层级包含该价格位置上的多仓和空仓挂单信息, |
| | | * 通过编号和网格价格唯一标识一个网格层级。 |
| | | * <h3>定位</h3> |
| | | * 每个 GridElement 对应网格中的一个价格点,同时持有该点的多仓和空仓挂单状态。 |
| | | * 与传统的"价格队列+Map"模式不同,GridElement 将价格、方向、状态、订单ID 聚合到一个对象中, |
| | | * 并维护全局静态 HashMap 索引实现 O(1) 双向查询。 |
| | | * |
| | | * <h3>关键字段</h3> |
| | | * <ul> |
| | | * <li><b>编号</b>:网格层级的唯一标识</li> |
| | | * <li><b>网格价格</b>:该层级对应的触发价格</li> |
| | | * <li><b>多仓挂单</b>:是否已有多头挂单、多头挂单参数</li> |
| | | * <li><b>空仓挂单</b>:是否有空头挂单、空头挂单参数</li> |
| | | * </ul> |
| | | * <h3>ID 体系与链表</h3> |
| | | * <pre> |
| | | * ID ≦ -1: 空仓队列区域(降序),ID 自减,gridPrice 递减 |
| | | * ID = 0: 基座位置,gridPrice = shortBaseEntryPrice |
| | | * ID ≧ 1: 多仓队列区域(升序),ID 自增,gridPrice 递增 |
| | | * |
| | | * 链表: ... ← -3 ← -2 ← -1 ← 0 → 1 → 2 → 3 → ... |
| | | * (通过 upId/downId + INDEX 实现 O(1) 遍历) |
| | | * </pre> |
| | | * |
| | | * <h3>字段分组</h3> |
| | | * <table> |
| | | * <tr><th>类别</th><th>字段</th><th>说明</th></tr> |
| | | * <tr><td>标识</td><td>id, gridPrice, upId, downId</td><td>编号、价格、双向链表指针</td></tr> |
| | | * <tr><td>多仓订单</td><td>hasLongOrder, longOrderId, longTraderParam</td><td>是否有挂单、订单ID、完整参数</td></tr> |
| | | * <tr><td>空仓订单</td><td>hasShortOrder, shortOrderId, shortTraderParam</td><td>是否有挂单、订单ID、完整参数</td></tr> |
| | | * <tr><td>止盈</td><td>longTakeProfitOrderId, shortTakeProfitOrderId</td><td>止盈条件单ID</td></tr> |
| | | * </table> |
| | | * |
| | | * <h3>6 个全局 O(1) 索引</h3> |
| | | * <pre> |
| | | * INDEX → findById(int) ID → 元素 |
| | | * PRICE_INDEX → findByPrice(BigDecimal) 价格 → 元素 |
| | | * LONG_ORDER_ID_INDEX → findByLongOrderId(String) 多仓挂单ID → 元素 |
| | | * SHORT_ORDER_ID_INDEX → findByShortOrderId(String) 空仓挂单ID → 元素 |
| | | * LONG_TP_ORDER_ID_INDEX→ findByLongTakeProfitOrderId(String) 多止盈ID → 元素 |
| | | * SHORT_TP_ORDER_ID_INDEX→ findByShortTakeProfitOrderId(String) 空止盈ID → 元素 |
| | | * </pre> |
| | | * 索引通过 {@link #rebuildIndex(List)}(全量重建)或 {@link #refreshIndices()}(增量刷新) |
| | | * 维护,每次操作后自动打印全量网格状态到控制台。 |
| | | * |
| | | * <h3>何时填充 TraderParam</h3> |
| | | * 初始化时 {@code updateGridElements()} 为每个元素预填充 longTraderParam 和 shortTraderParam |
| | | * (含 direction/entryPrice/takeProfitPrice/quantity),订单ID字段在挂单成功后由 |
| | | * {@link GateGridTradeService} 的 4 个辅助方法写入。 |
| | | * |
| | | * <h3>使用示例</h3> |
| | | * <pre> |
| | | * GridElement element = GridElement.builder() |
| | | * .id(1) |
| | | * .gridPrice(new BigDecimal("2300.0")) |
| | | * .build(); |
| | | * |
| | | * // 挂多仓单 |
| | | * TraderParam longParam = TraderParam.builder() |
| | | * .direction(TraderParam.Direction.LONG) |
| | | * .entryPrice(new BigDecimal("2293.7")) |
| | | * .takeProfitPrice(new BigDecimal("2301.6")) |
| | | * .build(); |
| | | * element.setHasLongOrder(true); |
| | | * element.setLongTraderParam(longParam); |
| | | * GridElement elem = GridElement.findById(1); |
| | | * boolean hasLong = elem.isHasLongOrder(); // 是否挂了多单 |
| | | * BigDecimal tpPrice = elem.getLongTraderParam().getTakeProfitPrice(); // 止盈价 |
| | | * elem.getUp(); // 上一个网格(O(1)) |
| | | * elem.getDown(); // 下一个网格(O(1)) |
| | | * </pre> |
| | | * |
| | | * @author Administrator |
| | |
| | | sorted.sort((a, b) -> Integer.compare(a.getId(), b.getId())); |
| | | StringBuilder sb = new StringBuilder("\n========== 网格数据 ==========\n"); |
| | | for (GridElement e : sorted) { |
| | | sb.append(String.format( |
| | | " ID=%4d 价格=%s up=%s down=%s 多仓=%s(%s) 空仓=%s(%s) 多止盈=%s 空止盈=%s\n", |
| | | e.getId(), |
| | | e.getGridPrice(), |
| | | e.getUpId(), |
| | | e.getDownId(), |
| | | e.isHasLongOrder() ? "有" : "无", |
| | | e.getLongOrderId() != null ? e.getLongOrderId() : "-", |
| | | e.isHasShortOrder() ? "有" : "无", |
| | | e.getShortOrderId() != null ? e.getShortOrderId() : "-", |
| | | e.getLongTakeProfitOrderId() != null ? e.getLongTakeProfitOrderId() : "-", |
| | | e.getShortTakeProfitOrderId() != null ? e.getShortTakeProfitOrderId() : "-" |
| | | )); |
| | | if (e.isHasLongOrder() || e.isHasShortOrder()){ |
| | | sb.append(String.format( |
| | | " ID=%4d 价格=%s up=%s down=%s 多仓=%s(%s) 空仓=%s(%s) 多止盈=%s 空止盈=%s\n", |
| | | e.getId(), |
| | | e.getGridPrice(), |
| | | e.getUpId(), |
| | | e.getDownId(), |
| | | e.isHasLongOrder() ? "有" : "无", |
| | | e.getLongOrderId() != null ? e.getLongOrderId() : "-", |
| | | e.isHasShortOrder() ? "有" : "无", |
| | | e.getShortOrderId() != null ? e.getShortOrderId() : "-", |
| | | e.getLongTakeProfitOrderId() != null ? e.getLongTakeProfitOrderId() : "-", |
| | | e.getShortTakeProfitOrderId() != null ? e.getShortTakeProfitOrderId() : "-" |
| | | )); |
| | | } |
| | | } |
| | | sb.append(String.format( |
| | | "------------------------------------------------------------\n" + |
| | | " 索引统计: ID=%d 价格=%d 多仓订单ID=%d 空仓订单ID=%d 多止盈ID=%d 空止盈ID=%d\n", |
| | | INDEX.size(), |
| | | PRICE_INDEX.size(), |
| | | LONG_ORDER_ID_INDEX.size(), |
| | | SHORT_ORDER_ID_INDEX.size(), |
| | | LONG_TP_ORDER_ID_INDEX.size(), |
| | | SHORT_TP_ORDER_ID_INDEX.size() |
| | | )); |
| | | sb.append(String.format(" 多仓订单ID索引: %s\n", LONG_ORDER_ID_INDEX.keySet())); |
| | | sb.append(String.format(" 空仓订单ID索引: %s\n", SHORT_ORDER_ID_INDEX.keySet())); |
| | | sb.append(String.format(" 多止盈ID索引: %s\n", LONG_TP_ORDER_ID_INDEX.keySet())); |
| | | sb.append(String.format(" 空止盈ID索引: %s\n", SHORT_TP_ORDER_ID_INDEX.keySet())); |
| | | sb.append("================================\n"); |
| | | System.out.println(sb); |
| | | } |
| | |
| | | public static List<GridElement> findAllShortOrders(BigDecimal currentPrice) { |
| | | List<GridElement> result = new ArrayList<>(); |
| | | for (GridElement e : INDEX.values()) { |
| | | if (e.isHasShortOrder() && e.getGridPrice().compareTo(currentPrice) < 0) { |
| | | if (e.isHasShortOrder() && e.getGridPrice().compareTo(currentPrice) > 0) { |
| | | result.add(e); |
| | | } |
| | | } |