1
zyy
8 hours ago 4fefff17528a878d345ff3311c297a66a671b8d6
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package com.yami.trading.huobi.data.internal;
 
import cn.hutool.core.util.StrUtil;
import com.yami.trading.bean.data.domain.*;
import com.yami.trading.bean.item.domain.Item;
import com.yami.trading.huobi.data.DataCache;
import com.yami.trading.service.data.DataService;
import com.yami.trading.service.item.ItemService;
import com.yami.trading.service.syspara.SysparaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
 
@Service("dataService")
@Slf4j
public class DataServiceImpl implements DataService {
    @Autowired
    SysparaService sysparaService;
    @Autowired
    ItemService itemService;
    @Autowired
    KlineService klineService;
 
    /**
     * 根据币种分类 获取实时价格数据
     */
    @Override
    public List<Realtime> realtimeByType(String symbolType) {
        List<Item> items = new ArrayList<>(itemService.list()).stream().filter(item -> item.getType().equals(symbolType)).collect(Collectors.toList());
        List<Realtime> list = new ArrayList<>();
        items.forEach(item -> {
            Realtime realtime = DataCache.getRealtime(item.getSymbol());
            if (null != realtime) {
                list.add(realtime);
            }
        });
        return list;
    }
 
    /**
     * 行情实时价格
     */
    @Override
    public List<Realtime> realtime(String symbols) {
        List<Realtime> list = new ArrayList<>();
        // 单个币种
        if (!StrUtil.isEmpty(symbols) && !symbols.contains(",")) {
            Realtime realtime = DataCache.getRealtime(symbols);
            if (realtime != null) {
                list.add(realtime);
            }
        }
        // 如果不传参数 返回全部币种行情
        else if (StrUtil.isEmpty(symbols)) {
            List<Item> items = new ArrayList<>(itemService.list());
            for (Item item : items) {
                Realtime realtime = DataCache.getRealtime(item.getSymbol());
                if (realtime != null) {
                    list.add(realtime);
                }
            }
        }
        // 多个币种
        else if (!StrUtil.isEmpty(symbols) && symbols.contains(",")) {
            String[] symbolArr = symbols.split(",");
            for (String oneSymbol : symbolArr) {
                Realtime realtime = DataCache.getRealtime(oneSymbol);
                if (realtime != null) {
                    list.add(realtime);
                } else {
                    //log.error("realtime is null; 币种->{}", oneSymbol);
                }
            }
        }
        return list;
    }
 
