Skip to content

Commit d593c85

Browse files
committed
refactor: 수익 계산 로직을 FuturesPostion 엔티티로 옮겨서 응집력 향상
1 parent 4f30ce4 commit d593c85

2 files changed

Lines changed: 9 additions & 22 deletions

File tree

‎Tradin-Core/src/main/java/com/tradin/core/futuresPosition/domain/FuturesPosition.java‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,14 @@ public boolean isPositionShort() {
118118
return this.tradingType.isShort();
119119
}
120120

121-
public Amount calculateProfitAmount(Price currentPrice) {
122-
BigDecimal entryPriceValue = this.entryPrice.getValue();
123-
BigDecimal currentPriceValue = currentPrice.getValue();
124-
BigDecimal amountValue = this.amount.getValue();
121+
public Amount calculateProfitAmount(Price price) {
122+
BigDecimal currentPrice = price.getValue();
123+
BigDecimal entryPrice = this.entryPrice.getValue();
125124

126-
if (this.tradingType.isLong()) {
127-
return Amount.of(currentPriceValue.subtract(entryPriceValue).multiply(amountValue).divide(entryPriceValue, 2, RoundingMode.DOWN));
128-
}
125+
BigDecimal profit = this.tradingType.isLong()
126+
? currentPrice.subtract(entryPrice).multiply(amount.getValue()).divide(entryPrice, 2, RoundingMode.DOWN)
127+
: entryPrice.subtract(currentPrice).multiply(amount.getValue()).divide(entryPrice, 2, RoundingMode.DOWN);
129128

130-
return Amount.of(entryPriceValue.subtract(currentPriceValue).multiply(amountValue).divide(entryPriceValue, 2, RoundingMode.DOWN));
129+
return Amount.of(profit);
131130
}
132131
}

‎Tradin-Core/src/main/java/com/tradin/core/futuresPosition/service/FuturesPositionService.java‎

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,7 @@ public Optional<FuturesPosition> findOpenFuturesPositionByAccountAndCoinType(Lon
3838
}
3939

4040
public Amount calculateProfitAmount(FuturesPosition futuresPosition) {
41-
BigDecimal currentPrice = priceCache.getPrice(futuresPosition.getCoinType()).getValue();
42-
BigDecimal entryPrice = futuresPosition.getEntryPrice().getValue();
43-
44-
if (futuresPosition.isPositionLong()) {
45-
return Amount.of(
46-
currentPrice.subtract(entryPrice)
47-
.multiply(futuresPosition.getAmount().getValue())
48-
.divide(futuresPosition.getEntryPrice().getValue(),2, RoundingMode.DOWN));
49-
}
50-
51-
return Amount.of(
52-
entryPrice.subtract(currentPrice)
53-
.multiply(futuresPosition.getAmount().getValue())
54-
.divide(futuresPosition.getEntryPrice().getValue(), 2, RoundingMode.DOWN));
41+
Price currentPrice = priceCache.getPrice(futuresPosition.getCoinType());
42+
return futuresPosition.calculateProfitAmount(currentPrice);
5543
}
5644
}

0 commit comments

Comments
 (0)