1
zj
2024-07-22 402610fcff3830dc78def8f7b7a22b05a4af59fe
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
package org.example.server.impl;
 
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import org.example.dao.CurrencyMapper;
import org.example.pojo.Currency;
import org.example.pojo.Market;
import org.example.pojo.bo.AsksBo;
import org.example.pojo.bo.BidsBo;
import org.example.pojo.bo.MarketBo;
import org.example.server.CurrencySerivce;
import org.example.util.RedisUtil;
import org.springframework.stereotype.Service;
import org.yaml.snakeyaml.error.Mark;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.ArrayList;
import java.util.stream.Collectors;
 
/**
 * @program: demo
 * @description:
 * @create: 2024-07-16 15:23
 **/
@Service
public class CurrencySerivceImpl extends ServiceImpl<CurrencyMapper, Currency> implements CurrencySerivce {
 
    private HashMap hashMap = new HashMap();
    private static final Gson gson = new Gson();
    private ArrayList<String> keys;
 
    List<MarketBo> mexcList = new ArrayList<>();
    List<MarketBo> gateList = new ArrayList<>();
    List<MarketBo> bitgetList = new ArrayList<>();
    List<MarketBo> kucoinList = new ArrayList<>();
 
    @Override
    public void start() throws JsonProcessingException {
        Set<String> mexcSet = RedisUtil.keys("mexc");
        Set<String> gateSet = RedisUtil.keys("gate");
        Set<String> bitgetSet = RedisUtil.keys("bitget");
        Set<String> kucoinSet = RedisUtil.keys("kucoin");
 
 
        //这里做一个定时器,每10分钟更新一次
        Set<String> setKeys = RedisUtil.keys("*");
        keys = new ArrayList<>(setKeys);
        for (String key : mexcSet) {
            MarketBo marketBo = new MarketBo();
 
            String v = RedisUtil.get(key);
            Map<String, Object> redisValueMap = gson.fromJson(v, new TypeToken<Map<String, Object>>() {}.getType());
            String asks = redisValueMap.get("asks").toString();
            String bids = redisValueMap.get("bids").toString();
 
            if(!asks.equals("[]") && !StringUtils.isEmpty(asks)){
                Gson gson = new Gson();
                AsksBo asksBo = new AsksBo();
                Market[] asksDataArray = gson.fromJson(asks, Market[].class);
                Market asksElement = asksDataArray[0];
                asksBo.setP(asksElement.getP());
                asksBo.setV(asksElement.getV());
                marketBo.setAsks(asksBo);
            }
 
            if(!bids.equals("[]") && !StringUtils.isEmpty(bids)){
                BidsBo bidsBo = new BidsBo();
                Market[] bidsDataArray = gson.fromJson(bids, Market[].class);
                Market bidsElement = bidsDataArray[bidsDataArray.length-1];
                bidsBo.setP(bidsElement.getP());
                bidsBo.setV(bidsElement.getV());
                marketBo.setBids(bidsBo);
            }
            marketBo.setKey(key.replaceAll("mexc",""));
            marketBo.setExchange("mexc");
            mexcList.add(marketBo);
        }
 
 
        for (String key : gateSet) {
            String v = RedisUtil.get(key);
            Map<String, Object> redisValueMap = gson.fromJson(v, new TypeToken<Map<String, Object>>() {}.getType());
            String asks = redisValueMap.get("asks").toString();
            String bids = redisValueMap.get("bids").toString();
 
            MarketBo marketBo = new MarketBo();
            if(!asks.equals("[]") && !StringUtils.isEmpty(asks)){
                String[][] dataArray = gson.fromJson(asks, String[][].class);
                String[] asksData = dataArray[0];
                AsksBo asksBo = new AsksBo();
                asksBo.setP(new BigDecimal(asksData[0]));
                asksBo.setV(new BigDecimal(asksData[1]));
                marketBo.setAsks(asksBo);
            }
 
            if(!bids.equals("[]") && !StringUtils.isEmpty(bids)){
                String[][] dataArray = gson.fromJson(bids, String[][].class);
                String[] bidsData = dataArray[dataArray.length-1];
                BidsBo bidsBo = new BidsBo();
                bidsBo.setP(new BigDecimal(bidsData[0]));
                bidsBo.setV(new BigDecimal(bidsData[1]));
                marketBo.setBids(bidsBo);
            }
            marketBo.setKey(key.replaceAll("gate",""));
            marketBo.setExchange("gate");
            gateList.add(marketBo);
        }
 
 
        for (String key : bitgetSet) {
            String v = RedisUtil.get(key);
            Map<String, Object> redisValueMap = gson.fromJson(v, new TypeToken<Map<String, Object>>() {}.getType());
            String asks = redisValueMap.get("asks").toString();
            String bids = redisValueMap.get("bids").toString();
 
            MarketBo marketBo = new MarketBo();
            if(!asks.equals("[]") && !StringUtils.isEmpty(asks)){
                String[][] dataArray = gson.fromJson(asks, String[][].class);
                String[] asksData = dataArray[0];
                AsksBo asksBo = new AsksBo();
                asksBo.setP(new BigDecimal(asksData[0]));
                asksBo.setV(new BigDecimal(asksData[1]));
                marketBo.setAsks(asksBo);
            }
 
            if(!bids.equals("[]") && !StringUtils.isEmpty(bids)){
                String[][] dataArray = gson.fromJson(bids, String[][].class);
                String[] bidsData = dataArray[dataArray.length-1];
                BidsBo bidsBo = new BidsBo();
                bidsBo.setP(new BigDecimal(bidsData[0]));
                bidsBo.setV(new BigDecimal(bidsData[1]));
                marketBo.setBids(bidsBo);
            }
            marketBo.setKey(key.replaceAll("bitget",""));
            marketBo.setExchange("bitget");
            bitgetList.add(marketBo);
        }
 
 
        for (String key : kucoinSet) {
            String v = RedisUtil.get(key);
            Map<String, Object> redisValueMap = gson.fromJson(v, new TypeToken<Map<String, Object>>() {}.getType());
            String asks = redisValueMap.get("asks").toString();
            String bids = redisValueMap.get("bids").toString();
 
            MarketBo marketBo = new MarketBo();
            if(!asks.equals("[]") && !StringUtils.isEmpty(asks)){
                String[][] dataArray = gson.fromJson(asks, String[][].class);
                String[] asksData = dataArray[0];
                AsksBo asksBo = new AsksBo();
                asksBo.setP(new BigDecimal(asksData[0]));
                asksBo.setV(new BigDecimal(asksData[1]));
                marketBo.setAsks(asksBo);
            }
 
            if(!bids.equals("[]") && !StringUtils.isEmpty(bids)){
                String[][] dataArray = gson.fromJson(bids, String[][].class);
                String[] bidsData = dataArray[dataArray.length-1];
                BidsBo bidsBo = new BidsBo();
                bidsBo.setP(new BigDecimal(bidsData[0]));
                bidsBo.setV(new BigDecimal(bidsData[1]));
                marketBo.setBids(bidsBo);
            }
            marketBo.setKey(key.replaceAll("kucoin",""));
            marketBo.setExchange("kucoin");
            kucoinList.add(marketBo);
        }
        testDemo();
    }
 
