完善指标计算

pull/1/head
huan.xu 5 years ago
parent c16a12ca12
commit 2451f7205a
  1. 57
      blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/IndicatorsUtils.java
  2. 14
      blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/dto/LastAndFirstAssetsDto.java
  3. 19
      blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/AnnualGrowthRateIndicatorsHandler.java
  4. 1
      blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/FluctuationRateIndicatorsHandler.java
  5. 18
      blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/GrowthRateIndicatorsHandler.java
  6. 43
      blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/MaxDrawdownRateIndicatorsHandler.java
  7. 45
      blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/MaxLossRateIndicatorsHandler.java
  8. 2
      blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/ShapeRateIndicatorsHandler.java
  9. 18
      blockchain-server/blockchain-server-yyyf/src/main/java/com/blockchain/server/yyyf/indicators/handle/TotalGrowthRateIndicatorsHandler.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<AssetsReporter> 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");
}
}
}

@ -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;
}

@ -2,14 +2,13 @@ package com.blockchain.server.yyyf.indicators.handle;
import com.blockchain.server.yyyf.indicators.IndicatorsHandler; import com.blockchain.server.yyyf.indicators.IndicatorsHandler;
import com.blockchain.server.yyyf.indicators.IndicatorsManager; 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.AssetsReporter;
import com.blockchain.server.yyyf.indicators.dto.LastAndFirstAssetsDto;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.persistence.EntityExistsException;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List; import java.util.List;
/** /**
@ -26,14 +25,12 @@ public class AnnualGrowthRateIndicatorsHandler implements IndicatorsHandler {
public Float handleIndicator(String assessUserId) { public Float handleIndicator(String assessUserId) {
//TODO should be cached //TODO should be cached
List<AssetsReporter> reporterList = indicatorsManager.getAssetsReporterListByAssessUserId(assessUserId); List<AssetsReporter> reporterList = indicatorsManager.getAssetsReporterListByAssessUserId(assessUserId);
AssetsReporter lastAssets = reporterList
.stream() //获取最初、最后交易的信息
.max(Comparator.comparingLong(AssetsReporter::getDays)) LastAndFirstAssetsDto lastAndFirstAssetsDto = IndicatorsUtils.getLastAndFirstAssetsDto(reporterList);
.orElseThrow(() -> new EntityExistsException("assessUser:" + assessUserId));
AssetsReporter firstAssets = reporterList AssetsReporter lastAssets =lastAndFirstAssetsDto.getLastAssets();
.stream() AssetsReporter firstAssets =lastAndFirstAssetsDto.getFirstAssets();
.min(Comparator.comparingLong(AssetsReporter::getDays))
.orElseThrow(() -> new EntityExistsException("assessUser:" + assessUserId));
BigDecimal result = lastAssets.getAssets() BigDecimal result = lastAssets.getAssets()
.divide(firstAssets.getAssets(), 5, BigDecimal.ROUND_HALF_UP) .divide(firstAssets.getAssets(), 5, BigDecimal.ROUND_HALF_UP)
.pow((int) (360.0 / lastAssets.getDays())) .pow((int) (360.0 / lastAssets.getDays()))

@ -60,7 +60,6 @@ public class FluctuationRateIndicatorsHandler implements IndicatorsHandler {
yeid2Count = yeid2Count.add(yeid.subtract(avgYeid).pow(2)); 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()))); 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 { } else {
throw new EntityExistsException("assessUser:" + assessUserId); throw new EntityExistsException("assessUser:" + assessUserId);
} }

@ -2,13 +2,13 @@ package com.blockchain.server.yyyf.indicators.handle;
import com.blockchain.server.yyyf.indicators.IndicatorsHandler; import com.blockchain.server.yyyf.indicators.IndicatorsHandler;
import com.blockchain.server.yyyf.indicators.IndicatorsManager; 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.AssetsReporter;
import com.blockchain.server.yyyf.indicators.dto.LastAndFirstAssetsDto;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.persistence.EntityExistsException;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.Comparator;
import java.util.List; import java.util.List;
/** /**
@ -24,14 +24,12 @@ public class GrowthRateIndicatorsHandler implements IndicatorsHandler {
@Override @Override
public Float handleIndicator(String assessUserId) { public Float handleIndicator(String assessUserId) {
List<AssetsReporter> reporterList = indicatorsManager.getAssetsReporterListByAssessUserId(assessUserId); List<AssetsReporter> reporterList = indicatorsManager.getAssetsReporterListByAssessUserId(assessUserId);
AssetsReporter lastAssets = reporterList //获取最初、最后交易的信息
.stream() LastAndFirstAssetsDto lastAndFirstAssetsDto = IndicatorsUtils.getLastAndFirstAssetsDto(reporterList);
.max(Comparator.comparingLong(AssetsReporter::getDays))
.orElseThrow(() -> new EntityExistsException("assessUser:" + assessUserId)); AssetsReporter lastAssets =lastAndFirstAssetsDto.getLastAssets();
AssetsReporter firstAssets = reporterList AssetsReporter firstAssets =lastAndFirstAssetsDto.getFirstAssets();
.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(); return lastAssets.getAssets().subtract(firstAssets.getAssets()).divide(firstAssets.getAssets(), 4, BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(100)).floatValue();
} }
} }

@ -1,17 +1,58 @@
package com.blockchain.server.yyyf.indicators.handle; package com.blockchain.server.yyyf.indicators.handle;
import com.blockchain.server.yyyf.indicators.IndicatorsHandler; 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 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 * @author fengyu
*/ */
@AllArgsConstructor
@Component(MaxDrawdownRateIndicatorsHandler.HANDLER_NAME + IndicatorsHandler.HANDLER_NAME) @Component(MaxDrawdownRateIndicatorsHandler.HANDLER_NAME + IndicatorsHandler.HANDLER_NAME)
public class MaxDrawdownRateIndicatorsHandler implements IndicatorsHandler { public class MaxDrawdownRateIndicatorsHandler implements IndicatorsHandler {
public static final String HANDLER_NAME = "MaxDrawdownRate"; public static final String HANDLER_NAME = "MaxDrawdownRate";
private final IndicatorsManager indicatorsManager;
@Override @Override
public Float handleIndicator(String assessUserId) { public Float handleIndicator(String assessUserId) {
return null; List<AssetsReporter> 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);
}
} }
} }

