| | |
| | | import com.yami.trading.bean.data.domain.Realtime; |
| | | import com.yami.trading.bean.item.domain.Item; |
| | | import com.yami.trading.bean.model.StockRealTimeBean; |
| | | import com.yami.trading.common.constants.RedisKeys; |
| | | import com.yami.trading.common.util.RedisUtil; |
| | | import com.yami.trading.huobi.data.DataCache; |
| | | import com.yami.trading.huobi.websocket.constant.enums.EStockType; |
| | | import com.yami.trading.service.item.ItemService; |
| | |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.boot.CommandLineRunner; |
| | | import org.springframework.scheduling.annotation.Scheduled; |
| | | import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; |
| | | import org.springframework.stereotype.Component; |
| | |
| | | |
| | | |
| | | @Component |
| | | public class StockTask { |
| | | public class StockTask implements CommandLineRunner { |
| | | private static final Logger log = LoggerFactory.getLogger(StockTask.class); |
| | | |
| | | private final AtomicBoolean syncINStockData = new AtomicBoolean(false); |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 加载所有股票数据 |
| | | */ |
| | |
| | | .eq("type", Item.indices)); |
| | | |
| | | log.info("同步股票 已有数据 {} 获取数据 {}", stockList.size(), list.size()); |
| | | System.out.println(stockList); |
| | | List<Item> updateStockList = new ArrayList<>(); |
| | | for (StockRealTimeBean o : list) { |
| | | //System.out.println(o); |
| | | Item indices = indicesList.stream() |
| | | .filter(x -> x.getSymbol().equals(o.getSymbol())) |
| | | .findFirst() |
| | | .orElse(null); |
| | | if (indices != null) { //指数不添加 |
| | | continue; |
| | | } |
| | | |
| | | Item item = stockList.stream() |
| | | .filter(x -> x.getSymbol().equals(o.getSymbol()) && |
| | | x.getStockCode().equals(o.getId())) |
| | | .findFirst() |
| | | .orElse(null); |
| | | if (item != null) { //已有不添加 |
| | | if (item != null) { //已有不添加 |
| | | continue; |
| | | } |
| | | item = indicesList.stream() |
| | | .filter(x -> x.getSymbol().equals(o.getSymbol())) |
| | | .findFirst() |
| | | .orElse(null); |
| | | if (item != null) { //已有不添加 |
| | | continue; |
| | | } |
| | | |
| | | item = new Item(); |
| | | String name = StringUtils.trim(o.getName()); |
| | | item.setEnName(name); |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 初始化所有股票数据 |
| | | */ |
| | | public void initAllStock(EStockType eStockType) { |
| | | log.info("init US股票 数据 {}", eStockType.getCode()); |
| | | List<StockRealTimeBean> list = new ArrayList<>(); |
| | | int totleStock = 1; |
| | | int page = 0; |
| | | try { |
| | | while (totleStock > list.size()) { |
| | | try { |
| | | String result = HttpClientRequest.doGet(eStockType.stockUrl + "list?country_id=" + eStockType.getContryId() + "&size=100000&page=" + page + "&key=" + eStockType.stockKey); |
| | | ReponseBase reponseBase = new Gson().fromJson(result, ReponseBase.class); |
| | | list.addAll(reponseBase.getData()); |
| | | page++; |
| | | totleStock = reponseBase.getTotal(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | break; |
| | | } |
| | | } |
| | | if (list.isEmpty()) { |
| | | return; |
| | | } |
| | | /*list.forEach( x-> { |
| | | if (x.getSymbol().equalsIgnoreCase("IONM")) { |
| | | System.out.println(x); |
| | | System.out.println(x); |
| | | } |
| | | });*/ |
| | | |
| | | List<Item> indicesList = itemService.list(new QueryWrapper<Item>() |
| | | .eq("type", Item.indices)); |
| | | for (StockRealTimeBean o : list) { |
| | | Item indices = indicesList.stream() |
| | | .filter(x -> x.getSymbol().equals(o.getSymbol())) |
| | | .findFirst() |
| | | .orElse(null); |
| | | if (indices != null) { //指数不添加 |
| | | continue; |
| | | } |
| | | |
| | | Realtime realtime = new Realtime(); |
| | | realtime.setUuid(o.getId()); |
| | | realtime.setSymbol(o.getSymbol()); |
| | | realtime.setName(o.getName()); |
| | | realtime.setClose(new BigDecimal(o.getLast().trim()).doubleValue()); |
| | | realtime.setLow(new BigDecimal(o.getLow().trim()).doubleValue()); |
| | | realtime.setHigh(new BigDecimal(o.getHigh().trim()).doubleValue()); |
| | | realtime.setOpen(new BigDecimal(o.getOpen().trim()).doubleValue()); |
| | | realtime.setPrevClose(new BigDecimal(o.getPrevClose().trim()).doubleValue()); |
| | | realtime.setTs(Long.valueOf(o.getTime() + "000")); |
| | | realtime.setVolume(new BigDecimal(o.getVolume().trim()).doubleValue()); |
| | | realtime.setNetChange(new BigDecimal(o.getChg().trim()).doubleValue()); |
| | | realtime.setChangeRatio(new BigDecimal(o.getChgPct()).doubleValue()); |
| | | realtime.setType(o.getType()); |
| | | realtime.setBid(new BigDecimal(o.getBid()).doubleValue()); |
| | | realtime.setAsk(new BigDecimal(o.getAsk()).doubleValue()); |
| | | |
| | | DataCache.putRealtime(realtime.getSymbol(), realtime); |
| | | } |
| | | log.info("init US股票 数据 成功"); |
| | | } catch ( |
| | | Exception e) { |
| | | log.error("同步出错", e); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | public static void main(String[] args) { |
| | | StockTask task = new StockTask(); |
| | | task.loadAllStock(EStockType.US); |
| | | task.initAllStock(EStockType.US); |
| | | } |
| | | |
| | | @Override |
| | | public void run(String... args) throws Exception { |
| | | initAllStock(EStockType.US); |
| | | } |
| | | } |