| | |
| | | package com.xcong.excoin.quartz.job; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.huobi.client.model.Candlestick; |
| | | import com.xcong.excoin.modules.coin.entity.OrderCoinsDealEntity; |
| | | import com.xcong.excoin.modules.coin.service.OrderCoinService; |
| | | import com.xcong.excoin.processor.CoinProcessorFactory; |
| | | import com.xcong.excoin.trade.TradePlateModel; |
| | | import com.xcong.excoin.utils.RedisUtils; |
| | | import com.xcong.excoin.websocket.TradePlateSendWebSocket; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| | | import org.springframework.scheduling.annotation.Scheduled; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.math.BigDecimal; |
| | | import java.util.ArrayList; |
| | | import java.math.RoundingMode; |
| | | import java.util.Calendar; |
| | | import java.util.List; |
| | | import java.util.Random; |
| | | |
| | | /** |
| | | * 生成各时间段的K线信息 |
| | |
| | | */ |
| | | @Component |
| | | @Slf4j |
| | | @ConditionalOnProperty(prefix = "app", name = "exchange-trade", havingValue = "true") |
| | | public class KLineGeneratorJob { |
| | | @Resource |
| | | private CoinProcessorFactory processorFactory; |
| | | |
| | | @Resource |
| | | private TradePlateSendWebSocket plateSendWebSocket; |
| | | |
| | | @Resource |
| | | private RedisUtils redisUtils; |
| | | |
| | | @Scheduled(cron = "0/10 * * * * *") |
| | | public void tradePlate(){ |
| | | redisUtils.set("NEKK_NEW_PRICE",new BigDecimal(Math.random()*20)); |
| | | Candlestick candlestick = new Candlestick(); |
| | | candlestick.setOpen(new BigDecimal("10.33")); |
| | | candlestick.setHigh(new BigDecimal("15.23")); |
| | | candlestick.setVolume(new BigDecimal("12121.34")); |
| | | candlestick.setLow(new BigDecimal("8.234")); |
| | | candlestick.setAmount(new BigDecimal("1199")); |
| | | candlestick.setTimestamp(1599840000); |
| | | candlestick.setId(1599840000L); |
| | | candlestick.setCount(100002); |
| | | candlestick.setClose(new BigDecimal("12.2323")); |
| | | //redisUtils.set("NEKK/USDT",candlestick); |
| | | // [[10244.21, 0.000855], [10243.7, 0.008777], [10243.59, 0.14], [10243.37, 0.467663]] |
| | | TradePlateModel tradePlateModel = new TradePlateModel(); |
| | | List<BigDecimal> buy; |
| | | List<BigDecimal> sell; |
| | | for(int i=0;i<5;i++){ |
| | | buy = new ArrayList<>(2); |
| | | buy.add(new BigDecimal(Math.random()*i)); |
| | | buy.add(new BigDecimal(Math.random()*i)); |
| | | tradePlateModel.getBuy().add(buy); |
| | | sell = new ArrayList<>(2); |
| | | sell.add(new BigDecimal(Math.random()*i*2)); |
| | | sell.add(new BigDecimal(Math.random()*i*2)); |
| | | tradePlateModel.getSell().add(sell); |
| | | } |
| | | log.info("准备发送消息"); |
| | | plateSendWebSocket.sendMessagePlate("NEKK/USDT",JSON.toJSONString(tradePlateModel),null); |
| | | plateSendWebSocket.sendMessageKline("NEKK/USDT","1min","{amount: 114419.67835656216,close: 2.7261,count: 782,high: 2.7299,id: 1599632100,low: 2.723,open: 2.7288,vol: 311958.06091543}",null); |
| | | plateSendWebSocket.sendMessageKline("NEKK/USDT","5min","{amount: 514419.67835656216,close: 2.7261,count: 782,high: 2.7299,id: 1599632100,low: 2.723,open: 2.7288,vol: 911958.06091543}",null); |
| | | plateSendWebSocket.sendMessageKline("NEKK/USDT","15min","{amount: 514419.67835656216,close: 2.7261,count: 782,high: 2.7299,id: 1599632100,low: 2.723,open: 2.7288,vol: 911958.06091543}",null); |
| | | } |
| | | |
| | | /** |
| | | * 每分钟定时器,处理分钟K线 |
| | |
| | | public void handle5minKLine(){ |
| | | |
| | | Calendar calendar = Calendar.getInstance(); |
| | | log.debug("分钟K线:{}",calendar.getTime()); |
| | | //log.debug("分钟K线:{}",calendar.getTime()); |
| | | //将秒、微秒字段置为0 |
| | | calendar.set(Calendar.SECOND,0); |
| | | calendar.set(Calendar.MILLISECOND,0); |
| | |
| | | int hour = calendar.get(Calendar.HOUR_OF_DAY); |
| | | processorFactory.getProcessorMap().forEach((symbol,processor)->{ |
| | | if(!processor.isStopKline()) { |
| | | log.debug("生成{}分钟k线:{}",symbol); |
| | | //log.debug("生成{}分钟k线:{}",symbol); |
| | | //生成1分钟K线 |
| | | processor.autoGenerate(); |
| | | processor.generateKLine(1, Calendar.MINUTE, time); |
| | | //更新24H成交量 |
| | | processor.update24HVolume(time); |
| | | if(minute%5 == 0) { |
| | |
| | | long time = calendar.getTimeInMillis(); |
| | | |
| | | processor.generateKLine(1, Calendar.HOUR_OF_DAY, time); |
| | | // 四小时K线 |
| | | int hour = calendar.get(Calendar.HOUR_OF_DAY); |
| | | if(hour%4==0){ |
| | | processor.generateKLine(4, Calendar.HOUR_OF_DAY, time); |
| | | } |
| | | } |
| | | }); |
| | | } |