parent
0345bf7449
commit
c16a12ca12
6 changed files with 157 additions and 8 deletions
@ -1,17 +1,69 @@ |
|||||||
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(FluctuationRateIndicatorsHandler.HANDLER_NAME + IndicatorsHandler.HANDLER_NAME) |
@Component(FluctuationRateIndicatorsHandler.HANDLER_NAME + IndicatorsHandler.HANDLER_NAME) |
||||||
public class FluctuationRateIndicatorsHandler implements IndicatorsHandler { |
public class FluctuationRateIndicatorsHandler implements IndicatorsHandler { |
||||||
public static final String HANDLER_NAME = "FluctuationRate"; |
public static final String HANDLER_NAME = "FluctuationRate"; |
||||||
|
|
||||||
|
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)); |
||||||
|
//总天数
|
||||||
|
int dayCount = reporterList.size() - 1; |
||||||
|
//每日收益率List
|
||||||
|
List<BigDecimal> dayYieldList = new ArrayList<BigDecimal>(dayCount); |
||||||
|
//收益率总和
|
||||||
|
BigDecimal totalYield = BigDecimal.ZERO; |
||||||
|
//上一天的资产总额
|
||||||
|
BigDecimal upAssets = reporterList.get(0).getAssets(); |
||||||
|
|
||||||
|
//计算总收益率以及每日收益率
|
||||||
|
for (AssetsReporter assetsReporter : reporterList) { |
||||||
|
if (assetsReporter.getDays() > 0) { |
||||||
|
BigDecimal thisAssets = assetsReporter.getAssets(); |
||||||
|
BigDecimal subtract = thisAssets.subtract(upAssets); |
||||||
|
//当天收益率
|
||||||
|
BigDecimal thisYeid = subtract.multiply(BigDecimal.valueOf(100)).divide(upAssets, 5, BigDecimal.ROUND_HALF_UP); |
||||||
|
//加入每日收益率集合
|
||||||
|
dayYieldList.add(thisYeid); |
||||||
|
//总收益率
|
||||||
|
totalYield = totalYield.add(thisYeid); |
||||||
|
//更换上一天的总额
|
||||||
|
upAssets = thisAssets; |
||||||
|
} |
||||||
|
} |
||||||
|
//平均收益率
|
||||||
|
BigDecimal avgYeid = totalYield.divide(BigDecimal.valueOf(dayCount), 5, BigDecimal.ROUND_HALF_UP); |
||||||
|
//(每日收益率-平均收益率)的平方和
|
||||||
|
BigDecimal yeid2Count = BigDecimal.ZERO; |
||||||
|
for (BigDecimal yeid : dayYieldList) { |
||||||
|
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); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
} |
} |
||||||
|
@ -1,17 +1,64 @@ |
|||||||
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(ProfitLossRateRateIndicatorsHandler.HANDLER_NAME + IndicatorsHandler.HANDLER_NAME) |
@Component(ProfitLossRateRateIndicatorsHandler.HANDLER_NAME + IndicatorsHandler.HANDLER_NAME) |
||||||
public class ProfitLossRateRateIndicatorsHandler implements IndicatorsHandler { |
public class ProfitLossRateRateIndicatorsHandler implements IndicatorsHandler { |
||||||
public static final String HANDLER_NAME = "ProfitLossRate"; |
public static final String HANDLER_NAME = "ProfitLossRate"; |
||||||
|
|
||||||
|
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 upAssets = reporterList.get(0).getAssets(); |
||||||
|
|
||||||
|
//总亏损额
|
||||||
|
BigDecimal lossMoneyCount = BigDecimal.ZERO; |
||||||
|
//盈利计数
|
||||||
|
int profitCount = 0; |
||||||
|
//总盈利额
|
||||||
|
BigDecimal profitMoneyCount = BigDecimal.ZERO; |
||||||
|
for (AssetsReporter assetsReporter : reporterList) { |
||||||
|
|
||||||
|
BigDecimal thisYeid = assetsReporter.getAssets(); |
||||||
|
//盈利
|
||||||
|
if (thisYeid.compareTo(upAssets) > 0) { //盈利
|
||||||
|
profitCount++; |
||||||
|
profitMoneyCount = profitMoneyCount.add(thisYeid).subtract(upAssets); |
||||||
|
}else{ |
||||||
|
lossMoneyCount = lossMoneyCount.add(thisYeid).subtract(upAssets); |
||||||
|
} |
||||||
|
upAssets=thisYeid; |
||||||
|
} |
||||||
|
if (profitCount == 0) { //每天亏损、或者每天既不盈利也不亏损
|
||||||
|
return Float.valueOf(0); |
||||||
|
}else if(profitCount==(reporterList.size()-1)){ //每天都在盈利
|
||||||
|
return Float.POSITIVE_INFINITY; |
||||||
|
}else { //计算盈亏比
|
||||||
|
return profitMoneyCount.divide(lossMoneyCount,4,BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(100)).floatValue(); |
||||||
|
} |
||||||
|
} else { |
||||||
|
throw new EntityExistsException("assessUser:" + assessUserId); |
||||||
|
} |
||||||
} |
} |
||||||
} |
} |
||||||
|
@ -1,17 +1,39 @@ |
|||||||
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 lombok.AllArgsConstructor; |
||||||
|
import org.springframework.beans.factory.annotation.Value; |
||||||
import org.springframework.stereotype.Component; |
import org.springframework.stereotype.Component; |
||||||
|
|
||||||
|
import java.math.BigDecimal; |
||||||
|
|
||||||
/** |
/** |
||||||
* @author fengyu |
* @author fengyu |
||||||
*/ |
*/ |
||||||
|
@AllArgsConstructor |
||||||
@Component(ShapeRateIndicatorsHandler.HANDLER_NAME + IndicatorsHandler.HANDLER_NAME) |
@Component(ShapeRateIndicatorsHandler.HANDLER_NAME + IndicatorsHandler.HANDLER_NAME) |
||||||
public class ShapeRateIndicatorsHandler implements IndicatorsHandler { |
public class ShapeRateIndicatorsHandler implements IndicatorsHandler { |
||||||
public static final String HANDLER_NAME = "AnnualGrowthRate"; |
public static final String HANDLER_NAME = "ShapeRate"; |
||||||
|
|
||||||
|
//无风险利率,配置文件注入,防止后期更改
|
||||||
|
@Value("${rate.riskFree}") |
||||||
|
private BigDecimal riskFree; |
||||||
|
|
||||||
|
private final AnnualGrowthRateIndicatorsHandler annualGrowthRateIndicatorsHandler; |
||||||
|
|
||||||
|
private final FluctuationRateIndicatorsHandler fluctuationRateIndicatorsHandler; |
||||||
|
|
||||||
@Override |
@Override |
||||||
public Float handleIndicator(String assessUserId) { |
public Float handleIndicator(String assessUserId) { |
||||||
return null; |
|
||||||
|
//年化收益率
|
||||||
|
Float annualGrowthRate=annualGrowthRateIndicatorsHandler.handleIndicator(assessUserId); |
||||||
|
//波动率
|
||||||
|
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(); |
||||||
|
|
||||||
} |
} |
||||||
|
|
||||||
} |
} |
||||||
|
@ -1,17 +1,44 @@ |
|||||||
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.Comparator; |
||||||
|
import java.util.List; |
||||||
|
|
||||||
/** |
/** |
||||||
* @author fengyu |
* @author fengyu |
||||||
*/ |
*/ |
||||||
|
@AllArgsConstructor |
||||||
@Component(TotalGrowthRateIndicatorsHandler.HANDLER_NAME + IndicatorsHandler.HANDLER_NAME) |
@Component(TotalGrowthRateIndicatorsHandler.HANDLER_NAME + IndicatorsHandler.HANDLER_NAME) |
||||||
public class TotalGrowthRateIndicatorsHandler implements IndicatorsHandler { |
public class TotalGrowthRateIndicatorsHandler implements IndicatorsHandler { |
||||||
public static final String HANDLER_NAME = "TotalGrowthRate"; |
public static final String HANDLER_NAME = "TotalGrowthRate"; |
||||||
|
|
||||||
|
private final IndicatorsManager indicatorsManager; |
||||||
|
|
||||||
@Override |
@Override |
||||||
public Float handleIndicator(String assessUserId) { |
public Float handleIndicator(String assessUserId) { |
||||||
return null; |
List<AssetsReporter> 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)); |
||||||
|
//充值的总金额
|
||||||
|
//todo 不知道如何获取
|
||||||
|
BigDecimal rechargeAmount=null; |
||||||
|
|
||||||
|
return lastAssets.getAssets().subtract(firstAssets.getAssets()).divide(rechargeAmount,4,BigDecimal.ROUND_HALF_UP) |
||||||
|
.multiply(BigDecimal.valueOf(100)).floatValue(); |
||||||
} |
} |
||||||
} |
} |
||||||
|
Loading…
Reference in new issue