1
|
|
|
# |
2
|
|
|
# Copyright 2015 Quantopian, Inc. |
3
|
|
|
# |
4
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); |
5
|
|
|
# you may not use this file except in compliance with the License. |
6
|
|
|
# You may obtain a copy of the License at |
7
|
|
|
# |
8
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
9
|
|
|
# |
10
|
|
|
# Unless required by applicable law or agreed to in writing, software |
11
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
12
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13
|
|
|
# See the License for the specific language governing permissions and |
14
|
|
|
# limitations under the License. |
15
|
|
|
|
16
|
|
|
from logbook import Logger |
17
|
|
|
|
18
|
|
|
log = Logger('DataPortal') |
19
|
|
|
|
20
|
|
|
BASE_FIELDS = { |
21
|
|
|
'open': 'open', |
22
|
|
|
'open_price': 'open', |
23
|
|
|
'high': 'high', |
24
|
|
|
'low': 'low', |
25
|
|
|
'close': 'close', |
26
|
|
|
'close_price': 'close', |
27
|
|
|
'volume': 'volume', |
28
|
|
|
'price': 'close' |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
class DataPortal(object): |
33
|
|
|
def __init__(self, |
34
|
|
|
env, |
35
|
|
|
equity_daily_reader=None, |
36
|
|
|
equity_minute_reader=None, |
37
|
|
|
future_daily_reader=None, |
38
|
|
|
future_minute_reader=None, |
39
|
|
|
adjustment_reader=None): |
40
|
|
|
|
41
|
|
|
self._adjustment_reader = adjustment_reader |
42
|
|
|
|
43
|
|
|
self._equity_daily_reader = equity_daily_reader |
44
|
|
|
self._equity_minute_reader = equity_minute_reader |
45
|
|
|
self._future_daily_reader = future_daily_reader |
46
|
|
|
self._future_minute_reader = future_minute_reader |
47
|
|
|
|
48
|
|
|
def get_previous_value(self, asset, field, dt, data_frequency): |
49
|
|
|
""" |
50
|
|
|
Given an asset and a column and a dt, returns the previous value for |
51
|
|
|
the same asset/column pair. If this data portal is in minute mode, |
52
|
|
|
it's the previous minute value, otherwise it's the previous day's |
53
|
|
|
value. |
54
|
|
|
|
55
|
|
|
Parameters |
56
|
|
|
--------- |
57
|
|
|
asset : Asset |
58
|
|
|
The asset whose data is desired. |
59
|
|
|
|
60
|
|
|
field: string |
61
|
|
|
The desired field of the asset. Valid values are "open", |
62
|
|
|
"open_price", "high", "low", "close", "close_price", "volume", and |
63
|
|
|
"price". |
64
|
|
|
|
65
|
|
|
dt: pd.Timestamp |
66
|
|
|
The timestamp from which to go back in time one slot. |
67
|
|
|
|
68
|
|
|
data_frequency: string |
69
|
|
|
The frequency of the data to query; i.e. whether the data is |
70
|
|
|
'daily' or 'minute' bars |
71
|
|
|
|
72
|
|
|
Returns |
73
|
|
|
------- |
74
|
|
|
The value of the desired field at the desired time. |
75
|
|
|
""" |
76
|
|
|
NotImplementedError |
77
|
|
|
|
78
|
|
|
def get_spot_value(self, asset, field, dt, data_frequency): |
79
|
|
|
""" |
80
|
|
|
Public API method that returns a scalar value representing the value |
81
|
|
|
of the desired asset's field at either the given dt. |
82
|
|
|
|
83
|
|
|
Parameters |
84
|
|
|
--------- |
85
|
|
|
asset : Asset |
86
|
|
|
The asset whose data is desired.gith |
87
|
|
|
|
88
|
|
|
field: string |
89
|
|
|
The desired field of the asset. Valid values are "open", |
90
|
|
|
"open_price", "high", "low", "close", "close_price", "volume", and |
91
|
|
|
"price". |
92
|
|
|
|
93
|
|
|
dt: pd.Timestamp |
94
|
|
|
The timestamp for the desired value. |
95
|
|
|
|
96
|
|
|
data_frequency: string |
97
|
|
|
The frequency of the data to query; i.e. whether the data is |
98
|
|
|
'daily' or 'minute' bars |
99
|
|
|
|
100
|
|
|
Returns |
101
|
|
|
------- |
102
|
|
|
The value of the desired field at the desired time. |
103
|
|
|
""" |
104
|
|
|
NotImplementedError |
105
|
|
|
|
106
|
|
|
def get_history_window(self, assets, end_dt, bar_count, frequency, field, |
107
|
|
|
ffill=True): |
108
|
|
|
""" |
109
|
|
|
Public API method that returns a dataframe containing the requested |
110
|
|
|
history window. Data is fully adjusted. |
111
|
|
|
|
112
|
|
|
Parameters |
113
|
|
|
--------- |
114
|
|
|
assets : list of zipline.data.Asset objects |
115
|
|
|
The assets whose data is desired. |
116
|
|
|
|
117
|
|
|
bar_count: int |
118
|
|
|
The number of bars desired. |
119
|
|
|
|
120
|
|
|
frequency: string |
121
|
|
|
"1d" or "1m" |
122
|
|
|
|
123
|
|
|
field: string |
124
|
|
|
The desired field of the asset. |
125
|
|
|
|
126
|
|
|
ffill: boolean |
127
|
|
|
Forward-fill missing values. Only has effect if field |
128
|
|
|
is 'price'. |
129
|
|
|
|
130
|
|
|
Returns |
131
|
|
|
------- |
132
|
|
|
A dataframe containing the requested data. |
133
|
|
|
""" |
134
|
|
|
NotImplementedError |
135
|
|
|
|
136
|
|
|
def get_splits(self, sids, dt): |
137
|
|
|
""" |
138
|
|
|
Returns any splits for the given sids and the given dt. |
139
|
|
|
|
140
|
|
|
Parameters |
141
|
|
|
---------- |
142
|
|
|
sids : list |
143
|
|
|
Sids for which we want splits. |
144
|
|
|
|
145
|
|
|
dt: pd.Timestamp |
146
|
|
|
The date for which we are checking for splits. Note: this is |
147
|
|
|
expected to be midnight UTC. |
148
|
|
|
|
149
|
|
|
Returns |
150
|
|
|
------- |
151
|
|
|
list: List of splits, where each split is a (sid, ratio) tuple. |
152
|
|
|
""" |
153
|
|
|
NotImplementedError |
154
|
|
|
|
155
|
|
|
def get_stock_dividends(self, sid, trading_days): |
156
|
|
|
""" |
157
|
|
|
Returns all the stock dividends for a specific sid that occur |
158
|
|
|
in the given trading range. |
159
|
|
|
|
160
|
|
|
Parameters |
161
|
|
|
---------- |
162
|
|
|
sid: int |
163
|
|
|
The asset whose stock dividends should be returned. |
164
|
|
|
|
165
|
|
|
trading_days: pd.DatetimeIndex |
166
|
|
|
The trading range. |
167
|
|
|
|
168
|
|
|
Returns |
169
|
|
|
------- |
170
|
|
|
list: A list of objects with all relevant attributes populated. |
171
|
|
|
All timestamp fields are converted to pd.Timestamps. |
172
|
|
|
""" |
173
|
|
|
NotImplementedError |
174
|
|
|
|
175
|
|
|
def get_fetcher_assets(self, day): |
176
|
|
|
""" |
177
|
|
|
Returns a list of assets for the current date, as defined by the |
178
|
|
|
fetcher data. |
179
|
|
|
|
180
|
|
|
Notes |
181
|
|
|
----- |
182
|
|
|
Data is forward-filled. If there is no fetcher data defined for day |
183
|
|
|
N, we use day N-1's data (if available, otherwise we keep going back). |
184
|
|
|
|
185
|
|
|
Returns |
186
|
|
|
------- |
187
|
|
|
list: a list of Asset objects. |
188
|
|
|
""" |
189
|
|
|
NotImplementedError |
190
|
|
|
|