Skip to content

Commit

Permalink
Merge pull request #38 from CSID-DGU/feature/#21/portfolio
Browse files Browse the repository at this point in the history
[fix] : 수익률 계산 에러 해결
  • Loading branch information
bbbang105 authored May 31, 2024
2 parents 6937625 + 7e402cf commit f28f1ec
Showing 1 changed file with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public List<BackTestingDto.BackTestingResult> run(List<CandleInfo> candles, Back
}

// 거래를 실행하는 메서드
private void executeTrade(List<CandleInfo> candles, int startIndex, int tradingUnit, List<BackTestingDto.BackTestingResult> backTestingResults) {
private void executeTrade(List<CandleInfo> candles, int startIndex, int tradingCnt, List<BackTestingDto.BackTestingResult> backTestingResults) {
// 초기 세팅
coin = 0.0;
executeBuy(candles.get(startIndex).getDateTime(), candles.get(startIndex).getTradePrice(), backTestingResults);
Expand All @@ -126,20 +126,22 @@ private void executeTrade(List<CandleInfo> candles, int startIndex, int tradingU
startDate = currentDate;
}

Long initialCapital = capital + tradingUnit * (buyingCnt);
Double curRate = calculateRate(capital, initialCapital, currentPrice, coin);
// 매수 처리
if (buyingCnt < tradingUnit && currentPrice < avgPrice * (100 - buyingPoint) / 100) {
if (buyingCnt < tradingCnt && currentPrice < avgPrice * (100 - buyingPoint) / 100) {
executeBuy(currentDate, currentPrice, backTestingResults);
buyingCnt++;
tradePrices.add(currentPrice);
avgPrice = tradePrices.stream().mapToDouble(Double::doubleValue).sum() / buyingCnt;
}
// 전체 자본 대비 수익률을 기준으로 한 익절 처리
else if (((currentPrice * coin + capital) - (tradingUnit * buyingCnt)) / (tradingUnit * buyingCnt) * 100 > sellingPoint) {
else if (curRate > sellingPoint) {
executeSell(currentDate, currentPrice, backTestingResults);
break;
}
// 전체 자본 대비 수익률을 기준으로 한 손절 처리
else if (((currentPrice * coin + capital) - (tradingUnit * buyingCnt)) / (tradingUnit * buyingCnt) * 100 < -stopLossPoint) {
else if (curRate < -stopLossPoint) {
executeStopLoss(currentDate, currentPrice, backTestingResults);
break;
}
Expand All @@ -156,22 +158,22 @@ private void executeBuy(LocalDateTime currentDate, Double currentPrice, List<Bac

// 익절 처리 메서드
private void executeSell(LocalDateTime currentDate, Double currentPrice, List<BackTestingDto.BackTestingResult> backTestingResults) {
Long orgCapital = capital + tradingUnit * buyingCnt;
Long initialCapital = capital + tradingUnit * buyingCnt;
capital += (long) (currentPrice * coin);
coin = 0.0;
Long income = capital - orgCapital;
Double rate = ((double) income / orgCapital) * 100;
Long income = capital - initialCapital;
Double rate = ((double) income / initialCapital) * 100;

backTestingResults.add(BackTestingDto.BackTestingResult.of(currentDate, "SELL", currentPrice, coin, capital, rate, income, currentDate.compareTo(startDate)));
}

// 손절 처리 메서드
private void executeStopLoss(LocalDateTime currentDate, Double currentPrice, List<BackTestingDto.BackTestingResult> backTestingResults) {
Long orgCapital = capital + tradingUnit * buyingCnt;
Long initialCapital = capital + tradingUnit * buyingCnt;
capital += (long) (currentPrice * coin);
coin = 0.0;
Long income = capital - orgCapital;
Double rate = ((double) income / orgCapital) * 100;
Long income = capital - initialCapital;
Double rate = ((double) income / initialCapital) * 100;

backTestingResults.add(BackTestingDto.BackTestingResult.of(currentDate, "STOP_LOSS", currentPrice, coin, capital, rate, income, currentDate.compareTo(startDate)));
}
Expand Down Expand Up @@ -288,4 +290,11 @@ private BackTestingDto.TradingLog createTradingLog(BackTestingDto.BackTestingRes

return BackTestingDto.TradingLog.of(backTestingResult);
}

// 수익률을 계산하는 메서드
private Double calculateRate(Long capital, Long initialCapital, Double currentPrice, Double coin) {
Double currentAssetValue = capital + (currentPrice * coin);

return ((currentAssetValue - initialCapital) / initialCapital) * 100;
}
}

0 comments on commit f28f1ec

Please sign in to comment.