From b63b1fc1f19617eaed3307ae98f2914ddff730c1 Mon Sep 17 00:00:00 2001 From: lunaticbum Date: Tue, 17 Nov 2026 17:45:36 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EB=AF=B8=EC=B2=B4=EA=B2=B0/=EB=B6=80?= =?UTF-8?q?=EB=B6=84=EC=B2=B4=EA=B2=B0=20=EC=83=81=ED=83=9C=20=EC=A0=95?= =?UTF-8?q?=EB=B0=80=20=EB=B6=84=EB=A6=AC=20=EB=B0=8F=20=EC=B2=B4=EA=B2=B0?= =?UTF-8?q?=20=EC=86=8C=EC=9A=94=EC=8B=9C=EA=B0=84=20=EC=97=B0=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/report/LocalReportGenerator.kt | 70 ++++++++++++------- 1 file changed, 43 insertions(+), 27 deletions(-) diff --git a/src/main/kotlin/report/LocalReportGenerator.kt b/src/main/kotlin/report/LocalReportGenerator.kt index bfa77e7..a617121 100644 --- a/src/main/kotlin/report/LocalReportGenerator.kt +++ b/src/main/kotlin/report/LocalReportGenerator.kt @@ -139,30 +139,20 @@ object LocalReportGenerator { buyQueue.add(BuyLot(resolvedBuyPrice, 999999, "기초스윙")) } - // 2. 당일 매수 체결 내역 큐에 적재 + // 2. 당일 매수 체결 내역 큐에 적재 (실제 체결된 수량만) buys.forEach { b -> - val execs = b.executions - if (execs.isNotEmpty()) { - execs.forEach { exec -> - if (exec.quantity > 0) { - buyQueue.add(BuyLot(exec.price, exec.quantity, exec.execTime)) - } - } - } else if (b.currentPrice > 0) { - buyQueue.add(BuyLot(b.currentPrice, 1, b.orderTime)) + b.executions.filter { it.quantity > 0 }.forEach { exec -> + buyQueue.add(BuyLot(exec.price, exec.quantity, exec.execTime)) } } - // 3. 당일 매도 체결 내역과 FIFO 매칭 + // 3. 당일 매도 체결 내역과 FIFO 매칭 (실제 체결된 수량만) var stockRealizedProfit = 0L var stockCostBasis = 0.0 var stockSellQty = 0 sells.forEach { s -> - val execs = s.executions - val sellLots = if (execs.isNotEmpty()) execs else listOf(RawExecutionData(s.currentPrice, 1, s.orderTime)) - - sellLots.forEach { sExec -> + s.executions.filter { it.quantity > 0 }.forEach { sExec -> var qtyToMatch = sExec.quantity val sellPrice = sExec.price @@ -268,6 +258,21 @@ object LocalReportGenerator { } } + private fun formatTimeTaken(orderTimeStr: String, executions: List, status: String): String { + if (executions.isEmpty()) return "미체결 (대기중)" + val lastExecTimeStr = executions.maxByOrNull { it.execTime }?.execTime ?: return "체결 진행중" + return try { + val orderTime = LocalDateTime.parse(orderTimeStr) + val lastExec = LocalDateTime.parse(lastExecTimeStr) + val duration = Duration.between(orderTime, lastExec) + val sec = duration.seconds + val timeStr = if (sec >= 60) "${sec / 60}분 ${sec % 60}초" else "${duration.toMillis() / 1000.0}초" + if (status == "COMPLETED") "완료 ($timeStr)" else "진행중 ($timeStr)" + } catch (e: Exception) { + if (status == "COMPLETED") "체결 완료" else "체결 진행중" + } + } + private fun processTrades(rawTrades: List): String { if (rawTrades.isEmpty()) return "당일 거래 내역이 없습니다." val htmlBuilder = StringBuilder() @@ -292,7 +297,7 @@ object LocalReportGenerator { """.trimIndent() } else { - "-" // 시세 데이터가 없을 경우 표시할 텍스트 (또는 "") + "-" } if (buyTrades.isNotEmpty() && sellTrades.isNotEmpty()) { @@ -302,21 +307,26 @@ object LocalReportGenerator { val grossProfit = (avgSellPrice - avgBuyPrice) * totalSellQty val feeEst = (avgBuyPrice * totalSellQty * 0.00015) + (avgSellPrice * totalSellQty * 0.00195) val profitAmount = (grossProfit - feeEst).toLong() - val profitRate = if (avgBuyPrice > 0) (profitAmount.toDouble() / (avgBuyPrice * totalSellQty)) * 100 else 0.0 + val profitRate = if (avgBuyPrice > 0 && totalSellQty > 0) (profitAmount.toDouble() / (avgBuyPrice * totalSellQty)) * 100 else 0.0 val profitClass = if (profitAmount > 0) "plus" else if (profitAmount < 0) "minus" else "" val buyTime = buyTrades.first().orderTime.substringAfter("T").substringBefore(".") val sellTime = sellTrades.last().orderTime.substringAfter("T").substringBefore(".") + val allExecs = sellTrades.flatMap { it.executions } + val timeTakenStr = formatTimeTaken(sellTrades.first().orderTime, allExecs, sellTrades.last().status) + + val profitDisplay = if (totalSellQty > 0) "실현: ${String.format("%,d", profitAmount)}원" else "체결 대기" + val rateDisplay = if (totalSellQty > 0) String.format("%.2f%%", profitRate) else "-" htmlBuilder.append(""" 당일 청산 $stockName
$investmentGrade 진입: $buyTime
청산: $sellTime - - + $timeTakenStr ${totalSellQty}주 매수: ${String.format("%,d", avgBuyPrice.toLong())}원
매도: ${String.format("%,d", avgSellPrice.toLong())}원 - ${String.format("%.2f%%", profitRate)} - 실현: ${String.format("%,d", profitAmount)}원 + $rateDisplay + $profitDisplay $ohlcHtml 💡 AI (${String.format("%.1f", aiScore)}점): $reason @@ -331,17 +341,20 @@ object LocalReportGenerator { val profitAmount = if (buyPrice > 0) (grossProfit - feeEst).toLong() else 0L val profitRate = if (buyPrice > 0 && sellQty > 0) (profitAmount.toDouble() / (buyPrice * sellQty)) * 100 else 0.0 val profitClass = if (profitAmount > 0) "plus" else if (profitAmount < 0) "minus" else "" + val timeTakenStr = formatTimeTaken(sell.orderTime, sell.executions, sell.status) + val profitDisplay = if (sellQty > 0) "실현: ${String.format("%,d", profitAmount)}원" else "체결 대기" + val rateDisplay = if (sellQty > 0) String.format("%.2f%%", profitRate) else "-" htmlBuilder.append(""" 스윙 청산 $stockName ${sell.orderTime.substringAfter("T").substringBefore(".")} - - + $timeTakenStr ${sellQty}주 매도단가: ${String.format("%,d", avgSellPrice.toLong())}원 - ${String.format("%.2f%%", profitRate)} - 실현: ${String.format("%,d", profitAmount)}원 + $rateDisplay + $profitDisplay $ohlcHtml """) @@ -351,20 +364,23 @@ object LocalReportGenerator { val buyQty = buy.executions.sumOf { it.quantity } val avgBuyPrice = calculateAvgPrice(listOf(buy)) val currentPrice = buy.currentPrice - val valuationAmount = if (avgBuyPrice > 0) ((currentPrice - avgBuyPrice) * buyQty).toLong() else 0L + val valuationAmount = if (avgBuyPrice > 0 && buyQty > 0) ((currentPrice - avgBuyPrice) * buyQty).toLong() else 0L val profitRate = if (avgBuyPrice > 0) ((currentPrice - avgBuyPrice) / avgBuyPrice) * 100 else 0.0 val profitClass = if (valuationAmount > 0) "plus" else if (valuationAmount < 0) "minus" else "" + val timeTakenStr = formatTimeTaken(buy.orderTime, buy.executions, buy.status) + val evalDisplay = if (buyQty > 0) "평가: ${String.format("%,d", valuationAmount)}원" else "체결 대기" + val rateDisplay = if (buyQty > 0) String.format("%.2f%%", profitRate) else "-" htmlBuilder.append(""" 신규 진입 $stockName
$investmentGrade ${buy.orderTime.substringAfter("T").substringBefore(".")} - - + $timeTakenStr ${buyQty}주 매수단가: ${String.format("%,d", avgBuyPrice.toLong())}원 - ${String.format("%.2f%%", profitRate)} - 평가: ${String.format("%,d", valuationAmount)}원 + $rateDisplay + $evalDisplay $ohlcHtml 💡 AI (${String.format("%.1f", aiScore)}점): $reason