krx-quant-core
한국 주식(KOSPI·KOSDAQ) 퀀트 공통 코어. 호가단위·가격제한폭·세션 규칙, 일자별 증권거래세 비용모델, 키움 주문 가드, DART 중대공시 분류, 킬스위치, 체결 시뮬레이션, Deflated Sharpe·purged CV 검증 통계를 한 패키지로 묶었다.
전략은 여기 없다. 전략을 가진 레포들이 같은 시장 규칙과 같은 비용 숫자를 쓰게 하는 것이 이 패키지의 존재 이유다.
| 소비자 | 매매 스타일 | 실행 | 이 패키지에서 쓰는 것 |
|---|---|---|---|
| scalp-it (비공개) | 스캘핑 | 자동 실주문 | 호가·상한가·주문 가드·호가 스윕·체결 규칙·호스트 가드 |
| daytrade-it (비공개) | 데이트레이딩 | 자동 실주문 | 비용모델·공시 리스크 게이트·DART DB·코드 정규화·호스트 가드 |
| swing-it | 스윙 | 리서치 + 수동 체결 | DSR·purged CV·부트스트랩·취약성 진단·횡단면 시뮬 |
왜 따로 뺐나
세 레포가 같은 규칙을 각자 들고 있었고, 실제로 어긋나 있었다.
normalize_code가 두 레포에 복붙돼 있었다. 한쪽 옛 구현은0155E0을001550(완전히 다른 회사)으로 바꾸던 버그를 안고 있었다.- 거래비용이 레포마다
0.0023·0.0034·0.0064·50bp로 흩어져 있었고, 거래세가 2023→2024→2025→2026 에 네 번 바뀌었는데 일자별로 적용하는 코드가 어디에도 없었다. - 체결 판정(
through/touch)이 같은 레포 안에서도 알 수 없는 값을 받으면 한 곳은 through, 다른 곳은 touch 로 반대로 읽었다. - 하한가 계산은 없었다. 대칭식으로 짜면 틀린다 — 실측 대조 결과는 아래.
설치
pip install "krx-quant-core @ git+https://github.com/younghwan91/krx-quant-core@v0.1.1"
# PyPI 릴리스 전까지는 git 태그로 고정한다.
Python ≥ 3.11. 의존성은 kiwoom-client(호가단위 표의 정본), numpy, pandas 뿐이다.
모듈
krx_quant_core/
├── market/ 종목코드·Market, 호가단위, 상/하한가, KST 세션, 거래일 달력
├── costs/ 일자별 거래세 스케줄, KoreanCostModel(Decimal), round_trip_cost(float)
├── execution/ 키움 REST 주문 스펙, OrderIntent/OrderResult, OrderGuard(순수 가드)
├── risk/ DART 중대공시 분류·RiskGate, DartDisclosureDB, KillSwitch
├── backtest/ 호가 스윕 VWAP·왕복비용, 지정가 체결 규칙, 트레이드 원장 지표, 횡단면 시뮬
├── stats/ Deflated/Probabilistic Sharpe, t-haircut, purged walk-forward, 부트스트랩, 취약성
└── runtime/ 호스트 가드(백테스트는 simnode 에서만)
from datetime import date
from krx_quant_core.market import Market, limit_up_price, limit_down_price, shift_ticks
from krx_quant_core.costs import round_trip_cost, tax_rate
limit_up_price(24_250) # 31500
limit_down_price(239_000) # 167500.0
shift_ticks(49_950, 2) # Decimal('50100') — 밴드 경계를 넘어도 유효 호가
tax_rate(date(2025, 6, 2), Market.KOSPI).total # Decimal('0.0015')
round_trip_cost(date(2026, 9, 14), Market.KOSDAQ, slippage_one_way=0.0015) # 0.0053
설계 원칙
- 이식은 수치 동일. 소비 레포가 실매매일에 갈아탈 수 있어야 한다. 포트마다 원본
코드와 무작위 입력으로 대조했다 — 주문 가드 16,000건 사유 문자열 동일, 킬스위치
9,000 스텝 동일, 실제
dart.db공시 26,551건 분류 동일, 통계·횡단면 시뮬 40 시드 NaN 포함 동일. - 판정하지 않는다. 통계 함수는 숫자를 리포트할 뿐
PASS/FAIL을 돌려주지 않는다 (테스트가 강제). 합격선은 사전등록 문서에 있어야 한다. - 모르는 건 모른다고 적는다. 시장가
trde_tp는"03"/"3"둘 중 무엇인지 실호출로 확인된 적이 없어TRDE_TP_MARKET_VERIFIED = False로 둔다. 2025 이전 거래세 단계는 2차 출처만 있다고 docstring 에 적었다. - 토큰은 공유 자원이다. 두 실매매 프로세스가 같은 앱키를 쓴다. 연결 해제 시 토큰을
폐기하면 다른 프로세스가 죽는다(
kiwoom_spec.TOKEN_SHARING_WARNING).
가격제한폭 — 실측 대조
daily_bars(2023-02 이후)에서 고가·저가가 기준가 ±29~31% 인 행에 후보 규칙을 대조했다.
| 규칙 | 규칙끼리 갈리는 행에서 적중 |
|---|---|
| 상한가 = ⌊기준가×1.3 을 그 가격대 틱⌋ | 1,211 |
| 상한가 = 기준가 + ⌊기준가×0.3 을 기준가 틱⌋ | 165 |
| 하한가 = ⌈기준가×0.7 을 그 가격대 틱⌉ (대칭식) | 25 |
| 하한가 = 기준가 − ⌊기준가×0.3 을 기준가 틱⌋ | 105 |
즉 상한가와 하한가는 대칭이 아니다.
거래세 스케줄 (매도 시)
| 시행일 | KOSPI 거래세 + 농특세 | KOSDAQ |
|---|---|---|
| 2026-01-01 | 0.05% + 0.15% = 0.20% | 0.20% |
| 2025-01-01 | 0.00% + 0.15% = 0.15% | 0.15% |
| 2024-01-01 | 0.03% + 0.15% = 0.18% | 0.18% |
| 2023-01-01 | 0.05% + 0.15% = 0.20% | 0.20% |
| 2021-01-01 | 0.08% + 0.15% = 0.23% | 0.23% |
| 2019-06-03 | 0.10% + 0.15% = 0.25% | 0.25% |
2026 개정은 시행령 개정 보도로 확인했고, 그 이전 단계는 2차 출처다. 법령 원문으로 확인되면 이 표를 갱신한다.
개발
uv sync --extra dev
uv run pytest -q # 437 tests
uv run ruff check src tests
태그 v* 를 푸시하면 publish.yml 이 PyPI 로 올린다(trusted publishing 설정 후).
로드맵
- v0.2 키움 주문 HTTP 클라이언트 통합 — 지금은 scalp-it(원시 httpx)과 daytrade-it
(kiwoom-client 기반
KiwoomBroker)이 서로 다른 주문 스택을 쓴다. 가드·스펙은 공유했고 전송 계층은 실주문 대조 뒤에 합친다. - scalp-it
RiskGuard의 킬 판정을KillSwitch로 위임(대조 테스트는 이미 있음). - 이벤트 기반 일중 백테스트 엔진(틱·호가 재생 +
fills+costs). - PyPI 릴리스.
라이선스
Apache-2.0
Metadata
Release files for krx-quant-core 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| krx_quant_core-0.1.1.tar.gz | 143.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| krx_quant_core-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 228.5 kB
Release files / krx_quant_core-0.1.1.tar.gz
| Download URL | krx_quant_core-0.1.1.tar.gz |
|---|---|
| Size | 143.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1ba6373e24b26ad372234f9d30044f92809a6efcc10e274998397d6264251634
|
|
BLAKE2b-256 checksum How to use checksums |
59ed15eee7039ed4eb918b7fbc322d19972b69b02a4ebe660828e18e9ef73890
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 14, 2026.
Transparency logRelease files / krx_quant_core-0.1.1-py3-none-any.whl
| Download URL | krx_quant_core-0.1.1-py3-none-any.whl |
|---|---|
| Size | 85.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
06e61ca500a706e61035e469eea298716c96652eb810f44069a67a6fd2aa81ea
|
|
BLAKE2b-256 checksum How to use checksums |
ee3f5bb7b23af88582599a675b6c43640db5b2fd9039b53e099d4351ea271451
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 14, 2026.
Transparency log