From 2451f7205ac4ff2182bb5e645825fdc6eb2e046d Mon Sep 17 00:00:00 2001 From: "huan.xu" <275942173@qq.com> Date: Fri, 5 Jun 2020 10:51:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=8C=87=E6=A0=87=E8=AE=A1?= =?UTF-8?q?=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yyyf/indicators/IndicatorsUtils.java | 57 +++++++++++++++++++ .../indicators/dto/LastAndFirstAssetsDto.java | 14 +++++ .../AnnualGrowthRateIndicatorsHandler.java | 19 +++---- .../FluctuationRateIndicatorsHandler.java | 1 - .../handle/GrowthRateIndicatorsHandler.java | 20 +++---- .../MaxDrawdownRateIndicatorsHandler.java | 43 +++++++++++++- .../handle/MaxLossRateIndicatorsHandler.java | 45 ++++++++++++++- .../handle/ShapeRateIndicatorsHandler.java | 2 +- .../TotalGrowthRateIndicatorsHandler.java | 18 +++--- 9 files changed, 183 insertions(+), 36 deletions(-) create mode 100644 blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/IndicatorsUtils.java create mode 100644 blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/dto/LastAndFirstAssetsDto.java diff --git a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/IndicatorsUtils.java b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/IndicatorsUtils.java new file mode 100644 index 0000000..a64e434 --- /dev/null +++ b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/IndicatorsUtils.java @@ -0,0 +1,57 @@ +package com.blockchain.server.yyyf.indicators; + +import com.blockchain.server.yyyf.indicators.dto.AssetsReporter; +import com.blockchain.server.yyyf.indicators.dto.LastAndFirstAssetsDto; +import lombok.Data; + +import javax.persistence.EntityExistsException; +import java.util.List; + +/** + * @author huan.xu + * @version 1.0 + * @className IndicatorsUtils + * @description + * @date 2020-06-05 10:21 + */ +@Data +public class IndicatorsUtils { + + + /** + * 获取 期初与期末的信息 + * @param reporterList + * @return + */ + public static LastAndFirstAssetsDto getLastAndFirstAssetsDto(List reporterList) { + LastAndFirstAssetsDto result = new LastAndFirstAssetsDto(); + + if (reporterList != null && reporterList.size() > 0) { + //期初资产 + AssetsReporter firstAssets = null; + //交易期内最后一天 + AssetsReporter lastAssets = null; + for (AssetsReporter assetsReporter : reporterList) { + if (assetsReporter.getDays() == 0) { //如果期初资产 + firstAssets = assetsReporter; + } else { + if (lastAssets == null) { //最小的若为null + lastAssets = assetsReporter; + } else { + if (assetsReporter.getDays() > lastAssets.getDays()) { + lastAssets = assetsReporter; + } + } + } + } + result.setFirstAssets(firstAssets); + result.setLastAssets(lastAssets); + return result; + } else { + throw new EntityExistsException("assessUser not exit"); + } + + + } + +} \ No newline at end of file diff --git a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/dto/LastAndFirstAssetsDto.java b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/dto/LastAndFirstAssetsDto.java new file mode 100644 index 0000000..f97eba6 --- /dev/null +++ b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/dto/LastAndFirstAssetsDto.java @@ -0,0 +1,14 @@ +package com.blockchain.server.yyyf.indicators.dto; +import lombok.Data; +/** + * @author huan.xu + * @version 1.0 + * @className LastAndFirstAssetsReporterDto + * @description + * @date 2020-06-05 10:22 + */ +@Data +public class LastAndFirstAssetsDto { + private AssetsReporter lastAssets; + private AssetsReporter firstAssets; +} \ No newline at end of file diff --git a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/AnnualGrowthRateIndicatorsHandler.java b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/AnnualGrowthRateIndicatorsHandler.java index aa51fae..d9851bd 100644 --- a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/AnnualGrowthRateIndicatorsHandler.java +++ b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/AnnualGrowthRateIndicatorsHandler.java @@ -2,14 +2,13 @@ package com.blockchain.server.yyyf.indicators.handle; import com.blockchain.server.yyyf.indicators.IndicatorsHandler; import com.blockchain.server.yyyf.indicators.IndicatorsManager; +import com.blockchain.server.yyyf.indicators.IndicatorsUtils; import com.blockchain.server.yyyf.indicators.dto.AssetsReporter; +import com.blockchain.server.yyyf.indicators.dto.LastAndFirstAssetsDto; import lombok.AllArgsConstructor; import org.springframework.stereotype.Component; -import javax.persistence.EntityExistsException; import java.math.BigDecimal; -import java.util.ArrayList; -import java.util.Comparator; import java.util.List; /** @@ -26,14 +25,12 @@ public class AnnualGrowthRateIndicatorsHandler implements IndicatorsHandler { public Float handleIndicator(String assessUserId) { //TODO should be cached List reporterList = indicatorsManager.getAssetsReporterListByAssessUserId(assessUserId); - AssetsReporter lastAssets = reporterList - .stream() - .max(Comparator.comparingLong(AssetsReporter::getDays)) - .orElseThrow(() -> new EntityExistsException("assessUser:" + assessUserId)); - AssetsReporter firstAssets = reporterList - .stream() - .min(Comparator.comparingLong(AssetsReporter::getDays)) - .orElseThrow(() -> new EntityExistsException("assessUser:" + assessUserId)); + + //获取最初、最后交易的信息 + LastAndFirstAssetsDto lastAndFirstAssetsDto = IndicatorsUtils.getLastAndFirstAssetsDto(reporterList); + + AssetsReporter lastAssets =lastAndFirstAssetsDto.getLastAssets(); + AssetsReporter firstAssets =lastAndFirstAssetsDto.getFirstAssets(); BigDecimal result = lastAssets.getAssets() .divide(firstAssets.getAssets(), 5, BigDecimal.ROUND_HALF_UP) .pow((int) (360.0 / lastAssets.getDays())) diff --git a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/FluctuationRateIndicatorsHandler.java b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/FluctuationRateIndicatorsHandler.java index dc1744f..326a5ca 100644 --- a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/FluctuationRateIndicatorsHandler.java +++ b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/FluctuationRateIndicatorsHandler.java @@ -60,7 +60,6 @@ public class FluctuationRateIndicatorsHandler implements IndicatorsHandler { yeid2Count = yeid2Count.add(yeid.subtract(avgYeid).pow(2)); } return Float.valueOf(String.format("%.2f", Math.sqrt(BigDecimal.valueOf(360).divide(BigDecimal.valueOf(dayCount - 1), 5, BigDecimal.ROUND_HALF_UP).multiply(yeid2Count).doubleValue()))); - } else { throw new EntityExistsException("assessUser:" + assessUserId); } diff --git a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/GrowthRateIndicatorsHandler.java b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/GrowthRateIndicatorsHandler.java index 80cb987..7c93d1e 100644 --- a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/GrowthRateIndicatorsHandler.java +++ b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/GrowthRateIndicatorsHandler.java @@ -2,13 +2,13 @@ package com.blockchain.server.yyyf.indicators.handle; import com.blockchain.server.yyyf.indicators.IndicatorsHandler; import com.blockchain.server.yyyf.indicators.IndicatorsManager; +import com.blockchain.server.yyyf.indicators.IndicatorsUtils; import com.blockchain.server.yyyf.indicators.dto.AssetsReporter; +import com.blockchain.server.yyyf.indicators.dto.LastAndFirstAssetsDto; import lombok.AllArgsConstructor; import org.springframework.stereotype.Component; -import javax.persistence.EntityExistsException; import java.math.BigDecimal; -import java.util.Comparator; import java.util.List; /** @@ -24,14 +24,12 @@ public class GrowthRateIndicatorsHandler implements IndicatorsHandler { @Override public Float handleIndicator(String assessUserId) { List reporterList = indicatorsManager.getAssetsReporterListByAssessUserId(assessUserId); - AssetsReporter lastAssets = reporterList - .stream() - .max(Comparator.comparingLong(AssetsReporter::getDays)) - .orElseThrow(() -> new EntityExistsException("assessUser:" + assessUserId)); - AssetsReporter firstAssets = reporterList - .stream() - .min(Comparator.comparingLong(AssetsReporter::getDays)) - .orElseThrow(() -> new EntityExistsException("assessUser:" + assessUserId)); - return lastAssets.getAssets().subtract(firstAssets.getAssets()).divide(firstAssets.getAssets(),4,BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(100)).floatValue(); + //获取最初、最后交易的信息 + LastAndFirstAssetsDto lastAndFirstAssetsDto = IndicatorsUtils.getLastAndFirstAssetsDto(reporterList); + + AssetsReporter lastAssets =lastAndFirstAssetsDto.getLastAssets(); + AssetsReporter firstAssets =lastAndFirstAssetsDto.getFirstAssets(); + + return lastAssets.getAssets().subtract(firstAssets.getAssets()).divide(firstAssets.getAssets(), 4, BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(100)).floatValue(); } } diff --git a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/MaxDrawdownRateIndicatorsHandler.java b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/MaxDrawdownRateIndicatorsHandler.java index c3c4cc7..e1fffcd 100644 --- a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/MaxDrawdownRateIndicatorsHandler.java +++ b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/MaxDrawdownRateIndicatorsHandler.java @@ -1,17 +1,58 @@ package com.blockchain.server.yyyf.indicators.handle; import com.blockchain.server.yyyf.indicators.IndicatorsHandler; +import com.blockchain.server.yyyf.indicators.IndicatorsManager; +import com.blockchain.server.yyyf.indicators.dto.AssetsReporter; +import lombok.AllArgsConstructor; import org.springframework.stereotype.Component; +import javax.persistence.EntityExistsException; +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; + /** * @author fengyu */ +@AllArgsConstructor @Component(MaxDrawdownRateIndicatorsHandler.HANDLER_NAME + IndicatorsHandler.HANDLER_NAME) public class MaxDrawdownRateIndicatorsHandler implements IndicatorsHandler { public static final String HANDLER_NAME = "MaxDrawdownRate"; + + private final IndicatorsManager indicatorsManager; + @Override public Float handleIndicator(String assessUserId) { - return null; + List reporterList = indicatorsManager.getAssetsReporterListByAssessUserId(assessUserId); + if (reporterList != null && reporterList.size() > 0) { + //排序 + reporterList.sort(Comparator.comparingLong(AssetsReporter::getDays)); + + BigDecimal maxDrawdownRate=BigDecimal.ZERO; + //上一天的资产总额 + BigDecimal upAssets =BigDecimal.ZERO; + for (AssetsReporter assetsReporter : reporterList) { + BigDecimal thisAssets=assetsReporter.getAssets(); + //交易第二天开始 + if(assetsReporter.getDays()>1){ + BigDecimal thisDrawdownRate=BigDecimal.ONE.subtract(thisAssets.divide(upAssets,4,BigDecimal.ROUND_HALF_UP)); + if(thisDrawdownRate.compareTo(maxDrawdownRate)==1){ + maxDrawdownRate=thisDrawdownRate; + } + }else{ + //交易的第一天 + if(assetsReporter.getDays()==1){ + maxDrawdownRate=BigDecimal.ONE.subtract(thisAssets.divide(upAssets,4,BigDecimal.ROUND_HALF_UP)); + } + + } + upAssets=thisAssets; + } + return maxDrawdownRate.multiply(BigDecimal.valueOf(100)).floatValue(); + } else { + throw new EntityExistsException("assessUser:" + assessUserId); + } } } diff --git a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/MaxLossRateIndicatorsHandler.java b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/MaxLossRateIndicatorsHandler.java index 08cb390..dad8f98 100644 --- a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/MaxLossRateIndicatorsHandler.java +++ b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/MaxLossRateIndicatorsHandler.java @@ -1,17 +1,60 @@ package com.blockchain.server.yyyf.indicators.handle; import com.blockchain.server.yyyf.indicators.IndicatorsHandler; +import com.blockchain.server.yyyf.indicators.IndicatorsManager; +import com.blockchain.server.yyyf.indicators.dto.AssetsReporter; +import lombok.AllArgsConstructor; import org.springframework.stereotype.Component; +import javax.persistence.EntityExistsException; +import java.math.BigDecimal; +import java.util.List; + /** * @author fengyu */ +@AllArgsConstructor @Component(MaxLossRateIndicatorsHandler.HANDLER_NAME + IndicatorsHandler.HANDLER_NAME) public class MaxLossRateIndicatorsHandler implements IndicatorsHandler { public static final String HANDLER_NAME = "MaxLossRate"; + private final IndicatorsManager indicatorsManager; @Override public Float handleIndicator(String assessUserId) { - return null; + List reporterList = indicatorsManager.getAssetsReporterListByAssessUserId(assessUserId); + + if (reporterList != null && reporterList.size() > 0) { + //如果期初资产 + AssetsReporter firstAssets = null; + //交易期内资产最小值 + AssetsReporter minAssets = null; + for (AssetsReporter assetsReporter : reporterList) { + if (assetsReporter.getDays() == 0) { //如果期初资产 + firstAssets = assetsReporter; + } else { + if (minAssets == null) { //最小的若为null + minAssets = assetsReporter; + } else { + if (assetsReporter.getAssets().compareTo(minAssets.getAssets()) == -1) { + minAssets = assetsReporter; + } + } + } + } + long minDay = minAssets.getDays(); + int n = (int) (360 / minDay); + + Float result = BigDecimal.ONE.subtract(minAssets.getAssets().divide(firstAssets.getAssets(), 5, BigDecimal.ROUND_HALF_UP).pow(n)) + .setScale(2, BigDecimal.ROUND_HALF_UP).floatValue(); + + //负无穷的时候为0 + if (result.isInfinite()) { + result = Float.valueOf(0); + } + return result; + + } else { + throw new EntityExistsException("assessUser:"); + } } } diff --git a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/ShapeRateIndicatorsHandler.java b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/ShapeRateIndicatorsHandler.java index a7116f0..1d3a1a8 100644 --- a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/ShapeRateIndicatorsHandler.java +++ b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/ShapeRateIndicatorsHandler.java @@ -32,7 +32,7 @@ public class ShapeRateIndicatorsHandler implements IndicatorsHandler { Float fluctuationRate=fluctuationRateIndicatorsHandler.handleIndicator(assessUserId); return BigDecimal.valueOf(annualGrowthRate).subtract(riskFree).divide(BigDecimal.valueOf(fluctuationRate),4,BigDecimal.ROUND_HALF_UP) - .multiply(BigDecimal.valueOf(100)).floatValue(); + .floatValue(); } diff --git a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/TotalGrowthRateIndicatorsHandler.java b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/TotalGrowthRateIndicatorsHandler.java index 2bcd177..6be2247 100644 --- a/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/TotalGrowthRateIndicatorsHandler.java +++ b/blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/TotalGrowthRateIndicatorsHandler.java @@ -2,7 +2,9 @@ package com.blockchain.server.yyyf.indicators.handle; import com.blockchain.server.yyyf.indicators.IndicatorsHandler; import com.blockchain.server.yyyf.indicators.IndicatorsManager; +import com.blockchain.server.yyyf.indicators.IndicatorsUtils; import com.blockchain.server.yyyf.indicators.dto.AssetsReporter; +import com.blockchain.server.yyyf.indicators.dto.LastAndFirstAssetsDto; import lombok.AllArgsConstructor; import org.springframework.stereotype.Component; @@ -24,16 +26,12 @@ public class TotalGrowthRateIndicatorsHandler implements IndicatorsHandler { @Override public Float handleIndicator(String assessUserId) { List reporterList = indicatorsManager.getAssetsReporterListByAssessUserId(assessUserId); - AssetsReporter lastAssets = reporterList - .stream() - .max(Comparator.comparingLong(AssetsReporter::getDays)) - .orElseThrow(() -> new EntityExistsException("assessUser:" + assessUserId)); - - //最初的金额 - AssetsReporter firstAssets = reporterList - .stream() - .min(Comparator.comparingLong(AssetsReporter::getDays)) - .orElseThrow(() -> new EntityExistsException("assessUser:" + assessUserId)); + //获取最初、最后交易的信息 + LastAndFirstAssetsDto lastAndFirstAssetsDto = IndicatorsUtils.getLastAndFirstAssetsDto(reporterList); + + AssetsReporter lastAssets =lastAndFirstAssetsDto.getLastAssets(); + AssetsReporter firstAssets =lastAndFirstAssetsDto.getFirstAssets(); + //充值的总金额 //todo 不知道如何获取 BigDecimal rechargeAmount=null;