Last active
March 25, 2019 09:35
-
-
Save scubamut/922fed9c685fe538cb538be37bb1e32a to your computer and use it in GitHub Desktop.
COLAB: setup for strategy backtests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function | |
!pip install git+https://github.com/scubamut/fintools.git | |
!pip install cvxopt | |
!pip install pyfolio | |
!pip install ffn | |
!pip install itable | |
!pip install git+https://github.com/pydata/pandas-datareader.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas_datareader.data as web | |
import matplotlib.pyplot as plt | |
import pandas as pd | |
from datetime import datetime | |
import itable | |
import ffn | |
from fintools.Parameters import Parameters | |
from fintools.set_start_end import set_start_end | |
from fintools.get_yahoo_prices import get_yahoo_prices | |
from fintools.compute_weights_RS_DM import compute_weights_RS_DM | |
from fintools.compute_weights_PMA import compute_weights_PMA | |
from fintools.endpoints import endpoints | |
from fintools.backtest import backtest | |
from fintools.monthly_return_table import monthly_return_table | |
from fintools.show_return_table import show_return_table |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
portfolios = { | |
'RS0001': { 'symbols': ['VCVSX','VWEHX','VFIIX','FGOVX','VWAHX'], | |
'prices':data_source, 'start':start_date, 'end':end_date, | |
'rs_lookback': 1, 'risk_lookback': 1, 'n_top': 2, 'frequency': 'M', | |
'cash_proxy': 'CASHX', 'risk_free': 0}, | |
'RS0002': {'symbols': ['MMHYX','FAGIX','VFIIX'], | |
'prices':data_source, 'start':start_date, 'end':end_date, | |
'rs_lookback': 3, 'risk_lookback': 2, 'n_top': 1, 'frequency': 'M', | |
'cash_proxy': 'CASHX', 'risk_free': 0}, | |
'RS0003': {'symbols': ['MMHYX','FAGIX','VFIIX'], | |
'prices':data_source, 'start':start_date, 'end':end_date, | |
'rs_lookback': 1, 'risk_lookback': 1, 'n_top': 1, 'frequency': 'Q', | |
'cash_proxy': 'CASHX', 'risk_free': 0}, | |
'DM0001': {'symbols': ['VCVSX','VWINX','VWEHX','VGHCX','VUSTX','VFIIX','VWAHX','FGOVX','FFXSX'], | |
'prices':data_source, 'start':start_date, 'end':end_date, | |
'rs_lookback': 1, 'risk_lookback': 1, 'n_top': 3, 'frequency': 'M', | |
'cash_proxy': 'CASHX', 'risk_free': 'FFXSX'}, | |
'DM0002': {'symbols': ['VCVSX','VUSTX','VWEHX','VFIIX','VGHCX','FRESX'], | |
'prices':data_source, 'start':start_date, 'end':end_date, | |
'rs_lookback': 1, 'risk_lookback': 1, 'n_top': 5, 'frequency': 'M', | |
'cash_proxy': 'VFIIX', 'risk_free': 'FFXSX'}, | |
'PMA001': {'symbols': ['VCVSX', 'VFIIX'], | |
'prices':data_source, 'start':start_date, 'end':end_date, | |
'risk_lookback': 3, 'frequency': 'M', 'allocations': [0.6, 0.4], | |
'cash_proxy': 'VUSTX'}, | |
'PMA002': {'symbols': ['VCVSX', 'VWINX', 'VWEHX'], | |
'prices':data_source, 'start':start_date, 'end':end_date, | |
'risk_lookback': 3, 'frequency': 'M', 'allocations': [0.6, 0.2, 0.2], | |
'cash_proxy': 'VUSTX'}, | |
'PMA003': {'symbols': ['VCVSX', 'FAGIX', 'VGHCX'], | |
'prices':data_source, 'start':start_date, 'end':end_date, | |
'risk_lookback': 2, 'frequency': 'M', 'allocations': [1./3., 1./3., 1./3.], | |
'cash_proxy': 'VUSTX'} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define which online source one should use | |
data_source = 'yahoo' | |
# We would like all available data from 01/01/2000 until today. | |
# start_date = '1986-01-01' | |
start_date = '2000-01-01' | |
# end_date = '2016-12-31' | |
end_date = datetime.today().strftime('%Y-%m-%d') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name = 'RS0001' | |
if 'PMA' in name : | |
p_value, p_holdings, p_weights, prices = compute_weights_PMA (name, portfolios[name]) | |
else : | |
p_value, p_holdings, p_weights, prices = compute_weights_RS_DM (name, portfolios[name]) | |
p_value.plot(figsize=(15, 10), grid=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ffn.calc_perf_stats(p_value).display() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def highlight_pos_neg (s) : | |
is_positive = s > 0 | |
return ['background-color : rgb(127,255,0)' if v else 'background-color : rgb(255,99,71)' for v in is_positive] | |
df = monthly_return_table (p_value) | |
df.style.\ | |
apply(highlight_pos_neg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
frame = df['Annual Returns'].to_frame() | |
frame['positive'] = df['Annual Returns'] >= 0 | |
frame['Annual Returns'].plot(figsize=(15,10),kind='bar',color=frame.positive.map({True: 'g', False: 'r'}), grid=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment