etf
zyy
2026-01-15 bb146de0f32bfbbb516e1c25ff4873986d548673
trading-order-service/src/main/java/com/yami/trading/service/dz/impl/StockDzServiceImpl.java
@@ -30,6 +30,7 @@
import com.yami.trading.dao.dz.StockDzMapper;
import com.yami.trading.service.MarketOpenChecker;
import com.yami.trading.service.MoneyLogService;
import com.yami.trading.service.UsStockTradingDayCalculator;
import com.yami.trading.service.WalletService;
import com.yami.trading.service.data.DataService;
import com.yami.trading.service.dz.StockDzService;
@@ -46,6 +47,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.*;
@@ -270,11 +272,34 @@
                    }
                    if (dz.getDayRate() > 0) {
                        Date startTime = dz.getCreateTime();
                        Date now = new Date();
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTime(startTime);
                        calendar.add(Calendar.DATE, dz.getPeriod());
                        // 锁仓截至时间
                        Date resultTime = calendar.getTime();
                        //显示收益不超过 锁仓截至时间
                        if(now.getTime() > resultTime.getTime()){
                            now = resultTime;
                        }
                        // 计算美股交易天数
                        int num = UsStockTradingDayCalculator.countUsStockTradingDays(startTime, now);
                        // 计算相差天数
                        int num = com.yami.trading.common.util.DateUtil.dateNum(startTime, new Date());
                        int days = com.yami.trading.common.util.DateUtil.dateNum(startTime, now);
                        if (days >= dz.getPeriod()) {
                            num--;
                        }
                        num = Math.max(1, Math.min(num, dz.getPeriod()));
                        double dayEarnings = dz.getDayRate() * dz.getVolume();
                        double profitLoss = dayEarnings * num;
                        double dayEarnings = 0; //日收益
                        double profitLoss = 0;  //盈利
                        double volume = dz.getVolume();  //本金
                        for (int i = 0; i < num; i++) {
                            dayEarnings = dz.getDayRate() * volume;
                            profitLoss += dayEarnings;
                            volume += dayEarnings;
                        }
                        DecimalFormat df = new DecimalFormat("#.##");
                        String resultStr = df.format(profitLoss);
                        String resultStr2 = df.format(dayEarnings);
@@ -356,30 +381,30 @@
    public Result buyDz(String dzId, String password, double num, String partyId, Boolean isAdmin) {
        try {
            if (num <= 0) {
                throw new YamiShopBindException("请输入");
                return Result.failed("请购买最小数量");
            }
            User party = userService.getById(partyId);
            if (!party.isEnabled()) {
                throw new YamiShopBindException("用户已禁用");
                return Result.failed("用户已禁用");
            }
            Syspara syspara = sysparaService.find("stop_user_internet");
            /*Syspara syspara = sysparaService.find("stop_user_internet");
            String stopUserInternet = syspara.getSvalue();
            if (org.apache.commons.lang3.StringUtils.isNotEmpty(stopUserInternet)) {
                String[] stopUsers = stopUserInternet.split(",");
                if (Arrays.asList(stopUsers).contains(party.getUserName())) {
                    throw new YamiShopBindException("无网络");
                }
            }
            }*/
            StockDz stockDz = this.stockDzMapper.selectOne(new QueryWrapper<StockDz>().eq("uuid", dzId));
            if (stockDz == null) {
                throw new YamiShopBindException("大宗不存在");
                return Result.failed("大宗数据错误");
            }
            if (StringUtils.isNotEmpty(stockDz.getPassword()) && !Objects.equals(stockDz.getPassword(), password)) {
                throw new YamiShopBindException("密码错误");
                return Result.failed("密码错误");
            }
            if (stockDz.getIsLock() != null && stockDz.getIsLock() != 0) {
                throw new YamiShopBindException("股票被锁定,不能购买");
                return Result.failed("股票被锁定");
            }
            if(stockDz.getStartTime().getTime() > new Date().getTime() || stockDz.getEndTime().getTime() < new Date().getTime()){
@@ -388,7 +413,7 @@
            BigDecimal nowPrice = stockDz.getNowPrice();
            if (nowPrice.compareTo(new BigDecimal("0")) == 0) {
                throw new YamiShopBindException("股票价格0,请重试");
                return Result.failed("大宗数据错误");
            }
            boolean isETF = stockDz.getStockType().equalsIgnoreCase(Item.indices);
            if (isETF) {
@@ -447,7 +472,7 @@
            if(stockDz.getSwitchType() == 1) {
                order.setState(ExchangeApplyOrderDz.STATE_SUBMITTED);
                exchangeApplyOrderDzMapper.insert(order);
                return Result.succeed("购买成功,等待审核");
                return Result.succeed("购买成功");
            }
            order.setState(ExchangeApplyOrderDz.STATE_POSITION);
@@ -623,16 +648,16 @@
        try {
            ExchangeApplyOrderDz order = exchangeApplyOrderDzMapper.selectById(id);
            if (order == null) {
                throw new YamiShopBindException("订单不存在");
                return Result.failed("失败");
            }
            if (!order.getState().equals(ExchangeApplyOrderDz.STATE_POSITION)) {
                throw new YamiShopBindException("订单不能平仓");
                return Result.failed("订单不能平仓");
            }
            if (num == null) {  //不传默认平全部
                num = order.getSymbolValue();
            }
            if (num < 0 || num > order.getSymbolValue()) {
                throw new YamiShopBindException("数量错误");
                return Result.failed("数量错误");
            }
            if (partyId == null) {
                partyId = order.getPartyId();
@@ -640,7 +665,7 @@
            StockDz stockDz = this.stockDzMapper.selectOne(new QueryWrapper<StockDz>().eq("uuid", order.getDzId()));
            if (stockDz == null) {
                throw new YamiShopBindException("大宗不存在");
                return Result.failed("大宗数据错误");
            }
            BigDecimal nowPrice;
@@ -651,22 +676,27 @@
                nowPrice = stockDz.getNowPrice();
            }
            if (nowPrice.compareTo(new BigDecimal("0")) == 0) {
                throw new YamiShopBindException("股票价格0,请重试");
                return Result.failed("大宗数据错误");
            }
            Date now = new Date();
            if (!isAdmin) {
                if (stockDz.getPeriod() != null && stockDz.getPeriod() > 0) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(order.getCreateTime());
                    calendar.add(Calendar.DATE, stockDz.getPeriod());
                    // 锁仓时间
                    Date resultTime = calendar.getTime();
                    if(now.getTime() < resultTime.getTime()){
            Date now = new Date();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(order.getCreateTime());
            calendar.add(Calendar.DATE, stockDz.getPeriod());
            // 锁仓截至时间
            Date resultTime = calendar.getTime();
            if (!isAdmin) {
                if (isETF && (order.getUnLock() == null || order.getUnLock() == 0)) {
                    return Result.failed("未到平仓时间");
                }
                if (stockDz.getPeriod() != null && stockDz.getPeriod() > 0) {
                    if (now.getTime() < resultTime.getTime()) {
                        return Result.failed("未到平仓时间");
                    }
                }
                if (!stockDz.getStockName().contains("测试")) {
                    boolean isOpen = MarketOpenChecker.isMarketOpenByItemCloseType(Item.US_STOCKS);
                    if (!isOpen) {
@@ -698,7 +728,14 @@
            if (isETF) {
                //按日收益率结算
                closeAmt = order.getPrice();
                closeAmt = closeAmt + stockDz.getDayRate() * closeAmt * stockDz.getPeriod();
                Date startTime = order.getCreateTime();
                // 计算美股交易天数
                int day = UsStockTradingDayCalculator.countUsStockTradingDays(startTime, resultTime) - 1;
                day = Math.max(1, Math.min(day, stockDz.getPeriod()));
                // 复利总金额 = 本金 × (1 + 日收益率) ^ 交易日数
                closeAmt = closeAmt * Math.pow(1 + stockDz.getDayRate(), day);
                closeAmt = BigDecimal.valueOf(closeAmt).setScale(4, RoundingMode.DOWN).doubleValue();
                closePrice = closeAmt;
                closeAmt = closeAmt - orderFree.doubleValue();
            } else {
@@ -763,4 +800,28 @@
    }
    public static void main(String[] args) {
        double dayEarnings = 0; //日收益
        double profitLoss = 0;  //盈利
        double volume = 6000;  //本金
        double price = volume;
        int num = 11;
        double dayRate = 0.12;
        for (int i = 0; i < num; i++) {
            dayEarnings = dayRate * volume;
            profitLoss += dayEarnings;
            volume += dayEarnings;
        }
        System.out.println("dayEarnings:" + dayEarnings);
        System.out.println("profitLoss:" + profitLoss);
        System.out.println("volume:" + volume);
        // 复利总金额 = 本金 × (1 + 日收益率) ^ 交易日数
        double compoundAmount = price * Math.pow(1 + dayRate, num);
        // 复利总盈亏 = 复利总金额 - 本金
        double profitLoss2 = compoundAmount - price;
        System.out.println("compoundAmount:" + compoundAmount);
        System.out.println("profitLoss2:" + profitLoss2);
    }
}