Helius
2021-06-11 fed58c8b88dc71ea5a5fa1cf1b5527f0b4555089
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
package com.xcong.excoin.quartz.job;
 
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.huobi.client.SubscriptionClient;
import com.huobi.client.SubscriptionOptions;
import com.huobi.client.model.Candlestick;
import com.huobi.client.model.enums.CandlestickInterval;
import com.xcong.excoin.modules.coin.dao.OrderCoinDealDao;
import com.xcong.excoin.modules.coin.dao.OrderCoinsDao;
import com.xcong.excoin.modules.coin.entity.OrderCoinsEntity;
import com.xcong.excoin.modules.coin.service.OrderCoinService;
import com.xcong.excoin.modules.symbols.constants.SymbolsConstats;
import com.xcong.excoin.modules.symbols.service.SymbolsService;
import com.xcong.excoin.processor.CoinProcessor;
import com.xcong.excoin.processor.CoinProcessorFactory;
import com.xcong.excoin.processor.DefaultCoinProcessor;
import com.xcong.excoin.processor.MarketService;
import com.xcong.excoin.rabbit.pricequeue.WebsocketPriceService;
import com.xcong.excoin.rabbit.producer.ExchangeProducer;
import com.xcong.excoin.trade.CoinTrader;
import com.xcong.excoin.trade.CoinTraderFactory;
import com.xcong.excoin.trade.ExchangeTrade;
import com.xcong.excoin.utils.CoinTypeConvert;
import com.xcong.excoin.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
 
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 *  开启撮合交易
 *
 * @author wzy
 * @date 2020-05-28
 **/
@Slf4j
@Component
@ConditionalOnProperty(prefix = "app", name = "exchange-trade", havingValue = "true")
public class CoinTradeInitJob {
 
    @Resource
    private OrderCoinsDao orderCoinsDao;
    @Resource
    private OrderCoinDealDao orderCoinDealDao;
 
    @Resource
    private CoinTraderFactory factory;
 
    @Resource
    private RedisUtils redisUtils;
 
    @Resource
    private MarketService marketService;
 
    @Resource
    private CoinProcessorFactory processorFactory;
 
    @Resource
    ExchangeProducer exchangeProducer;
 
    @PostConstruct
    public void initCoinTrade() {
        log.info("#=======撮合交易器开启=======#");
        String symbol = SymbolsConstats.ROC;
        CoinTrader newTrader = new CoinTrader(symbol);
        newTrader.setExchangeProducer(exchangeProducer);
        //newTrader.setKafkaTemplate(kafkaTemplate);
        //newTrader.setBaseCoinScale(coin.getBaseCoinScale());
        //newTrader.setCoinScale(coin.getCoinScale());
        // newTrader.setPublishType(coin.getPublishType());
        //newTrader.setClearTime(coin.getClearTime());
 
        // 创建成功以后需要对未处理订单预处理
        log.info("======CoinTrader Process: " + symbol + "======");
        List<String> symbolList = new ArrayList<>();
        symbolList.add(symbol);
        List<OrderCoinsEntity> orders = orderCoinsDao.selectCoinOrderOnTrade(symbolList);
        List<OrderCoinsEntity> tradingOrders = new ArrayList<>();
        List<OrderCoinsEntity> completedOrders = new ArrayList<>();
        orders.forEach(order -> {
            if(order.getDealCnt()==null){
                order.setDealCnt(BigDecimal.ZERO);
            }
            if(order.getDealAmount()==null){
                order.setDealAmount(BigDecimal.ZERO);
            }
            tradingOrders.add(order);
        });
        try {
            newTrader.trade(tradingOrders);
        } catch (ParseException e) {
            e.printStackTrace();
            log.info("异常:trader.trade(tradingOrders);");
        }
        newTrader.setReady(true);
        factory.addTrader(symbol, newTrader);
 
        // 创建K线生成器
        CoinProcessor processor = new DefaultCoinProcessor(symbol, "USDT");
        processor.setMarketService(marketService);
        processor.setRedisUtils(redisUtils);
        //processor.setExchangeRate(exchangeRate);
        processor.initializeThumb();
        //processor.initializeUsdRate();
        processor.setIsHalt(false);
        List<ExchangeTrade> nekk = orderCoinDealDao.selectOrderCoinDealByTime(SymbolsConstats.ROC, null, null);
        processor.process(nekk);
        String symbolUsdt = symbol;
        if(!symbol.contains("USDT")){
            symbolUsdt = symbol+"/USDT";
        }
        String key = "NEW_KINE_{}";
        key = StrUtil.format(key, symbolUsdt);
        Object o = redisUtils.get(key);
        if(o!=null){
            Map<String, Candlestick> currentKlineMap = (Map<String, Candlestick> )o;
            ((DefaultCoinProcessor) processor).setCurrentKlineMap(currentKlineMap);
        }else{
            // 当最新K线不存在时 需要初始化
            // 1min 5min 15min 30min  1hour 4hour 1day 1week
            String[] rang = {"1min","5min","15min","30min","1hour","4hour","1day","1week"};
            Map<String, Candlestick> currentKlineMap = new HashMap<>();
            long currentTimeMillis = System.currentTimeMillis();
            for (String s : rang) {
                Candlestick candlestick = new Candlestick();
                candlestick.setTimestamp(currentTimeMillis);
                currentKlineMap.put(s,candlestick);
            }
            redisUtils.set(key,currentKlineMap);
        }
 
        processorFactory.addProcessor(symbol, processor);
 
    }
}