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:
@@ -122,7 +122,6 @@ data class AppConfig(
|
|||||||
return if (isSimulation) vtsAccountNo else realAccountNo
|
return if (isSimulation) vtsAccountNo else realAccountNo
|
||||||
}
|
}
|
||||||
|
|
||||||
var firstSet = mutableSetOf<ConfigIndex>()
|
|
||||||
fun setValues(index :ConfigIndex , value : Double) {
|
fun setValues(index :ConfigIndex , value : Double) {
|
||||||
val oldValue = getValues(index)
|
val oldValue = getValues(index)
|
||||||
when (index) {
|
when (index) {
|
||||||
@@ -153,26 +152,24 @@ data class AppConfig(
|
|||||||
ConfigIndex.GRADE_3_ALLOCATIONRATE -> {GRADE_3_ALLOCATIONRATE = value}
|
ConfigIndex.GRADE_3_ALLOCATIONRATE -> {GRADE_3_ALLOCATIONRATE = value}
|
||||||
ConfigIndex.GRADE_2_ALLOCATIONRATE -> {GRADE_2_ALLOCATIONRATE = value}
|
ConfigIndex.GRADE_2_ALLOCATIONRATE -> {GRADE_2_ALLOCATIONRATE = value}
|
||||||
ConfigIndex.GRADE_1_ALLOCATIONRATE -> {GRADE_1_ALLOCATIONRATE = value}
|
ConfigIndex.GRADE_1_ALLOCATIONRATE -> {GRADE_1_ALLOCATIONRATE = value}
|
||||||
ConfigIndex.LOSS_MAXRATE -> { loss_max = value}
|
ConfigIndex.LOSS_MAXRATE -> { loss_max = abs(value) * -1 }
|
||||||
ConfigIndex.LOSS_MINRATE -> { loss_min = value}
|
ConfigIndex.LOSS_MINRATE -> { loss_min = abs(value) * -1 }
|
||||||
ConfigIndex.LOSS_MAX_MONEY -> { loss_money = value}
|
ConfigIndex.LOSS_MAX_MONEY -> { loss_money = abs(value) }
|
||||||
ConfigIndex.STOP_LOSS -> { stop_Loss = value > 0.1 }
|
ConfigIndex.STOP_LOSS -> { stop_Loss = value > 0.1 }
|
||||||
ConfigIndex.TAKE_PROFIT -> { take_profit = value > 0.1 }
|
ConfigIndex.TAKE_PROFIT -> { take_profit = value > 0.1 }
|
||||||
ConfigIndex.MAX_HOLDING_COUNT -> { max_holding_count = value }
|
ConfigIndex.MAX_HOLDING_COUNT -> { max_holding_count = value }
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
if (firstSet.contains(index)) {
|
val newValue = getValues(index)
|
||||||
|
if (oldValue != newValue) {
|
||||||
DatabaseFactory.saveConfig(KisSession.config)
|
DatabaseFactory.saveConfig(KisSession.config)
|
||||||
TradingLogStore.addSettingLog(
|
TradingLogStore.addSettingLog(
|
||||||
index.label,
|
index.label,
|
||||||
oldValue.toString(),
|
oldValue.toString(),
|
||||||
value.toString(),
|
newValue.toString(),
|
||||||
"💾 저장됨: ${index.label} = ${getValues(index)}"
|
"💾 저장됨: ${index.label} = $newValue"
|
||||||
)
|
)
|
||||||
TradingReportManager.recordConfigChange()
|
TradingReportManager.recordConfigChange()
|
||||||
} else {
|
|
||||||
firstSet.add(index)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fun getValues(index :ConfigIndex) : Double {
|
fun getValues(index :ConfigIndex) : Double {
|
||||||
@@ -217,7 +214,7 @@ data class AppConfig(
|
|||||||
ConfigIndex.GRADE_1_ALLOCATIONRATE -> {GRADE_1_ALLOCATIONRATE}
|
ConfigIndex.GRADE_1_ALLOCATIONRATE -> {GRADE_1_ALLOCATIONRATE}
|
||||||
ConfigIndex.LOSS_MAXRATE -> { abs(loss_max) * -1 }
|
ConfigIndex.LOSS_MAXRATE -> { abs(loss_max) * -1 }
|
||||||
ConfigIndex.LOSS_MINRATE -> { abs(loss_min) * -1 }
|
ConfigIndex.LOSS_MINRATE -> { abs(loss_min) * -1 }
|
||||||
ConfigIndex.LOSS_MAX_MONEY -> { abs(loss_money) * -1}
|
ConfigIndex.LOSS_MAX_MONEY -> { abs(loss_money) }
|
||||||
ConfigIndex.STOP_LOSS -> {if(!stop_Loss) 0.0 else 1.0}
|
ConfigIndex.STOP_LOSS -> {if(!stop_Loss) 0.0 else 1.0}
|
||||||
ConfigIndex.TAKE_PROFIT -> {if(!take_profit) 0.0 else 1.0}
|
ConfigIndex.TAKE_PROFIT -> {if(!take_profit) 0.0 else 1.0}
|
||||||
ConfigIndex.MAX_COUNT_INDEX -> {MAX_COUNT.toDouble()}
|
ConfigIndex.MAX_COUNT_INDEX -> {MAX_COUNT.toDouble()}
|
||||||
|
|||||||
@@ -305,13 +305,13 @@ object AutoTradingManager {
|
|||||||
var stockName = decision.stockName
|
var stockName = decision.stockName
|
||||||
val maxStocks = KisSession.config.getValues(ConfigIndex.MAX_HOLDING_COUNT).toInt()
|
val maxStocks = KisSession.config.getValues(ConfigIndex.MAX_HOLDING_COUNT).toInt()
|
||||||
|
|
||||||
if (!canAddNewPosition(maxStocks)) {
|
if (!hasCode && !canAddNewPosition(maxStocks)) {
|
||||||
TradingLogStore.addNotice("SYSTEM", "LIMIT", "최대 보유 종목 도달로 신규 매수 일시 중단")
|
TradingLogStore.addNotice("SYSTEM", "LIMIT", "최대 보유 종목 수($maxStocks) 도달로 신규 매수 일시 중단")
|
||||||
addToReanalysis(RankingStock(mksc_shrn_iscd = stockCode, hts_kor_isnm = stockName))
|
addToReanalysis(RankingStock(mksc_shrn_iscd = stockCode, hts_kor_isnm = stockName))
|
||||||
TradingLogStore.addWatchLog(
|
TradingLogStore.addWatchLog(
|
||||||
decision,
|
decision,
|
||||||
"WATCH",
|
"WATCH",
|
||||||
"매수 실패 : 최대 보유 종목 도달로 신규 매수 일시 중단 => 재분석 대기열에 추가"
|
"매수 실패 : 최대 보유 종목 수($maxStocks) 도달로 신규 매수 일시 중단 => 재분석 대기열에 추가"
|
||||||
)
|
)
|
||||||
} else if (KisSession.isAvailBuyTime(LocalTime.now()) || hasCode) {
|
} else if (KisSession.isAvailBuyTime(LocalTime.now()) || hasCode) {
|
||||||
|
|
||||||
@@ -680,9 +680,9 @@ object AutoTradingManager {
|
|||||||
&& holding.profitRate.toDouble() >= KisSession.config.getValues(
|
&& holding.profitRate.toDouble() >= KisSession.config.getValues(
|
||||||
ConfigIndex.LOSS_MAXRATE
|
ConfigIndex.LOSS_MAXRATE
|
||||||
)
|
)
|
||||||
&& holding.valuationProfitAmount.toDouble() >= KisSession.config.getValues(
|
&& holding.valuationProfitAmount.toDouble() >= (-abs(KisSession.config.getValues(
|
||||||
ConfigIndex.LOSS_MAX_MONEY
|
ConfigIndex.LOSS_MAX_MONEY
|
||||||
)
|
)))
|
||||||
) {
|
) {
|
||||||
println(
|
println(
|
||||||
"${holding.name} ${holding.profitRate.toDouble()} ${holding.valuationProfitAmount.toDouble()} ${
|
"${holding.name} ${holding.profitRate.toDouble()} ${holding.valuationProfitAmount.toDouble()} ${
|
||||||
@@ -897,9 +897,9 @@ object AutoTradingManager {
|
|||||||
&& holding.profitRate.toDouble() >= KisSession.config.getValues(
|
&& holding.profitRate.toDouble() >= KisSession.config.getValues(
|
||||||
ConfigIndex.LOSS_MAXRATE
|
ConfigIndex.LOSS_MAXRATE
|
||||||
)
|
)
|
||||||
&& holding.valuationProfitAmount.toDouble() >= KisSession.config.getValues(
|
&& holding.valuationProfitAmount.toDouble() >= (-abs(KisSession.config.getValues(
|
||||||
ConfigIndex.LOSS_MAX_MONEY
|
ConfigIndex.LOSS_MAX_MONEY
|
||||||
)
|
)))
|
||||||
) {
|
) {
|
||||||
println(
|
println(
|
||||||
"${holding.name} ${holding.profitRate.toDouble()} ${holding.valuationProfitAmount.toDouble()} ${
|
"${holding.name} ${holding.profitRate.toDouble()} ${holding.valuationProfitAmount.toDouble()} ${
|
||||||
@@ -1018,13 +1018,12 @@ object AutoTradingManager {
|
|||||||
private var lastRetryTime = 0L
|
private var lastRetryTime = 0L
|
||||||
val binPath = getLlamaBinPath()
|
val binPath = getLlamaBinPath()
|
||||||
|
|
||||||
fun canAddNewPosition( // 대표님의 시스템에 맞는 미체결 주문 객체 리스트
|
fun canAddNewPosition(
|
||||||
maxAllowedStocks: Int
|
maxAllowedStocks: Int
|
||||||
): Boolean {
|
): Boolean {
|
||||||
|
// 현재 잔고의 보유 종목 수(보유수량 > 0)가 최대 허용치 미만일 때만 신규 매수 가능
|
||||||
|
val currentHoldingsCount = currentBalance?.getHoldings()?.count { it.quantity.toInt() > 0 } ?: 0
|
||||||
// 현재 노출 수가 최대 허용치보다 작을 때만 true(매수 가능) 반환
|
return currentHoldingsCount < maxAllowedStocks
|
||||||
return true //(currentBalance?.getHoldings()?.count { it.availOrderCount.toInt() > 0 } ?: 0) > maxAllowedStocks
|
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun tryRefreshToken() {
|
suspend fun tryRefreshToken() {
|
||||||
|
|||||||
@@ -274,7 +274,7 @@ private fun BasicRiskTabContent(tradeConfig: TradeConfig) {
|
|||||||
label = "최대 허용 손실 금액 (원)",
|
label = "최대 허용 손실 금액 (원)",
|
||||||
initialValue = KisSession.config.getValues(ConfigIndex.LOSS_MAX_MONEY).toLong().toString(),
|
initialValue = KisSession.config.getValues(ConfigIndex.LOSS_MAX_MONEY).toLong().toString(),
|
||||||
helperText = "1종목당 최대 손실 금액",
|
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) }
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user