    // 计算利润百分比
    private static BigDecimal calculateProfitPercentage(BigDecimal buyPrice, BigDecimal sellPrice) {
        BigDecimal profit = sellPrice.subtract(buyPrice);
        if (buyPrice.compareTo(BigDecimal.ZERO) == 0) {
            return BigDecimal.ZERO; // 防止除以零
        }
        BigDecimal profitPercentage = profit.divide(buyPrice, 12, RoundingMode.DOWN).multiply(new BigDecimal(100));
        return profitPercentage;
    }
 
    private static List<String> findProfitablePairs(List<MarketBo>... exchangeLists) {
        List<String> result = new ArrayList<>();
        Map<String, Map<String, MarketBo>> marketMap = new HashMap<>();
 
        // 按币种名称和交易所分组市场数据
        for (List<MarketBo> exchangeList : exchangeLists) {
            for (MarketBo market : exchangeList) {
                String coinName = market.getKey();
                String exchangeName = market.getExchange(); // 假设有获取交易所名称的方法 getExchange()
 
                // 如果该币种在该交易所不存在,则创建新的列表
                if (!marketMap.containsKey(coinName)) {
                    marketMap.put(coinName, new HashMap<>());
                }
                if (!marketMap.get(coinName).containsKey(exchangeName)) {
                    marketMap.get(coinName).put(exchangeName, new MarketBo());
                }
 
                // 将市场数据添加到对应的交易所列表中
                marketMap.get(coinName).put(exchangeName,market);
            }
        }
        marketMap = marketMap.entrySet().stream()
                .filter(entry -> entry.getValue().size() != 1)
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
        // 遍历每个币种
        for (String coinName : marketMap.keySet()) {
            Map<String, MarketBo> exchangeMap = marketMap.get(coinName);
            // 遍历每个交易所
            for (String exchangeName : exchangeMap.keySet()) {
                MarketBo markets1 = exchangeMap.get(exchangeName);
                // 在同一交易所中寻找有差价的交易对
                for (String exchangeName2 : exchangeMap.keySet()) {
                    MarketBo markets2 = exchangeMap.get(exchangeName2);
                    if(markets1.getExchange().equals(markets2.getExchange())){
                        continue;
                    }
 
                    if(null != markets1.getBids() && null != markets2.getAsks()){
                        BigDecimal buyPrice = markets1.getBids().getP();
                        BigDecimal sellPrice = markets2.getAsks().getP();
                        // 计算利润百分比
                        BigDecimal profitPercentage = calculateProfitPercentage(buyPrice, sellPrice);
 
                        // 如果利润大于零,则添加到结果列表
                        if (profitPercentage.compareTo(BigDecimal.ZERO) > 0) {
                            // 准备输出字符串
                            String pair = String.format("%s: 在 %s 以 %s 买入,在 %s 以 %s 卖出,利润为 %.4f%%",
                                    coinName, markets1.getExchange(), buyPrice, markets2.getExchange(), sellPrice, profitPercentage);
                            result.add(pair);
                        }
                    }
                }
            }
        }
        return result;
    }
 
 
 
    public void testDemo(){
        List<String> profitablePairs = findProfitablePairs(mexcList, gateList, bitgetList, kucoinList);
 
        // 输出结果
        for (String pair : profitablePairs) {
            System.out.println(pair);
        }
    }
 
 
}