feat: 简化交易计算
This commit is contained in:
@@ -3,13 +3,11 @@ package com.lanyuanxiaoyao.leopard.core.strategy;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.lanyuanxiaoyao.leopard.core.entity.Daily;
|
||||
import com.lanyuanxiaoyao.leopard.core.entity.QDaily;
|
||||
import com.lanyuanxiaoyao.leopard.core.entity.Stock;
|
||||
import com.lanyuanxiaoyao.leopard.core.repository.DailyRepository;
|
||||
import java.time.LocalDate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -29,9 +27,9 @@ public class TradeEngine {
|
||||
this.dailyRepository = dailyRepository;
|
||||
}
|
||||
|
||||
public Asset backtest(List<Long> stocks, TradeStrategy strategy, LocalDate startDate, LocalDate endDate) {
|
||||
public Asset backtest(Stock stock, TradeStrategy strategy, LocalDate startDate, LocalDate endDate) {
|
||||
var dailies = dailyRepository.findAll(
|
||||
QDaily.daily.stock.id.in(stocks)
|
||||
QDaily.daily.stock.eq(stock)
|
||||
.and(QDaily.daily.tradeDate.before(endDate)),
|
||||
QDaily.daily.tradeDate.asc()
|
||||
);
|
||||
@@ -45,67 +43,59 @@ public class TradeEngine {
|
||||
continue;
|
||||
}
|
||||
final var currentDate = now;
|
||||
var trades = strategy.trade(
|
||||
var trade = strategy.trade(
|
||||
now,
|
||||
asset,
|
||||
dailies.stream()
|
||||
.filter(daily -> daily.getTradeDate().isBefore(currentDate))
|
||||
.collect(Collectors.groupingBy(daily -> daily.getStock().getId()))
|
||||
.toList()
|
||||
);
|
||||
for (var trade : trades) {
|
||||
dailies.stream()
|
||||
.filter(daily -> ObjectUtil.equals(daily.getStock().getId(), trade.stockId))
|
||||
.filter(daily -> ObjectUtil.equals(daily.getTradeDate(), currentDate))
|
||||
.findFirst()
|
||||
.map(Daily::getHfqClose)
|
||||
.ifPresent(close -> {
|
||||
if (trade.volume < 0) {
|
||||
asset.setCash(asset.getCash() + Math.abs(trade.volume) * close);
|
||||
} else if (trade.volume > 0) {
|
||||
asset.setCash(asset.getCash() - Math.abs(trade.volume) * close);
|
||||
}
|
||||
asset.getStocks().put(
|
||||
trade.stockId,
|
||||
asset.getStocks().getOrDefault(trade.stockId, 0) + trade.volume
|
||||
);
|
||||
});
|
||||
if (trade == 0) {
|
||||
continue;
|
||||
}
|
||||
asset.getHistories().add(new Asset.History(
|
||||
now,
|
||||
asset.getCash(),
|
||||
asset.getStocks(),
|
||||
trades.stream()
|
||||
.collect(Collectors.groupingBy(trade -> trade.stockId))
|
||||
));
|
||||
var daily = dailies.stream()
|
||||
.filter(d -> ObjectUtil.equals(d.getTradeDate(), currentDate))
|
||||
.findFirst()
|
||||
.orElseThrow();
|
||||
asset.addTrade(now, trade, daily.getHfqClose());
|
||||
}
|
||||
return asset;
|
||||
}
|
||||
|
||||
public interface TradeStrategy {
|
||||
List<Trade> trade(LocalDate now, Asset asset, Map<Long, List<Daily>> dailies);
|
||||
int trade(LocalDate now, Asset asset, List<Daily> dailies);
|
||||
}
|
||||
|
||||
@Data
|
||||
public static final class Asset {
|
||||
private double cash = 0;
|
||||
private double profit = 0.0;
|
||||
private Map<Long, Integer> stocks = new HashMap<>();
|
||||
private List<History> histories = new ArrayList<>();
|
||||
private List<Trade> trades = new ArrayList<>();
|
||||
|
||||
public record History(
|
||||
public void addTrade(LocalDate date, int volume, double price) {
|
||||
trades.add(new Trade(date, volume, price));
|
||||
}
|
||||
|
||||
public int getVolume() {
|
||||
return trades.stream()
|
||||
.mapToInt(Trade::volume)
|
||||
.sum();
|
||||
}
|
||||
|
||||
public double getCash() {
|
||||
return trades.stream()
|
||||
.mapToDouble(trade -> -1 * trade.volume() * trade.price())
|
||||
.sum();
|
||||
}
|
||||
|
||||
public double getPrice() {
|
||||
int volume = getVolume();
|
||||
return volume == 0 ? 0 : getCash() / volume;
|
||||
}
|
||||
|
||||
public record Trade(
|
||||
LocalDate date,
|
||||
double cash,
|
||||
Map<Long, Integer> stocks,
|
||||
Map<Long, List<Trade>> trades
|
||||
int volume,
|
||||
double price
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
||||
public record Trade(
|
||||
LocalDate date,
|
||||
Long stockId,
|
||||
// 用正负数表达买卖
|
||||
Integer volume
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user