Search in sources :

Example 1 with TradeRecord

use of com.mistra.plank.pojo.entity.TradeRecord in project plank by MistraR.

the class Barbarossa method reduceStock.

/**
 * 减仓股票
 *
 * @param holdShare 持仓记录
 * @param clearanceReasonEnum 清仓原因
 * @param date 时间
 * @param sellPrice 清仓价格
 */
private void reduceStock(HoldShares holdShare, ClearanceReasonEnum clearanceReasonEnum, Date date, DailyRecord todayRecord, double sellPrice) {
    if (holdShare.getNumber() <= 0) {
        holdSharesMapper.delete(new QueryWrapper<HoldShares>().eq("id", holdShare.getId()));
        return;
    }
    // 卖出数量
    int number = holdShare.getNumber() <= 100 ? 100 : holdShare.getNumber() / 2;
    // 卖出金额
    double money = number * sellPrice;
    // 本次卖出部分盈利金额
    BigDecimal profit = new BigDecimal(number * (sellPrice - holdShare.getBuyPrice().doubleValue()));
    // 可用金额
    BALANCE_AVAILABLE = BALANCE_AVAILABLE.add(new BigDecimal(money));
    TradeRecord tradeRecord = new TradeRecord();
    tradeRecord.setName(holdShare.getName());
    tradeRecord.setCode(holdShare.getCode());
    tradeRecord.setDate(date);
    tradeRecord.setMoney((int) money);
    tradeRecord.setReason("减仓" + holdShare.getName() + number + "股,卖出金额" + (int) money + "元,当前可用余额" + BALANCE_AVAILABLE.intValue() + ",减仓原因" + clearanceReasonEnum.getDesc());
    tradeRecord.setBalance(BALANCE.setScale(2, RoundingMode.HALF_UP));
    tradeRecord.setAvailableBalance(BALANCE_AVAILABLE.setScale(2, RoundingMode.HALF_UP));
    tradeRecord.setPrice(new BigDecimal(sellPrice));
    tradeRecord.setNumber(number);
    tradeRecord.setType(1);
    tradeRecordMapper.insert(tradeRecord);
    if (holdShare.getNumber() - number == 0) {
        holdSharesMapper.delete(new QueryWrapper<HoldShares>().eq("id", holdShare.getId()));
        return;
    }
    holdShare.setNumber(holdShare.getNumber() - number);
    holdShare.setCost(holdShare.getBuyPrice().multiply(new BigDecimal(holdShare.getBuyNumber())).subtract(profit).divide(new BigDecimal(number), 2, RoundingMode.HALF_UP));
    holdShare.setProfit(holdShare.getProfit().add(profit));
    holdShare.setFifteenProfit(true);
    holdShare.setCurrentPrice(todayRecord.getClosePrice());
    holdShare.setRate(todayRecord.getClosePrice().subtract(holdShare.getBuyPrice()).divide(holdShare.getBuyPrice(), 2, RoundingMode.HALF_UP));
    holdSharesMapper.updateById(holdShare);
    log.info("{}日减仓 {},目前盈利 {} 元!", sdf.format(date), holdShare.getName(), holdShare.getProfit().add(profit).intValue());
}
Also used : QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) TradeRecord(com.mistra.plank.pojo.entity.TradeRecord) BigDecimal(java.math.BigDecimal)

Example 2 with TradeRecord

use of com.mistra.plank.pojo.entity.TradeRecord in project plank by MistraR.

the class Barbarossa method clearanceStock.

/**
 * 清仓股票
 *
 * @param holdShare 持仓记录
 * @param clearanceReasonEnum 清仓原因
 * @param date 时间
 * @param sellPrice 清仓价格
 */
