package com.nq.utils;
|
|
|
import com.github.pagehelper.util.StringUtil;
|
import com.nq.pojo.UserPosition;
|
import com.nq.vo.position.PositionProfitVO;
|
import com.nq.vo.position.UserPositionVO;
|
|
import java.math.BigDecimal;
|
|
/**
|
* 仓位计算工具类
|
* */
|
public class UserPointUtil {
|
|
|
/**
|
* 返回当前 仓位表 实时数据
|
* */
|
public static UserPositionVO assembleUserPositionVO(UserPosition position,BigDecimal nowPrice) {
|
UserPositionVO userPositionVO = new UserPositionVO();
|
|
userPositionVO.setId(position.getId());
|
userPositionVO.setPositionType(position.getPositionType());
|
userPositionVO.setPositionSn(position.getPositionSn());
|
userPositionVO.setUserId(position.getUserId());
|
userPositionVO.setNickName(position.getNickName());
|
userPositionVO.setAgentId(position.getAgentId());
|
userPositionVO.setStockName(position.getStockName());
|
userPositionVO.setStockCode(position.getStockCode());
|
userPositionVO.setStockGid(position.getStockGid());
|
userPositionVO.setStockSpell(position.getStockSpell());
|
userPositionVO.setBuyOrderId(position.getBuyOrderId());
|
userPositionVO.setBuyOrderTime(position.getBuyOrderTime());
|
userPositionVO.setBuyOrderPrice(position.getBuyOrderPrice());
|
userPositionVO.setSellOrderId(position.getSellOrderId());
|
userPositionVO.setSellOrderTime(position.getSellOrderTime());
|
userPositionVO.setSellOrderPrice(position.getSellOrderPrice());
|
userPositionVO.setProfitTargetPrice(position.getProfitTargetPrice());
|
userPositionVO.setStopTargetPrice(position.getStopTargetPrice());
|
userPositionVO.setOrderDirection(position.getOrderDirection());
|
userPositionVO.setOrderNum(position.getOrderNum());
|
userPositionVO.setOrderLever(position.getOrderLever());
|
userPositionVO.setOrderTotalPrice(position.getOrderTotalPrice());
|
userPositionVO.setOrderFee(position.getOrderFee());
|
userPositionVO.setOrderSpread(position.getOrderSpread());
|
userPositionVO.setOrderStayFee(position.getOrderStayFee());
|
userPositionVO.setOrderStayDays(position.getOrderStayDays());
|
userPositionVO.setMarginAdd(position.getMarginAdd());
|
userPositionVO.setStockPlate(position.getStockPlate());
|
userPositionVO.setSpreadRatePrice(position.getSpreadRatePrice());
|
PositionProfitVO positionProfitVO = getPositionProfitVO(position,nowPrice);
|
BigDecimal ss = positionProfitVO.getProfitAndLose().divide(userPositionVO.getOrderTotalPrice(),BigDecimal.ROUND_CEILING)
|
.multiply(new BigDecimal(100));
|
userPositionVO.setProfitAndLoseParent(ss.setScale(2, BigDecimal.ROUND_DOWN)+"%");
|
userPositionVO.setProfitAndLose(positionProfitVO.getProfitAndLose());
|
userPositionVO.setAllProfitAndLose(positionProfitVO.getAllProfitAndLose());
|
if(StringUtil.isEmpty(userPositionVO.getSellOrderId())){
|
userPositionVO.setNow_price(nowPrice.toString());
|
}else{
|
userPositionVO.setNow_price(position.getSellOrderPrice().toString());
|
}
|
userPositionVO.setAmountToBeCovered(position.getAmountToBeCovered());
|
return userPositionVO;
|
}
|
|
/**
|
* 计算仓位盈亏计算方法
|
* */
|
public static PositionProfitVO getPositionProfitVO(UserPosition position,BigDecimal nowPrice) {
|
BigDecimal profitAndLose = new BigDecimal("0");
|
BigDecimal allProfitAndLose = new BigDecimal("0");
|
|
if (position.getSellOrderId() != null) {
|
|
BigDecimal subPrice = position.getSellOrderPrice().subtract(position.getBuyOrderPrice());
|
profitAndLose = subPrice.multiply(new BigDecimal(position.getOrderNum().intValue()));
|
if ("买跌".equals(position.getOrderDirection())) {
|
profitAndLose = profitAndLose.negate();
|
}
|
allProfitAndLose = profitAndLose.subtract(position.getOrderSpread()).subtract(position.getOrderStayFee()).subtract(position.getSpreadRatePrice());
|
} else {
|
BigDecimal subPrice = nowPrice.subtract(position.getBuyOrderPrice());
|
profitAndLose = subPrice.multiply(new BigDecimal(position.getOrderNum().intValue()));
|
if ("买跌".equals(position.getOrderDirection())) {
|
profitAndLose = profitAndLose.negate();
|
}
|
//总盈亏= 浮动盈亏 – 手续费 – 印花税 – 留仓费 – 点差费
|
allProfitAndLose = profitAndLose.subtract(position.getOrderSpread()).subtract(position.getOrderStayFee()).subtract(position.getSpreadRatePrice());
|
}
|
PositionProfitVO positionProfitVO = new PositionProfitVO();
|
positionProfitVO.setProfitAndLose(profitAndLose);
|
positionProfitVO.setAllProfitAndLose(allProfitAndLose);
|
positionProfitVO.setNowPrice(nowPrice.setScale(5,BigDecimal.ROUND_UP).toString());
|
return positionProfitVO;
|
}
|
}
|