peter
2025-12-05 3a6f68a40bb8be25ce19109ea595e8d0d7bfe510
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
package com.nq.config;
 
import com.nq.dao.RealTimeMapper;
import com.nq.dao.StockFuturesMapper;
import com.nq.dao.StockIndexMapper;
import com.nq.dao.StockMapper;
import com.nq.pojo.Stock;
import com.nq.pojo.StockFutures;
import com.nq.pojo.StockIndex;
import com.nq.utils.stock.BuyAndSellUtils;
import com.nq.utils.stock.sina.SinaStockApi;
 
import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
 
@Component
public class StockPoll {
    @Resource
    StockMapper stockMapper;
 
    @Resource
    RealTimeMapper realTimeMapper;
 
    @Resource
    StockFuturesMapper stockFuturesMapper;
 
    @Resource
    StockIndexMapper stockIndexMapper;
 
    private ThreadPoolExecutor pool;
 
    @PostConstruct
    public void initPool() {
        this.pool = new ThreadPoolExecutor(50, 70, 20L, TimeUnit.SECONDS, new ArrayBlockingQueue<>(240));
        this.pool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy());
    }
 
    public void test(String stockType, Integer stock_num, Integer stock_nums) {
        List<Stock> stockCodes = this.stockMapper.findStockCode(stockType, stock_num, stock_nums);
        System.out.println("stockCodes" + stockCodes.size() + "--stockCodes");
        for (Stock stock : stockCodes) {
            String stockGid = stock.getStockGid();
            String sinaStock = SinaStockApi.getSinaStock("s_" + stockGid);
            String[] arrayOfString = sinaStock.split(",");
            StockTask stockTask = new StockTask();
            stockTask.splits((Object[])arrayOfString);
            stockTask.stockGid(stockGid);
 
            /*//处理均价
            String details = SinaStockApi.getSinaStock(stockGid);
            String averagePrice = details.split(",")[29];
            stockTask.averagePrice(Double.parseDouble(averagePrice));*/
 
            stockTask.averagePrice(Double.parseDouble("0"));
            stockTask.StockPoll(this);
            stockTask.RealTimeMapper(this.realTimeMapper);
 
            this.pool.submit(stockTask);
        }
    }
 
    //0-540
    public void z1() {
        test("sz", Integer.valueOf(0), Integer.valueOf(300));
    }
    public void z11() {
        test("sz", Integer.valueOf(300), Integer.valueOf(300));
    }
    public void z12() {
        test("sz", Integer.valueOf(600), Integer.valueOf(200));
    }
 
    //540-540
    public void z2() {
        test("sz", Integer.valueOf(800), Integer.valueOf(300));
    }
    public void z21() {
        test("sz", Integer.valueOf(1100), Integer.valueOf(300));
    }
    public void z22() {
        test("sz", Integer.valueOf(1400), Integer.valueOf(200));
    }
 
    //1080 - 540
    public void z3() {
        test("sz", Integer.valueOf(1600), Integer.valueOf(300));
    }
    public void z31() {
        test("sz", Integer.valueOf(1900), Integer.valueOf(300));
    }
    public void z32() {
        test("sz", Integer.valueOf(2200), Integer.valueOf(200));
    }
 
    //1620 - 539
    public void z4() {
        test("sz", Integer.valueOf(2400), Integer.valueOf(200));
    }
    public void z41() {
        test("sz", Integer.valueOf(2600), Integer.valueOf(100));
    }
    public void z42() {
        test("sz", Integer.valueOf(2700), Integer.valueOf(100));
    }
 
    //0-484
    public void h1() {
        test("sh", Integer.valueOf(0), Integer.valueOf(300));
    }
    public void h11() {
        test("sh", Integer.valueOf(300), Integer.valueOf(300));
    }
    public void h12() {
        test("sh", Integer.valueOf(600), Integer.valueOf(100));
    }
 
    //484-484
    public void h2() {
        test("sh", Integer.valueOf(700), Integer.valueOf(300));
    }
    public void h21() {
        test("sh", Integer.valueOf(1000), Integer.valueOf(300));
    }
    public void h22() {
        test("sh", Integer.valueOf(1300), Integer.valueOf(100));
    }
 
    //968-485
    public void h3() {
        test("sh", Integer.valueOf(1400), Integer.valueOf(300));
    }
    public void h31() {
        test("sh", Integer.valueOf(1700), Integer.valueOf(300));
    }
    public void h32() {
        test("sh", Integer.valueOf(2000), Integer.valueOf(200));
    }
 
    //bj 0-121
    public void bj1() {
        test("bj", Integer.valueOf(0), Integer.valueOf(200));
    }
 
    public void qh1() {
        qhDataGrab("qh", Integer.valueOf(0), Integer.valueOf(540));
    }
 
    /*期货k线分时数据抓取*/
    public void qhDataGrab(String stockType, Integer stock_num, Integer stock_nums) {
        List<StockFutures> stockCodes = this.stockFuturesMapper.queryList();
        System.out.println("qh-stockCodes" + stockCodes.size() + "--stockCodes");
        boolean am = false;
        boolean pm = false;
        boolean pm2 = false;
        try {
            am = BuyAndSellUtils.isTransTime("9:15", "12:00");
            pm = BuyAndSellUtils.isTransTime("13:00", "16:30");
            pm2 = BuyAndSellUtils.isTransTime("17:15", "23:45");
        } catch (Exception e) {
        }
        for (StockFutures stock : stockCodes) {
            String stockGid = stock.getFuturesGid();
            //恒生指数特殊处理,不在指定时间段跳过
            if("hf_HSI".equals(stockGid) && !am && !pm && !pm2){
                continue;
            }
            String sinaStock = SinaStockApi.getSinaStock(stockGid);
            if(sinaStock.length()>10) {
                String[] arrayOfString = sinaStock.split(",");
                //伦敦金格式不正确,特殊处理
                if (arrayOfString.length <= 14) {
                    sinaStock = sinaStock.replace("\"", ",1\"");
                    arrayOfString = sinaStock.split(",");
                }
                //拼接需要的字符串:1价格+3涨跌幅度+4成交量+5成交额
                double rates = 0;
                BigDecimal b1 = new BigDecimal(arrayOfString[3].toString());
                BigDecimal b2 = new BigDecimal(arrayOfString[4].toString());
                BigDecimal b3 = b1.subtract(b2);
                String s = arrayOfString[14].toString();
                int index = s.indexOf("\"");
                String substring = s.substring(0, index);
                rates = b3.multiply(new BigDecimal("100")).divide(b1, 2, BigDecimal.ROUND_HALF_UP).doubleValue();
                String qhstr = "0," + arrayOfString[0].toString() + ",0," + rates + "," + substring + "," + arrayOfString[9].split("\\.")[0].toString() + "\"";
                String[] arrayqh = qhstr.split(",");
                StockTask stockTask = new StockTask();
                stockTask.splits((Object[]) arrayqh);
                stockTask.stockGid(stockGid);
                stockTask.averagePrice(new Double("0"));
                stockTask.StockPoll(this);
                stockTask.RealTimeMapper(this.realTimeMapper);
                this.pool.submit(stockTask);
            }
        }
    }
 
    public void zs1() {
        zsDataGrab();
    }
 
    /*指数k线分时数据抓取*/
    public void zsDataGrab() {
        List<StockIndex> stockCodes = this.stockIndexMapper.queryListIndex();
        System.out.println("zs-stockCodes" + stockCodes.size() + "--stockCodes");
        for (StockIndex stock : stockCodes) {
            String stockGid = stock.getIndexGid();
            String sinaStock = SinaStockApi.getSinaStock("s_" + stockGid);
            String[] arrayOfString = sinaStock.split(",");
            StockTask stockTask = new StockTask();
            stockTask.splits((Object[])arrayOfString);
            stockTask.stockGid(stockGid);
            stockTask.StockPoll(this);
            stockTask.RealTimeMapper(this.realTimeMapper);
            this.pool.submit(stockTask);
        }
    }
}