private void clearanceStock(HoldShares holdShare, ClearanceReasonEnum clearanceReasonEnum, Date date, double sellPrice) {
    if (holdShare.getNumber() <= 0) {
        holdSharesMapper.delete(new QueryWrapper<HoldShares>().eq("id", holdShare.getId()));
        return;
    }
    // 卖出金额
    double money = holdShare.getNumber() * sellPrice;
    // 本次卖出部分盈利金额
    BigDecimal profit = BigDecimal.valueOf(holdShare.getNumber() * (sellPrice - holdShare.getBuyPrice().doubleValue()));
    // 总盈利
    profit = holdShare.getProfit().add(profit);
    // 总资产
    BALANCE = BALANCE.add(profit);
    // 可用金额
    BALANCE_AVAILABLE = BALANCE_AVAILABLE.add(new BigDecimal(money));
    TradeRecord tradeRecord = new TradeRecord();
    tradeRecord.setName(holdShare.getName());
    tradeRecord.setCode(holdShare.getCode());
    tradeRecord.setDate(date);
    tradeRecord.setMoney((int) money);
    tradeRecord.setReason("清仓" + holdShare.getName() + holdShare.getNumber() + "股,卖出金额" + (int) money + "元,当前可用余额" + BALANCE_AVAILABLE.intValue() + ",清仓原因" + clearanceReasonEnum.getDesc());
    tradeRecord.setBalance(BALANCE.setScale(2, RoundingMode.HALF_UP));
    tradeRecord.setAvailableBalance(BALANCE_AVAILABLE.setScale(2, RoundingMode.HALF_UP));
    tradeRecord.setPrice(new BigDecimal(sellPrice));
    tradeRecord.setNumber(holdShare.getNumber());
    tradeRecord.setType(1);
    tradeRecordMapper.insert(tradeRecord);
    Clearance clearance = new Clearance();
    clearance.setCode(holdShare.getCode());
    clearance.setName(holdShare.getName());
    clearance.setCostPrice(holdShare.getBuyPrice());
    clearance.setNumber(holdShare.getBuyNumber());
    clearance.setPrice(new BigDecimal(sellPrice));
    clearance.setRate(profit.divide(BigDecimal.valueOf(holdShare.getBuyNumber() * holdShare.getBuyPrice().doubleValue()), 2, RoundingMode.HALF_UP));
    clearance.setProfit(profit);
    clearance.setReason("清仓" + holdShare.getName() + "总计盈亏" + profit.intValue() + "元,清仓原因:" + clearanceReasonEnum.getDesc() + ",建仓日期" + sdf.format(holdShare.getBuyTime()));
    clearance.setDate(date);
    clearance.setBalance(BALANCE.setScale(2, RoundingMode.HALF_UP));
    clearance.setAvailableBalance(BALANCE_AVAILABLE.setScale(2, RoundingMode.HALF_UP));
    clearance.setDayNumber(Days.daysBetween(new LocalDate(holdShare.getBuyTime().getTime()), new LocalDate(date.getTime())).getDays());
    clearanceMapper.insert(clearance);
    holdSharesMapper.delete(new QueryWrapper<HoldShares>().eq("id", holdShare.getId()));
    log.info("{}日清仓 {},总共盈利 {} 元!当前总资产: {} ", sdf.format(date), holdShare.getName(), profit.intValue(), BALANCE.intValue());
}
Also used : QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) TradeRecord(com.mistra.plank.pojo.entity.TradeRecord) Clearance(com.mistra.plank.pojo.entity.Clearance) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal)

Example 3 with TradeRecord

use of com.mistra.plank.pojo.entity.TradeRecord in project plank by MistraR.

the class Barbarossa method buyStock.

