zainali5120
2020-10-14 fc52c99639d8d817192985b295d39e6ba34484f8
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
package com.xcong.excoin.modules.exchange.service.impl;
 
import cn.hutool.core.util.StrUtil;
import com.huobi.client.model.Candlestick;
import com.xcong.excoin.modules.exchange.service.HandleKlineService;
import com.xcong.excoin.processor.CoinProcessor;
import com.xcong.excoin.processor.CoinProcessorFactory;
import com.xcong.excoin.trade.ExchangeTrade;
import com.xcong.excoin.utils.CoinTypeConvert;
import com.xcong.excoin.utils.RedisUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;
import java.util.Map;
 
@Service
public class HandleKlineServiceImpl implements HandleKlineService {
 
    @Resource
    private CoinProcessorFactory processorFactory;
 
    @Resource
    private RedisUtils redisUtils;
 
    @Override
    public void handleExchangeOrderToKline(List<ExchangeTrade> trades) {
        if (CollectionUtils.isEmpty(trades)) {
            return;
        }
        String symbol = trades.get(0).getSymbol();
        String symbolUsdt = symbol;
        if(!symbol.contains("USDT")){
            symbolUsdt = symbol+"/USDT";
        }
        CoinProcessor processor = processorFactory.getProcessor(symbol);
        Map<String, Candlestick> currentKlineMap = processor.getCurrentKlineMap();
        Collection<Candlestick> values = currentKlineMap.values();
        BigDecimal newPrice =null;
        for (Candlestick candlestick : values) {
            for (ExchangeTrade exchangeTrade : trades) {
                if(exchangeTrade==null){
                    continue;
                }
                processor.processTrade(candlestick, exchangeTrade);
                newPrice=exchangeTrade.getPrice();
            }
        }
        // 更新今日高地价
        BigDecimal min=BigDecimal.ZERO;
        BigDecimal max=BigDecimal.ZERO;
        BigDecimal vol = BigDecimal.ZERO;
        for (ExchangeTrade exchangeTrade : trades) {
            if(exchangeTrade==null){
                continue;
            }
            min=exchangeTrade.getPrice().min(min);
            max=exchangeTrade.getPrice().max(max);
            vol=vol.add(exchangeTrade.getAmount());
        }
        Object o = redisUtils.get(symbolUsdt);
        if(o!=null){
            Candlestick today =   (Candlestick)o;
            today.setHigh(today.getHigh().max(max));
            today.setLow(today.getLow().min(min));
            today.setLow(today.getVolume().add(vol));
            redisUtils.set(symbolUsdt,today);
        }else{
            Candlestick today =   new Candlestick();
            today.setClose(newPrice);
            today.setLow(newPrice);
            today.setHigh(newPrice);
            today.setHigh(today.getHigh().max(max));
            today.setLow(today.getLow().min(min));
            today.setLow(vol);
            redisUtils.set(symbolUsdt,today);
        }
        // 存入redis,websocket去取
        String key = "NEW_KINE_{}";
        key = StrUtil.format(key, symbolUsdt);
        redisUtils.set(key,currentKlineMap);
        // 更新最新价
        if(newPrice!=null){
            redisUtils.set(CoinTypeConvert.convertToKey(symbolUsdt), newPrice);
        }
 
    }
 
}