Administrator
2025-12-17 90ae4f1fdad2be2720945e0f1474c971a0fdf1a6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.xcong.excoin.modules.okxNewPrice.okxpi.trade;
 
import com.xcong.excoin.modules.okxNewPrice.utils.FebsException;
import com.xcong.excoin.modules.okxNewPrice.okxpi.config.OKXAccount;
import com.xcong.excoin.modules.okxNewPrice.okxpi.trade.impl.TradeServiceBuy;
import com.xcong.excoin.modules.okxNewPrice.okxpi.trade.impl.TradeServiceClosePosition;
import com.xcong.excoin.modules.okxNewPrice.okxpi.trade.impl.TradeServiceSell;
 
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
 
/**
 * 交易事件运行类,用于根据不同的事件点选择相应的交易策略并执行
 */
public class TradeEventRunner {
 
    // 使用线程安全的ConcurrentHashMap存储交易事件和对应的策略服务
    private static final Map<String, TradeService> TRADE_EVENT_MAP = new ConcurrentHashMap<>();
 
    static {
        // 初始化策略
        List<TradeService> tradeServices = Arrays.asList(
                new TradeServiceBuy(), // 买入策略
                new TradeServiceSell(), // 卖出策略
                new TradeServiceClosePosition() // 平仓策略
        );
 
        // 将策略放入ConcurrentHashMap中,确保线程安全
        for (TradeService service : tradeServices) {
            TRADE_EVENT_MAP.put(service.tradeEvent(), service);
        }
    }
 
    /**
     * 根据事件点选择并执行相应的交易策略
     *
     * @param eventPoint 事件点,用于匹配相应的交易策略
     * @param tradeRequest 包含交易请求信息的LinkedHashMap
     * @param okxAccount OKX账户信息,用于执行交易
     * @return 交易执行的结果
     * @throws FebsException 如果找不到对应的交易策略,则抛出异常
     */
    public static String runTradeEvent(String eventPoint, LinkedHashMap<String, Object> tradeRequest, OKXAccount okxAccount) {
        TradeService tradeService = TRADE_EVENT_MAP.get(eventPoint);
        if (tradeService == null) {
            throw new FebsException("交易EVENT异常");
        }
 
        return tradeService.doTrade(tradeRequest,okxAccount);
    }
 
}