| | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.xcong.excoin.modules.okxNewPrice.okxWs.enums.CoinEnums; |
| | | import com.xcong.excoin.modules.okxNewPrice.okxWs.enums.OrderParamEnums; |
| | | import com.xcong.excoin.modules.okxNewPrice.okxWs.wanggeList.WangGeListEnum; |
| | | import com.xcong.excoin.modules.okxNewPrice.okxpi.MallUtils; |
| | | import com.xcong.excoin.modules.okxNewPrice.utils.WsMapBuild; |
| | | import com.xcong.excoin.modules.okxNewPrice.utils.WsParamBuild; |
| | | import com.xcong.excoin.utils.RedisUtils; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.java_websocket.client.WebSocketClient; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.util.Map; |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | | |
| | | /** |
| | | * @author Administrator |
| | | */ |
| | | @Slf4j |
| | | public class OrderInfoWs { |
| | | |
| | | // 使用双层Map,第一层key为账号名称,第二层key为数据key |
| | | public static final Map<String, Map<String, String>> ORDERINFOWSMAP = new ConcurrentHashMap<>(); |
| | | |
| | | // 获取指定账号的Map,如果不存在则创建 |
| | | public static Map<String, String> getAccountMap(String accountName) { |
| | | return ORDERINFOWSMAP.computeIfAbsent(accountName, k -> new ConcurrentHashMap<>()); |
| | | } |
| | | |
| | | public static final String ORDERINFOWS_CHANNEL = "orders"; |
| | | |
| | |
| | | String connId = MallUtils.getOrderNum(ORDERINFOWS_CHANNEL); |
| | | JSONObject jsonObject = WsParamBuild.buildJsonObject(connId, option, argsArray); |
| | | webSocketClient.send(jsonObject.toJSONString()); |
| | | log.info("发送订单频道频道:{}", option); |
| | | // log.info("发送订单频道频道:{}", option); |
| | | } catch (Exception e) { |
| | | log.error("订阅订单频道构建失败", e); |
| | | } |
| | | } |
| | | |
| | | public static void initEvent(JSONObject response, String accountName) { |
| | | // log.info("订阅成功: {}", response.getJSONObject("arg")); |
| | | } |
| | | |
| | | |
| | |
| | | private static final String ACCFILLSZ_KEY = "accFillSz"; |
| | | private static final String AVGPX_KEY = "avgPx"; |
| | | private static final String STATE_KEY = "state"; |
| | | public static void handleEvent(JSONObject response, RedisUtils redisUtils) { |
| | | public static void handleEvent(JSONObject response, RedisUtils redisUtils, String accountName) { |
| | | |
| | | // log.info("开始执行OrderInfoWs......"); |
| | | try { |
| | | JSONArray dataArray = response.getJSONArray(DATA_KEY); |
| | | if (dataArray == null || dataArray.isEmpty()) { |
| | |
| | | String state = detail.getString(STATE_KEY); |
| | | |
| | | log.info( |
| | | "订单详情-币种: {}, 系统编号: {}, 自定义编号: {}, 订单方向: {}, 交易模式: {}," + |
| | | "{}: 订单详情-币种: {}, 系统编号: {}, 自定义编号: {}, 订单方向: {}, 交易模式: {}," + |
| | | " 累计成交数量: {}, 成交均价: {}, 订单状态: {}", |
| | | instId, ordId, clOrdId, side, tdMode, |
| | | accountName, instId, ordId, clOrdId, side, tdMode, |
| | | accFillSz, avgPx,state |
| | | ); |
| | | |
| | | String clOrdIdStr = (String) redisUtils.get(TradeOrderWs.ORDERWS_CHANNEL + ":" + CoinEnums.HE_YUE.getCode() + ":clOrdId"); |
| | | String stateStr = (String) redisUtils.get(TradeOrderWs.ORDERWS_CHANNEL + ":" + CoinEnums.HE_YUE.getCode() + ":state"); |
| | | String clOrdIdStr = TradeOrderWs.getAccountMap(accountName).get("clOrdId"); |
| | | String stateStr = TradeOrderWs.getAccountMap(accountName).get("state"); |
| | | if ( |
| | | StrUtil.isNotBlank(clOrdIdStr) |
| | | && clOrdId.equals(clOrdIdStr) |
| | | && StrUtil.isNotBlank(stateStr) |
| | | && state.equals(stateStr) |
| | | ){ |
| | | redisUtils.set(InstrumentsWs.INSTRUMENTSWS_CHANNEL + ":" + CoinEnums.HE_YUE.getCode() + ":state", OrderParamEnums.STATE_1.getValue(), 0); |
| | | Map<String, String> accountMap = getAccountMap(accountName); |
| | | //记录成交均价 |
| | | log.info("成交均价: {}", avgPx); |
| | | WsMapBuild.saveStringToMap(accountMap, "orderPrice",avgPx); |
| | | |
| | | WsMapBuild.saveStringToMap(TradeOrderWs.getAccountMap(accountName), "state", CoinEnums.ORDER_LIVE.getCode()); |
| | | |
| | | //保存上一个网格信息 |
| | | WangGeListEnum gridByPrice = WangGeListEnum.getGridByPrice(new BigDecimal(avgPx)); |
| | | if (gridByPrice != null){ |
| | | log.info("保存上一个网格: {}", gridByPrice.name()); |
| | | Map<String, String> instrumentsMap = InstrumentsWs.getAccountMap(accountName); |
| | | WsMapBuild.saveStringToMap(instrumentsMap, CoinEnums.WANG_GE_OLD.name(), gridByPrice.name()); |
| | | } |
| | | |
| | | // 使用账号特定的Map |
| | | String positionAccountName = PositionsWs.initAccountName(accountName, side); |
| | | Map<String, BigDecimal> positionsMap = PositionsWs.getAccountMap(positionAccountName); |
| | | WsMapBuild.saveBigDecimalToMap(positionsMap, CoinEnums.READY_STATE.name(), WsMapBuild.parseBigDecimalSafe(CoinEnums.READY_STATE_NO.getCode())); |
| | | |
| | | Map<String, String> accountWsMap = AccountWs.getAccountMap(accountName); |
| | | WsMapBuild.saveStringToMap(accountWsMap, CoinEnums.READY_STATE.name(), CoinEnums.READY_STATE_NO.getCode()); |
| | | |
| | | log.info("{}: 订单详情已完成: {}, 自定义编号: {}", accountName, CoinEnums.HE_YUE.getCode(), clOrdId); |
| | | } |
| | | } |
| | | } catch (Exception e) { |