From 176234abc197903272fe22e15450658393303afa Mon Sep 17 00:00:00 2001 From: Roman Struchev Date: Mon, 22 Apr 2024 19:45:35 +0200 Subject: [PATCH] log if price significantly changed (cherry picked from commit 43a12588c3e084b3f2e94758f02b63a52da8afe0) --- .../service/processor/PurchaseService.java | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/struchev/invest/service/processor/PurchaseService.java b/src/main/java/com/struchev/invest/service/processor/PurchaseService.java index 6b110592..e43fc323 100644 --- a/src/main/java/com/struchev/invest/service/processor/PurchaseService.java +++ b/src/main/java/com/struchev/invest/service/processor/PurchaseService.java @@ -11,6 +11,8 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; +import java.math.BigDecimal; +import java.math.RoundingMode; import java.time.temporal.ChronoUnit; /** @@ -106,7 +108,8 @@ private void buy(CandleDomainEntity candle, AStrategy strategy) { var msg = String.format("Buy %s (%s), %s x%s, %s, %s. Wanted %s", order.getFigi(), order.getFigiTitle(), order.getPurchasePrice(), order.getLots(), order.getPurchaseDateTime(), order.getStrategy(), candle.getClosingPrice()); - log.warn(msg); + log.info(msg); + logIfAmountChanged(candle, order.getPurchasePrice()); notificationService.sendMessage(msg); } @@ -135,7 +138,26 @@ private void sell(CandleDomainEntity candle, OrderDomainEntity order, AStrategy var msg = String.format("Sell %s (%s), %s x%s (%s), %s, %s. Wanted: %s", candle.getFigi(), order.getFigiTitle(), order.getSellPrice(), order.getLots(), order.getSellProfit(), order.getSellDateTime(), order.getStrategy(), candle.getClosingPrice()); - log.warn(msg); + log.info(msg); + logIfAmountChanged(candle, order.getSellPrice()); notificationService.sendMessage(msg); } + + /** + * Логируем если фактическая и ожидаемая цена отличаются более чем на 0.1% + * + * @param candle + * @param amountFact + */ + private void logIfAmountChanged(CandleDomainEntity candle, BigDecimal amountFact) { + try { + var distance = candle.getClosingPrice().subtract(amountFact).abs(); + var percent = BigDecimal.valueOf(0.001); + if (distance.divide(amountFact, 4, RoundingMode.HALF_EVEN).compareTo(percent) > 0) { + log.warn("Price for {} significantly changed: {} -> {}", candle.getFigi(), candle.getClosingPrice(), amountFact); + } + } catch (Exception e) { + log.error("An error during logIfAmountChanged", e); + } + } }