| | |
| | | |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.yami.trading.bean.data.domain.Kline; |
| | | import com.yami.trading.bean.data.domain.Realtime; |
| | | import com.yami.trading.bean.item.domain.Item; |
| | | import com.yami.trading.common.config.RequestDataHelper; |
| | | import com.yami.trading.common.constants.Constants; |
| | | import com.yami.trading.common.constants.RedisKeys; |
| | | import com.yami.trading.common.util.DateUtils; |
| | | import com.yami.trading.huobi.data.DataCache; |
| | | import com.yami.trading.huobi.data.job.RealtimeQueue; |
| | |
| | | import com.yami.trading.service.syspara.SysparaService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.jdbc.core.JdbcTemplate; |
| | | import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.time.*; |
| | | import java.time.temporal.ChronoUnit; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | |
| | | private RealtimeService realtimeService; |
| | | @Autowired |
| | | private JdbcTemplate jdbcTemplate; |
| | | @Autowired |
| | | RedisTemplate redisTemplate; |
| | | |
| | | @Override |
| | | public void saveAsyn(Realtime realtime) { |
| | |
| | | if (itemService.isSuspended(symbol)) { |
| | | return; |
| | | } |
| | | /*if (realtime.getSymbol().equals("galausdt")) { |
| | | System.out.println("galausdt111" + realtime); |
| | | }*/ |
| | | DataCache.putLatestRealTime(symbol, realtime); |
| | | DataCache.putLatestOpen(symbol, realtime.getOpen()); |
| | | |
| | | // 最近60s内实时价格集合 |
| | | List<Realtime> list = DataCache.latestRealTimeMap_60s.get(symbol); |
| | |
| | | RequestDataHelper.clear(); |
| | | return realtime; |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public Realtime getBefore(String symbol) { |
| | | Realtime realtime = (Realtime) redisTemplate.opsForValue().get(RedisKeys.REAL_TIME_BEFORE + symbol); |
| | | if (realtime != null) { |
| | | long ts = realtime.getTs(); |
| | | // 1. 确定时间戳单位(假设ts是毫秒级,若为秒级需用ofEpochSecond()) |
| | | Instant instant = Instant.ofEpochMilli(ts); |
| | | |
| | | // 2. 将时间戳转换为当地时区的日期(指定时区更准确,如Asia/Shanghai) |
| | | LocalDate tsDate = instant.atZone(ZoneId.of("Asia/Shanghai")).toLocalDate(); |
| | | |
| | | // 3. 获取“昨天的日期”(当前日期减1天) |
| | | LocalDate yesterday = LocalDate.now(ZoneId.of("Asia/Shanghai")).minusDays(1); |
| | | |
| | | // 4. 判断是否为昨天 |
| | | boolean isYesterday = tsDate.equals(yesterday); |
| | | if (isYesterday) { |
| | | return realtime; |
| | | } |
| | | } |
| | | // 没缓存重新保存 |
| | | // 计算当天0点的时间戳(毫秒级) |
| | | LocalDateTime todayStart = LocalDateTime.of(LocalDate.now(), LocalTime.MIN); |
| | | long todayStartTime = todayStart.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); |
| | | RequestDataHelper.set("symbol", symbol); |
| | | LambdaQueryWrapper<Realtime> queryWrapper = new LambdaQueryWrapper<Realtime>() |
| | | .eq(Realtime::getSymbol, symbol) |
| | | .lt(Realtime::getTs, todayStartTime) |
| | | .orderByDesc(Realtime::getTs) |
| | | .last("LIMIT 1"); |
| | | realtime = realtimeService.getBaseMapper().selectOne(queryWrapper); |
| | | RequestDataHelper.clear(); |
| | | if (realtime != null) { |
| | | redisTemplate.opsForValue().set(RedisKeys.REAL_TIME_BEFORE + symbol, realtime); |
| | | } |
| | | return realtime; |
| | | } |
| | | |
| | | @Override |
| | | public void cacheBefore24Hour(String symbol) { |
| | | // 计算“24小时前”的时间戳(毫秒级) |
| | | long twentyFourHoursAgo = Instant.now().minus(24, ChronoUnit.HOURS).toEpochMilli(); |
| | | RequestDataHelper.set("symbol", symbol); |
| | | LambdaQueryWrapper<Realtime> queryWrapper = new LambdaQueryWrapper<Realtime>() |
| | | .eq(Realtime::getSymbol, symbol) |
| | | .ge(Realtime::getTs, twentyFourHoursAgo) // 时间戳 >= 24小时前(前24小时内) |
| | | .orderByDesc(Realtime::getClose) // 24小时最高 |
| | | .last("LIMIT 1"); |
| | | Realtime realtimeHigh = realtimeService.getBaseMapper().selectOne(queryWrapper); |
| | | LambdaQueryWrapper<Realtime> queryWrapper2 = new LambdaQueryWrapper<Realtime>() |
| | | .eq(Realtime::getSymbol, symbol) |
| | | .ge(Realtime::getTs, twentyFourHoursAgo) // 时间戳 >= 24小时前(前24小时内) |
| | | .orderByAsc(Realtime::getClose) // 24小时最低 |
| | | .last("LIMIT 1"); |
| | | Realtime realtimeLow = realtimeService.getBaseMapper().selectOne(queryWrapper2); |
| | | RequestDataHelper.clear(); |
| | | System.out.println("realtimeHigh:" + realtimeHigh); |
| | | System.out.println("realtimeLow:" + realtimeLow); |
| | | if (realtimeHigh != null) { |
| | | DataCache.getRealtimeHigh().put(symbol, realtimeHigh.getClose().doubleValue()); |
| | | System.out.println("putRealtimeHigh:" + realtimeHigh.getClose().doubleValue()); |
| | | } |
| | | if (realtimeLow != null) { |
| | | DataCache.getRealtimeLow().put(symbol, realtimeLow.getClose().doubleValue()); |
| | | System.out.println("putRealtimeLow:" + realtimeLow.getClose().doubleValue()); |
| | | } |
| | | } |
| | | |
| | | public void deleteRealtime(int days) { |
| | |
| | | return realtimes; |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public BigDecimal getChangeRatio(Realtime realtime, String symbol) { |
| | | Item item = itemService.findBySymbol(symbol); |
| | | if (item.getType().equals(Item.cryptos) && (item.getCurrencyType() != null && item.getCurrencyType() == 1)) { |
| | | Realtime realtimeBefore = getBefore(symbol); |
| | | if (realtimeBefore == null) { |
| | | return BigDecimal.ZERO; |
| | | } |
| | | BigDecimal open = realtimeBefore.getClose(); |
| | | BigDecimal changeRatio = realtime.getClose().subtract(open).divide(open, 10, RoundingMode.HALF_UP); |
| | | changeRatio = changeRatio.multiply(new BigDecimal(100)).setScale(2, RoundingMode.DOWN); |
| | | return changeRatio; |
| | | } else { |
| | | return realtime.getChangeRatio2(); |
| | | } |
| | | } |
| | | } |