forked from robcarver17/pysystemtrade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquandl_futures.py
executable file
·274 lines (200 loc) · 8 KB
/
quandl_futures.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
"""
Get data from quandl for futures
"""
from sysobjects.contracts import futuresContract
from sysdata.futures.futures_per_contract_prices import (
futuresContractPriceData,
)
from sysobjects.futures_per_contract_prices import futuresContractPrices
from syscore.fileutils import get_filename_for_package
from sysdata.quandl.quandl_utils import load_private_key
import quandl
import pandas as pd
QUANDL_FUTURES_CONFIG_FILE = get_filename_for_package(
"sysdata.quandl.QuandlFuturesConfig.csv"
)
quandl.ApiConfig.api_key = load_private_key()
class quandlFuturesConfiguration(object):
def __init__(self, config_file=QUANDL_FUTURES_CONFIG_FILE):
self._config_file = config_file
def get_list_of_instruments(self):
config_data = self._get_config_information()
return list(config_data.index)
def get_instrument_config(self, instrument_code):
if instrument_code not in self.get_list_of_instruments():
raise Exception(
"Instrument %s missing from config file %s"
% (instrument_code, self._config_file)
)
config_data = self._get_config_information()
data_for_code = config_data.loc[instrument_code]
return data_for_code
def _get_config_information(self):
"""
Get configuration information
:return: dict of config information relating to self.instrument_code
"""
try:
config_data = pd.read_csv(self._config_file)
except BaseException:
raise Exception("Can't read file %s" % self._config_file)
try:
config_data.index = config_data.CODE
config_data.drop("CODE", 1, inplace=True)
except BaseException:
raise Exception("Badly configured file %s" % (self._config_file))
return config_data
def get_quandlcode_for_instrument(self, instrument_code):
config = self.get_instrument_config(instrument_code)
return config.QCODE
def get_quandlmarket_for_instrument(self, instrument_code):
config = self.get_instrument_config(instrument_code)
return config.MARKET
def get_first_contract_date(self, instrument_code):
config = self.get_instrument_config(instrument_code)
start_date = config.FIRST_CONTRACT
return "%d" % start_date
def get_quandl_dividing_factor(self, instrument_code):
config = self.get_instrument_config(instrument_code)
factor = config.FACTOR
return float(factor)
USE_DEFAULT = object()
class _quandlFuturesContract(futuresContract):
"""
An individual futures contract, with additional Quandl methods
"""
def __init__(self, futures_contract, quandl_instrument_data=USE_DEFAULT):
"""
We always create a quandl contract from an existing, normal, contract
:param futures_contract: of type FuturesContract
"""
super().__init__(futures_contract.instrument, futures_contract.date_str)
if quandl_instrument_data is USE_DEFAULT:
quandl_instrument_data = quandlFuturesConfiguration()
self._quandl_instrument_data = quandl_instrument_data
def quandl_identifier(self):
"""
Returns the Quandl identifier for a given contract
:return: str
"""
quandl_year = str(self.contract_date.year())
quandl_month = self.contract_date.letter_month()
try:
quandl_date_id = quandl_month + quandl_year
market = self.get_quandlmarket_for_instrument()
codename = self.get_quandlcode_for_instrument()
quandldef = "%s/%s%s" % (market, codename, quandl_date_id)
return quandldef
except BaseException:
raise ValueError(
"Can't turn %s %s into a Quandl Contract"
% (self.instrument_code, self.contract_date)
)
def get_quandlcode_for_instrument(self):
return self._quandl_instrument_data.get_quandlcode_for_instrument(
self.instrument_code
)
def get_quandlmarket_for_instrument(self):
return self._quandl_instrument_data.get_quandlmarket_for_instrument(
self.instrument_code
)
def get_start_date(self):
return self._quandl_instrument_data.get_start_date(
self.instrument_code)
def get_dividing_factor(self):
return self._quandl_instrument_data.get_quandl_dividing_factor(
self.instrument_code
)
class quandlFuturesContractPriceData(futuresContractPriceData):
"""
Class to specifically get individual futures price data for quandl
"""
def __init__(self):
super().__init__()
self.name = "simData connection for individual futures contracts prices, Quandl"
def __repr__(self):
return self.name
def get_prices_for_contract_object(self, contract_object):
"""
We do this because we have no way of checking if QUANDL has something without actually trying to get it
"""
return self._get_prices_for_contract_object_no_checking(
contract_object)
def _get_prices_for_contract_object_no_checking(
self, futures_contract_object):
"""
:param futures_contract_object: futuresContract
:return: futuresContractPrices
"""
self.log.label(
instrument_code=futures_contract_object.instrument_code,
contract_date=futures_contract_object.date_str,
)
try:
quandl_contract = _quandlFuturesContract(futures_contract_object)
except BaseException:
self.log.warning(
"Can't parse contract object to find the QUANDL identifier"
)
return futuresContractPrices.create_empty()
try:
contract_data = quandl.get(quandl_contract.quandl_identifier())
except Exception as exception:
self.log.warn(
"Can't get QUANDL data for %s error %s"
% (quandl_contract.quandl_identifier(), exception)
)
return futuresContractPrices.create_empty()
try:
data = quandlFuturesContractPrices(contract_data)
except BaseException:
self.log.error(
"Quandl API error: data fields are not as expected %s"
% ",".join(list(contract_data.columns))
)
return futuresContractPrices.create_empty()
# apply multiplier
factor = quandl_contract.get_dividing_factor()
data = data / factor
return data
class quandlFuturesContractPrices(futuresContractPrices):
"""
Parses Quandl format into our format
Does any transformations needed to price etc
"""
def __init__(self, contract_data):
try:
new_data = pd.DataFrame(
dict(
OPEN=contract_data.Open,
FINAL=contract_data.Last,
HIGH=contract_data.High,
LOW=contract_data.Low,
)
)
except AttributeError:
try:
new_data = pd.DataFrame(
dict(
OPEN=contract_data.Open,
FINAL=contract_data.Close,
HIGH=contract_data.High,
LOW=contract_data.Low,
)
)
except AttributeError:
try:
new_data = pd.DataFrame(
dict(
OPEN=contract_data.Open,
FINAL=contract_data.Settle,
HIGH=contract_data.High,
LOW=contract_data.Low,
)
)
except BaseException:
raise Exception(
"Quandl API error: data fields %s are not as expected"
% ",".join(list(contract_data.columns))
)
super().__init__(new_data)