feat: 새로운 안전 규칙 및 최적화 적용을 통한 트레이딩 시스템 개선
변경 사항 (Changes): 구문 오류(Syntax error) 및 토큰 낭비를 방지하기 위해 에이전트 쉘(Agent shell)과 파이썬 코드 스니펫에 다수의 신규 안전 규칙(Safety rules)을 추가함. 스키마 검증 및 적절한 SQL 포맷팅을 보장하기 위해 임시(Ad-hoc) 데이터베이스 쿼리 작성 가이드라인을 도입함. 코드 수정 후 UI 기능이 정상 작동하는지 확인하기 위해, 백테스트 웹 서비스 재시작 및 브라우저 검증에 대한 새로운 규칙을 구현함. 시스템 전반의 무결성(Integrity)을 유지하기 위해 실전 매매(Live trading), 웹 백테스팅, 파라미터 탐색(Parameter searches) 간의 일관성 검사(Consistency checks) 체계를 확립함. 기대 효과 (Impact): 이러한 개선 사항들은 트레이딩 시스템의 견고성(Robustness)과 신뢰성을 향상시키며, 에러 발생을 최소화하고 다양한 시스템 컴포넌트 간의 원활한 상호작용을 보장함.
This commit is contained in:
@@ -9,7 +9,7 @@ from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import os
|
||||
from typing import Optional
|
||||
from typing import Any, Optional
|
||||
from urllib.parse import quote_plus
|
||||
|
||||
from kis_trader.utils.env import get_env_from_db
|
||||
@@ -18,7 +18,7 @@ logger = logging.getLogger("optuna_common")
|
||||
|
||||
# Optuna 전용 MariaDB (매매 DB kis_quant_db 와 분리)
|
||||
DEFAULT_OPTUNA_DB_NAME = "kis_optuna"
|
||||
OPTUNA_STRATEGIES = ("tail", "momentum", "breakout")
|
||||
OPTUNA_STRATEGIES = ("tail", "momentum", "breakout", "scalp")
|
||||
|
||||
|
||||
def mariadb_creds() -> dict:
|
||||
@@ -146,3 +146,66 @@ def resolve_study_name(
|
||||
|
||||
def optuna_run_lock_name(strategy: str) -> str:
|
||||
return f"{strategy}_param_search_optuna"
|
||||
|
||||
|
||||
def release_shared_tick_store(ctx: Any, *, log: Optional[logging.Logger] = None) -> None:
|
||||
"""
|
||||
Optuna ctx.shared_tick_store 해제.
|
||||
|
||||
주의: ticks_by_code 가 공유메모리 뷰인 경우, unlink 이후 접근하면
|
||||
SIGBUS/강제종료(트레이스백 없음) 난다. 최빈(mode_combo) 실측·JSON 저장이
|
||||
끝난 뒤에만 호출할 것. optimize() 직후 즉시 unlink 금지.
|
||||
"""
|
||||
lg = log or logger
|
||||
store = getattr(ctx, "shared_tick_store", None)
|
||||
if store is None:
|
||||
return
|
||||
try:
|
||||
store.unlink()
|
||||
except Exception as exc:
|
||||
lg.warning("⚠️ shared_tick_store unlink 실패: %s", exc)
|
||||
try:
|
||||
ctx.shared_tick_store = None
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
|
||||
def announce_optuna_json_path(
|
||||
out_path: str,
|
||||
*,
|
||||
strategy: str = "",
|
||||
mode: str = "",
|
||||
note: str = "",
|
||||
log: Optional[logging.Logger] = None,
|
||||
) -> str:
|
||||
"""
|
||||
결과 JSON 절대경로를 터미널·로그에 눈에 띄게 고지.
|
||||
또한 logs/optuna_<strategy>_<mode>_latest.jsonpath 에 기록 (없으면 strategy만).
|
||||
"""
|
||||
abs_path = os.path.abspath(str(out_path or "").strip())
|
||||
lg = log or logger
|
||||
tag = note.strip() or "결과 JSON"
|
||||
line = f"📁 [{tag}] {abs_path}"
|
||||
# logger + print 이중 — nohup 로그·터미널 모두에서 바로 보이게
|
||||
lg.info("%s", line)
|
||||
print(line, flush=True)
|
||||
print(f"OPTUNA_RESULT_JSON={abs_path}", flush=True)
|
||||
|
||||
try:
|
||||
root = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))
|
||||
logs_dir = os.path.join(root, "logs")
|
||||
os.makedirs(logs_dir, exist_ok=True)
|
||||
s = (strategy or "optuna").strip().lower() or "optuna"
|
||||
m = (mode or "run").strip().lower() or "run"
|
||||
for name in (
|
||||
f"optuna_{s}_{m}_latest.jsonpath",
|
||||
f"optuna_{s}_latest.jsonpath",
|
||||
"optuna_latest.jsonpath",
|
||||
):
|
||||
with open(os.path.join(logs_dir, name), "w", encoding="utf-8") as f:
|
||||
f.write(abs_path + "\n")
|
||||
except OSError as exc:
|
||||
lg.warning("⚠️ jsonpath 사이드카 기록 실패: %s", exc)
|
||||
return abs_path
|
||||
|
||||
|
||||
Reference in New Issue
Block a user