public void calTrendMaMonitor(String code, List<Indicator> indicatorList) { List<ProdIndex> prodIndexList = prodIndexBuss.queryAscByCode(code); indicatorList .stream() .forEach( indicator -> { TrendMaMonitor trendMaMonitor1 = trendMaMonitorRepos.findByCodeAndTrend(code, indicator.getName()); TrendMaMonitor trendMaMonitor2 = new TrendMaMonitor(); trendMaMonitor2.setTrend(indicator.getName()); trendMaMonitor2.setCode(code); TrendMaMonitor trendMaMonitor = trendMaMonitor1 == null ? trendMaMonitor2 : trendMaMonitor1; prodIndexList .stream() .forEach( prodIndex -> { indicator.push(prodIndex.getDt(), prodIndex.getPrice()); if (indicator.getLine().isEmpty()) { return; } Price maPrice = indicator.getLine().get(indicator.getLine().size() - 1); calTrendMaMonitor( trendMaMonitor, prodIndex.getDt(), prodIndex.getPrice(), maPrice.getP()); }); trendMaMonitorRepos.save(trendMaMonitor); }); trendMaMonitorRepos.flush(); }
private void calTrendMaMonitor( TrendMaMonitor trendMaMonitor, Date date, BigDecimal prodPrice, BigDecimal maPrice) { if (trendMaMonitor.getCalDate() == null) { trendMaMonitor.setCalDate(date); trendMaMonitor.setProdPrice(prodPrice); trendMaMonitor.setMaPrice(maPrice); trendMaMonitor.setDiff(maPrice.subtract(prodPrice).divide(prodPrice, 4, RoundingMode.FLOOR)); trendMaMonitor.setTrendDays(1); } else if (trendMaMonitor.getCalDate().before(date)) { BigDecimal lastDiff = trendMaMonitor.getDiff(); BigDecimal diff = maPrice.subtract(prodPrice).divide(prodPrice, 4, RoundingMode.FLOOR); trendMaMonitor.setCalDate(date); trendMaMonitor.setProdPrice(prodPrice); trendMaMonitor.setMaPrice(maPrice); trendMaMonitor.setDiff(diff); if (diff.compareTo(BigDecimal.ZERO) == 0) { trendMaMonitor.setTrendDays(trendMaMonitor.getTrendDays() + 1); trendMaMonitor.setReturnDays(0); } else if (lastDiff.compareTo(BigDecimal.ZERO) == 0) { trendMaMonitor.setTrendDays(trendMaMonitor.getTrendDays() + 1); trendMaMonitor.setReturnDays(null); } else if (diff.compareTo(BigDecimal.ZERO) * lastDiff.compareTo(BigDecimal.ZERO) > 0) { trendMaMonitor.setTrendDays(trendMaMonitor.getTrendDays() + 1); BigDecimal absDiff = diff.abs(); BigDecimal absLastDiff = lastDiff.abs(); if (absLastDiff.compareTo(absDiff) > 0) { int returnDays = absDiff.divide(absLastDiff.subtract(absDiff), 0, RoundingMode.UP).intValue(); trendMaMonitor.setReturnDays(returnDays); } else { trendMaMonitor.setReturnDays(null); } } else if (diff.compareTo(BigDecimal.ZERO) * lastDiff.compareTo(BigDecimal.ZERO) < 0) { trendMaMonitor.setTrendDays(1); trendMaMonitor.setReturnDays(0); } } }