import csv
from datetime import date
def load_prices(path):
# 수정주가(adjclose)를 쓰면 분배금이 재투자된 가격으로 계산됩니다.
with open(path, encoding="utf-8") as f:
return {row["date"]: float(row["adjclose"]) for row in csv.DictReader(f)}
EQUITY = load_prices("SPY.csv") # SPDR S&P 500 ETF (SPY, 주식)
BOND = load_prices("AGG.csv") # iShares Core U.S. Aggregate Bond ETF (AGG, 채권)
DATES = sorted(set(EQUITY) & set(BOND))
def first_days(key):
seen, picked = set(), set()
for d in DATES:
k = key(d)
if k not in seen:
seen.add(k)
picked.add(d)
return picked
def last_days(key):
last = {}
for d in DATES:
last[key(d)] = d
return set(last.values())
def simulate(check_days, band_pp=None, target=0.6, cost=0.001, principal=100_000_000):
eq = principal * target / EQUITY[DATES[0]]
bd = principal * (1 - target) / BOND[DATES[0]]
peak, mdd, trades, fee = principal, 0.0, 0, 0.0
for d in DATES:
total = eq * EQUITY[d] + bd * BOND[d]
if d in check_days:
w = eq * EQUITY[d] / total
outside = abs(w - target) * 100 > band_pp if band_pp is not None else True
gap = abs(eq * EQUITY[d] - target * total)
if outside and gap > 0.0005 * total:
trades += 1
fee += gap * cost
total -= gap * cost # 거래비용은 거래금액에 비례해 차감
eq = target * total / EQUITY[d]
bd = (1 - target) * total / BOND[d]
total = eq * EQUITY[d] + bd * BOND[d]
peak = max(peak, total)
mdd = min(mdd, total / peak - 1)
final = eq * EQUITY[DATES[-1]] + bd * BOND[DATES[-1]]
years = (date.fromisoformat(DATES[-1]) - date.fromisoformat(DATES[0])).days / 365.25
return {"final": round(final), "cagr": round(((final / principal) ** (1 / years) - 1) * 100, 2),
"mdd": round(mdd * 100, 1), "trades": trades, "fee": round(fee)}
if __name__ == "__main__":
month_ends = last_days(lambda d: d[:7])
rules = {
"보유만": simulate(set()),
"연 1회": simulate(first_days(lambda d: d[:4])),
"분기 1회": simulate(first_days(lambda d: d[:4] + "Q" + str((int(d[5:7]) - 1) // 3))),
"밴드 ±5%p": simulate(month_ends, band_pp=5),
"밴드 ±10%p": simulate(month_ends, band_pp=10),
}
for name, r in rules.items():
print(name, r)