| 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 |  |  |                  sim_params=None, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |                  equity_daily_reader=None, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                  equity_minute_reader=None, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                  future_daily_reader=None, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                  future_minute_reader=None, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                  adjustment_reader=None): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         self._adjustment_reader = adjustment_reader | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         self._equity_daily_reader = equity_daily_reader | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         self._equity_minute_reader = equity_minute_reader | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         self._future_daily_reader = future_daily_reader | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         self._future_minute_reader = future_minute_reader | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     def get_previous_value(self, asset, field, dt, data_frequency): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         Given an asset and a column and a dt, returns the previous value for | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         the same asset/column pair.  If this data portal is in minute mode, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         it's the previous minute value, otherwise it's the previous day's | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         value. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         Parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         --------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         asset : Asset | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             The asset whose data is desired. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         field: string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             The desired field of the asset.  Valid values are "open", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             "open_price", "high", "low", "close", "close_price", "volume", and | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             "price". | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         dt: pd.Timestamp | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             The timestamp from which to go back in time one slot. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         data_frequency: string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             The frequency of the data to query; i.e. whether the data is | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             'daily' or 'minute' bars | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         Returns | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         ------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         The value of the desired field at the desired time. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         NotImplementedError | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |     def get_spot_value(self, asset, field, dt, data_frequency): | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |         """ | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |         Public API method that returns a scalar value representing the value | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |         of the desired asset's field at either the given dt. | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |         Parameters | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |         --------- | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |         asset : Asset | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |             The asset whose data is desired.gith | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |         field: string | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |             The desired field of the asset.  Valid values are "open", | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |             "open_price", "high", "low", "close", "close_price", "volume", and | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |             "price". | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |         dt: pd.Timestamp | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |             The timestamp for the desired value. | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |         data_frequency: string | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |             The frequency of the data to query; i.e. whether the data is | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |             'daily' or 'minute' bars | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |         Returns | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |         ------- | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |         The value of the desired field at the desired time. | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |         """ | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |         NotImplementedError | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     def get_history_window(self, assets, end_dt, bar_count, frequency, field, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                            ffill=True): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         Public API method that returns a dataframe containing the requested | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         history window.  Data is fully adjusted. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         Parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         --------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         assets : list of zipline.data.Asset objects | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             The assets whose data is desired. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         bar_count: int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             The number of bars desired. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         frequency: string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             "1d" or "1m" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         field: string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             The desired field of the asset. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         ffill: boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             Forward-fill missing values. Only has effect if field | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             is 'price'. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         Returns | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         ------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         A dataframe containing the requested data. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         NotImplementedError | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     def get_splits(self, sids, dt): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         Returns any splits for the given sids and the given dt. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         Parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         ---------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         sids : list | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |             Sids for which we want splits. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         dt: pd.Timestamp | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |             The date for which we are checking for splits.  Note: this is | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             expected to be midnight UTC. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         Returns | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         ------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         list: List of splits, where each split is a (sid, ratio) tuple. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         NotImplementedError | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     def get_stock_dividends(self, sid, trading_days): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         Returns all the stock dividends for a specific sid that occur | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         in the given trading range. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         Parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         ---------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         sid: int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |             The asset whose stock dividends should be returned. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         trading_days: pd.DatetimeIndex | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |             The trading range. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |         Returns | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         ------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |         list: A list of objects with all relevant attributes populated. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         All timestamp fields are converted to pd.Timestamps. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |         NotImplementedError | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |     def get_fetcher_assets(self, day): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |         """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |         Returns a list of assets for the current date, as defined by the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |         fetcher data. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |         Notes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |         ----- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |         Data is forward-filled.  If there is no fetcher data defined for day | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |         N, we use day N-1's data (if available, otherwise we keep going back). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |         Returns | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |         ------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |         list: a list of Asset objects. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |         """ | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 190 |  |  |         NotImplementedError | 
            
                                                        
            
                                    
            
            
                | 191 |  |  |  |