1
zj
2024-06-13 a4662cc65a02f258062bf6cc392ceb1017db9292
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
package com.yami.trading.huobi.data.job;
 
import com.yami.trading.bean.data.domain.Realtime;
import com.yami.trading.bean.item.domain.Item;
import com.yami.trading.common.util.ThreadUtils;
import com.yami.trading.huobi.data.DataCache;
import com.yami.trading.service.item.ItemService;
import com.yami.trading.service.syspara.SysparaService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
/**
 * 最高最低修正
 *
 */
@Component
public class HighLowHandleJob implements Runnable {
 
    private static Logger logger = LoggerFactory.getLogger(HighLowHandleJob.class);
 
    /**
     * 数据接口调用间隔时长(毫秒)
     */
    private int interval;
    public static boolean first = true;
    @Autowired
    private ItemService itemService;
 
    public void start() {
        new Thread(this, "HighLowHandleJob").start();
    }
 
    @Override
    public void run() {
        ThreadUtils.sleep(1000 * 60 * 3);
        while (true) {
            bulidHighLow();
            ThreadUtils.sleep(1000 * 60 * 3);
        }
    }
 
    public void bulidHighLow() {
        try {
            if (first) {
                // data数据保存间隔时长(毫秒)
                this.interval = 3;
                first = false;
            }
            // 秒
            int num = (24 * 60 * 60) / this.interval;
            List<Item> item_list = itemService.findByType(Item.cryptos);
            for (int i = 0; i < item_list.size(); i++) {
                Item item = item_list.get(i);
                try {
 
                    List<Realtime> history = bulidNum(DataCache.getCryptosRealtimeHistory(item.getSymbol()), num);
                    if (history == null || history.size() == 0) {
                        continue;
                    }
                    Double high = null;
                    Double low = null;
 
                    for (int j = 0; j < history.size(); j++) {
                        Realtime realtime = history.get(j);
 
                        if (high == null || high < realtime.getClose()) {
                            high = realtime.getClose();
                        }
 
                        if ((low == null || low > realtime.getClose()) && realtime.getClose() > 0) {
                            low = realtime.getClose();
                        }
                    }
                    if (item == null || item.getSymbol() == null) {
                        logger.error("run fail");
                    }
                    if (high != null) {
                        DataCache.putRealtimeHigh(item.getSymbol(), high);
                    }
                    if (low != null && low > 0) {
                        DataCache.putRealtimeLow(item.getSymbol(), low);
                    }
 
                    Collections.sort(history);
                    DataCache.putRealtime24HBeforeOpen(item.getSymbol(), history.get(0).getClose());
 
                } catch (Exception e) {
                    logger.error("run fail", e);
                }
            }
 
        } catch (Exception e) {
            logger.error("run fail", e);
        }
    }
 
    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;
    }
}