...
This commit is contained in:
parent
794b3fb5cb
commit
d512919d41
@ -78,10 +78,10 @@ object KisTradeService {
|
||||
/**
|
||||
* [1] 통합 잔고 조회 (국내 + 해외 합산)
|
||||
*/
|
||||
suspend fun fetchIntegratedBalance(): Result<UnifiedBalance> = coroutineScope {
|
||||
suspend fun fetchIntegratedBalance(marketCode : String = "N"): Result<UnifiedBalance> = coroutineScope {
|
||||
val config = KisSession.config
|
||||
// 국내와 해외 잔고를 비동기로 동시 호출
|
||||
val domesticJob = async { fetchDomesticRawBalance() }
|
||||
val domesticJob = async { fetchDomesticRawBalance(marketCode) }
|
||||
val overseasJob = async { fetchOverseasRawBalance() }
|
||||
try {
|
||||
val domRes = domesticJob.await().getOrNull()
|
||||
@ -322,7 +322,8 @@ object KisTradeService {
|
||||
qty: String,
|
||||
price: String,
|
||||
isBuy: Boolean,
|
||||
orderDivision: String = ""
|
||||
orderDivision: String = "",
|
||||
marketCode : String = "KRX"
|
||||
): Result<String> {
|
||||
val config = KisSession.config
|
||||
val isDomestic = stockCode.length == 6 && stockCode.all { it.isDigit() }
|
||||
@ -341,8 +342,7 @@ object KisTradeService {
|
||||
else -> if (isBuy) "TTTS3002U" else "TTTS3001U"
|
||||
}
|
||||
val finalOrderDivision = when {
|
||||
orderDivision.isNotEmpty() -> orderDivision // 외부에서 넘겨준 코드 우선 (02, 03, 34 등)
|
||||
price == "0" || price.isEmpty() -> "01" // 시장가
|
||||
marketCode.equals("SOR") || price == "0" || price.isEmpty() -> "01" // 시장가
|
||||
else -> "00" // 지정가
|
||||
}
|
||||
|
||||
@ -357,6 +357,7 @@ object KisTradeService {
|
||||
header("Content-Type", "application/json")
|
||||
|
||||
setBody(mapOf(
|
||||
"EXCG_ID_DVSN_CD" to marketCode,
|
||||
"CANO" to cano,
|
||||
"ACNT_PRDT_CD" to acntPrdtCd,
|
||||
"PDNO" to stockCode,
|
||||
@ -539,7 +540,7 @@ object KisTradeService {
|
||||
}
|
||||
|
||||
// --- 내부 Raw 호출용 (통합 잔고에서 사용) ---
|
||||
private suspend fun fetchDomesticRawBalance(): Result<StockBalanceResponse> {
|
||||
private suspend fun fetchDomesticRawBalance(markgetCode : String = "N"): Result<StockBalanceResponse> {
|
||||
val config = KisSession.config
|
||||
val baseUrl = prodUrl
|
||||
val trId = "TTTC8434R"
|
||||
@ -571,7 +572,7 @@ object KisTradeService {
|
||||
|
||||
parameter("CANO", cano)
|
||||
parameter("ACNT_PRDT_CD", acntPrdtCd)
|
||||
parameter("AFHR_FLPR_YN", "N")
|
||||
parameter("AFHR_FLPR_YN", markgetCode)
|
||||
parameter("OFL_YN", "N")
|
||||
parameter("INQR_DVSN", "0")
|
||||
parameter("UNPR_DVSN", "01")
|
||||
|
||||
@ -365,7 +365,7 @@ object AutoTradingManager {
|
||||
runDiscoveryLoop(globalCallback)
|
||||
}
|
||||
|
||||
suspend fun sellingAfterMarketOnePrice(tradeService: KisTradeService,balance : UnifiedBalance) {
|
||||
suspend fun sellingAfterMarketOnePrice(tradeService: KisTradeService,balance : UnifiedBalance,marketCode : String = "Y") {
|
||||
println("sellingAfterMarketOnePrice")
|
||||
balance.holdings.forEach { holding ->
|
||||
if (BLACKLISTEDSTOCKCODES.contains(holding.code)){
|
||||
@ -377,7 +377,7 @@ object AutoTradingManager {
|
||||
)
|
||||
} else {
|
||||
println("${holding.name} - 매수 : ${holding.avgPrice} - 현재 : ${holding.currentPrice} , 주문 가능 : ${holding.availOrderCount}, 수익율 : ${holding.profitRate}")
|
||||
if (holding != null && holding.quantity.toInt() > 0 && holding.availOrderCount.toInt() > 0 && holding.profitRate.toDouble() > 0.5) {
|
||||
if (holding != null && holding.quantity.toInt() > 0 && holding.availOrderCount.toInt() > 0 && holding.profitRate.toDouble() > KisSession.config.SELL_PROFIT) {
|
||||
println("${holding.name} - 매수 : ${holding.avgPrice} - 현재 : ${holding.currentPrice} ")
|
||||
var targetPrice = holding.currentPrice.toDouble()
|
||||
targetPrice = MarketUtil.roundToTickSize(targetPrice)
|
||||
@ -386,7 +386,8 @@ object AutoTradingManager {
|
||||
qty = holding.availOrderCount,
|
||||
price = targetPrice.toInt().toString(),
|
||||
isBuy = false,
|
||||
"34"
|
||||
orderDivision = if (marketCode.equals("Y")) "07" else "",
|
||||
marketCode = if (marketCode.equals("Y")) "KRX" else "SOR"
|
||||
).onSuccess { newOrderNo ->
|
||||
println("✅ [재주문 완료] ${holding.name}: $newOrderNo")
|
||||
TradingLogStore.addSellLog(
|
||||
@ -612,13 +613,20 @@ object AutoTradingManager {
|
||||
}
|
||||
|
||||
suspend fun checkBalance(isMorning: Boolean = true) : UnifiedBalance? {
|
||||
val balance = KisTradeService.fetchIntegratedBalance().getOrNull()
|
||||
var balance : UnifiedBalance? = null
|
||||
if (isMorning) {
|
||||
balance = KisTradeService.fetchIntegratedBalance().getOrNull()
|
||||
if (AUTOSELL) balance?.let { resumePendingSellOrders(KisTradeService, it) }
|
||||
return balance
|
||||
} else {
|
||||
balance?.let { sellingAfterMarketOnePrice(KisTradeService, it) }
|
||||
// listOf<String>("Y","X").forEach { code ->
|
||||
// KisTradeService.fetchIntegratedBalance(code).getOrNull()?.let {
|
||||
// sellingAfterMarketOnePrice(KisTradeService, it, code)
|
||||
// }
|
||||
// delay(1000)
|
||||
// }
|
||||
}
|
||||
return balance
|
||||
return null
|
||||
}
|
||||
|
||||
suspend fun executeMarketLoop() {
|
||||
@ -702,7 +710,7 @@ object AutoTradingManager {
|
||||
lastForceCheckMinute = currentMinute // 실행 완료 기록
|
||||
}
|
||||
}
|
||||
else if((now.hour == 16 || now.hour == 17) && (currentMinute == 5 || currentMinute == 55)) {
|
||||
else if((now.hour == 16 || now.hour == 17) && (currentMinute == 31 || currentMinute == 36)) {
|
||||
if (lastForceCheckMinute != currentMinute) {
|
||||
TradingLogStore.addAnalyzer(
|
||||
" - ",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user