use of com.mistra.plank.pojo.entity.HoldShares in project plank by MistraR.
the class Barbarossa method sellStock.
/**
* 减仓或清仓股票
*
* @param date 日期
*/
private void sellStock(Date date) {
List<HoldShares> holdShares = holdSharesMapper.selectList(new QueryWrapper<>());
if (CollectionUtils.isNotEmpty(holdShares)) {
for (HoldShares holdShare : holdShares) {
if (!DateUtils.isSameDay(holdShare.getBuyTime(), date) && holdShare.getBuyTime().getTime() < date.getTime()) {
Page<DailyRecord> selectPage = dailyRecordMapper.selectPage(new Page<>(1, 25), new QueryWrapper<DailyRecord>().eq("code", holdShare.getCode()).ge("date", DateUtils.addDays(date, -plankConfig.getDeficitMovingAverage() - 9)).le("date", date).orderByDesc("date"));
// 今日数据明细
DailyRecord todayRecord = selectPage.getRecords().get(0);
List<DailyRecord> dailyRecords = selectPage.getRecords().size() >= plankConfig.getDeficitMovingAverage() ? selectPage.getRecords().subList(0, plankConfig.getDeficitMovingAverage() - 1) : selectPage.getRecords();
// 止损均线价格
OptionalDouble average = dailyRecords.stream().mapToDouble(dailyRecord -> dailyRecord.getClosePrice().doubleValue()).average();
if (average.isPresent() && (todayRecord.getLowest().doubleValue() <= average.getAsDouble())) {
// 跌破均线,清仓
this.clearanceStock(holdShare, ClearanceReasonEnum.BREAK_POSITION, date, average.getAsDouble());
continue;
}
// 盘中最低收益率
double profitLowRatio = todayRecord.getLowest().subtract(holdShare.getBuyPrice()).divide(holdShare.getBuyPrice(), 2, RoundingMode.HALF_UP).doubleValue();
if (profitLowRatio < plankConfig.getDeficitRatio().doubleValue()) {
// 跌破止损线,清仓
this.clearanceStock(holdShare, ClearanceReasonEnum.BREAK_LOSS_LINE, date, holdShare.getBuyPrice().doubleValue() * (1 + plankConfig.getDeficitRatio().doubleValue()));
continue;
}
if (holdShare.getFifteenProfit() && profitLowRatio <= plankConfig.getProfitClearanceRatio().doubleValue()) {
// 收益回撤到10个点止盈清仓
this.clearanceStock(holdShare, ClearanceReasonEnum.TAKE_PROFIT, date, holdShare.getBuyPrice().doubleValue() * 1.1);
continue;
}
// 盘中最高收益率
double profitHighRatio = todayRecord.getHighest().subtract(holdShare.getBuyPrice()).divide(holdShare.getBuyPrice(), 2, RoundingMode.HALF_UP).doubleValue();
if (profitHighRatio >= plankConfig.getProfitUpperRatio().doubleValue()) {
// 收益25% 清仓
this.clearanceStock(holdShare, ClearanceReasonEnum.PROFIT_UPPER, date, holdShare.getBuyPrice().doubleValue() * (1 + plankConfig.getProfitUpperRatio().doubleValue()));
} else if (profitHighRatio >= plankConfig.getProfitQuarterRatio().doubleValue()) {
// 收益20% 减至1/4仓
this.reduceStock(holdShare, ClearanceReasonEnum.POSITION_QUARTER, date, todayRecord, holdShare.getBuyPrice().doubleValue() * (1 + plankConfig.getProfitQuarterRatio().doubleValue()));
} else if (profitHighRatio >= plankConfig.getProfitHalfRatio().doubleValue()) {
// 收益15% 减半仓
this.reduceStock(holdShare, ClearanceReasonEnum.POSITION_HALF, date, todayRecord, holdShare.getBuyPrice().doubleValue() * (1 + plankConfig.getProfitHalfRatio().doubleValue()));
}
// 持股超过x天 并且 收益不到20% 清仓
if (Days.daysBetween(new LocalDate(holdShare.getBuyTime().getTime()), new LocalDate(date.getTime())).getDays() > plankConfig.getClearanceDay()) {
this.clearanceStock(holdShare, ClearanceReasonEnum.TEN_DAY, date, todayRecord.getOpenPrice().add(todayRecord.getClosePrice()).doubleValue() / 2);
}
}
}
}
}
use of com.mistra.plank.pojo.entity.HoldShares 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