新版仿ok交易所-后端
zj
2025-01-06 6e21cf6973aa1898259ddceda665f0f1b06272ab
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package com.yami.trading.huobi.data;
 
 
import com.yami.trading.bean.data.domain.*;
import com.yami.trading.huobi.data.internal.DepthTimeObject;
import com.yami.trading.huobi.data.internal.KlineTimeObject;
import com.yami.trading.huobi.data.internal.TradeTimeObject;
import com.yami.trading.huobi.data.internal.TrendTimeObject;
import org.apache.commons.lang3.StringUtils;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
 
public class DataCache {
    /**
     * 分时
     */
    private volatile static Map<String, TrendTimeObject> trend = new ConcurrentHashMap<String, TrendTimeObject>();
    /**
     * K线
     */
    private volatile static Map<String, PanKou> panKou = new ConcurrentHashMap<String, PanKou>();
    /**
     * K线
     */
    private volatile static Map<String, KlineTimeObject> kline = new ConcurrentHashMap<String, KlineTimeObject>();
 
 
    /**
     * 24小时的历史记录
     */
    private volatile static Map<String, List<Realtime>> realtimeHistory = new ConcurrentHashMap<String, List<Realtime>>();
    /**
     * 最高最低
     */
    private volatile static Map<String, Double> realtimeHigh = new ConcurrentHashMap<String, Double>();
    private volatile static Map<String, Double> realtimeLow = new ConcurrentHashMap<String, Double>();
    /**
     * 向前24小时时间点的开盘价
     */
    private volatile static Map<String, Double> realtime24HBeforeOpen = new ConcurrentHashMap<String, Double>();
 
    private volatile static Map<String, Kline> kline_hobi = new ConcurrentHashMap<String, Kline>();
 
    /**
     * 市场深度数据
     */
    private volatile static Map<String, DepthTimeObject> depth = new ConcurrentHashMap<String, DepthTimeObject>();
    /**
     * 市场深度数据
     */
    private volatile static Map<String, List<TradeDetails>> tradeList = new ConcurrentHashMap<String, List<TradeDetails>>();
    /**
     * 近期交易记录
     */
    private volatile static Map<String, TradeTimeObject> trade = new ConcurrentHashMap<String, TradeTimeObject>();
    /**
     * 最近60s实时价格信息列表
     */
    public static Map<String, List<Realtime>> latestRealTimeMap_60s = new ConcurrentHashMap<>();
 
    public static Realtime getLatestRealTime(String symbol) {
        return latestRealTime.get(symbol);
    }
 
    /**
     * 最新的实时价格信息缓存 <币种code, RealTime>
     */
    public static Map<String, Realtime> latestRealTime = new ConcurrentHashMap<>();
    //股票时区暂存
    public static Map<String, StockMarket> realMarketMap = new ConcurrentHashMap<>();
    public static void depthToTrade(Depth depth) {
        String symbol = depth.getSymbol();
        TradeTimeObject timeObject = DataCache.getTrade().get(symbol);
        if (timeObject == null) {
            timeObject = new TradeTimeObject();
        }
        timeObject.setLastTime(new Date());
 
        List<TradeEntry> data = new ArrayList<TradeEntry>();
        List<DepthEntry> asks = depth.getAsks();
        List<DepthEntry> bids = depth.getBids();
        List<TradeEntry> sell = asks.stream().map(a -> {
            TradeEntry tradeEntry = new TradeEntry();
            tradeEntry.setDirection("sell");
            tradeEntry.setAmount(a.getAmount());
            tradeEntry.setPrice(a.getPrice());
            tradeEntry.setTs(depth.getTs());
            return tradeEntry;
        }).collect(Collectors.toList());
        List<TradeEntry> buy = bids.stream().map(a -> {
            TradeEntry tradeEntry = new TradeEntry();
            tradeEntry.setDirection("buy");
            tradeEntry.setAmount(a.getAmount());
            tradeEntry.setPrice(a.getPrice());
            tradeEntry.setTs(depth.getTs());
            return tradeEntry;
        }).collect(Collectors.toList());
        data.addAll(sell);
        data.addAll(buy);
        timeObject.put(symbol, data);
        DataCache.getTrade().put(symbol, timeObject);
    }
 
    public static KlineTimeObject getKline(String symbol, String line) {
        String key = symbol;
        if (!StringUtils.isBlank(line)) {
            key = key + "_" + line;
        }
        return kline.get(key);
    }
 
    public static TrendTimeObject getTrend(String symbol) {
        return trend.get(symbol);
    }
 
    public static void putTrend(String symbol, TrendTimeObject model) {
        trend.put(symbol, model);
    }
 
    public static void putKline(String symbol, String line, KlineTimeObject model) {
        String key = symbol;
        if (!StringUtils.isBlank(line)) {
            key = key + "_" + line;
        }
        kline.put(key, model);
    }
 
    public static List<TradeDetails> getStockTradeList(String symbol) {
        return tradeList.get(symbol);
    }
 
    public static void putStockTradeList(String symbol, List<TradeDetails> model) {
        tradeList.put(symbol, model);
    }
 
    public static Realtime getRealtime(String symbol) {
        Realtime realtime = latestRealTime.get(symbol);
        if (realtime == null) {
            if (StringUtils.isAllLowerCase(symbol)) {
                symbol = symbol.toUpperCase();
            } else if (StringUtils.isAllUpperCase(symbol)) {
                symbol = symbol.toLowerCase();
            }
            return latestRealTime.get(symbol);
 
        } else {
            return realtime;
        }
    }
 
    public static void putRealtime(String symbol, Realtime model) {
        latestRealTime.put(symbol, model);
    }
 
    public static void putMarket(String symbol,  StockMarket market) {
        realMarketMap.put(symbol, market);
    }
    public static StockMarket getMarket(String symbol) {
        return realMarketMap.get(symbol);
    }
    public static Map<String, List<Realtime>> getRealtimeHistory() {
        return realtimeHistory;
    }
 
    public static void setRealtimeHistory(Map<String, List<Realtime>> realtimeHistory) {
        DataCache.realtimeHistory = realtimeHistory;
    }
 
    public static Map<String, DepthTimeObject> getDepth() {
        return depth;
    }
 
    public static Map<String, TradeTimeObject> getTrade() {
        return trade;
    }
 
    public static Map<String, Double> getRealtimeHigh() {
        return realtimeHigh;
    }
 
    public static Map<String, Double> getRealtimeLow() {
        return realtimeLow;
    }
 
    public static Map<String, Kline> getKline_hobi() {
        return kline_hobi;
    }
 
    public static Map<String, Double> getRealtime24HBeforeOpen() {
        return realtime24HBeforeOpen;
    }
 
    public static void putLatestRealTime(String symbol, Realtime model) {
        latestRealTime.put(symbol, model);
    }
}