fix: 미체결/부분체결 상태 정밀 분리 및 체결 소요시간 연동
This commit is contained in:
@@ -139,30 +139,20 @@ object LocalReportGenerator {
|
|||||||
buyQueue.add(BuyLot(resolvedBuyPrice, 999999, "기초스윙"))
|
buyQueue.add(BuyLot(resolvedBuyPrice, 999999, "기초스윙"))
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. 당일 매수 체결 내역 큐에 적재
|
// 2. 당일 매수 체결 내역 큐에 적재 (실제 체결된 수량만)
|
||||||
buys.forEach { b ->
|
buys.forEach { b ->
|
||||||
val execs = b.executions
|
b.executions.filter { it.quantity > 0 }.forEach { exec ->
|
||||||
if (execs.isNotEmpty()) {
|
|
||||||
execs.forEach { exec ->
|
|
||||||
if (exec.quantity > 0) {
|
|
||||||
buyQueue.add(BuyLot(exec.price, exec.quantity, exec.execTime))
|
buyQueue.add(BuyLot(exec.price, exec.quantity, exec.execTime))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (b.currentPrice > 0) {
|
|
||||||
buyQueue.add(BuyLot(b.currentPrice, 1, b.orderTime))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 3. 당일 매도 체결 내역과 FIFO 매칭
|
// 3. 당일 매도 체결 내역과 FIFO 매칭 (실제 체결된 수량만)
|
||||||
var stockRealizedProfit = 0L
|
var stockRealizedProfit = 0L
|
||||||
var stockCostBasis = 0.0
|
var stockCostBasis = 0.0
|
||||||
var stockSellQty = 0
|
var stockSellQty = 0
|
||||||
|
|
||||||
sells.forEach { s ->
|
sells.forEach { s ->
|
||||||
val execs = s.executions
|
s.executions.filter { it.quantity > 0 }.forEach { sExec ->
|
||||||
val sellLots = if (execs.isNotEmpty()) execs else listOf(RawExecutionData(s.currentPrice, 1, s.orderTime))
|
|
||||||
|
|
||||||
sellLots.forEach { sExec ->
|
|
||||||
var qtyToMatch = sExec.quantity
|
var qtyToMatch = sExec.quantity
|
||||||
val sellPrice = sExec.price
|
val sellPrice = sExec.price
|
||||||
|
|
||||||
@@ -268,6 +258,21 @@ object LocalReportGenerator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun formatTimeTaken(orderTimeStr: String, executions: List<RawExecutionData>, status: String): String {
|
||||||
|
if (executions.isEmpty()) return "<span style='color:#f59e0b;'>미체결 (대기중)</span>"
|
||||||
|
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<RawTradeData>): String {
|
private fun processTrades(rawTrades: List<RawTradeData>): String {
|
||||||
if (rawTrades.isEmpty()) return "<tr><td colspan='9'>당일 거래 내역이 없습니다.</td></tr>"
|
if (rawTrades.isEmpty()) return "<tr><td colspan='9'>당일 거래 내역이 없습니다.</td></tr>"
|
||||||
val htmlBuilder = StringBuilder()
|
val htmlBuilder = StringBuilder()
|
||||||
@@ -292,7 +297,7 @@ object LocalReportGenerator {
|
|||||||
</span>
|
</span>
|
||||||
""".trimIndent()
|
""".trimIndent()
|
||||||
} else {
|
} else {
|
||||||
"-" // 시세 데이터가 없을 경우 표시할 텍스트 (또는 "")
|
"-"
|
||||||
}
|
}
|
||||||
|
|
||||||
if (buyTrades.isNotEmpty() && sellTrades.isNotEmpty()) {
|
if (buyTrades.isNotEmpty() && sellTrades.isNotEmpty()) {
|
||||||
@@ -302,21 +307,26 @@ object LocalReportGenerator {
|
|||||||
val grossProfit = (avgSellPrice - avgBuyPrice) * totalSellQty
|
val grossProfit = (avgSellPrice - avgBuyPrice) * totalSellQty
|
||||||
val feeEst = (avgBuyPrice * totalSellQty * 0.00015) + (avgSellPrice * totalSellQty * 0.00195)
|
val feeEst = (avgBuyPrice * totalSellQty * 0.00015) + (avgSellPrice * totalSellQty * 0.00195)
|
||||||
val profitAmount = (grossProfit - feeEst).toLong()
|
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 profitClass = if (profitAmount > 0) "plus" else if (profitAmount < 0) "minus" else ""
|
||||||
val buyTime = buyTrades.first().orderTime.substringAfter("T").substringBefore(".")
|
val buyTime = buyTrades.first().orderTime.substringAfter("T").substringBefore(".")
|
||||||
val sellTime = sellTrades.last().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 "<span style='color:#f59e0b;'>체결 대기</span>"
|
||||||
|
val rateDisplay = if (totalSellQty > 0) String.format("%.2f%%", profitRate) else "-"
|
||||||
|
|
||||||
htmlBuilder.append("""
|
htmlBuilder.append("""
|
||||||
<tr style="background-color: #f0f8ff;">
|
<tr style="background-color: #f0f8ff;">
|
||||||
<td><span style="color:#8e44ad; font-weight:bold;">당일 청산</span></td>
|
<td><span style="color:#8e44ad; font-weight:bold;">당일 청산</span></td>
|
||||||
<td>$stockName <br><span style="font-size:0.8em; color:#7f8c8d;">$investmentGrade</span></td>
|
<td>$stockName <br><span style="font-size:0.8em; color:#7f8c8d;">$investmentGrade</span></td>
|
||||||
<td>진입: $buyTime<br>청산: $sellTime</td>
|
<td>진입: $buyTime<br>청산: $sellTime</td>
|
||||||
<td>-</td>
|
<td>$timeTakenStr</td>
|
||||||
<td>${totalSellQty}주</td>
|
<td>${totalSellQty}주</td>
|
||||||
<td>매수: ${String.format("%,d", avgBuyPrice.toLong())}원<br>매도: ${String.format("%,d", avgSellPrice.toLong())}원</td>
|
<td>매수: ${String.format("%,d", avgBuyPrice.toLong())}원<br>매도: ${String.format("%,d", avgSellPrice.toLong())}원</td>
|
||||||
<td class="$profitClass">${String.format("%.2f%%", profitRate)}</td>
|
<td class="$profitClass">$rateDisplay</td>
|
||||||
<td class="$profitClass">실현: ${String.format("%,d", profitAmount)}원</td>
|
<td class="$profitClass">$profitDisplay</td>
|
||||||
<td >$ohlcHtml</td>
|
<td >$ohlcHtml</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr class="reason"><td colspan="9">💡 <strong>AI (${String.format("%.1f", aiScore)}점):</strong> $reason</td></tr>
|
<tr class="reason"><td colspan="9">💡 <strong>AI (${String.format("%.1f", aiScore)}점):</strong> $reason</td></tr>
|
||||||
@@ -331,17 +341,20 @@ object LocalReportGenerator {
|
|||||||
val profitAmount = if (buyPrice > 0) (grossProfit - feeEst).toLong() else 0L
|
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 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 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 "<span style='color:#f59e0b;'>체결 대기</span>"
|
||||||
|
val rateDisplay = if (sellQty > 0) String.format("%.2f%%", profitRate) else "-"
|
||||||
|
|
||||||
htmlBuilder.append("""
|
htmlBuilder.append("""
|
||||||
<tr>
|
<tr>
|
||||||
<td><span class='sell'>스윙 청산</span></td>
|
<td><span class='sell'>스윙 청산</span></td>
|
||||||
<td>$stockName</td>
|
<td>$stockName</td>
|
||||||
<td>${sell.orderTime.substringAfter("T").substringBefore(".")}</td>
|
<td>${sell.orderTime.substringAfter("T").substringBefore(".")}</td>
|
||||||
<td>-</td>
|
<td>$timeTakenStr</td>
|
||||||
<td>${sellQty}주</td>
|
<td>${sellQty}주</td>
|
||||||
<td>매도단가: ${String.format("%,d", avgSellPrice.toLong())}원</td>
|
<td>매도단가: ${String.format("%,d", avgSellPrice.toLong())}원</td>
|
||||||
<td class="$profitClass">${String.format("%.2f%%", profitRate)}</td>
|
<td class="$profitClass">$rateDisplay</td>
|
||||||
<td class="$profitClass">실현: ${String.format("%,d", profitAmount)}원</td>
|
<td class="$profitClass">$profitDisplay</td>
|
||||||
<td >$ohlcHtml</td>
|
<td >$ohlcHtml</td>
|
||||||
</tr>
|
</tr>
|
||||||
""")
|
""")
|
||||||
@@ -351,20 +364,23 @@ object LocalReportGenerator {
|
|||||||
val buyQty = buy.executions.sumOf { it.quantity }
|
val buyQty = buy.executions.sumOf { it.quantity }
|
||||||
val avgBuyPrice = calculateAvgPrice(listOf(buy))
|
val avgBuyPrice = calculateAvgPrice(listOf(buy))
|
||||||
val currentPrice = buy.currentPrice
|
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 profitRate = if (avgBuyPrice > 0) ((currentPrice - avgBuyPrice) / avgBuyPrice) * 100 else 0.0
|
||||||
val profitClass = if (valuationAmount > 0) "plus" else if (valuationAmount < 0) "minus" else ""
|
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 "<span style='color:#f59e0b;'>체결 대기</span>"
|
||||||
|
val rateDisplay = if (buyQty > 0) String.format("%.2f%%", profitRate) else "-"
|
||||||
|
|
||||||
htmlBuilder.append("""
|
htmlBuilder.append("""
|
||||||
<tr>
|
<tr>
|
||||||
<td><span class='buy'>신규 진입</span></td>
|
<td><span class='buy'>신규 진입</span></td>
|
||||||
<td>$stockName <br><span style="font-size:0.8em; color:#7f8c8d;">$investmentGrade</span></td>
|
<td>$stockName <br><span style="font-size:0.8em; color:#7f8c8d;">$investmentGrade</span></td>
|
||||||
<td>${buy.orderTime.substringAfter("T").substringBefore(".")}</td>
|
<td>${buy.orderTime.substringAfter("T").substringBefore(".")}</td>
|
||||||
<td>-</td>
|
<td>$timeTakenStr</td>
|
||||||
<td>${buyQty}주</td>
|
<td>${buyQty}주</td>
|
||||||
<td>매수단가: ${String.format("%,d", avgBuyPrice.toLong())}원</td>
|
<td>매수단가: ${String.format("%,d", avgBuyPrice.toLong())}원</td>
|
||||||
<td class="$profitClass">${String.format("%.2f%%", profitRate)}</td>
|
<td class="$profitClass">$rateDisplay</td>
|
||||||
<td class="$profitClass">평가: ${String.format("%,d", valuationAmount)}원</td>
|
<td class="$profitClass">$evalDisplay</td>
|
||||||
<td >$ohlcHtml</td>
|
<td >$ohlcHtml</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr class="reason"><td colspan="9">💡 <strong>AI (${String.format("%.1f", aiScore)}점):</strong> $reason</td></tr>
|
<tr class="reason"><td colspan="9">💡 <strong>AI (${String.format("%.1f", aiScore)}점):</strong> $reason</td></tr>
|
||||||
|
|||||||
Reference in New Issue
Block a user