forked from robcarver17/pysystemtrade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.py
308 lines (218 loc) · 8.13 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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import datetime
import pandas as pd
from syslogdiag.log import logtoscreen
from syscore.objects import get_methods
class baseData(object):
"""
Core data object - Base class
simData objects are used to get data from a particular source, and give certain information about it
The baseData class is highly generic
Normally we'd inherit from this for specific implementations (eg simulation, production for different data types),
specific asset classes (eg carry data for futures), and then for a
specific source of data (eg csv files, databases, ...)
The inheritance is:
Base generic class: simData
-> implementation specific eg simData for simulation
-> asset class specific eg futuresdata.FuturesData
-> source specific eg legacy.csvFuturesSimData
"""
def __init__(self, log=logtoscreen("baseData")):
"""
simData socket base class
>>> data = baseData()
>>> data
simData object
"""
# this will normally be overriden by the base system
setattr(self, "log", log)
def __repr__(self):
return "simData object"
def _system_init(self, base_system):
"""
This is run when added to a base system
:param base_system
:return: nothing
"""
# inherit the log
setattr(self, "log", base_system.log.setup(stage="data"))
def methods(self):
return get_methods(self)
def __getitem__(self, keyname):
"""
convenience method to get the price, make it look like a dict
:param keyname: instrument to get prices for
:type keyname: str
:returns: pd.DataFrame
"""
raise Exception(
"__getitem__ not defined for baseData class: use a class where it has been overriden"
)
def keys(self):
"""
list of things in this data set (futures contracts, instruments...)
:returns: list of str
>>> data=simData()
>>> data.keys()
[]
"""
return []
class simData(baseData):
"""
Core data object - Base class for simulation
simData objects are used to get a collection of data
The bare simData class isn't much good and holds only price and fx data
Normally we'd inherit from this for specific asset classes (eg carry data for futures), and then for a
specific source of data (eg csv files, databases, ...)
The inheritance is:
-> asset class specific eg futuresdata.FuturesData
-> source specific eg legacy.csvFuturesSimData
We can plug in different sources if desired
"""
def __repr__(self):
return "simData object with %d instruments" % len(
self.get_instrument_list())
def daily_prices(self, instrument_code):
"""
Gets daily prices
:param instrument_code: Instrument to get prices for
:type trading_rules: str
:returns: Tx1 pd.Series
"""
instrprice = self.get_raw_price(instrument_code)
dailyprice = instrprice.resample("1B").last()
return dailyprice
def get_raw_price(self, instrument_code):
"""
Default method to get instrument price
Will usually be overriden when inherited with specific data source
:param instrument_code: instrument to get prices for
:type instrument_code: str
:returns: pd.Series
"""
error_msg = (
"You have created a simData() object; you might need to use a more specific data object" %
instrument_code)
self.log.critical(error_msg)
def __getitem__(self, keyname):
"""
convenience method to get the price, make it look like a dict
:param keyname: instrument to get prices for
:type keyname: str
:returns: pd.DataFrame
"""
price = self.get_raw_price(keyname)
return price
def get_instrument_list(self):
"""
list of instruments in this data set
:returns: list of str
"""
return []
def keys(self):
"""
list of instruments in this data set
:returns: list of str
>>> data=simData()
>>> data.keys()
[]
"""
return self.get_instrument_list()
def get_value_of_block_price_move(self, instrument_code):
"""
How much does a $1 (or whatever) move in the price of an instrument block affect its value?
eg 100.0 for 100 shares
:param instrument_code: instrument to value for
:type instrument_code: str
:returns: float
"""
return 1.0
def get_raw_cost_data(self, instrument_code):
"""
Get cost data
Execution slippage [half spread] price units
Commission (local currency) per block
Commission - percentage of value (0.01 is 1%)
Commission (local currency) per block
:param instrument_code: instrument to value for
:type instrument_code: str
:returns: dict of floats
"""
return dict(
price_slippage=0.0,
value_of_block_commission=0.0,
percentage_cost=0.0,
value_of_pertrade_commission=0.0,
)
def get_instrument_currency(self, instrument_code):
"""
Get the currency for a particular instrument
:param instrument_code: instrument to value for
:type instrument_code: str
:returns: str
"""
raise NotImplementedError(
"Need to inherit from base class for specific data source"
)
def _get_fx_data(self, currency1, currency2):
"""
Get the FX rate currency1/currency2 between two currencies
Or return None if not available
(Normally we'd over ride this with a specific source)
"""
raise NotImplementedError("Need to inherit for a specific data source")
def get_fx_for_instrument(self, instrument_code, base_currency):
"""
Get the FX rate between the FX rate for the instrument and the base (account) currency
:param instrument_code: instrument to value for
:type instrument_code: str
:param base_currency: instrument to value for
:type instrument_code: str
:returns: Tx1 pd.Series
>>> data=simData()
>>> data.get_fx_for_instrument("wibble", "USD").tail(5)
2040-12-04 1.0
2040-12-05 1.0
2040-12-06 1.0
2040-12-07 1.0
2040-12-10 1.0
Freq: B, dtype: float64
"""
instrument_currency = self.get_instrument_currency(instrument_code)
fx_rate_series = self._get_fx_data(instrument_currency, base_currency)
return fx_rate_series
def get_instrument_asset_classes(self):
"""
:return: A pd.Series, row names are instruments, content is asset class
"""
error_msg = "You have created a simData() object; you need to use a more specific data object to use .get_instrument_asset_classes"
self.log.critical(error_msg)
def all_instruments_in_asset_class(self, asset_class):
"""
Return all the instruments in a given asset class
:param asset_class: str
:return: list of instrument codes
"""
asset_class_data = self.get_instrument_asset_classes()
asset_class_instrument_list = list(
asset_class_data[asset_class_data == asset_class].index
)
# Remove anything that's missing
instrument_list = self.get_instrument_list()
filtered_asset_class_instrument_list = [
instrument
for instrument in asset_class_instrument_list
if instrument in instrument_list
]
return filtered_asset_class_instrument_list
def asset_class_for_instrument(self, instrument_code):
"""
Which asset class is some instrument in?
:param instrument_code:
:return: str
"""
asset_class_data = self.get_instrument_asset_classes()
asset_class = asset_class_data[instrument_code]
return asset_class
if __name__ == "__main__":
import doctest
doctest.testmod()