private void buyStock(List<Stock> stocks, Date date) {
    for (Stock stock : stocks) {
        List<HoldShares> holdShares = holdSharesMapper.selectList(new QueryWrapper<>());
        if (holdShares.size() >= plankConfig.getFundsPart()) {
            log.info("仓位已打满!无法开新仓!");
            return;
        }
        Page<DailyRecord> selectPage = dailyRecordMapper.selectPage(new Page<>(1, 5), new QueryWrapper<DailyRecord>().eq("code", stock.getCode()).ge("date", date).le("date", DateUtils.addDays(date, 12)).orderByAsc("date"));
        if (selectPage.getRecords().size() < 2) {
            continue;
        }
        DailyRecord dailyRecord = selectPage.getRecords().get(1);
        double openRatio = (selectPage.getRecords().get(1).getOpenPrice().subtract(selectPage.getRecords().get(0).getClosePrice())).divide(selectPage.getRecords().get(0).getClosePrice(), 2, RoundingMode.HALF_UP).doubleValue();
        if (openRatio > -0.03 && openRatio < plankConfig.getBuyPlankRatioLimit().doubleValue() && BALANCE_AVAILABLE.intValue() > 10000) {
            // 低开2个点以下不买
            HoldShares one = holdSharesMapper.selectOne(new QueryWrapper<HoldShares>().eq("code", stock.getCode()));
            if (Objects.isNull(one)) {
                int money = BALANCE.intValue() / plankConfig.getFundsPart();
                money = Math.min(money, BALANCE_AVAILABLE.intValue());
                int number = money / dailyRecord.getOpenPrice().multiply(new BigDecimal(100)).intValue();
                double cost = number * 100 * dailyRecord.getOpenPrice().doubleValue();
                BALANCE_AVAILABLE = BALANCE_AVAILABLE.subtract(new BigDecimal(cost));
                HoldShares holdShare = HoldShares.builder().buyTime(DateUtils.addHours(dailyRecord.getDate(), 9)).code(stock.getCode()).name(stock.getName()).cost(dailyRecord.getOpenPrice()).fifteenProfit(false).number(number * 100).profit(new BigDecimal(0)).currentPrice(dailyRecord.getOpenPrice()).rate(new BigDecimal(0)).buyPrice(dailyRecord.getOpenPrice()).buyNumber(number * 100).build();
                holdSharesMapper.insert(holdShare);
                TradeRecord tradeRecord = new TradeRecord();
                tradeRecord.setName(holdShare.getName());
                tradeRecord.setCode(holdShare.getCode());
                tradeRecord.setDate(DateUtils.addHours(dailyRecord.getDate(), 9));
                tradeRecord.setMoney((int) (number * 100 * dailyRecord.getOpenPrice().doubleValue()));
                tradeRecord.setReason("买入" + holdShare.getName() + number * 100 + "股,花费" + cost + "元,当前可用余额" + BALANCE_AVAILABLE.intValue());
                tradeRecord.setBalance(BALANCE.setScale(2, RoundingMode.HALF_UP));
                tradeRecord.setAvailableBalance(BALANCE_AVAILABLE.setScale(2, RoundingMode.HALF_UP));
                tradeRecord.setPrice(dailyRecord.getOpenPrice());
                tradeRecord.setNumber(number * 100);
                tradeRecord.setType(0);
                tradeRecordMapper.insert(tradeRecord);
            }
        }
    }
}
Also used : QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) DailyRecord(com.mistra.plank.pojo.entity.DailyRecord) BigDecimal(java.math.BigDecimal) TradeRecord(com.mistra.plank.pojo.entity.TradeRecord) HoldShares(com.mistra.plank.pojo.entity.HoldShares) Stock(com.mistra.plank.pojo.entity.Stock)

Aggregations

QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)3 TradeRecord (com.mistra.plank.pojo.entity.TradeRecord)3 BigDecimal (java.math.BigDecimal)3 Clearance (com.mistra.plank.pojo.entity.Clearance)1 DailyRecord (com.mistra.plank.pojo.entity.DailyRecord)1 HoldShares (com.mistra.plank.pojo.entity.HoldShares)1 Stock (com.mistra.plank.pojo.entity.Stock)1 LocalDate (org.joda.time.LocalDate)1