forked from tobiasbrodd/backtester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.py
230 lines (191 loc) · 8.45 KB
/
data.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import pandas as pd
import os.path
import queue
import quandl
from abc import ABCMeta, abstractmethod
from event import MarketEvent
from datetime import datetime
from enum import Enum
class DataSource(Enum):
NASDAQ = "NASDAQ"
YAHOO = "YAHOO"
class DataHandler(metaclass=ABCMeta):
"""
DataHandler is an abstract base class providing an interface for
all subsequent (inherited) data handlers (both live and historic).
The goal of a (derived) DataHandler object is to output a generated
set of bars (OLHCVI) for each symbol requested.
This will replicate how a live strategy would function as current
market data would be sent "down the pipe". Thus a historic and live
system will be treated identically by the rest of the backtesting suite.
"""
__metaclass__ = ABCMeta
@abstractmethod
def get_latest_bars(self, symbol, N=1):
"""
Returns the last N
from the latest_symbol list,
or fewer if less bars are available.
"""
raise NotImplementedError("Should implement get_latest_bars()")
@abstractmethod
def update_bars(self):
"""
Pushes the latest bar to the latest symbol structure
for all symbols in the symbol list.
"""
raise NotImplementedError("Should implement update_bars()")
class HistoricCSVDataHandler(DataHandler):
"""
HistoricCSVDataHandler is designed to read CSV files for
each requested symbol from disk and provide an interface
to obtain the "latest" bar in a manner identical to a live
trading interface.
"""
def __init__(self, events, csv_dir, symbol_list, source=DataSource.NASDAQ):
self.events = events
self.csv_dir = csv_dir
self.symbol_list = symbol_list
self.symbol_data = {}
self.symbol_dataframe = {}
self.latest_symbol_data = {}
self.all_data = {}
self.continue_backtest = True
self.time_col = 1
self.price_col = 2
self._open_convert_csv_files(source)
def _open_convert_csv_files(self, source):
"""
Opens the CSV files from the data directory, converting
them into pandas DataFrames within a symbol dictionary.
For this handler it will be assumed that the data is
taken from DTN IQFeed. Thus its format will be respected.
"""
combined_index = None
for symbol in self.symbol_list:
if source == DataSource.NASDAQ:
self.parse_nasdaq_csv(symbol)
else:
self.parse_yahoo_csv(symbol)
if combined_index is None:
combined_index = self.symbol_data[symbol].index
else:
combined_index.union(self.symbol_data[symbol].index)
self.latest_symbol_data[symbol] = []
for symbol in self.symbol_list:
self.symbol_dataframe[symbol] = self.symbol_data[symbol].reindex(index=combined_index, method='pad')
self.all_data[symbol] = self.symbol_dataframe[symbol].copy()
self.symbol_data[symbol] = self.symbol_dataframe[symbol].iterrows()
def _get_new_data(self, symbol):
"""
Returns the latest bar from the data feed as a tuple of
(sybmbol, datetime, open, low, high, close, volume).
"""
for row in self.symbol_data[symbol]:
yield tuple([symbol, row[0], row[1][0]])
def get_latest_data(self, symbol, N=1):
try:
return self.latest_symbol_data[symbol][-N:]
except KeyError:
print("{symbol} is not a valid symbol.").format(symbol=symbol)
def update_latest_data(self):
for symbol in self.symbol_list:
data = None
try:
data = next(self._get_new_data(symbol))
except StopIteration:
self.continue_backtest = False
if data is not None:
self.latest_symbol_data[symbol].append(data)
self.events.put(MarketEvent())
def create_baseline_dataframe(self):
dataframe = None
for symbol in self.symbol_list:
df = self.symbol_dataframe[symbol]
if dataframe == None:
dataframe = pd.DataFrame(df['Close'])
dataframe.columns = [symbol]
else:
dataframe[symbol] = pd.DataFrame(df['Close'])
dataframe[symbol] = dataframe[symbol].pct_change()
dataframe[symbol] = (1.0 + dataframe[symbol]).cumprod()
return dataframe
def parse_yahoo_csv(self, symbol):
self.symbol_data[symbol] = pd.read_csv(os.path.join(self.csv_dir, symbol + '.csv'), header=0, index_col=0, parse_dates=True)
def parse_nasdaq_csv(self, symbol):
tmp = pd.read_csv(os.path.join(self.csv_dir, symbol + '.csv'), header=0, index_col=0, parse_dates=True).iloc[::-1]
self.symbol_data[symbol] = pd.DataFrame(tmp['Closing price'])
self.symbol_data[symbol].columns = ['Close']
# self.symbol_data[symbol]['Open'] = tmp['Closing price']
# self.symbol_data[symbol]['High'] = tmp['High price']
# self.symbol_data[symbol]['Low'] = tmp['Low price']
self.symbol_data[symbol]['Close'] = tmp['Closing price']
# self.symbol_data[symbol]['Adj Close'] = tmp['Closing price']
# self.symbol_data[symbol]['Volume'] = tmp['Total volume']
self.symbol_data[symbol] = self.symbol_data[symbol][self.symbol_data[symbol]['Close'] > 0.0]
class QuandlDataHandler(DataHandler):
def __init__(self, events, symbol_list, api_key, start_date='2000-01-01', end_date=None):
quandl.ApiConfig.api_key = api_key
self.events = events
self.symbol_list = symbol_list
self.start_date = start_date
self.end_date = end_date
if self.end_date == None:
self.end_date = datetime.today().strftime('%Y-%m-%d')
self.symbol_data = {}
self.symbol_dataframe = {}
self.latest_symbol_data = {}
self.all_data = {}
self.continue_backtest = True
self.time_col = 1
self.price_col = 2
self._load_convert_quandl_data()
def _load_convert_quandl_data(self):
combined_index = None
for symbol in self.symbol_list:
self._get_nasdaq_data(symbol)
if combined_index is None:
combined_index = self.symbol_data[symbol].index
else:
combined_index.union(self.symbol_data[symbol].index)
self.latest_symbol_data[symbol] = []
for symbol in self.symbol_list:
self.symbol_dataframe[symbol] = self.symbol_data[symbol].reindex(index=combined_index, method='pad')
self.all_data[symbol] = self.symbol_dataframe[symbol].copy()
self.symbol_data[symbol] = self.symbol_dataframe[symbol].iterrows()
def _get_new_data(self, symbol):
for row in self.symbol_data[symbol]:
yield tuple([symbol, row[0], row[1][0]])
def get_latest_data(self, symbol, N=1):
try:
return self.latest_symbol_data[symbol][-N:]
except KeyError:
print("{symbol} is not a valid symbol.").format(symbol=symbol)
def update_latest_data(self):
for symbol in self.symbol_list:
data = None
try:
data = next(self._get_new_data(symbol))
except StopIteration:
self.continue_backtest = False
if data is not None:
self.latest_symbol_data[symbol].append(data)
self.events.put(MarketEvent())
def create_baseline_dataframe(self):
dataframe = None
for symbol in self.symbol_list:
df = self.symbol_dataframe[symbol]
if dataframe == None:
dataframe = pd.DataFrame(df['Close'])
dataframe.columns = [symbol]
else:
dataframe[symbol] = pd.DataFrame(df['Close'])
dataframe[symbol] = dataframe[symbol].pct_change()
dataframe[symbol] = (1.0 + dataframe[symbol]).cumprod()
return dataframe
def _get_nasdaq_data(self, symbol):
self.symbol_data[symbol] = quandl.get('NASDAQOMX/' + symbol, start_date=self.start_date, end_date=self.end_date)
self.symbol_data[symbol].drop(columns=['High', 'Low', 'Total Market Value', 'Dividend Market Value'], inplace=True)
self.symbol_data[symbol].columns = ['Close']
self.symbol_data[symbol].index.names = ['Date']
self.symbol_data[symbol] = self.symbol_data[symbol][self.symbol_data[symbol]['Close'] > 0.0]