From 2cbf988a8aa347586d1b24c71f5550fcaccb83dd Mon Sep 17 00:00:00 2001 From: lunaticbum Date: Tue, 17 Nov 2026 16:22:05 +0900 Subject: [PATCH] feat(trading): add split vs single order toggles for buy and sell execution MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add useSplitBuy and useSplitSell boolean toggles to TradeConfig - Implement conditional 2-stage split order vs single all-in order for buys and sells in AutoTradingManager - Safely route 100% single orders when toggles are OFF or when holding/order quantity is 1 - Add '2중 분할 매매 주문 전략' card to TradeConfigPanel under Basic & Risk tab - Verified with compileKotlin and full project build --- src/main/kotlin/model/AppConfig.kt | 2 + src/main/kotlin/service/AutoTradingManager.kt | 49 ++++++----- src/main/kotlin/ui/panel/TradeConfigPanel.kt | 82 +++++++++++++++++++ 3 files changed, 114 insertions(+), 19 deletions(-) diff --git a/src/main/kotlin/model/AppConfig.kt b/src/main/kotlin/model/AppConfig.kt index 7ea4b15..cdf4937 100644 --- a/src/main/kotlin/model/AppConfig.kt +++ b/src/main/kotlin/model/AppConfig.kt @@ -268,6 +268,8 @@ class TradeConfig { var isUpcomingDividend : Boolean = false var buyTickOffset : Int = 4 var sellTickOffset : Int = 4 + var useSplitBuy : Boolean = true // 2중 분할 매수 사용 (false면 기준가 전량 일괄 매수) + var useSplitSell : Boolean = true // 2중 분할 매도 사용 (false면 현재가 전량 일괄 매도) var useGrade5: Boolean = true var useGrade4: Boolean = true var useGrade3: Boolean = true diff --git a/src/main/kotlin/service/AutoTradingManager.kt b/src/main/kotlin/service/AutoTradingManager.kt index be106c5..3f56aed 100644 --- a/src/main/kotlin/service/AutoTradingManager.kt +++ b/src/main/kotlin/service/AutoTradingManager.kt @@ -371,14 +371,19 @@ object AutoTradingManager { val unifiedTargetPrice = MarketUtil.roundToTickSize(targetBasePrice * (1 + effectiveProfitRate / 100.0)) val unifiedStopPrice = MarketUtil.roundToTickSize(targetBasePrice * (1 + sRate / 100.0)) - // 🌟 2. 분할 매수 수량 및 가격 설정 - val qty1 = (totalQty + 1) / 2 - val qty2 = totalQty - qty1 + // 🌟 2. 매수 수량 및 가격 설정 (2중 분할 매수 vs 단일 일괄 매수) + val isSplitBuy = KisSession.tradeConfig.useSplitBuy && totalQty >= 2 + val qty1 = if (isSplitBuy) (totalQty + 1) / 2 else totalQty + val qty2 = if (isSplitBuy) totalQty - qty1 else 0 val price1 = finalPrice - val price2 = MarketUtil.roundToTickSize(price1 - (tickSize * KisSession.tradeConfig.buyTickOffset)) + val price2 = if (isSplitBuy) MarketUtil.roundToTickSize(price1 - (tickSize * KisSession.tradeConfig.buyTickOffset)) else 0.0 - println("🔄 [분할 매수 진입] $stockName 1차(${qty1}주 / $price1) | 2차(${qty2}주 / $price2) -> 🎯통일된 목표가: $unifiedTargetPrice") + if (isSplitBuy) { + println("🔄 [2중 분할 매수 진입] $stockName 1차(${qty1}주 / $price1) | 2차(${qty2}주 / $price2) -> 🎯통일된 목표가: $unifiedTargetPrice") + } else { + println("🎯 [단일 일괄 매수 진입] $stockName 전량(${qty1}주 / $price1) -> 🎯목표가: $unifiedTargetPrice") + } val orderList = mutableListOf>() if (qty1 > 0) orderList.add(qty1 to price1) @@ -603,17 +608,19 @@ object AutoTradingManager { val totalQty = holding.availOrderCount.toInt() val currentPrice = holding.currentPrice.toDouble() - // 🌟 [수정] 1주 남았을 때는 현재가(안전)로, 그 이상은 절반씩 분할 - val qty1 = (totalQty + 1) / 2 // 절반 (홀수면 현재가 비중을 높임) - val qty2 = totalQty - qty1 // 나머지 (4호가 위) + // 🌟 2중 분할 매도 vs 단일 일괄 매도 설정 반영 + val isSplitSell = KisSession.tradeConfig.useSplitSell && totalQty >= 2 + val qty1 = if (isSplitSell) (totalQty + 1) / 2 else totalQty + val qty2 = if (isSplitSell) totalQty - qty1 else 0 val price1 = currentPrice - val price2 = MarketUtil.roundToTickSize(currentPrice + (MarketUtil.getTickSize(currentPrice) * KisSession.tradeConfig.sellTickOffset)) + val price2 = if (isSplitSell) MarketUtil.roundToTickSize(currentPrice + (MarketUtil.getTickSize(currentPrice) * KisSession.tradeConfig.sellTickOffset)) else 0.0 + val marketName = if ("Y" == marketCode) "시간외 단일가" else "대체거래소" TradingLogStore.addAfterMarketLog( holding.name, holding.code, - "${if ("Y" == marketCode) "시간외 단일가" else "대체거래소"} 시세로 ${holding.profitRate} 수익 예상 (분할매도 가동)" + "$marketName 시세로 ${holding.profitRate} 수익 예상 (${if (isSplitSell) "2중 분할매도" else "단일 일괄매도"} 가동)" ) // 1차 매도: 현재가 @@ -631,7 +638,7 @@ object AutoTradingManager { "${holding.name}[${holding.code}]", price1.toString(), "SELL", - "🎊 ${if (marketCode == "Y") "시간외 단일가" else "대체거래소"} 1차 분할 매도(현재가) ${qty1}주 매도 완료" + "🎊 $marketName ${if (isSplitSell) "1차 분할 매도(현재가)" else "단일 일괄 매도"} ${qty1}주 매도 완료" ) DatabaseFactory.saveAutoTrade( AutoTradeItem( @@ -796,15 +803,19 @@ object AutoTradingManager { val totalQty = holding.availOrderCount.toInt() val currentPrice = holding.currentPrice.toDouble() - // 🌟 [수정] 1주 남았을 때는 현재가(안전)로, 그 이상은 절반씩 분할 - val qty1 = (totalQty + 1) / 2 - val qty2 = totalQty - qty1 + // 🌟 2중 분할 매도 vs 단일 일괄 매도 설정 반영 + val isSplitSell = KisSession.tradeConfig.useSplitSell && totalQty >= 2 + val qty1 = if (isSplitSell) (totalQty + 1) / 2 else totalQty + val qty2 = if (isSplitSell) totalQty - qty1 else 0 val price1 = currentPrice - val price2 = MarketUtil.roundToTickSize(currentPrice + (MarketUtil.getTickSize(currentPrice) * KisSession.tradeConfig.sellTickOffset)) + val price2 = if (isSplitSell) MarketUtil.roundToTickSize(currentPrice + (MarketUtil.getTickSize(currentPrice) * KisSession.tradeConfig.sellTickOffset)) else 0.0 - - println("🔄 [보유 주식 주문] ${holding.name} (${holding.code}) 분할 매도: 현재가($price1) ${qty1}주, ${KisSession.tradeConfig.sellTickOffset}호가 위($price2) ${qty2}주") + if (isSplitSell) { + println("🔄 [보유 주식 주문] ${holding.name} (${holding.code}) 2중 분할 매도: 1차 현재가($price1) ${qty1}주, 2차 ${KisSession.tradeConfig.sellTickOffset}호가 위($price2) ${qty2}주") + } else { + println("🎯 [보유 주식 주문] ${holding.name} (${holding.code}) 단일 일괄 매도: 현재가($price1) 전량 ${qty1}주") + } // 1차 매도: 현재가 if (qty1 > 0) { @@ -814,12 +825,12 @@ object AutoTradingManager { price = price1.toInt().toString(), isBuy = false, ).onSuccess { newOrderNo -> - println("✅ [1차 분할주문 완료] ${holding.name}: $newOrderNo") + println("✅ [1차 주문 완료] ${holding.name}: $newOrderNo") TradingLogStore.addSellLog( holding.code, price1.toString(), "SELL", - "🎊 보유 주식[수익률: ${holding.profitRate}] 1차 분할매도(현재가) ${qty1}주 완료" + "🎊 보유 주식[수익률: ${holding.profitRate}] ${if (isSplitSell) "1차 분할매도(현재가)" else "단일 일괄매도"} ${qty1}주 완료" ) DatabaseFactory.saveAutoTrade( AutoTradeItem( diff --git a/src/main/kotlin/ui/panel/TradeConfigPanel.kt b/src/main/kotlin/ui/panel/TradeConfigPanel.kt index a0abef2..a7f9bdb 100644 --- a/src/main/kotlin/ui/panel/TradeConfigPanel.kt +++ b/src/main/kotlin/ui/panel/TradeConfigPanel.kt @@ -281,6 +281,88 @@ private fun BasicRiskTabContent(tradeConfig: TradeConfig) { ) } } + + // [카드 4] 2중 분할 매매 주문 전략 (스플릿/단일 일괄 선택) + Card( + modifier = Modifier.fillMaxWidth(), + elevation = 1.dp, + shape = RoundedCornerShape(8.dp), + border = BorderStroke(1.dp, Color(0xFFE2E8F0)) + ) { + Column(modifier = Modifier.padding(12.dp)) { + Row( + modifier = Modifier.fillMaxWidth().padding(bottom = 6.dp), + horizontalArrangement = Arrangement.SpaceBetween, + verticalAlignment = Alignment.CenterVertically + ) { + Text( + "🔄 2중 분할 매매 주문 전략", + fontSize = 12.sp, + fontWeight = FontWeight.Bold, + color = Color(0xFF1E293B) + ) + Text( + "스플릿 분할 / 단일 일괄 주문 제어", + fontSize = 10.sp, + color = Color(0xFF64748B) + ) + } + + Row( + horizontalArrangement = Arrangement.spacedBy(8.dp), + modifier = Modifier.fillMaxWidth() + ) { + SettingSwitchField( + modifier = Modifier.weight(1f), + label = "2중 분할 매수", + initialChecked = tradeConfig.useSplitBuy, + helperText = "ON: 1차 기준가 / 2차 N호가 아래 | OFF: 기준가 전량 매수", + onCheckedChange = { + tradeConfig.useSplitBuy = it + KisSession.saveTradeConfig() + } + ) + SettingInputField( + modifier = Modifier.weight(1f), + label = "2차 매수 호가 간격 (호가)", + initialValue = tradeConfig.buyTickOffset.toString(), + helperText = "1차 매수가 대비 아래 호가 간격", + onSave = { + tradeConfig.buyTickOffset = it.toIntOrNull() ?: 4 + KisSession.saveTradeConfig() + } + ) + } + + Spacer(modifier = Modifier.height(6.dp)) + + Row( + horizontalArrangement = Arrangement.spacedBy(8.dp), + modifier = Modifier.fillMaxWidth() + ) { + SettingSwitchField( + modifier = Modifier.weight(1f), + label = "2중 분할 매도", + initialChecked = tradeConfig.useSplitSell, + helperText = "ON: 1차 현재가 / 2차 N호가 위 | OFF: 현재가 전량 매도", + onCheckedChange = { + tradeConfig.useSplitSell = it + KisSession.saveTradeConfig() + } + ) + SettingInputField( + modifier = Modifier.weight(1f), + label = "2차 매도 호가 간격 (호가)", + initialValue = tradeConfig.sellTickOffset.toString(), + helperText = "1차 매도가 대비 위 호가 간격", + onSave = { + tradeConfig.sellTickOffset = it.toIntOrNull() ?: 4 + KisSession.saveTradeConfig() + } + ) + } + } + } } }