-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMACDBacktester.py
191 lines (153 loc) · 7.03 KB
/
MACDBacktester.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import os
import numpy as np
import pandas as pd
import seaborn as sns
from oandapyV20 import API
from dotenv import load_dotenv
import matplotlib.pyplot as plt
from scipy.optimize import brute
import oandapyV20.endpoints.instruments as instruments
load_dotenv()
oanda_api_key = os.getenv('OANDA_API_KEY')
class MACDBacktester():
''' Class for the vectorized backtesting of MACD-based trading strategies.
Attributes
==========
symbol: str
ticker symbol with which to work with
EMA_S: int
time window in days for shorter EMA
EMA_L: int
time window in days for longer EMA
signal_mw: int
time window is days for MACD Signal
start: str
start date for data retrieval
end: str
end date for data retrieval
tc: float
proportional transaction costs per trade
Methods
=======
get_data:
retrieves and prepares the data
set_parameters:
sets new MACD parameter(s)
test_strategy:
runs the backtest for the MACD-based strategy
plot_results:
plots the performance of the strategy compared to buy and hold
update_and_run:
updates MACD parameters and returns the negative absolute performance (for minimization algorithm)
optimize_parameters:
implements a brute force optimization for the three MACD parameters
'''
def __init__(self, symbol, EMA_S, EMA_L, signal_mw, start, end, granularity, tc):
self.symbol = symbol
self.EMA_S = EMA_S
self.EMA_L = EMA_L
self.signal_mw = signal_mw
self.start = start
self.end = end
self.granularity = granularity
self.tc = tc
self.results = None
self.get_data()
def __repr__(self):
return "MACDBacktester(symbol = {}, MACD({}, {}, {}), start = {}, end = {})".format(self.symbol, self.EMA_S, self.EMA_L, self.signal_mw, self.start, self.end)
def get_data(self):
''' Retrieves and prepares the data from Oanda.
'''
client = API(access_token= oanda_api_key)
# Define the request parameters
params = {
"from": self.start,
"to": self.end,
"granularity": self.granularity, # Daily granularity, adjust as needed
}
# Fetch historical forex data from OANDA
request = instruments.InstrumentsCandles(instrument=self.symbol, params=params)
client.request(request)
response = request.response
# Isolate candlestick data from API response
candles = response['candles']
data_list = []
for candle in candles:
time = pd.to_datetime(candle['time'])
open_price = float(candle['mid']['o'])
high_price = float(candle['mid']['h'])
low_price = float(candle['mid']['l'])
close_price = float(candle['mid']['c'])
volume = int(candle['volume'])
data_list.append([time, open_price, high_price, low_price, close_price, volume])
# Create a pandas DataFrame
columns = ['Date', 'Open', 'High', 'Low', 'Close', 'Volume']
data = pd.DataFrame(data_list, columns=columns)
# Set the 'Date' column as the index
data.set_index('Date', inplace=True)
data["returns"] = np.log(data["Close"] / data["Close"].shift(1))
data["EMA_S"] = data["Close"].ewm(span = self.EMA_S, min_periods = self.EMA_S).mean()
data["EMA_L"] = data["Close"].ewm(span = self.EMA_L, min_periods = self.EMA_L).mean()
data["MACD"] = data.EMA_S - data.EMA_L
data["MACD_Signal"] = data.MACD.ewm(span = self.signal_mw, min_periods = self.signal_mw).mean()
self.data = data
def set_parameters(self, EMA_S = None, EMA_L = None, signal_mw = None):
''' Updates MACD parameters and resp. time series.
'''
if EMA_S is not None:
self.EMA_S = EMA_S
self.data["EMA_S"] = self.data["Close"].ewm(span = self.EMA_S, min_periods = self.EMA_S).mean()
self.data["MACD"] = self.data.EMA_S - self.data.EMA_L
self.data["MACD_Signal"] = self.data.MACD.ewm(span = self.signal_mw, min_periods = self.signal_mw).mean()
if EMA_L is not None:
self.EMA_L = EMA_L
self.data["EMA_L"] = self.data["Close"].ewm(span = self.EMA_L, min_periods = self.EMA_L).mean()
self.data["MACD"] = self.data.EMA_S - self.data.EMA_L
self.data["MACD_Signal"] = self.data.MACD.ewm(span = self.signal_mw, min_periods = self.signal_mw).mean()
if signal_mw is not None:
self.signal_mw = signal_mw
self.data["MACD_Signal"] = self.data.MACD.ewm(span = self.signal_mw, min_periods = self.signal_mw).mean()
def test_strategy(self):
''' Backtests the trading strategy.
'''
data = self.data.copy().dropna()
data["position"] = np.where(data["MACD"] > data["MACD_Signal"], 1, -1)
data["strategy"] = data["position"].shift(1) * data["returns"]
data.dropna(inplace=True)
# determine when a trade takes place
data["trades"] = data.position.diff().fillna(0).abs()
# subtract transaction costs from return when trade takes place
data.strategy = data.strategy - data.trades * self.tc
data["creturns"] = data["returns"].cumsum().apply(np.exp)
data["cstrategy"] = data["strategy"].cumsum().apply(np.exp)
self.results = data
perf = data["cstrategy"].iloc[-1] # absolute performance of the strategy
outperf = perf - data["creturns"].iloc[-1] # out-/underperformance of strategy
return [perf, outperf, self.results]
def plot_results(self):
''' Plots the cumulative performance of the trading strategy
compared to buy and hold.
'''
if self.results is None:
print("No results to plot yet. Run a strategy.")
else:
title = "{} | MACD ({}, {}, {}) | TC = {}".format(self.symbol, self.EMA_S, self.EMA_L, self.signal_mw, self.tc)
self.results[["creturns", "cstrategy"]].plot(title=title, figsize=(12, 8))
def update_and_run(self, MACD):
''' Updates MACD parameters and returns the negative absolute performance (for minimization algorithm).
Parameters
==========
MACD: tuple
MACD parameter tuple
'''
self.set_parameters(int(MACD[0]), int(MACD[1]), int(MACD[2]))
return -self.test_strategy()[0]
def optimize_parameters(self, EMA_S_range, EMA_L_range, signal_mw_range):
''' Finds global maximum given the MACD parameter ranges.
Parameters
==========
EMA_S_range, EMA_L_range, signal_mw_range : tuple
tuples of the form (start, end, step size)
'''
opt = brute(self.update_and_run, (EMA_S_range, EMA_L_range, signal_mw_range), finish=None)
return opt, -self.update_and_run(opt)