1
0

feat: 实现基本的回测图表绘制

This commit is contained in:
2025-11-03 23:28:04 +08:00
parent f4fef2bd95
commit db8a094c8f
10 changed files with 560 additions and 60 deletions

View File

@@ -0,0 +1,6 @@
package com.lanyuanxiaoyao.leopard.core.entity.dto;
import java.time.LocalDate;
public record DailyDouble(LocalDate date, Double value) {
}

View File

@@ -0,0 +1,4 @@
package com.lanyuanxiaoyao.leopard.core.entity.dto;
public record YearAndMonth(int year, int month) {
}

View File

@@ -0,0 +1,4 @@
package com.lanyuanxiaoyao.leopard.core.entity.dto;
public record YearAndWeek(int year, int week) {
}

View File

@@ -1,5 +1,7 @@
package com.lanyuanxiaoyao.leopard.core.helper;
import cn.hutool.core.util.ObjectUtil;
import com.lanyuanxiaoyao.leopard.core.entity.Daily;
import java.time.Duration;
import java.time.ZonedDateTime;
import java.util.ArrayList;
@@ -34,4 +36,30 @@ public class TaHelper {
}
return result;
}
public static Double maxFromDaily(List<Daily> dailies, Function<Daily, Double> function) {
return dailies.stream()
.map(function)
.filter(ObjectUtil::isNotNull)
.mapToDouble(Double::doubleValue)
.max()
.orElse(0);
}
public static Double minFromDaily(List<Daily> dailies, Function<Daily, Double> function) {
return dailies.stream()
.map(function)
.filter(ObjectUtil::isNotNull)
.mapToDouble(Double::doubleValue)
.min()
.orElse(0);
}
public static Double sumFromDaily(List<Daily> dailies, Function<Daily, Double> function) {
return dailies.stream()
.map(function)
.filter(ObjectUtil::isNotNull)
.mapToDouble(Double::doubleValue)
.sum();
}
}

View File

@@ -1,6 +1,5 @@
package com.lanyuanxiaoyao.leopard.core.service;
import cn.hutool.core.util.ObjectUtil;
import com.lanyuanxiaoyao.leopard.core.entity.Daily;
import com.lanyuanxiaoyao.leopard.core.entity.FinanceIndicator;
import com.lanyuanxiaoyao.leopard.core.entity.QDaily;
@@ -8,6 +7,8 @@ import com.lanyuanxiaoyao.leopard.core.entity.QFinanceIndicator;
import com.lanyuanxiaoyao.leopard.core.entity.Stock;
import com.lanyuanxiaoyao.leopard.core.entity.dto.Monthly;
import com.lanyuanxiaoyao.leopard.core.entity.dto.Weekly;
import com.lanyuanxiaoyao.leopard.core.entity.dto.YearAndMonth;
import com.lanyuanxiaoyao.leopard.core.entity.dto.YearAndWeek;
import com.lanyuanxiaoyao.leopard.core.entity.dto.Yearly;
import com.lanyuanxiaoyao.leopard.core.repository.DailyRepository;
import com.lanyuanxiaoyao.leopard.core.repository.FinanceIndicatorRepository;
@@ -19,12 +20,15 @@ import java.time.temporal.WeekFields;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import static com.lanyuanxiaoyao.leopard.core.helper.TaHelper.maxFromDaily;
import static com.lanyuanxiaoyao.leopard.core.helper.TaHelper.minFromDaily;
import static com.lanyuanxiaoyao.leopard.core.helper.TaHelper.sumFromDaily;
/**
* @author lanyuanxiaoyao
* @version 20250828
@@ -143,9 +147,9 @@ public class StockService extends SimpleServiceSupport<Stock> {
var open = dailies.getFirst().getHfqOpen();
var close = dailies.getLast().getHfqClose();
return new Monthly(
LocalDate.of(yearAndMonth.year, yearAndMonth.month, 1),
yearAndMonth.year,
yearAndMonth.month,
LocalDate.of(yearAndMonth.year(), yearAndMonth.month(), 1),
yearAndMonth.year(),
yearAndMonth.month(),
open,
maxFromDaily(dailies, Daily::getHfqHigh),
minFromDaily(dailies, Daily::getHfqLow),
@@ -193,9 +197,9 @@ public class StockService extends SimpleServiceSupport<Stock> {
var open = dailies.getFirst().getHfqOpen();
var close = dailies.getLast().getHfqClose();
return new Weekly(
LocalDate.of(yearAndWeek.year, 1, 1).with(WeekFields.ISO.weekOfYear(), yearAndWeek.week),
yearAndWeek.year,
yearAndWeek.week,
LocalDate.of(yearAndWeek.year(), 1, 1).with(WeekFields.ISO.weekOfYear(), yearAndWeek.week()),
yearAndWeek.year(),
yearAndWeek.week(),
open,
maxFromDaily(dailies, Daily::getHfqHigh),
minFromDaily(dailies, Daily::getHfqLow),
@@ -209,36 +213,4 @@ public class StockService extends SimpleServiceSupport<Stock> {
.sorted(Comparator.comparingInt(weekly -> weekly.year() * 100 + weekly.week()))
.toList();
}
private Double maxFromDaily(List<Daily> dailies, Function<Daily, Double> function) {
return dailies.stream()
.map(function)
.filter(ObjectUtil::isNotNull)
.mapToDouble(Double::doubleValue)
.max()
.orElse(0);
}
private Double minFromDaily(List<Daily> dailies, Function<Daily, Double> function) {
return dailies.stream()
.map(function)
.filter(ObjectUtil::isNotNull)
.mapToDouble(Double::doubleValue)
.min()
.orElse(0);
}
private Double sumFromDaily(List<Daily> dailies, Function<Daily, Double> function) {
return dailies.stream()
.map(function)
.filter(ObjectUtil::isNotNull)
.mapToDouble(Double::doubleValue)
.sum();
}
private record YearAndMonth(int year, int month) {
}
private record YearAndWeek(int year, int week) {
}
}