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);
}
}
}
}
Aggregations