-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstrument.py
366 lines (340 loc) · 12.2 KB
/
instrument.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
from trading_calendars import get_calendar
class Instrument(object):
"""
An Instrument represents the metadata of a symbol
"""
_kwargnames = frozenset({
'exchange_symbol',
'instrument_name',
'instrument_country_id',
'underlying_name',
'underlying_asset_class_id',
'settle_start',
'settle_end',
'settle_method',
'settle_timezone',
'quote_currency_id',
'multiplier',
'tick_size',
'start_date',
'end_date',
'exchange_info',
'parent_calendar_id',
'child_calendar_id'
})
def __init__(self,
exchange_symbol="",
instrument_name="",
instrument_country_id="",
underlying_name="",
underlying_asset_class_id="",
settle_start=None,
settle_end=None,
settle_method=None,
settle_timezone=None,
quote_currency_id="",
multiplier=1,
tick_size=0.01,
start_date=None,
end_date=None,
exchange_info=None,
parent_calendar_id=None,
child_calendar_id=None):
self.exchange_symbol = exchange_symbol
self.instrument_name = instrument_name
self.instrument_country_id = instrument_country_id
self.underlying_name = underlying_name
self.underlying_asset_class_id = underlying_asset_class_id
self.settle_start = settle_start
self.settle_end = settle_end
self.settle_method = settle_method
self.settle_timezone = settle_timezone
self.quote_currency_id = quote_currency_id
self.multiplier = multiplier
self.tick_size = tick_size
self.start_date = start_date
self.end_date = end_date
self.exchange_info = exchange_info
self.parent_calendar_id = parent_calendar_id
self.child_calendar_id = child_calendar_id
@property
def exchange(self):
return self.exchange_info.canonical_name
@property
def exchange_full(self):
return self.exchange_info.name
@property
def exchange_country_id(self):
return self.exchange_info.exchange_country_id
@property
def exchange_financial_center(self):
return self.exchange_info.exchange_financial_center
@property
def exchange_timezone(self):
return self.exchange_info.exchange_timezone
## Quantopian has some equality checkers not implements here
def __repr__(self):
if self.symbol:
return '%s(%s [%s])' % (type(self).__name__, self.symbol_id, self.instrument_name)
else:
return '%s(%s)' % (type(self).__name__, self.symbol_id
)
## Quantopian has some function used by pickle to determine how to serialize/deserialized this class
def to_dict(self):
"""
Convert to a python dict.
"""
return {
'exchange_symbol': self.exchange_symbol,
'instrument_name': self.instrument_name,
'instrument_country_id': self.instrument_country_id,
'underlying_name': self.underlying_name,
'underlying_asset_class_id': self.underlying_asset_class_id,
'settle_start': self.settle_start,
'settle_end': self.settle_end,
'settle_method': self.settle_method,
'settle_timezone': self.settle_timezone,
'quote_currency_id': self.quote_currency_id,
'multiplier': self.multiplier,
'tick_size': self.tick_size,
'start_date': self.start_date,
'end_date': self.end_date,
'exchange_info': self.exchange_info,
'parent_calendar_id': self.parent_calendar_id,
'child_calendar_id': self.child_calendar_id,
'exchange_info': self.exchange_info,
'exchange': self.exchange,
'exchange_full': self.exchange_full,
'exchange_financial_center': self.exchange_financial_center,
'exchange_country_id': self.exchange_country_id,
'exchange_timezone': self.exchange_financial_center,
}
@classmethod
def from_dict(cls, dict_):
"""
Build an Asset instance from a dict.
"""
return cls(**{k: v for k, v in dict_.items() if k in cls._kwargnames})
def is_alive_for_session(self, session_label):
"""
Returns whether the asset is alive at the given dt.
Parameters
----------
session_label: pd.Timestamp
The desired session label to check. (midnight UTC)
Returns
-------
boolean: whether the asset is alive at the given dt.
"""
ref_start = self.start_date.value
ref_end = self.end_date.value
return ref_start <= session_label.value <= ref_end
def is_exchange_open(self, dt_minute):
"""
Parameters
----------
dt_minute: pd.Timestamp (UTC, tz-aware)
The minute to check.
Returns
-------
boolean: whether the asset's exchange is open at the given minute.
"""
calendar = get_calendar(self.exchange)
return calendar.is_open_on_minute(dt_minute)
class Future(Instrument):
_kwargnames = frozenset({
'exchange_symbol',
'root_symbol',
'instrument_name',
'instrument_country_id',
'underlying_name',
'underlying_asset_class_id',
'settle_start',
'settle_end',
'settle_method',
'settle_timezone',
'final_settle_start',
'final_settle_end',
'final_settle_method',
'final_settle_timezone',
'last_trade_time'
'quote_currency_id',
'multiplier',
'tick_size',
'start_date',
'end_date',
'first_trade',
'last_trade',
'first_position',
'last_position',
'first_notice',
'last_notice',
'first_delivery',
'last_delivery',
'settlement_date',
'volume_switch_date',
'open_interest_switch_date',
'auto_close_date',
'exchange_info',
'parent_calendar_id',
'child_calendar_id'
'average_pricing',
'deliverable',
'delivery_month',
'delivery_year',
})
def __init__(self,
exchange_symbol="",
root_symbol="",
instrument_name="",
instrument_country_id="",
underlying_name="",
underlying_asset_class_id="",
settle_start=None,
settle_end=None,
settle_method=None,
settle_timezone=None,
final_settle_start=None,
final_settle_end=None,
final_settle_method=None,
final_settle_timezone=None,
last_trade_time=None,
quote_currency_id="",
multiplier=1,
tick_size=0.01,
start_date=None,
end_date=None,
first_trade=None,
last_trade=None,
first_position=None,
last_position=None,
first_notice=None,
last_notice=None,
first_delivery=None,
last_delivery=None,
settlement_date=None,
volume_switch_date=None,
open_interest_switch_date=None,
auto_close_date=None,
exchange_info=None,
parent_calendar_id=None,
child_calendar_id=None,
average_pricing=0,
deliverable="",
delivery_month="",
delivery_year=""):
super().__init__(
exchange_symbol=exchange_symbol,
instrument_name=instrument_name,
instrument_country_id=instrument_country_id,
underlying_name=underlying_name,
underlying_asset_class_id=underlying_asset_class_id,
settle_start=settle_start,
settle_end=settle_end,
settle_method=settle_method,
settle_timezone=settle_timezone,
quote_currency_id=quote_currency_id,
multiplier=multiplier,
tick_size=tick_size,
start_date=start_date,
end_date=end_date,
exchange_info = exchange_info,
parent_calendar_id=parent_calendar_id,
child_calendar_id=child_calendar_id
)
self.root_symbol = root_symbol
self.final_settle_start = final_settle_start
self.final_settle_end = final_settle_end
self.final_settle_method = final_settle_method
self.final_settle_timezone = final_settle_timezone
self.last_trade_time = last_trade_time
self.first_trade = first_trade
self.last_trade = last_trade
self.first_position = first_position
self.last_position = last_position
self.first_notice = first_notice
self.last_notice = last_notice
self.first_delivery = first_delivery
self.last_delivery = last_delivery
self.settlement_date = settlement_date
self.volume_switch_date = volume_switch_date
self.open_interest_switch_date = open_interest_switch_date
self.average_pricing = average_pricing
self.deliverable = deliverable
self.delivery_month = delivery_month
self.delivery_year = delivery_year
if auto_close_date is None:
if first_notice is None:
self.auto_close_date = last_trade
else:
self.auto_close_date = first_notice
else:
self.auto_close_date = eval(auto_close_date)
def to_dict(self):
"""
Convert to a python dict.
"""
super_dict = super(Future, self).to_dict()
super_dict['root_symbol'] = self.root_symbol
super_dict['final_settle_start'] = self.final_settle_start
super_dict['final_settle_end'] = self.final_settle_end
super_dict['final_settle_method'] = self.final_settle_method
super_dict['final_settle_timezone'] = self.final_settle_timezone
super_dict['last_trade_time'] = self.last_trade_time
super_dict['first_trade'] = self.first_trade
super_dict['last_trade'] = self.last_trade
super_dict['first_position'] = self.first_position
super_dict['last_position'] = self.last_position
super_dict['first_notice'] = self.first_notice
super_dict['last_notice'] = self.last_notice
super_dict['first_delivery'] = self.first_delivery
super_dict['last_delivery'] = self.last_delivery
super_dict['settlement_date'] = self.settlement_date
super_dict['volume_switch_date'] = self.volume_switch_date
super_dict['open_interest_switch_date'] = self.open_interest_switch_date
super_dict['average_pricing'] = self.average_pricing
super_dict['deliverable'] = self.deliverable
super_dict['delivery_month'] = self.delivery_month
super_dict['delivery_year'] = self.delivery_year
class Equity(Instrument):
_kwargnames = frozenset({
'exchange_symbol',
'root_symbol',
'instrument_name',
'instrument_country_id',
'underlying_name',
'underlying_asset_class_id',
'settle_start',
'settle_end',
'settle_method',
'settle_timezone',
'final_settle_start',
'final_settle_end',
'final_settle_method',
'final_settle_timezone',
'last_trade_time'
'quote_currency_id',
'multiplier',
'tick_size',
'start_date',
'end_date',
'first_trade',
'last_trade',
'first_position',
'last_position',
'first_notice',
'last_notice',
'first_delivery',
'last_delivery',
'settlement_date',
'volume_switch_date',
'open_interest_switch_date',
'auto_close_date',
'exchange_info',
'parent_calendar_id',
'child_calendar_id'
'average_pricing',
'deliverable',
'delivery_month',
'delivery_year',
})