"""SQLite 캔들 조회.""" from __future__ import annotations from datetime import timedelta from pathlib import Path import pandas as pd from bithumb.data.candle_store import CandleStore def load_candles( db_path: Path | str, symbol: str, interval_min: int, lookback_days: int | None = None, ) -> pd.DataFrame: """DB에서 캔들 DataFrame을 로드한다. Args: db_path: SQLite 경로. symbol: 코인 심볼 (예: TRX). interval_min: 분 단위 인터벌 코드. lookback_days: 최근 N일만 사용. None이면 전체. Returns: ``datetime``, ``open``, ``high``, ``low``, ``close``, ``volume`` 컬럼. 시간 오름차순 정렬. """ store = CandleStore(db_path) try: since = None if lookback_days is not None and lookback_days > 0: _, _, db_max = store.get_range(symbol, interval_min) if db_max is not None: since = db_max - timedelta(days=lookback_days) df = store.read_dataframe(symbol, interval_min, since=since) finally: store.close() if df.empty: return df if lookback_days is not None and lookback_days > 0: cutoff = df["datetime"].max() - timedelta(days=lookback_days) df = df[df["datetime"] >= cutoff].reset_index(drop=True) return df def load_last_candles( db_path: Path | str, symbol: str, interval_min: int, last_n: int, ) -> pd.DataFrame: """최신 N봉만 로드 (차트 API용, SQL LIMIT).""" store = CandleStore(db_path) try: return store.read_dataframe(symbol, interval_min, last_n=int(last_n)) finally: store.close()