# -*- coding: utf-8 -*-
"""2강 5단계 정답"""
import csv
from pathlib import Path
FEE = 0.00055
STOP = 0.02
rows = []
with open(Path(__file__).parent.parent / "data" / "BTC_USD_daily.csv", encoding="utf-8") as f:
    for r in csv.DictReader(f):
        if r["timestamp"]:
            rows.append((r["timestamp"], float(r["close"])))
rows.sort(key=lambda x: x[0])
test = [x for x in rows if "2025-01-01" <= x[0] <= "2026-09-30"]
print(f"보유: {test[-1][1]/test[0][1]-1:+.1%}")
def stop_rule(p, fee):
    money = 1.0; holding = False; peak = last = 0; buys = 0
    for i, (d, price) in enumerate(p):
        if holding:
            money *= price / last; last = price; peak = max(peak, price)
            if price <= peak * (1 - STOP): money *= (1 - fee); holding = False
        elif i < len(p) - 1:
            money *= (1 - fee); holding = True; peak = last = price; buys += 1
    return money - 1, buys
r, n = stop_rule(test, FEE); r0, _ = stop_rule(test, 0.0)
print(f"규칙(수수료 0.11%): {r:+.1%} ({n}번 매수) / 수수료 0: {r0:+.1%}")
