Files
kis_bot/kis_trader/engine/candle_rollup.py
Your Name fc27e726f9 feat: 새로운 안전 규칙 및 최적화 적용을 통한 트레이딩 시스템 개선
변경 사항 (Changes):

구문 오류(Syntax error) 및 토큰 낭비를 방지하기 위해 에이전트 쉘(Agent shell)과 파이썬 코드 스니펫에 다수의 신규 안전 규칙(Safety rules)을 추가함.

스키마 검증 및 적절한 SQL 포맷팅을 보장하기 위해 임시(Ad-hoc) 데이터베이스 쿼리 작성 가이드라인을 도입함.

코드 수정 후 UI 기능이 정상 작동하는지 확인하기 위해, 백테스트 웹 서비스 재시작 및 브라우저 검증에 대한 새로운 규칙을 구현함.

시스템 전반의 무결성(Integrity)을 유지하기 위해 실전 매매(Live trading), 웹 백테스팅, 파라미터 탐색(Parameter searches) 간의 일관성 검사(Consistency checks) 체계를 확립함.

기대 효과 (Impact):

이러한 개선 사항들은 트레이딩 시스템의 견고성(Robustness)과 신뢰성을 향상시키며, 에러 발생을 최소화하고 다양한 시스템 컴포넌트 간의 원활한 상호작용을 보장함.
2026-07-17 01:09:09 +09:00

159 lines
5.3 KiB
Python

#!/usr/bin/env python3
"""
1분봉 → N분봉 재합성 (실매 갭보정 RAM · 백테/파람서치 공통).
키움/집계기 관례: candle_time(YYYYMMDDHHMM) = 봉 **시작** 시각.
예) 3분봉 1315 = 13:15·16·17 1분봉 합산.
"""
from __future__ import annotations
from datetime import datetime
from typing import Any, Dict, List, Optional, Tuple
def floor_candle_time_to_tf(candle_time: str, tf_min: int) -> str:
"""분봉 시각을 tf_min 격자 시작 시각으로 내림."""
raw = str(candle_time or "").strip()[:12]
if len(raw) < 12:
return ""
try:
dt0 = datetime.strptime(raw, "%Y%m%d%H%M")
except ValueError:
return ""
tf = max(1, int(tf_min))
total = dt0.hour * 60 + dt0.minute
floored = (total // tf) * tf
nh, nm = divmod(floored, 60)
return f"{dt0.strftime('%Y%m%d')}{nh:02d}{nm:02d}"
def add_candle_minutes(candle_time: str, minutes: int) -> str:
"""YYYYMMDDHHMM + minutes."""
raw = str(candle_time or "").strip()[:12]
if len(raw) < 12:
return ""
try:
dt0 = datetime.strptime(raw, "%Y%m%d%H%M")
except ValueError:
return ""
from datetime import timedelta
return (dt0 + timedelta(minutes=int(minutes))).strftime("%Y%m%d%H%M")
def minute_diff(a: str, b: str) -> Optional[int]:
"""b - a (분). 파싱 실패 시 None."""
try:
da = datetime.strptime(str(a)[:12], "%Y%m%d%H%M")
db = datetime.strptime(str(b)[:12], "%Y%m%d%H%M")
except ValueError:
return None
return int((db - da).total_seconds() // 60)
def rollup_1m_bars_to_tf(
bars_1m: List[Dict[str, Any]],
tf_min: int = 3,
) -> List[Dict[str, Any]]:
"""
1분봉 리스트(오래된→최신) → tf_min 분봉 OHLC 재합성.
O=구간 첫 시가, H=max, L=min, C=마지막 종가, V=합.
반환 candle_time = 구간 시작. source='rollup_1m'.
**완전 버킷만 반환**: 구간 안 1분봉이 ``tf_min`` 개 모두 있을 때만 포함.
(갭보정 직후 진행 중 버킷을 불완전 volume 으로 넣으면 실매 vol 필터가
DB·백테와 어긋남 — 2026-07-16 샘표 사례)
"""
tf = max(1, int(tf_min))
if tf == 1:
out: List[Dict[str, Any]] = []
for b in bars_1m or []:
ct = str(b.get("candle_time") or b.get("time") or "")[:12]
if len(ct) < 12:
continue
out.append({
"candle_time": ct,
"open": float(b.get("open") or 0),
"high": float(b.get("high") or 0),
"low": float(b.get("low") or 0),
"close": float(b.get("close") or 0),
"volume": int(float(b.get("volume") or 0)),
"is_confirmed": 1,
"source": str(b.get("source") or "rollup_1m"),
})
return out
buckets: Dict[str, Dict[str, Any]] = {}
child_mins: Dict[str, set] = {}
order: List[str] = []
for b in bars_1m or []:
ct = str(b.get("candle_time") or b.get("time") or "")[:12]
if len(ct) < 12:
continue
key = floor_candle_time_to_tf(ct, tf)
if not key:
continue
o = float(b.get("open") or 0)
h = float(b.get("high") or 0)
lo = float(b.get("low") or 0)
c = float(b.get("close") or 0)
v = int(float(b.get("volume") or 0))
if c <= 0 and o <= 0:
continue
child_mins.setdefault(key, set()).add(ct)
if key not in buckets:
buckets[key] = {
"candle_time": key,
"open": o if o > 0 else c,
"high": max(h, o, c, lo),
"low": min(x for x in (lo, o, c, h) if x > 0) if any(
x > 0 for x in (lo, o, c, h)
) else 0.0,
"close": c if c > 0 else o,
"volume": max(0, v),
"is_confirmed": 1,
"source": "rollup_1m",
}
order.append(key)
else:
agg = buckets[key]
if h > 0:
agg["high"] = max(float(agg["high"]), h, o, c)
pos = [x for x in (lo, o, c) if x > 0]
if pos:
agg["low"] = min(float(agg["low"]) if float(agg["low"]) > 0 else pos[0], *pos)
if c > 0:
agg["close"] = c
elif o > 0:
agg["close"] = o
agg["volume"] = int(agg["volume"]) + max(0, v)
# 자식 1M 이 tf 개 미만이면 미완성 — 실매/백테 공통으로 제외
return [buckets[k] for k in order if len(child_mins.get(k, ())) >= tf]
def merge_fill_holes(
primary: List[Dict[str, Any]],
filler: List[Dict[str, Any]],
) -> Tuple[List[Dict[str, Any]], int]:
"""
primary(DB 등)에 없는 candle_time 만 filler(합성)로 보강.
기존 봉은 덮어쓰지 않음. 반환 (병합 리스트, 보강 개수).
"""
by_t: Dict[str, Dict[str, Any]] = {}
for b in primary or []:
ct = str(b.get("candle_time") or "")[:12]
if len(ct) >= 12:
by_t[ct] = dict(b)
filled = 0
for b in filler or []:
ct = str(b.get("candle_time") or "")[:12]
if len(ct) < 12:
continue
if ct in by_t:
continue
by_t[ct] = dict(b)
filled += 1
merged = [by_t[k] for k in sorted(by_t.keys())]
return merged, filled