parent
c16a12ca12
commit
2451f7205a
9 changed files with 183 additions and 36 deletions
@ -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; |
||||||
|
} |
@ -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:"); |
||||||
|
} |
||||||
} |
} |
||||||
} |
} |
||||||
|
Loading…
Reference in new issue