Search in sources :

Example 6 with DailyRecord

use of com.mistra.plank.pojo.entity.DailyRecord 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)6 DailyRecord (com.mistra.plank.pojo.entity.DailyRecord)6 Stock (com.mistra.plank.pojo.entity.Stock)5 BigDecimal (java.math.BigDecimal)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 Date (java.util.Date)4 JSONArray (com.alibaba.fastjson.JSONArray)3 JSONObject (com.alibaba.fastjson.JSONObject)3 DragonList (com.mistra.plank.pojo.entity.DragonList)3 ForeignFundHoldingsTracking (com.mistra.plank.pojo.entity.ForeignFundHoldingsTracking)3 HoldShares (com.mistra.plank.pojo.entity.HoldShares)3 TradeRecord (com.mistra.plank.pojo.entity.TradeRecord)3 UploadDataListener (com.mistra.plank.util.UploadDataListener)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 DateUtil (cn.hutool.core.date.DateUtil)2 NamedThreadFactory (cn.hutool.core.thread.NamedThreadFactory)2 EasyExcel (com.alibaba.excel.EasyExcel)2