<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 <!--NAVIGATION-->
< Bonus Materials I | Contents >
Bonus Materials II
Vectorised backtesting
Step1:  Experiment With the Training Data Set
Step2:  Vectorized Backtesting With the Test Set - Momentum
Note
Step3:  Vectorized Backtesting With the Test Set - Reversion
Note
<ASSISTANT_TASK:>
Python Code:
import numpy as np
import pandas as pd
import oandapy
import configparser
%matplotlib inline
import seaborn as sns
import matplotlib.pyplot as plt
config = configparser.ConfigParser()
config.read('../config/config_v1.ini')
account_id = config['oanda']['account_id']
api_key = config['oanda']['api_key']
oanda = oandapy.API(environment="practice", 
                    access_token=api_key)
response = oanda.get_history(instrument="EUR_USD",
                             granularity="H1",
                             count = 5000)
res = pd.DataFrame(response['candles'])
res.columns = ['Close_Ask', 'Close_Bid', 'Complete', 
               'High_Ask', 'High_Bid', 'Low_Ask', 'Low_Bid', 
               'Open_Ask', 'Open_Bid', 'Time', 'Volume']
res = res.reindex_axis(['Time', 'Open_Bid', 'Open_Ask', 
                        'High_Bid', 'High_Ask', 'Low_Bid', 
                        'Low_Ask', 'Close_Bid', 'Close_Ask', 
                        'Complete', 'Volume'], 
                       axis=1)
df = res[['Time', 'Close_Bid', 'Close_Ask']].copy()
df['rtns'] = res['Close_Bid'].pct_change()
dtrain = df[0:4500].copy()
dtest = df[4500:].copy()
Explanation: <!--NAVIGATION-->
< Bonus Materials I | Contents >
Bonus Materials II
Vectorised backtesting
End of explanation
k = range(1,13)
h = 1
for oo in k:
    dtrain['signal'] = np.sign(dtrain['rtns'].rolling(oo).sum())
    dtrain['strategy_rtn'] =  dtrain['signal'].shift(1) * dtrain['rtns']
    res = dtrain['strategy_rtn'].dropna().sum()
    print('{0:3} {1:>8.4f}'.format(oo, res))
Explanation: Experiment With the Training Data Set
End of explanation
dtest['signal'] = np.sign(dtest['rtns'].rolling(11).sum())
dtest['result'] = dtest['signal'].shift(1) * dtest['rtns']
dtest['result'].dropna().cumsum().plot(figsize=(10,6));
Explanation: Vectorized Backtesting With the Test Set - Momentum
Note: we use the k=11,h=1 combination as it has the highest returns in the training set
End of explanation
dtest['signal'] = - np.sign(dtest['rtns'].rolling(3).sum())
dtest['result'] = dtest['signal'].shift(1) * dtest['rtns']
dtest['result'].dropna().cumsum().plot(figsize=(10,6));
Explanation: Vectorized Backtesting With the Test Set - Reversion
Note: we use the k=3,h=1 combination as it has the highest returns in the training set for a reversion strategy
End of explanation
<END_TASK>