Search in sources :

Example 1 with Clearance

use of com.mistra.plank.pojo.entity.Clearance 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)

Aggregations

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