feat: improve API rate limiting for overseas market scanning #51

Closed
opened 2026-02-05 00:31:53 +09:00 by agentson · 0 comments
Collaborator

Problem

Scanner frequently hits KIS API rate limit (EGW00201: 초당 거래건수를 초과하였습니다) when scanning multiple overseas stocks concurrently.

Error Log

WARNING: get_overseas_price failed (500): {"rt_cd":"1","msg_cd":"EGW00201","msg1":"초당 거래건수를 초과하였습니다."}

Occurs when:

  • Scanning 6+ stocks in NASDAQ market
  • Processing watchlist stocks in trading cycle
  • Multiple concurrent API calls without delay

Current Behavior

  • MarketScanner.scan_market() uses asyncio.gather(*tasks) to scan all stocks concurrently
  • No rate limiting between overseas API calls
  • Failures logged as warnings, stocks skipped

Impact

  • Severity: MEDIUM - reduces scan effectiveness but doesn't crash
  • Markets affected: All overseas markets (US_NASDAQ, US_NYSE, etc.)
  • Consequence: Incomplete market data, missed trading opportunities

Proposed Solutions

Option 1: Add delays between API calls

async def scan_stock(self, stock_code, market):
    if not market.is_domestic:
        await asyncio.sleep(0.2)  # 200ms delay for overseas
    ...

Option 2: Implement semaphore-based rate limiting

class MarketScanner:
    def __init__(self, ...):
        self._overseas_semaphore = asyncio.Semaphore(5)  # max 5 concurrent

Option 3: Use existing rate limiter from KISBroker

  • KISBroker already has rate limiting logic
  • Extend to overseas API calls

Recommendation

Start with Option 1 (simplest), then implement Option 2 if needed for finer control.

Files to Modify

  • src/analysis/scanner.py - Add rate limiting logic
  • src/broker/overseas.py - Consider adding delays in broker layer
  • Tests - Verify rate limiting behavior
## Problem Scanner frequently hits KIS API rate limit (`EGW00201: 초당 거래건수를 초과하였습니다`) when scanning multiple overseas stocks concurrently. ## Error Log ``` WARNING: get_overseas_price failed (500): {"rt_cd":"1","msg_cd":"EGW00201","msg1":"초당 거래건수를 초과하였습니다."} ``` Occurs when: - Scanning 6+ stocks in NASDAQ market - Processing watchlist stocks in trading cycle - Multiple concurrent API calls without delay ## Current Behavior - `MarketScanner.scan_market()` uses `asyncio.gather(*tasks)` to scan all stocks concurrently - No rate limiting between overseas API calls - Failures logged as warnings, stocks skipped ## Impact - **Severity**: MEDIUM - reduces scan effectiveness but doesn't crash - **Markets affected**: All overseas markets (US_NASDAQ, US_NYSE, etc.) - **Consequence**: Incomplete market data, missed trading opportunities ## Proposed Solutions **Option 1: Add delays between API calls** ```python async def scan_stock(self, stock_code, market): if not market.is_domestic: await asyncio.sleep(0.2) # 200ms delay for overseas ... ``` **Option 2: Implement semaphore-based rate limiting** ```python class MarketScanner: def __init__(self, ...): self._overseas_semaphore = asyncio.Semaphore(5) # max 5 concurrent ``` **Option 3: Use existing rate limiter from KISBroker** - KISBroker already has rate limiting logic - Extend to overseas API calls ## Recommendation Start with **Option 1** (simplest), then implement **Option 2** if needed for finer control. ## Files to Modify - `src/analysis/scanner.py` - Add rate limiting logic - `src/broker/overseas.py` - Consider adding delays in broker layer - Tests - Verify rate limiting behavior
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: jihoson/The-Ouroboros#51