Skip to content

Commit 1be0c9b

Browse files
authored
Merge pull request #78 from SynergyX-AI-Pattern/feat/#77_add_pattern_list_recent_backtests
Feat/#77 add pattern list recent backtests
2 parents ff59544 + cea1c4e commit 1be0c9b

File tree

4 files changed

+45
-12
lines changed

4 files changed

+45
-12
lines changed

src/main/java/com/synergyx/trading/dto/pattern/PatternResponseDTO.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@ public static class PatternDTO {
1818
private Long patternId; // 패턴 아이디
1919
private String patternName; // 패턴 이름
2020
private List<Double> points; // 좌표
21+
private List<BacktestSummaryDTO> recentBacktestResults; // 최근 백테스트 결과
22+
}
23+
// 최근 백테스트 결과 (목록 조회용)
24+
@Getter
25+
@Builder
26+
@AllArgsConstructor
27+
@NoArgsConstructor
28+
public static class BacktestSummaryDTO {
29+
private String stockName; // 종목 이름
30+
private Double averageReturn; // 평균 수익률
31+
private Double winRate; // 승률
32+
private Integer matchedCount; // 패턴 매칭 횟수
33+
private LocalDate executedAt; // 백테스팅 실행 날짜
2134
}
2235
// 패턴 상세 조회
2336
@Getter

src/main/java/com/synergyx/trading/model/Pattern.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class Pattern extends BaseEntity {
2626
private User user;
2727

2828
// 적용된 종목 목록
29-
@OneToMany(mappedBy = "pattern")
29+
@OneToMany(mappedBy = "pattern", cascade = CascadeType.REMOVE, orphanRemoval = true)
3030
private List<PatternApply> patternApplies;
3131

3232
@Column(name = "pattern_name", nullable = false)

src/main/java/com/synergyx/trading/repository/BacktestRepository.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import org.springframework.stereotype.Repository;
88

99
import org.springframework.data.domain.Pageable;
10+
11+
import java.util.List;
1012
import java.util.Optional;
1113

1214
@Repository
@@ -27,4 +29,7 @@ Optional<Backtest> findTop1ByPatternIdAndStockIdAndUserIdOrderByExecutedAtDesc(
2729
Long stockId,
2830
Long userId
2931
);
32+
33+
// 특정 패턴에 대한 최근 3개 백테스트 조회 (패턴 목록 조회용)
34+
List<Backtest> findTop3ByPatternIdAndUserIdOrderByExecutedAtDescIdDesc(Long patternId, Long userId);
3035
}

src/main/java/com/synergyx/trading/service/patternService/PatternQueryServiceImpl.java

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
import java.util.List;
1515
import java.util.Optional;
16-
import java.util.stream.Collectors;
1716

1817
@Service
1918
@RequiredArgsConstructor
@@ -30,16 +29,31 @@ public class PatternQueryServiceImpl implements PatternQueryService {
3029
public List<PatternResponseDTO.PatternDTO> getPatternList(Long userId) {
3130

3231
// 유저 존재 여부 확인
33-
User user = userRepository.findById(userId)
34-
.orElseThrow(() -> new GeneralException(ErrorStatus.USER_NOT_FOUND));
32+
if (!userRepository.existsById(userId)) {
33+
throw new GeneralException(ErrorStatus.USER_NOT_FOUND);
34+
}
3535

3636
return patternRepository.findByUserId(userId).stream()
37-
.map(p -> new PatternResponseDTO.PatternDTO(
38-
p.getId(),
39-
p.getPatternName(),
40-
p.getPoints()
41-
))
42-
.collect(Collectors.toList());
37+
.map(pattern -> {
38+
// 최근 백테스트 결과 최대 3개 조회
39+
List<Backtest> backtests = backtestRepository.findTop3ByPatternIdAndUserIdOrderByExecutedAtDescIdDesc(pattern.getId(), userId);
40+
List<PatternResponseDTO.BacktestSummaryDTO> summaries = backtests.stream()
41+
.map(b -> PatternResponseDTO.BacktestSummaryDTO.builder()
42+
.stockName(b.getStock().getName())
43+
.averageReturn(b.getAverageReturn())
44+
.winRate(b.getWinRate())
45+
.matchedCount(b.getMatchedCount())
46+
.executedAt(b.getExecutedAt())
47+
.build())
48+
.toList();
49+
return PatternResponseDTO.PatternDTO.builder()
50+
.patternId(pattern.getId())
51+
.patternName(pattern.getPatternName())
52+
.points(pattern.getPoints())
53+
.recentBacktestResults(summaries)
54+
.build();
55+
})
56+
.toList();
4357
}
4458

4559
// 패턴 상세 조회
@@ -48,8 +62,9 @@ public List<PatternResponseDTO.PatternDTO> getPatternList(Long userId) {
4862
public PatternResponseDTO.PatternDetailDTO getPatternDetail(Long userId, Long patternId) {
4963

5064
// 유저 존재 여부 확인
51-
User user = userRepository.findById(userId)
52-
.orElseThrow(() -> new GeneralException(ErrorStatus.USER_NOT_FOUND));
65+
if (!userRepository.existsById(userId)) {
66+
throw new GeneralException(ErrorStatus.USER_NOT_FOUND);
67+
}
5368

5469
// 패턴 존재 여부 확인
5570
Pattern pattern = patternRepository.findById(patternId)

0 commit comments

Comments
 (0)