유자차의 재테크 공부방

[자산배분] BAA - 파이썬으로 직접 백테스팅 해보기 - (2) 본문

자산배분

[자산배분] BAA - 파이썬으로 직접 백테스팅 해보기 - (2)

유자차H 2022. 9. 29. 15:40
반응형

quantstasts로 직접 구한 BAA 전략을 분석을 해보았습니다.

 

코드는 이전 글에서 사용했던 것에 뒷부분을 조금 바꾸어서 사용하였습니다.

 

import pandas as pd
import yfinance as yf # 주가 데이터 받기위한 라이브러리
import quantstats as qs # 리포트 형식으로 시각화해주는 라이브러리

# yahoo finance에서 필요 종목들의 공통 시작일부터 종가 데이터 받기
def get_yahoo_data(tickers, type="Adj Close"):
  df = yf.download(tickers)
  # df = df["Close"]
  df = df[type]
  df.dropna(inplace=True)
  return df
  
def get_rebal_date(df, rebal="month"):
    res_df = pd.DataFrame()
    df["year"] = df.index.year
    df["month"] = df.index.month
    df["day"] = df.index.day
    days_df = df.groupby(["year","month"])["day"].max()
    for i in range(len(days_df)):
        if days_df.iloc[i] > 25:
            day = "{}-{}-{}".format(days_df.index[i][0], days_df.index[i][1], days_df.iloc[i])
            res_df = pd.concat([res_df,df[df.index==day]])
    return res_df
 
tickers_canary = ["SPY","EEM","EFA","AGG"]
tickers_g4 = ["QQQ","EEM","EFA","AGG"]
tickers_g12 = ["SPY", "QQQ", "IWM","VGK","EWJ", "EEM", "VNQ","DBC","GLD", "TLT", "HYG", "LQD"]
tickers_safe = ["TIP","DBC","BIL","IEF","TLT","LQD","AGG"]
tickers_all = list(set(tickers_canary+tickers_g4+tickers_g12+tickers_safe)

data = get_yahoo_data(tickers_all) # 모든 데이터
rebal_data = get_rebal_date(data) # 월말 데이터
rebal_data.head(3)

 

 

# 공격형
baa_g4 = pd.DataFrame(columns=tickers_all)
res = [] # 총 자산 시작은 100

canary_data = rebal_data[tickers_canary]
profit = rebal_data.pct_change()
n=12
for i in range(n, rebal_data.shape[0]):
    # print(canary_data.index[i], profit.index[i])

    m1 = (canary_data.iloc[i]-canary_data.iloc[i-1])/canary_data.iloc[i-1]
    m3 = (canary_data.iloc[i]-canary_data.iloc[i-3])/canary_data.iloc[i-3]
    m6 = (canary_data.iloc[i]-canary_data.iloc[i-6])/canary_data.iloc[i-6]
    m12 = (canary_data.iloc[i]-canary_data.iloc[i-12])/canary_data.iloc[i-12]
    score = m1*12 + m3*4 + m6*2 +m12

    buy = dict()
    if min(score) <= 0:
        safe = rebal_data[tickers_safe].iloc[i] / rebal_data[tickers_safe].iloc[i-n:i].mean()
        safe_top3 = safe.nlargest(3)
        for j in range(3):
            try:
                if safe_top3[j] > safe["BIL"]:
                    name = safe_top3.index[j]
                else:
                    name = "BIL"
                buy[name] += (1/3)*100
            except:
                buy[name] = (1/3)*100
    else:
        aggresive = rebal_data[tickers_g4].iloc[i] / rebal_data[tickers_g4].iloc[i-n:i].mean()
        agg_top1 = aggresive.nlargest(1)
        buy[agg_top1.index[0]] = 100
    
    if i == n:
        one = pd.DataFrame([list(buy.values())], columns=list(buy.keys()), index = [rebal_data.index[i]])
        baa_g4 = pd.concat([baa_g4, one])
        res.append(100)
    else:
        total = sum(((1+profit.iloc[i])*baa_g4.iloc[-1]).fillna(0))
        res.append(total)
        one = pd.DataFrame([list(buy.values())], columns=list(buy.keys()), index = [rebal_data.index[i]]) * total / 100
        baa_g4 = pd.concat([baa_g4, one])

baa_g4["Total"] = res
baa_g4.tail()

 

 

이와 같이 g12(balance버전)도 만들어줍니다.

 

quantstats로 분석 및 비교 해보기

qs.reports.html(baa_g4["Total"], baa_g12["Total"] )

 

strategy가 g4, benchmark가 g12입니다.

2008년 5월부터 2022년 9월까지 BAA로 투자한다고 했을 때,

g4 CAGR : 11.9 MDD : 9.56

g12 CAGR : 7.59 MDD : 15.71

반응형
Comments