fix(trading): enable max holding count check and fix stop loss config persistence

- Remove firstSet guard in AppConfig.setValues to immediately persist stop loss and risk settings to DB
- Normalize LOSS_MAX_MONEY to positive value for UI while correctly comparing in AutoTradingManager
- Activate canAddNewPosition in AutoTradingManager based on actual holding count (< maxAllowedStocks)
- Bypass max holding limit for existing holding watering (!hasCode) so averaging down continues
- Verified with compileKotlin and full project build
This commit is contained in:
2026-11-17 15:51:20 +09:00
parent 4253d19d15
commit 00ddd0bbc8
3 changed files with 24 additions and 28 deletions
+11 -14
View File
@@ -122,7 +122,6 @@ data class AppConfig(
return if (isSimulation) vtsAccountNo else realAccountNo
}
var firstSet = mutableSetOf<ConfigIndex>()
fun setValues(index :ConfigIndex , value : Double) {
val oldValue = getValues(index)
when (index) {
@@ -153,26 +152,24 @@ data class AppConfig(
ConfigIndex.GRADE_3_ALLOCATIONRATE -> {GRADE_3_ALLOCATIONRATE = value}
ConfigIndex.GRADE_2_ALLOCATIONRATE -> {GRADE_2_ALLOCATIONRATE = value}
ConfigIndex.GRADE_1_ALLOCATIONRATE -> {GRADE_1_ALLOCATIONRATE = value}
ConfigIndex.LOSS_MAXRATE -> { loss_max = value}
ConfigIndex.LOSS_MINRATE -> { loss_min = value}
ConfigIndex.LOSS_MAX_MONEY -> { loss_money = value}
ConfigIndex.STOP_LOSS -> { stop_Loss = value > 0.1}
ConfigIndex.LOSS_MAXRATE -> { loss_max = abs(value) * -1 }
ConfigIndex.LOSS_MINRATE -> { loss_min = abs(value) * -1 }
ConfigIndex.LOSS_MAX_MONEY -> { loss_money = abs(value) }
ConfigIndex.STOP_LOSS -> { stop_Loss = value > 0.1 }
ConfigIndex.TAKE_PROFIT -> { take_profit = value > 0.1 }
ConfigIndex.MAX_HOLDING_COUNT -> { max_holding_count = value }
}
if (firstSet.contains(index)) {
val newValue = getValues(index)
if (oldValue != newValue) {
DatabaseFactory.saveConfig(KisSession.config)
TradingLogStore.addSettingLog(
index.label,
oldValue.toString(),
value.toString(),
"💾 저장됨: ${index.label} = ${getValues(index)}"
newValue.toString(),
"💾 저장됨: ${index.label} = $newValue"
)
TradingReportManager.recordConfigChange()
} else {
firstSet.add(index)
}
}
fun getValues(index :ConfigIndex) : Double {
@@ -215,9 +212,9 @@ data class AppConfig(
ConfigIndex.GRADE_3_ALLOCATIONRATE -> {GRADE_3_ALLOCATIONRATE}
ConfigIndex.GRADE_2_ALLOCATIONRATE -> {GRADE_2_ALLOCATIONRATE}
ConfigIndex.GRADE_1_ALLOCATIONRATE -> {GRADE_1_ALLOCATIONRATE}
ConfigIndex.LOSS_MAXRATE -> { abs(loss_max) * -1}
ConfigIndex.LOSS_MINRATE -> { abs(loss_min) * -1}
ConfigIndex.LOSS_MAX_MONEY -> { abs(loss_money) * -1}
ConfigIndex.LOSS_MAXRATE -> { abs(loss_max) * -1 }
ConfigIndex.LOSS_MINRATE -> { abs(loss_min) * -1 }
ConfigIndex.LOSS_MAX_MONEY -> { abs(loss_money) }
ConfigIndex.STOP_LOSS -> {if(!stop_Loss) 0.0 else 1.0}
ConfigIndex.TAKE_PROFIT -> {if(!take_profit) 0.0 else 1.0}
ConfigIndex.MAX_COUNT_INDEX -> {MAX_COUNT.toDouble()}
+11 -12
View File
@@ -305,13 +305,13 @@ object AutoTradingManager {
var stockName = decision.stockName
val maxStocks = KisSession.config.getValues(ConfigIndex.MAX_HOLDING_COUNT).toInt()
if (!canAddNewPosition(maxStocks)) {
TradingLogStore.addNotice("SYSTEM", "LIMIT", "최대 보유 종목 도달로 신규 매수 일시 중단")
if (!hasCode && !canAddNewPosition(maxStocks)) {
TradingLogStore.addNotice("SYSTEM", "LIMIT", "최대 보유 종목 수($maxStocks) 도달로 신규 매수 일시 중단")
addToReanalysis(RankingStock(mksc_shrn_iscd = stockCode, hts_kor_isnm = stockName))
TradingLogStore.addWatchLog(
decision,
"WATCH",
"매수 실패 : 최대 보유 종목 도달로 신규 매수 일시 중단 => 재분석 대기열에 추가"
"매수 실패 : 최대 보유 종목 수($maxStocks) 도달로 신규 매수 일시 중단 => 재분석 대기열에 추가"
)
} else if (KisSession.isAvailBuyTime(LocalTime.now()) || hasCode) {
@@ -680,9 +680,9 @@ object AutoTradingManager {
&& holding.profitRate.toDouble() >= KisSession.config.getValues(
ConfigIndex.LOSS_MAXRATE
)
&& holding.valuationProfitAmount.toDouble() >= KisSession.config.getValues(
&& holding.valuationProfitAmount.toDouble() >= (-abs(KisSession.config.getValues(
ConfigIndex.LOSS_MAX_MONEY
)
)))
) {
println(
"${holding.name} ${holding.profitRate.toDouble()} ${holding.valuationProfitAmount.toDouble()} ${
@@ -897,9 +897,9 @@ object AutoTradingManager {
&& holding.profitRate.toDouble() >= KisSession.config.getValues(
ConfigIndex.LOSS_MAXRATE
)
&& holding.valuationProfitAmount.toDouble() >= KisSession.config.getValues(
&& holding.valuationProfitAmount.toDouble() >= (-abs(KisSession.config.getValues(
ConfigIndex.LOSS_MAX_MONEY
)
)))
) {
println(
"${holding.name} ${holding.profitRate.toDouble()} ${holding.valuationProfitAmount.toDouble()} ${
@@ -1018,13 +1018,12 @@ object AutoTradingManager {
private var lastRetryTime = 0L
val binPath = getLlamaBinPath()
fun canAddNewPosition( // 대표님의 시스템에 맞는 미체결 주문 객체 리스트
fun canAddNewPosition(
maxAllowedStocks: Int
): Boolean {
// 현재 노출 수가 최대 허용치보다 작을 때만 true(매수 가능) 반환
return true //(currentBalance?.getHoldings()?.count { it.availOrderCount.toInt() > 0 } ?: 0) > maxAllowedStocks
// 현재 잔고의 보유 종목 수(보유수량 > 0)가 최대 허용치 미만일 때만 신규 매수 가능
val currentHoldingsCount = currentBalance?.getHoldings()?.count { it.quantity.toInt() > 0 } ?: 0
return currentHoldingsCount < maxAllowedStocks
}
suspend fun tryRefreshToken() {
+1 -1
View File
@@ -274,7 +274,7 @@ private fun BasicRiskTabContent(tradeConfig: TradeConfig) {
label = "최대 허용 손실 금액 (원)",
initialValue = KisSession.config.getValues(ConfigIndex.LOSS_MAX_MONEY).toLong().toString(),
helperText = "1종목당 최대 손실 금액",
onSave = { KisSession.config.setValues(ConfigIndex.LOSS_MAX_MONEY, it.toDoubleOrNull() ?: -10000.0) }
onSave = { KisSession.config.setValues(ConfigIndex.LOSS_MAX_MONEY, it.toDoubleOrNull() ?: 10000.0) }
)
}
}