refactor: enhance Optuna backtesting framework, optimize orderbook filtering, and update database management utilities.

This commit is contained in:
Your Name
2026-08-12 10:19:19 +09:00
parent cb7e5037a0
commit c6bd62a25f
218 changed files with 31613 additions and 759 deletions

View File

@@ -0,0 +1,75 @@
import sys
sys.path.insert(0, "/home/hoon/kis_bot")
from database import TradeDB
db = TradeDB()
code = "004490" # 대동기어
# 실제 컬럼: id, code, name, strategy, buy_price, sell_price, qty,
# profit_rate, realized_pnl, hold_minutes, buy_date, sell_date,
# sell_reason, env_snapshot, size_class, rsi, volume_ratio,
# tail_length_pct, ma5_gap_pct, ma20_gap_pct, foreign_net_buy,
# institution_net_buy, market_hour
try:
print(f"\n{'='*60}")
print(f" 대동기어({code}) 매매 이력 분석")
print(f"{'='*60}")
# 1. 오늘 매도 이력
hist = db.conn.execute(
"SELECT strategy, buy_price, sell_price, qty, profit_rate, realized_pnl, "
"buy_date, sell_date, sell_reason, rsi, volume_ratio "
"FROM trade_history WHERE code=%s AND sell_date >= '2026-08-07 00:00:00' ORDER BY sell_date DESC",
(code,)
).fetchall()
print(f"\n[오늘(8/7) 매도 이력 - {len(hist)}건]")
for r in hist:
d = dict(r)
print(f" [{d.get('strategy')}]")
print(f" 매수: {d.get('buy_date')} @ {d.get('buy_price'):,.0f}")
print(f" 매도: {d.get('sell_date')} @ {d.get('sell_price'):,.0f}")
print(f" 수량: {d.get('qty')}주 | 수익률: {d.get('profit_rate'):.2f}%")
print(f" 실현손익: {d.get('realized_pnl'):,.0f}원 | 사유: {d.get('sell_reason')}")
print(f" 진입당시 RSI: {d.get('rsi')} | 거래량비율: {d.get('volume_ratio')}")
print()
# 2. 최근 5건 전체
hist2 = db.conn.execute(
"SELECT strategy, buy_price, sell_price, qty, profit_rate, realized_pnl, "
"buy_date, sell_date, sell_reason, rsi, volume_ratio "
"FROM trade_history WHERE code=%s ORDER BY buy_date DESC LIMIT 5",
(code,)
).fetchall()
print(f"[최근 전체 매매 이력 - {len(hist2)}건]")
for r in hist2:
d = dict(r)
print(f" [{d.get('strategy')}] 매수: {d.get('buy_date')} @ {d.get('buy_price'):,.0f}"
f"→ 매도: {d.get('sell_date')} @ {d.get('sell_price'):,.0f}"
f"| {d.get('profit_rate'):.2f}% | 사유: {d.get('sell_reason')}")
# 3. 유니버스 포착 이력
print(f"\n[오늘 유니버스 포착 이력]")
rows = db.conn.execute(
"SELECT event_time, strategy_id FROM target_candidates_history "
"WHERE code=%s AND event_time >= '2026-08-07 00:00:00' ORDER BY event_time ASC LIMIT 5",
(code,)
).fetchall()
if rows:
for r in rows:
d = dict(r)
print(f" {d.get('event_time')} → 전략: {d.get('strategy_id')}")
else:
print(" 오늘 포착 없음. 어제 유입→오늘 이월 보유 케이스")
rows2 = db.conn.execute(
"SELECT event_time, strategy_id FROM target_candidates_history "
"WHERE code=%s ORDER BY event_time DESC LIMIT 3",
(code,)
).fetchall()
for r in rows2:
d = dict(r)
print(f" [최근] {d.get('event_time')} → 전략: {d.get('strategy_id')}")
finally:
db.close()
print(f"\n{'='*60}")