Recent Posts
Recent Comments
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Archives
Today
Total
관리 메뉴

Philippians 4:13

[code] Ray dalio all weather portfolio.py 본문

人工智能

[code] Ray dalio all weather portfolio.py

Ted zhao 2024. 7. 28. 12:25

[결과 값]

Ticker
DBC    887
GLD    582
IEF      0
SPY      0
TLT      0
dtype: int64
100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:00<00:00,  1.97it/s]
                                AllWeather
start                  2006-02-05 00:00:00
end                    2024-07-26 00:00:00
rf                                     0.0
total_return                      2.090214
cagr                              0.062991
max_drawdown                     -0.229789
calmar                            0.274125
mtd                               0.007496
three_month                       0.051314
six_month                         0.057837
ytd                               0.042093
one_year                          0.059717
three_year                       -0.011524
five_year                         0.042928
ten_year                           0.04921
incep                             0.062991
daily_sharpe                      0.803636
daily_sortino                     1.311759
daily_mean                        0.064373
daily_vol                         0.080102
daily_skew                       -0.207531
daily_kurt                        4.702641
best_day                          0.036756
worst_day                         -0.04392
monthly_sharpe                    0.778923
monthly_sortino                   1.337689
monthly_mean                      0.064784
monthly_vol                       0.083171
monthly_skew                      -0.49365
monthly_kurt                      2.271362
best_month                        0.082359
worst_month                      -0.088754
yearly_sharpe                     0.711926
yearly_sortino                     1.46455
yearly_mean                       0.065014
yearly_vol                        0.091322
yearly_skew                      -1.184732
yearly_skew                      -1.184732
yearly_kurt                        2.07618
best_year                         0.184576
worst_year                       -0.187553
avg_drawdown                     -0.012673
avg_drawdown_days                29.768473
avg_up_month                      0.018461
avg_down_month                   -0.018081
win_year_perc                     0.833333
twelve_month_win_perc             0.815166

 

[파이썬 코드]

import pandas as pd
import bt
import yfinance as yf
from setuptools import distutils # 추가된 부분

# 데이터 불러오기 (yfinance 활용)
tickers = ["SPY", "TLT", "GLD", "IEF", "DBC"]  
data = yf.download(tickers, start="2002-07-30")["Adj Close"] # 모든 데이터 다운로드

# 불필요한 데이터 제거
data = data.loc["2002-07-30":] # TLT 출시일 이후 데이터만 사용

# 결측치 확인 및 처리
print(data.isnull().sum()) # 결측치 개수 확인
data.dropna(inplace=True) # 결측치 제거 (혹은 fillna()로 채움)

# 올웨더 포트폴리오 비중 설정
weights = {"SPY": 0.3, "TLT": 0.4, "GLD": 0.075, "IEF": 0.15, "DBC": 0.075}

# bt 전략 생성
strategy = bt.Strategy(
    "AllWeather",
    [
        bt.algos.RunYearly(),  # 연간 리밸런싱
        bt.algos.SelectAll(),  # 모든 데이터 사용
        bt.algos.WeighSpecified(**weights),  # 비중 설정
        bt.algos.Rebalance(),  # 리밸런싱 실행
    ],
)

# 백테스트 실행
backtest = bt.Backtest(strategy, data)
result = bt.run(backtest)

# 결과 출력
print(result.stats)  # 주요 통계 출력
result.plot()  # 수익률 그래프 출력