-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRSIBacktester.py
More file actions
151 lines (123 loc) · 5.54 KB
/
RSIBacktester.py
File metadata and controls
151 lines (123 loc) · 5.54 KB
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
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import yfinance as yf
from scipy.optimize import brute
plt.style.use("seaborn")
class RSIBacktester():
''' Class for the vectorized backtesting of RSI-based trading strategies.
Attributes
==========
symbol: str
ticker symbol with which to work with
periods: int
time window in days to calculate moving average UP & DOWN
rsi_upper: int
upper rsi band indicating overbought instrument
rsi_lower: int
lower rsi band indicating oversold instrument
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 RSI parameter(s)
test_strategy:
runs the backtest for the RSI-based strategy
plot_results:
plots the performance of the strategy compared to buy and hold
update_and_run:
updates RSI parameters and returns the negative absolute performance (for minimization algorithm)
optimize_parameters:
implements a brute force optimization for the three RSI parameters
'''
def __init__(self, symbol, periods, rsi_upper, rsi_lower, start, end, tc):
self.symbol = symbol
self.periods = periods
self.rsi_upper = rsi_upper
self.rsi_lower = rsi_lower
self.start = start
self.end = end
self.tc = tc
self.results = None
self.get_data()
def __repr__(self):
return "RSIBacktester(symbol = {}, RSI({}, {}, {}), start = {}, end = {})".format(self.symbol, self.periods, self.rsi_upper, self.rsi_lower, self.start, self.end)
def get_data(self):
''' Retrieves and prepares the data.
'''
raw = pd.read_csv("forex_pairs.csv", parse_dates = ["Date"], index_col = "Date")
raw = raw[self.symbol].to_frame().dropna()
raw = raw.loc[self.start:self.end]
raw.rename(columns={self.symbol: "price"}, inplace=True)
raw["returns"] = np.log(raw / raw.shift(1))
raw["U"] = np.where(raw.price.diff() > 0, raw.price.diff(), 0)
raw["D"] = np.where(raw.price.diff() < 0, -raw.price.diff(), 0)
raw["MA_U"] = raw.U.rolling(self.periods).mean()
raw["MA_D"] = raw.D.rolling(self.periods).mean()
raw["RSI"] = raw.MA_U / (raw.MA_U + raw.MA_D) * 100
self.data = raw
def set_parameters(self, periods = None, rsi_upper = None, rsi_lower = None):
''' Updates RSI parameters and resp. time series.
'''
if periods is not None:
self.periods = periods
self.data["MA_U"] = self.data.U.rolling(self.periods).mean()
self.data["MA_D"] = self.data.D.rolling(self.periods).mean()
self.data["RSI"] = self.data.MA_U / (self.data.MA_U + self.data.MA_D) * 100
if rsi_upper is not None:
self.rsi_upper = rsi_upper
if rsi_lower is not None:
self.rsi_lower = rsi_lower
def test_strategy(self):
''' Backtests the trading strategy.
'''
data = self.data.copy().dropna()
data["position"] = np.where(data.RSI > self.rsi_upper, -1, np.nan)
data["position"] = np.where(data.RSI < self.rsi_lower, 1, data.position)
data.position = data.position.fillna(0)
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 round(perf, 6), round(outperf, 6)
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 = "{} | RSI ({}, {}, {}) | TC = {}".format(self.symbol, self.periods, self.rsi_upper, self.rsi_lower, self.tc)
self.results[["creturns", "cstrategy"]].plot(title=title, figsize=(12, 8))
def update_and_run(self, RSI):
''' Updates RSI parameters and returns the negative absolute performance (for minimization algorithm).
Parameters
==========
RSI: tuple
RSI parameter tuple
'''
self.set_parameters(int(RSI[0]), int(RSI[1]), int(RSI[2]))
return -self.test_strategy()[0]
def optimize_parameters(self, periods_range, rsi_upper_range, rsi_lower_range):
''' Finds global maximum given the RSI parameter ranges.
Parameters
==========
periods_range, rsi_upper_range, rsi_lower_range : tuple
tuples of the form (start, end, step size)
'''
opt = brute(self.update_and_run, (periods_range, rsi_upper_range, rsi_lower_range), finish=None)
return opt, -self.update_and_run(opt)