    /**
     * Kline
     */
    @Override
    public List<Kline> kline(String symbol, String line) {
        Item bySymbol = itemService.findBySymbol(symbol);
        if(Item.cryptos.equals(bySymbol.getType())){
            return klineCryptos(symbol, line);
        }
        KlineTimeObject timeObject = DataCache.getKline(symbol, line);
        List<Kline> list = new ArrayList<Kline>();
        if (timeObject != null) {
            list = timeObject.getKline();
        }
        List<Kline> list_clone = new ArrayList<Kline>();
        try {
            for (int i = 0; i < list.size(); i++) {
                if(list.get(i) == null){
                    continue;
                }
                Kline kline = (Kline) list.get(i).clone();
                list_clone.add(kline);
            }
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
 
        Realtime realtime = DataCache.getLatestRealTime(symbol);
        if (realtime != null) {
            Kline kline = null;
            if (KlineConstant.PERIOD_1MIN.equals(line)) {
                kline = klineService.bulidKline1Minute(realtime, KlineConstant.PERIOD_1MIN);
            } else if (KlineConstant.PERIOD_5MIN.equals(line)) {
                kline = klineService.bulidKline5Minute(realtime, KlineConstant.PERIOD_5MIN);
            } else if (KlineConstant.PERIOD_15MIN.equals(line)) {
                kline = klineService.bulidKline15Minute(realtime, KlineConstant.PERIOD_15MIN);
            } else if (KlineConstant.PERIOD_30MIN.equals(line)) {
                kline = klineService.bulidKline30Minute(realtime, KlineConstant.PERIOD_30MIN);
            } else if (KlineConstant.PERIOD_60MIN.equals(line)) {
                kline = klineService.bulidKline60Minute(realtime, KlineConstant.PERIOD_60MIN);
            } else if (KlineConstant.PERIOD_4HOUR.equals(line)) {
                kline = klineService.bulidKline4Hour(realtime, KlineConstant.PERIOD_4HOUR);
            } else if (KlineConstant.PERIOD_1DAY.equals(line)) {
                kline = klineService.bulidKline1Day(realtime, KlineConstant.PERIOD_1DAY);
            } else if (KlineConstant.PERIOD_5DAY.equals(line)) {
                kline = klineService.bulidKline5Day(realtime, KlineConstant.PERIOD_5DAY);
            } else if (KlineConstant.PERIOD_1WEEK.equals(line)) {
                kline = klineService.bulidKline1Week(realtime, KlineConstant.PERIOD_1WEEK);
            } else if (KlineConstant.PERIOD_1MON.equals(line)) {
                kline = klineService.bulidKline1Mon(realtime, KlineConstant.PERIOD_1MON);
            } else if (KlineConstant.PERIOD_QUARTER.equals(line)) {
                kline = klineService.bulidKline1Mon(realtime, KlineConstant.PERIOD_QUARTER);
            } else if (KlineConstant.PERIOD_YEAR.equals(line)) {
                kline = klineService.bulidKline1Mon(realtime, KlineConstant.PERIOD_YEAR);
            }
            if (null != kline) {
                list_clone.add(kline);
            }
        }
        // 按时间升序
        Collections.sort(list_clone);
        return list_clone;
    }
 
    /**
     * 分时图
     */
    @Override
    public List<Trend> trend(String symbol) {
        TrendTimeObject trendTimeObject = DataCache.getTrend(symbol);
        trendTimeObject = this.loadTrend(symbol, trendTimeObject);
        if (trendTimeObject != null) {
            return trendTimeObject.getTrend();
        }
        return new ArrayList<>();
    }
    /**
     * 深度数据
     */
    @Override
    public Depth depth(String symbol) {
        DepthTimeObject timeObject = DataCache.getDepth(symbol);
        if (timeObject != null) {
            Depth depth = timeObject.getDepth();
            depth.setSymbol(symbol);
            return depth;
        }
        timeObject = new DepthTimeObject();
        Depth depth = new Depth();
        depth.setSymbol(symbol);
        timeObject.setDepth(depth);
        DataCache.putDepth(symbol, timeObject);
        return depth;
    }
 
    /**
     * 近期交易记录
     */
    @Override
    public Trade trade(String symbol) {
        TradeTimeObject timeObject = DataCache.getTrade(symbol);
        // this.loadTrade(symbol, timeObject);
        if (timeObject != null) {
            return timeObject.getTrade();
        }
        timeObject = new TradeTimeObject();
        DataCache.putTrade(symbol, timeObject);
        return timeObject.getTrade();
    }
 
    private TrendTimeObject loadTrend(String symbol, TrendTimeObject trendTimeObject) {
        if (trendTimeObject == null) {
            // 秒
            int interval = 3;
            int num = (24 * 60 * 60) / interval;
            List<Trend> list = new ArrayList<>();
            // 24小时的历史记录
            List<Realtime> history = bulidNum(DataCache.getCryptosRealtimeHistory(symbol), num);
            history = this.take500(history);
            if (history.size() > 500) {
                // 按时间升序
                Collections.sort(history);
                List<Realtime> history_500 = new ArrayList<>();
                for (int i = 0; i < 500; i++) {
                    history_500.add(history.get(i));
                }
                history = history_500;
            }
            for (int i = 0; i < history.size(); i++) {
                Realtime realtime = history.get(i);
                Trend trend = bulidTrend(realtime);
                list.add(trend);
            }
            Realtime realtime_last = DataCache.getRealtime(symbol);
            if (realtime_last != null) {
                list.add(bulidTrend(DataCache.getRealtime(symbol)));
            }
            trendTimeObject = new TrendTimeObject();
            trendTimeObject.setTrend(list);
            DataCache.putTrend(symbol, trendTimeObject);
        }
        return trendTimeObject;
    }
 
    private List<Realtime> bulidNum(List<Realtime> cacheList, int num) {
        List<Realtime> list = new ArrayList<>();
        if (cacheList == null) {
            return list;
        }
        if (num > cacheList.size()) {
            num = cacheList.size();
        }
        for (int i = cacheList.size() - num; i < cacheList.size(); i++) {
            list.add(cacheList.get(i));
        }
        return list;
    }
 
    /**
     * 按平均频率取500个数据点
     */
    private List<Realtime> take500(List<Realtime> history) {
        List<Realtime> list = new ArrayList<>();
        int num = history.size() / 500;
        if (num <= 0) {
            return history;
        }
        int i = 0;
        while (true) {
            if (num >= 1.0D) {
                if (i % num == 0) {
                    list.add(history.get(i));
                }
            } else {
                list.add(history.get(i));
            }
            i++;
            if (i >= history.size()) {
                break;
            }
        }
        return list;
    }
 
    private Trend bulidTrend(Realtime realtime) {
        Trend trend = new Trend();
        trend.setSymbol(realtime.getSymbol());
        trend.setTs(realtime.getTs());
        trend.setTrend(realtime.getClose());
        trend.setVolume(realtime.getVolume());
        trend.setAmount(realtime.getAmount());
        return trend;
    }
 
    public List<Kline> klineCryptos(String symbol, String line) {
        KlineTimeObject timeObject = DataCache.getKline(symbol, line);
        List<Kline> list = new ArrayList<>();
        if (timeObject != null) {
            list = timeObject.getKline();
        }
        List<Kline> list_clone = new ArrayList<>();
        try {
            for (int i = 0; i < list.size(); i++) {
                Kline kline = (Kline) list.get(i).clone();
                list_clone.add(kline);
            }
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        Realtime realtime = DataCache.getRealtime(symbol);
        Kline hobiOne = DataCache.getKlineHoBi(symbol + "_" + line);
 
        Kline lastOne = null;
        if (list.size() > 0) {
            lastOne = list.get(list.size() - 1);
        }
        if (realtime != null && hobiOne != null && lastOne != null) {
            list_clone.add(this.klineService.bulidKline(realtime, lastOne, hobiOne, line));
        }
        // 按时间升序
        Collections.sort(list_clone);
        return list_clone;
    }
}