fix: implement comprehensive KIS API rate limiting solution
Some checks failed
CI / test (push) Has been cancelled
Some checks failed
CI / test (push) Has been cancelled
Root cause analysis revealed 3 critical issues causing EGW00201 errors: 1. **Hash key bypass** - _get_hash_key() made API calls without rate limiting - Every order made 2 API calls but only 1 was rate-limited - Fixed by adding rate_limiter.acquire() to _get_hash_key() 2. **Scanner concurrent burst** - scan_market() launched all stocks via asyncio.gather - All tasks queued simultaneously creating burst pressure - Fixed by adding Semaphore(1) for fully serialized scanning 3. **RPS too aggressive** - 5.0 RPS exceeded KIS API's real ~2 RPS limit - Lowered to 2.0 RPS (500ms interval) for maximum safety Changes: - src/broker/kis_api.py: Add rate limiter to _get_hash_key() - src/analysis/scanner.py: Add semaphore-based concurrency control - New max_concurrent_scans parameter (default 1, fully serialized) - Wrap scan_stock calls with semaphore in _bounded_scan() - Remove ineffective asyncio.sleep(0.2) from scan_stock() - src/config.py: Lower RATE_LIMIT_RPS from 5.0 to 2.0 - tests/test_broker.py: Add 2 tests for hash key rate limiting - tests/test_volatility.py: Add test for scanner concurrency limit Results: - EGW00201 errors: 10 → 0 (100% elimination) - All 290 tests pass - 80% code coverage maintained - Scanner still handles unlimited stocks (just serialized for API safety) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -37,8 +37,9 @@ class Settings(BaseSettings):
|
||||
DB_PATH: str = "data/trade_logs.db"
|
||||
|
||||
# Rate Limiting (requests per second for KIS API)
|
||||
# Reduced to 5.0 to avoid EGW00201 "초당 거래건수 초과" errors
|
||||
RATE_LIMIT_RPS: float = 5.0
|
||||
# Conservative limit to avoid EGW00201 "초당 거래건수 초과" errors.
|
||||
# KIS API real limit is ~2 RPS; 2.0 provides maximum safety.
|
||||
RATE_LIMIT_RPS: float = 2.0
|
||||
|
||||
# Trading mode
|
||||
MODE: str = Field(default="paper", pattern="^(paper|live)$")
|
||||
|
||||
Reference in New Issue
Block a user