<jupyter_start><jupyter_text>Backtesting Another Strategy<jupyter_code>import pandas_datareader as pdr
import datetime as dt
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib notebook
tickers = ['SPY', 'IWM', 'MDY', 'QQQ', 'TLT']

start = dt.datetime(2007, 1, 1)
end = dt.datetime(2021, 1, 1)

data = pdr.get_data_yahoo(tickers, start, end, interval='m')

data = data['Adj Close']
data['Short'] = 1.0
data = data[['SPY', 'IWM', 'MDY', 'QQQ', 'Short', 'TLT']]
log_returns = np.log(data/data.shift())
strat = log_returns[['SPY', 'IWM', 'MDY', 'QQQ', 'Short']].copy()
rolling_sum = strat.copy()

for ticker in ['SPY', 'IWM', 'MDY', 'QQQ', 'Short']:
    rolling_sum[ticker] = rolling_sum[ticker].rolling(3).sum()
rtn = strat[rolling_sum.apply(lambda x: x == rolling_sum.max(axis=1)).shift()].sum(axis=1)*.6
rtn = rtn + log_returns['TLT']*.4<jupyter_output><empty_output><jupyter_text>Project- Backtesting the strategy  Step 1- Modify to evaluate monthly data<jupyter_code>def calculate(log_return, start, end):
    years = int(end) - int(start) + 1
    
    data = log_return.loc[start:end]
    
    cagr = np.exp(data.sum())**(1/years) - 1
    
    norm = data.cumsum().apply(np.exp)
    
    rolling_max = norm.cummax()
    monthly_drawdown = norm/rolling_max - 1
    drawdown = monthly_drawdown.cummin().iloc[-1]

    volatility = data.std()*(12**.5)
    
    return cagr, drawdown, volatility<jupyter_output><empty_output><jupyter_text>Step 2- Copy paste visualization<jupyter_code>def visualize(backtest, spy, start, end):
    def x_titles(spy_val, bt_val):
        spy_str = str(round(spy_val*100, 1))
        bt_str = str(round(bt_val*100, 1))
        return ['SPY\n' + spy_str + '%', 'Backtest\n' + bt_str + '%']
        
    spy_cagr, spy_drawdown, spy_vol = calculate(spy, start, end)
    bt_cagr, bt_drawdown, bt_vol = calculate(backtest, start, end)

    fig, ax = plt.subplots(2, 2)
    
    spy.loc[start:end].cumsum().apply(np.exp).plot(ax=ax[0, 0])
    backtest.loc[start:end].cumsum().apply(np.exp).plot(ax=ax[0, 0], label='Backtest', c='c')
    ax[0, 0].legend()
    ax[0, 0].set_xticks([start, end])
    
    x = x_titles(spy_cagr, bt_cagr)
    ax[0, 1].bar(x, [spy_cagr, bt_cagr], color=['b', 'c'])
    ax[0, 1].set_title("CAGR")
    
    x = x_titles(spy_drawdown, bt_drawdown)
    ax[1, 0].bar(x, [spy_drawdown, bt_drawdown], color=['b', 'c'])
    ax[1, 0].set_title("Drawdown")

    x = x_titles(spy_vol, bt_vol)
    ax[1, 1].bar(x, [spy_vol, bt_vol], color=['b', 'c'])
    ax[1, 1].set_title("Volatility")

    plt.tight_layout()<jupyter_output><empty_output><jupyter_text>Step 3- Backtest 2008 to end 2017- Backtest 2011 to end 2020<jupyter_code>visualize(rtn, log_returns['SPY'], '2008', '2017')
visualize(rtn, log_returns['SPY'], '2011', '2020')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18156128576474745, 'fraction_numerical': 0.032497350759448956, 'mean_word_length': 4.467181467181467, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '407189', 'license': 'MIT', 'n_tokens_mistral': 1232, 'n_tokens_neox': 1152, 'n_words': 221, 'path': 'Python For Financial Analysis/09 - Backtesting Another Strategy.ipynb', 'repo_name': 'shreejitverma/Data-Scientist', 'size': '2831'}