From ac89d4f930e84e928a9247f5a62f775267f7af5b Mon Sep 17 00:00:00 2001 From: bbbang105 <2018111366@dgu.ac.kr> Date: Mon, 10 Jun 2024 00:55:01 +0900 Subject: [PATCH] =?UTF-8?q?#53=20[refactor]=20:=20=EC=BA=94=EB=93=A4=20?= =?UTF-8?q?=EC=9C=A0=ED=8B=B8=EB=A1=9C=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/BackTestingCalculator.java | 8 --- .../dgu/backend/util/CandleDataCollector.java | 19 ++----- .../java/org/dgu/backend/util/CandleUtil.java | 54 +++++++++++++++++++ 3 files changed, 57 insertions(+), 24 deletions(-) create mode 100644 backend/src/main/java/org/dgu/backend/util/CandleUtil.java diff --git a/backend/src/main/java/org/dgu/backend/service/BackTestingCalculator.java b/backend/src/main/java/org/dgu/backend/service/BackTestingCalculator.java index e2134bc..0123b9e 100644 --- a/backend/src/main/java/org/dgu/backend/service/BackTestingCalculator.java +++ b/backend/src/main/java/org/dgu/backend/service/BackTestingCalculator.java @@ -34,14 +34,6 @@ public class BackTestingCalculator { private static Long lowValueStrategy; private static Long highLossValueStrategy; - // 캔들 차트에서 중복 데이터를 제거하는 메서드 - public List removeDuplicatedCandles(List candles) { - Set uniqueDates = new HashSet<>(); - return candles.stream() - .filter(candle -> uniqueDates.add(candle.getDateTime())) - .collect(Collectors.toList()); - } - // 지수 이동평균선을 계산하는 메서드 public List calculateEMA(List candles, int date) { Double k = 2.0 / (date + 1); diff --git a/backend/src/main/java/org/dgu/backend/util/CandleDataCollector.java b/backend/src/main/java/org/dgu/backend/util/CandleDataCollector.java index db953d3..d3ac4fc 100644 --- a/backend/src/main/java/org/dgu/backend/util/CandleDataCollector.java +++ b/backend/src/main/java/org/dgu/backend/util/CandleDataCollector.java @@ -15,13 +15,14 @@ @RequiredArgsConstructor public class CandleDataCollector { private final CandleInfoService candleInfoService; + private final CandleUtil candleUtil; private final int batchSize = 200; private final RateLimiter rateLimiter = RateLimiter.create(10.0); // 초당 요청 허용량 10개로 제한 private final long retryDelayMillis = 100; // 재시도 대기 시간 (0.1초) - public void collectCandleData(String koreanName, LocalDateTime startDate, LocalDateTime endDate, String candleName) { + public void collectCandleData(String koreanName, String candleName, LocalDateTime startDate, LocalDateTime endDate) { // 캔들을 분 기준으로 변환 - int candleInterval = calculateCandleInterval(candleName); + int candleInterval = candleUtil.calculateCandleInterval(candleName); // 시작 시간부터 종료 시간까지의 총 분 수 계산 long totalMinutes = Duration.between(startDate, endDate).toMinutes(); @@ -79,18 +80,4 @@ public void collectCandleData(String koreanName, LocalDateTime startDate, LocalD e.printStackTrace(); } } - - // 캔들을 분 기준으로 변환하는 메서드 - private int calculateCandleInterval(String candleType) { - switch (candleType) { - case "days": - return 1440; // 1일(24시간 * 60분) - case "weeks": - return 10080; // 1주(7일 * 24시간 * 60분) - case "months": - return 43200; // 1개월(30일 * 24시간 * 60분) - default: // minutesN 형식의 캔들 타입 처리 - return Integer.parseInt(candleType.replace("minutes", "")); - } - } } \ No newline at end of file diff --git a/backend/src/main/java/org/dgu/backend/util/CandleUtil.java b/backend/src/main/java/org/dgu/backend/util/CandleUtil.java new file mode 100644 index 0000000..12fc9c3 --- /dev/null +++ b/backend/src/main/java/org/dgu/backend/util/CandleUtil.java @@ -0,0 +1,54 @@ +package org.dgu.backend.util; + +import lombok.RequiredArgsConstructor; +import org.dgu.backend.domain.CandleInfo; +import org.springframework.stereotype.Component; + +import java.time.LocalDateTime; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + + +@Component +@RequiredArgsConstructor +public class CandleUtil { + private static final List SEVEN_DAY_CANDLES = Arrays.asList("minutes1", "minutes3", "minutes5", "minutes10", "minutes15", "minutes30"); + private static final List SIX_MONTH_CANDLES = Arrays.asList("minutes60", "minutes240"); + + // 캔들을 분 기준으로 변환하는 메서드 + public int calculateCandleInterval(String candleName) { + switch (candleName) { + case "days": + return 1440; // 1일(24시간 * 60분) + case "weeks": + return 10080; // 1주(7일 * 24시간 * 60분) + case "months": + return 43200; // 1개월(30일 * 24시간 * 60분) + default: // minutesN 형식의 캔들 타입 처리 + return Integer.parseInt(candleName.replace("minutes", "")); + } + } + + // 캔들 종류에 따라 시작 기간을 계산해 반환하는 메서드 + public LocalDateTime getStartDateByCandleName(String candleName) { + LocalDateTime now = LocalDateTime.now(); + if (SEVEN_DAY_CANDLES.contains(candleName)) { + return now.minusDays(7); + } else if (SIX_MONTH_CANDLES.contains(candleName)) { + return now.minusMonths(6); + } else { + return LocalDateTime.of(2019, 1, 1, 0, 0); + } + } + + // 캔들 차트에서 중복 데이터를 제거하는 메서드 + public List removeDuplicatedCandles(List candles) { + Set uniqueDates = new HashSet<>(); + return candles.stream() + .filter(candle -> uniqueDates.add(candle.getDateTime())) + .collect(Collectors.toList()); + } +} \ No newline at end of file