@ -1,17 +1,60 @@
package com.blockchain.server.yyyf.indicators.handle; package com.blockchain.server.yyyf.indicators.handle;
import com.blockchain.server.yyyf.indicators.IndicatorsHandler; 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 org.springframework.stereotype.Component;
import javax.persistence.EntityExistsException;
import java.math.BigDecimal;
import java.util.List;
/** /**
* @author fengyu * @author fengyu
*/ */
@AllArgsConstructor
@Component(MaxLossRateIndicatorsHandler.HANDLER_NAME + IndicatorsHandler.HANDLER_NAME) @Component(MaxLossRateIndicatorsHandler.HANDLER_NAME + IndicatorsHandler.HANDLER_NAME)
public class MaxLossRateIndicatorsHandler implements IndicatorsHandler { public class MaxLossRateIndicatorsHandler implements IndicatorsHandler {
public static final String HANDLER_NAME = "MaxLossRate"; public static final String HANDLER_NAME = "MaxLossRate";
private final IndicatorsManager indicatorsManager;
@Override @Override
public Float handleIndicator(String assessUserId) { public Float handleIndicator(String assessUserId) {
return null; List<AssetsReporter> 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:");
}
} }
} }

@ -32,7 +32,7 @@ public class ShapeRateIndicatorsHandler implements IndicatorsHandler {
Float fluctuationRate=fluctuationRateIndicatorsHandler.handleIndicator(assessUserId); Float fluctuationRate=fluctuationRateIndicatorsHandler.handleIndicator(assessUserId);
return BigDecimal.valueOf(annualGrowthRate).subtract(riskFree).divide(BigDecimal.valueOf(fluctuationRate),4,BigDecimal.ROUND_HALF_UP) return BigDecimal.valueOf(annualGrowthRate).subtract(riskFree).divide(BigDecimal.valueOf(fluctuationRate),4,BigDecimal.ROUND_HALF_UP)
.multiply(BigDecimal.valueOf(100)).floatValue(); .floatValue();
} }

@ -2,7 +2,9 @@ package com.blockchain.server.yyyf.indicators.handle;
import com.blockchain.server.yyyf.indicators.IndicatorsHandler; import com.blockchain.server.yyyf.indicators.IndicatorsHandler;
import com.blockchain.server.yyyf.indicators.IndicatorsManager; 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.AssetsReporter;
import com.blockchain.server.yyyf.indicators.dto.LastAndFirstAssetsDto;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -24,16 +26,12 @@ public class TotalGrowthRateIndicatorsHandler implements IndicatorsHandler {
@Override @Override
public Float handleIndicator(String assessUserId) { public Float handleIndicator(String assessUserId) {
List<AssetsReporter> reporterList = indicatorsManager.getAssetsReporterListByAssessUserId(assessUserId); List<AssetsReporter> reporterList = indicatorsManager.getAssetsReporterListByAssessUserId(assessUserId);
AssetsReporter lastAssets = reporterList //获取最初、最后交易的信息
.stream() LastAndFirstAssetsDto lastAndFirstAssetsDto = IndicatorsUtils.getLastAndFirstAssetsDto(reporterList);
.max(Comparator.comparingLong(AssetsReporter::getDays))
.orElseThrow(() -> new EntityExistsException("assessUser:" + assessUserId)); AssetsReporter lastAssets =lastAndFirstAssetsDto.getLastAssets();
AssetsReporter firstAssets =lastAndFirstAssetsDto.getFirstAssets();
//最初的金额
AssetsReporter firstAssets = reporterList
.stream()
.min(Comparator.comparingLong(AssetsReporter::getDays))
.orElseThrow(() -> new EntityExistsException("assessUser:" + assessUserId));
//充值的总金额 //充值的总金额
//todo 不知道如何获取 //todo 不知道如何获取
BigDecimal rechargeAmount=null; BigDecimal rechargeAmount=null;

Loading…
Cancel
Save