scubamut
3/25/2019 - 9:32 AM

MY BACKTEST SETUP FOR STRATEGY

#*****************************************************************
# IMPORTS
#****************************************************************** 
 
from __future__ import print_function
from IPython.utils.path import get_ipython_dir
print(get_ipython().profile_dir.location)
 
import pandas_datareader.data as pdr
import matplotlib.pyplot as plt
import pandas as pd
import datetime
import talib
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
# from fintools.finhelpers3 import highlight_pos_neg

from fintools import get_DataArray,compute_weights_RS_DM,compute_weights_PMA,\
                     Parameters

 
%matplotlib inline

#*****************************************************************
# SYMBOLS
#****************************************************************** 
 
# these are the FUNDs used by Systematic Investor (2005 - today)
symbols = ['VCVSX','VWINX','VWEHX','VGHCX','VFIIX','VWAHX','FGOVX','FFXSX']
 
# these funds could be used as proxies for the Philbrick results
#symbols = ['^GSPC', 'VEURX', 'FJPNX', 'FEMKX', 'FRESX','EGLRX', 'VFITX', 'VUSTX', 'VGPMX', 'GOLDX' ]
#*****************************************************************
# VARIABLES
#****************************************************************** 
 
frequency = 'M'
    
cash_proxy = 'VUSTX'
risk_free = 0
 
rs_lookback = 1
risk_lookback = 1
n_top = 3
 
data_path = '/home/scubamut/Projects/DATA'
data_source = 'yahoo'
 
start_date = '2000-01-01'
#*****************************************************************
# CREATE TICKERS
#******************************************************************
 
tickers = symbols.copy()
if cash_proxy != 'CASHX' :
    tickers = list(set(tickers + [cash_proxy]))
if isinstance(risk_free, str) :
    tickers = list(set(tickers + [risk_free]))
#*****************************************************************
# PRICES
#******************************************************************
 
# User pandas_reader.data.DataReader to load the desired data.
raw_data = pdr.DataReader(tickers, data_source, start_date, end_date)
data = raw_data['Adj Close'].sort_index(ascending=True)
 
 
# print (inception_dates)
 
prices = data.copy().dropna()
#*****************************************************************
# END POINTS & PRICES AT END POINTS
#******************************************************************
 
end_points = endpoints(period=frequency, trading_days=prices.index)
prices_m = prices.loc[end_points]
prices_m[:3]
#*****************************************************************
# INCEPTION DATES
#******************************************************************
 
inception_dates = pd.DataFrame([data[ticker].first_valid_index() for ticker in data.columns], 
                               index=data.keys(), columns=['inception'])
#*****************************************************************
# STRATEGIES
#******************************************************************
 
strategies = {
    'RS0001': { 'assets': ['VCVSX','VWEHX','VFIIX','FGOVX','VWAHX'], 
               'start':start, 'end':end,
               'rs_lookback': 1, 'risk_lookback': 1, 'n_top': 2, 'frequency': 'M',
              'cash_proxy': 'CASHX', 'risk_free': 0},
    'PMA001': {'assets': ['VCVSX', 'VFIIX'], 
               'start':start, 'end':end,  
               'risk_lookback': 3, 'frequency': 'M', 'allocations': [0.6, 0.4],
              'cash_proxy': 'VUSTX'}
}
#*****************************************************************
# STRATEGY TYPES
#******************************************************************
 
s = [k for k in strategies.keys()]
 
common_parameters = ['symbols','prices','start','end','cash_proxy','risk_lookback','frequency']
RS_DM_parameters = ['risk_free','rs_lookback','n_top',]
PMA_parameters = ['allocations']
 
RS_DM_p = [p for p in common_parameters + RS_DM_parameters]
PMA_p = [p for p in common_parameters + PMA_parameters]
 
# Dictionaries for multiple strategies
strategy_values = pd.DataFrame(columns=strategies.keys())
security_weights = {}
security_holdings = {}
security_prices = {}
#*****************************************************************
# RUN STRATEGIES
#******************************************************************
 
for name in [k for k in strategies.keys()]:
    if (set(strategies[name]) == set(RS_DM_p)):
        print(name, 'RS_DM_p')
        s_value, s_holdings, s_weights, s_prices =  compute_weights_RS_DM (name, strategies[name])
    elif (set(strategies[name]) == set(PMA_p)):
        print(name, 'PMA_p')
        s_value, s_holdings, s_weights, s_prices =  compute_weights_PMA (name, strategies[name])
    else:
        print(name + ' IS UNDEFINED')
        
    s_value.plot(figsize=(15, 10), grid=True)    
    
    strategy_values[name] = s_value
    security_weights[name] = s_weights
    security_holdings[name] = s_holdings
    security_prices[name] = s_prices