Skip to main content

No project description provided

Project description

Snowball

Snowball은 자산 배분 전략을 손쉽게 백테스팅할 수 있는 파이썬 패키지입니다.

 

설치 방법

소스 코드는 GitHub에 올라와 있으며 최신버전은 pip로 설치할 수 있습니다.

pip install snowball

 

실행 예시

아래 예제를 실행하기 전에 yfinance 패키지를 우선 설치해 주세요.
또 다른 예제들은 sample 디렉토리안의 노트북을 참고하시기 바랍니다.  

주식/채권 60/40

  • 2013년부터 매월말 한국 주식 ETF와 한국 국채3년 ETF를 6:4로 리밸런싱
import yfinance as yf
import snowball as sb

data = yf.download('069500.KS 114820.KS', period='MAX')['Adj Close']

portfolio = sb.run_backtest(prices=data, 
                            schedule='EOM', 
                            rule={'069500.KS': 0.6, '114820.KS': 0.4},
                            start='2012-12-01')
portfolio.report()

report_sample  

사용 방법

결과 리포트내 챠트가 plotly로 구현되어 있으므로 jupyter notebook에서 실행하는 것을 권장합니다.  

0. 자산 가격 데이터 준비

  • 투자할 자산(주식, ETF, Index, Commodity, etc.)의 일일 가격 데이터를 준비합니다.
  • 데이터 형태는 pandas DataFrame이어야 합니다.
print(price_data)
date ETF_A ETF_B ETF_C
2000-11-21 140.2723 203.29 2317.70
2000-11-27 140.2707 204.39 2318.63
2000-11-28 140.2121 204.87 2318.44
... ... ... ...
2020-12-01 137.8898 200.26 2316.95
2020-12-02 139.8906 200.32 2316.53
2020-12-03 138.7654 200.19 2315.77
 

1. 백테스팅

  • run_backtest() 함수를 이용하여 간단하게 포트폴리오의 성과를 시뮬레이션할 수 있습니다.
  • 리밸런싱 시점은 월말, 분기말, 년말, 사용자 정의 일자를 지원합니다.
  • 리밸런싱 방법은 동일 비중, 리스크 패리티, 정적 배분, 사용자 정의 규칙을 지원합니다.
import snowball as sb

# 동일 비중 / 월말 리밸런싱
portfolio = sb.run_backtest(prices=price_data, 
                            schedule='EOM', 
                            rule='EqualWeight',
                            start='2000-12-31')
# 리스크 패리티 / 분기초(분기말 1영업일후) 리밸런싱 
portfolio = sb.run_backtest(prices=price_data, 
                            schedule='EOQ+1', 
                            rule='RiskParity',
                            start='2000-12-31')
# 정적 배분 / 매년말 5영업일전 리밸런싱
portfolio = sb.run_backtest(prices=price_data, 
                            schedule='EOY-5', 
                            rule={'ETF_A': 0.6, 'ETF_B': 0.4},
                            start='2000-12-31')
# 사용자 정의 규칙 / 지정일자
class MyRule(sb.Rule):
    def __init__(self):
        ...

    def calculate(self, date, *args, **kwargs):
        if date < pd.to_datetime('2005-01-01'):
            weights = {'ETF_A': 0.7, 'ETF_B': 0.3}
        else:
            weights = {'ETF_A': 0.3, 'ETF_B': 0.7}
        return pd.Series(weights)

my_rule = MyRule() 
my_schedule = ['2001-06-30', '2005-06-30']
portfolio = sb.run_backtest(prices=price_data, 
                            schedule=my_schedule,
                            rule=my_rule,
                            start='2000-12-31')

 

2. 성과 분석

  • run_backtest()가 반환한 오브젝트의 report() 함수를 이용하여 결과를 확인합니다.
  • 챠트는 plotly로 구현하여습니다. jupyter lab에서 챠트 표시가 정상적으로 되지 않는 경우 extension을 설치하여 주십시오).
  • plotly 챠트 때문에 노트북 렌더링이 느려지는 경우에는 charts='static' 또는 charts=None 옵션을 사용하시기 바랍니다.
portfolio.report() # charts='interactive'
  • 또는 오브젝트 내의 결과 데이터를 직접 처리할 수 있습니다.
print(portfolio.log, portfolio.stats, portfolio.returns, portfolio.weights, portfolio.trades) 

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

Snowball-0.4.0-py3-none-any.whl (13.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page