Skip to content

Commit

Permalink
#53 [refactor] : 캔들 유틸로 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
bbbang105 committed Jun 9, 2024
1 parent 84971d4 commit ac89d4f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,6 @@ public class BackTestingCalculator {
private static Long lowValueStrategy;
private static Long highLossValueStrategy;

// 캔들 차트에서 중복 데이터를 제거하는 메서드
public List<CandleInfo> removeDuplicatedCandles(List<CandleInfo> candles) {
Set<LocalDateTime> uniqueDates = new HashSet<>();
return candles.stream()
.filter(candle -> uniqueDates.add(candle.getDateTime()))
.collect(Collectors.toList());
}

// 지수 이동평균선을 계산하는 메서드
public List<BackTestingDto.EMAInfo> calculateEMA(List<CandleInfo> candles, int date) {
Double k = 2.0 / (date + 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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", ""));
}
}
}
54 changes: 54 additions & 0 deletions backend/src/main/java/org/dgu/backend/util/CandleUtil.java
Original file line number Diff line number Diff line change
@@ -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<String> SEVEN_DAY_CANDLES = Arrays.asList("minutes1", "minutes3", "minutes5", "minutes10", "minutes15", "minutes30");
private static final List<String> 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<CandleInfo> removeDuplicatedCandles(List<CandleInfo> candles) {
Set<LocalDateTime> uniqueDates = new HashSet<>();
return candles.stream()
.filter(candle -> uniqueDates.add(candle.getDateTime()))
.collect(Collectors.toList());
}
}

0 comments on commit ac89d4f

Please sign in to comment.