| Total Complexity | 159 | 
| Total Lines | 1252 | 
| Duplicated Lines | 0 % | 
Complex classes like zipline.data.DataPortal often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | #  | 
            ||
| 51 | class DataPortal(object):  | 
            ||
| 52 | def __init__(self,  | 
            ||
| 53 | env,  | 
            ||
| 54 | equity_daily_reader=None,  | 
            ||
| 55 | equity_minute_reader=None,  | 
            ||
| 56 | future_daily_reader=None,  | 
            ||
| 57 | future_minute_reader=None,  | 
            ||
| 58 | adjustment_reader=None):  | 
            ||
| 59 | self.env = env  | 
            ||
| 60 | |||
| 61 |         self.views = {} | 
            ||
| 62 | |||
| 63 | self._asset_finder = env.asset_finder  | 
            ||
| 64 | |||
| 65 |         self._carrays = { | 
            ||
| 66 |             'open': {}, | 
            ||
| 67 |             'high': {}, | 
            ||
| 68 |             'low': {}, | 
            ||
| 69 |             'close': {}, | 
            ||
| 70 |             'volume': {}, | 
            ||
| 71 |             'sid': {}, | 
            ||
| 72 |             'dt': {}, | 
            ||
| 73 | }  | 
            ||
| 74 | |||
| 75 | self._adjustment_reader = adjustment_reader  | 
            ||
| 76 | |||
| 77 | # caches of sid -> adjustment list  | 
            ||
| 78 |         self._splits_dict = {} | 
            ||
| 79 |         self._mergers_dict = {} | 
            ||
| 80 |         self._dividends_dict = {} | 
            ||
| 81 | |||
| 82 | # Cache of sid -> the first trading day of an asset, even if that day  | 
            ||
| 83 | # is before 1/2/2002.  | 
            ||
| 84 |         self._asset_start_dates = {} | 
            ||
| 85 |         self._asset_end_dates = {} | 
            ||
| 86 | |||
| 87 | # Handle extra sources, like Fetcher.  | 
            ||
| 88 |         self._augmented_sources_map = {} | 
            ||
| 89 | self._extra_source_df = None  | 
            ||
| 90 | |||
| 91 | self.MINUTE_PRICE_ADJUSTMENT_FACTOR = 0.001  | 
            ||
| 92 | |||
| 93 | self._equity_daily_reader = equity_daily_reader  | 
            ||
| 94 | self._equity_minute_reader = equity_minute_reader  | 
            ||
| 95 | self._future_daily_reader = future_daily_reader  | 
            ||
| 96 | self._future_minute_reader = future_minute_reader  | 
            ||
| 97 | |||
| 98 | def handle_extra_source(self, source_df, sim_params):  | 
            ||
| 99 | """  | 
            ||
| 100 | Extra sources always have a sid column.  | 
            ||
| 101 | |||
| 102 | We expand the given data (by forward filling) to the full range of  | 
            ||
| 103 | the simulation dates, so that lookup is fast during simulation.  | 
            ||
| 104 | """  | 
            ||
| 105 | if source_df is None:  | 
            ||
| 106 | return  | 
            ||
| 107 | |||
| 108 | self._extra_source_df = source_df  | 
            ||
| 109 | |||
| 110 | # source_df's sid column can either consist of assets we know about  | 
            ||
| 111 | # (such as sid(24)) or of assets we don't know about (such as  | 
            ||
| 112 | # palladium).  | 
            ||
| 113 | #  | 
            ||
| 114 | # In both cases, we break up the dataframe into individual dfs  | 
            ||
| 115 | # that only contain a single asset's information. ie, if source_df  | 
            ||
| 116 | # has data for PALLADIUM and GOLD, we split source_df into two  | 
            ||
| 117 | # dataframes, one for each. (same applies if source_df has data for  | 
            ||
| 118 | # AAPL and IBM).  | 
            ||
| 119 | #  | 
            ||
| 120 | # We then take each child df and reindex it to the simulation's date  | 
            ||
| 121 | # range by forward-filling missing values. this makes reads simpler.  | 
            ||
| 122 | #  | 
            ||
| 123 | # Finally, we store the data. For each column, we store a mapping in  | 
            ||
| 124 | # self.augmented_sources_map from the column to a dictionary of  | 
            ||
| 125 | # asset -> df. In other words,  | 
            ||
| 126 | # self.augmented_sources_map['days_to_cover']['AAPL'] gives us the df  | 
            ||
| 127 | # holding that data.  | 
            ||
| 128 | |||
| 129 | if sim_params.emission_rate == "daily":  | 
            ||
| 130 | source_date_index = self.env.days_in_range(  | 
            ||
| 131 | start=sim_params.period_start,  | 
            ||
| 132 | end=sim_params.period_end  | 
            ||
| 133 | )  | 
            ||
| 134 | else:  | 
            ||
| 135 | source_date_index = self.env.minutes_for_days_in_range(  | 
            ||
| 136 | start=sim_params.period_start,  | 
            ||
| 137 | end=sim_params.period_end  | 
            ||
| 138 | )  | 
            ||
| 139 | |||
| 140 | # Break the source_df up into one dataframe per sid. This lets  | 
            ||
| 141 | # us (more easily) calculate accurate start/end dates for each sid,  | 
            ||
| 142 | # de-dup data, and expand the data to fit the backtest start/end date.  | 
            ||
| 143 | grouped_by_sid = source_df.groupby(["sid"])  | 
            ||
| 144 | group_names = grouped_by_sid.groups.keys()  | 
            ||
| 145 |         group_dict = {} | 
            ||
| 146 | for group_name in group_names:  | 
            ||
| 147 | group_dict[group_name] = grouped_by_sid.get_group(group_name)  | 
            ||
| 148 | |||
| 149 | for identifier, df in iteritems(group_dict):  | 
            ||
| 150 | # Before reindexing, save the earliest and latest dates  | 
            ||
| 151 | earliest_date = df.index[0]  | 
            ||
| 152 | latest_date = df.index[-1]  | 
            ||
| 153 | |||
| 154 | # Since we know this df only contains a single sid, we can safely  | 
            ||
| 155 | # de-dupe by the index (dt)  | 
            ||
| 156 | df = df.groupby(level=0).last()  | 
            ||
| 157 | |||
| 158 | # Reindex the dataframe based on the backtest start/end date.  | 
            ||
| 159 | # This makes reads easier during the backtest.  | 
            ||
| 160 | df = df.reindex(index=source_date_index, method='ffill')  | 
            ||
| 161 | |||
| 162 | if not isinstance(identifier, Asset):  | 
            ||
| 163 | # for fake assets we need to store a start/end date  | 
            ||
| 164 | self._asset_start_dates[identifier] = earliest_date  | 
            ||
| 165 | self._asset_end_dates[identifier] = latest_date  | 
            ||
| 166 | |||
| 167 | for col_name in df.columns.difference(['sid']):  | 
            ||
| 168 | if col_name not in self._augmented_sources_map:  | 
            ||
| 169 |                     self._augmented_sources_map[col_name] = {} | 
            ||
| 170 | |||
| 171 | self._augmented_sources_map[col_name][identifier] = df  | 
            ||
| 172 | |||
| 173 | def _open_minute_file(self, field, asset):  | 
            ||
| 174 | sid_str = str(int(asset))  | 
            ||
| 175 | |||
| 176 | try:  | 
            ||
| 177 | carray = self._carrays[field][sid_str]  | 
            ||
| 178 | except KeyError:  | 
            ||
| 179 | carray = self._carrays[field][sid_str] = \  | 
            ||
| 180 | self._get_ctable(asset)[field]  | 
            ||
| 181 | |||
| 182 | return carray  | 
            ||
| 183 | |||
| 184 | def _get_ctable(self, asset):  | 
            ||
| 185 | sid = int(asset)  | 
            ||
| 186 | |||
| 187 | if isinstance(asset, Future):  | 
            ||
| 188 | if self._future_minute_reader.sid_path_func is not None:  | 
            ||
| 189 | path = self._future_minute_reader.sid_path_func(  | 
            ||
| 190 | self._future_minute_reader.rootdir, sid  | 
            ||
| 191 | )  | 
            ||
| 192 | else:  | 
            ||
| 193 |                 path = "{0}/{1}.bcolz".format( | 
            ||
| 194 | self._future_minute_reader.rootdir, sid)  | 
            ||
| 195 | elif isinstance(asset, Equity):  | 
            ||
| 196 | if self._equity_minute_reader.sid_path_func is not None:  | 
            ||
| 197 | path = self._equity_minute_reader.sid_path_func(  | 
            ||
| 198 | self._equity_minute_reader.rootdir, sid  | 
            ||
| 199 | )  | 
            ||
| 200 | else:  | 
            ||
| 201 |                 path = "{0}/{1}.bcolz".format( | 
            ||
| 202 | self._equity_minute_reader.rootdir, sid)  | 
            ||
| 203 | |||
| 204 | else:  | 
            ||
| 205 | # TODO: Figure out if assets should be allowed if neither, and  | 
            ||
| 206 | # why this code path is being hit.  | 
            ||
| 207 | if self._equity_minute_reader.sid_path_func is not None:  | 
            ||
| 208 | path = self._equity_minute_reader.sid_path_func(  | 
            ||
| 209 | self._equity_minute_reader.rootdir, sid  | 
            ||
| 210 | )  | 
            ||
| 211 | else:  | 
            ||
| 212 |                 path = "{0}/{1}.bcolz".format( | 
            ||
| 213 | self._equity_minute_reader.rootdir, sid)  | 
            ||
| 214 | |||
| 215 | return bcolz.open(path, mode='r')  | 
            ||
| 216 | |||
| 217 | def get_last_traded_dt(self, asset, dt, data_frequency):  | 
            ||
| 218 | """  | 
            ||
| 219 | Given an asset and dt, returns the last traded dt from the viewpoint  | 
            ||
| 220 | of the given dt.  | 
            ||
| 221 | |||
| 222 | If there is a trade on the dt, the answer is dt provided.  | 
            ||
| 223 | """  | 
            ||
| 224 | if data_frequency == 'minute':  | 
            ||
| 225 | return self._equity_minute_reader.get_last_traded_dt(asset, dt)  | 
            ||
| 226 | elif data_frequency == 'daily':  | 
            ||
| 227 | return self._equity_daily_reader.get_last_traded_dt(asset, dt)  | 
            ||
| 228 | |||
| 229 | def get_previous_value(self, asset, field, dt, data_frequency):  | 
            ||
| 230 | """  | 
            ||
| 231 | Given an asset and a column and a dt, returns the previous value for  | 
            ||
| 232 | the same asset/column pair. If this data portal is in minute mode,  | 
            ||
| 233 | it's the previous minute value, otherwise it's the previous day's  | 
            ||
| 234 | value.  | 
            ||
| 235 | |||
| 236 | Parameters  | 
            ||
| 237 | ---------  | 
            ||
| 238 | asset : Asset  | 
            ||
| 239 | The asset whose data is desired.  | 
            ||
| 240 | |||
| 241 | field: string  | 
            ||
| 242 | The desired field of the asset. Valid values are "open",  | 
            ||
| 243 | "open_price", "high", "low", "close", "close_price", "volume", and  | 
            ||
| 244 | "price".  | 
            ||
| 245 | |||
| 246 | dt: pd.Timestamp  | 
            ||
| 247 | The timestamp from which to go back in time one slot.  | 
            ||
| 248 | |||
| 249 | data_frequency: string  | 
            ||
| 250 | The frequency of the data to query; i.e. whether the data is  | 
            ||
| 251 | 'daily' or 'minute' bars  | 
            ||
| 252 | |||
| 253 | Returns  | 
            ||
| 254 | -------  | 
            ||
| 255 | The value of the desired field at the desired time.  | 
            ||
| 256 | """  | 
            ||
| 257 | if data_frequency == 'daily':  | 
            ||
| 258 | prev_dt = self.env.previous_trading_day(dt)  | 
            ||
| 259 | elif data_frequency == 'minute':  | 
            ||
| 260 | prev_dt = self.env.previous_market_minute(dt)  | 
            ||
| 261 | |||
| 262 | return self.get_spot_value(asset, field, prev_dt, data_frequency)  | 
            ||
| 263 | |||
| 264 | def _check_extra_sources(self, asset, column, day):  | 
            ||
| 265 | # If we have an extra source with a column called "price", only look  | 
            ||
| 266 | # at it if it's on something like palladium and not AAPL (since our  | 
            ||
| 267 | # own price data always wins when dealing with assets).  | 
            ||
| 268 | look_in_augmented_sources = column in self._augmented_sources_map and \  | 
            ||
| 269 | not (column in BASE_FIELDS and isinstance(asset, Asset))  | 
            ||
| 270 | |||
| 271 | if look_in_augmented_sources:  | 
            ||
| 272 | # we're being asked for a field in an extra source  | 
            ||
| 273 | try:  | 
            ||
| 274 | return self._augmented_sources_map[column][asset].\  | 
            ||
| 275 | loc[day, column]  | 
            ||
| 276 | except:  | 
            ||
| 277 | log.error(  | 
            ||
| 278 |                     "Could not find value for asset={0}, day={1}," | 
            ||
| 279 |                     "column={2}".format( | 
            ||
| 280 | str(asset),  | 
            ||
| 281 | str(day),  | 
            ||
| 282 | str(column)))  | 
            ||
| 283 | |||
| 284 | raise KeyError  | 
            ||
| 285 | |||
| 286 | def get_spot_value(self, asset, field, dt, data_frequency):  | 
            ||
| 287 | """  | 
            ||
| 288 | Public API method that returns a scalar value representing the value  | 
            ||
| 289 | of the desired asset's field at either the given dt.  | 
            ||
| 290 | |||
| 291 | Parameters  | 
            ||
| 292 | ---------  | 
            ||
| 293 | asset : Asset  | 
            ||
| 294 | The asset whose data is desired.gith  | 
            ||
| 295 | |||
| 296 | field: string  | 
            ||
| 297 | The desired field of the asset. Valid values are "open",  | 
            ||
| 298 | "open_price", "high", "low", "close", "close_price", "volume", and  | 
            ||
| 299 | "price".  | 
            ||
| 300 | |||
| 301 | dt: pd.Timestamp  | 
            ||
| 302 | The timestamp for the desired value.  | 
            ||
| 303 | |||
| 304 | data_frequency: string  | 
            ||
| 305 | The frequency of the data to query; i.e. whether the data is  | 
            ||
| 306 | 'daily' or 'minute' bars  | 
            ||
| 307 | |||
| 308 | Returns  | 
            ||
| 309 | -------  | 
            ||
| 310 | The value of the desired field at the desired time.  | 
            ||
| 311 | """  | 
            ||
| 312 | extra_source_val = self._check_extra_sources(  | 
            ||
| 313 | asset,  | 
            ||
| 314 | field,  | 
            ||
| 315 | dt,  | 
            ||
| 316 | )  | 
            ||
| 317 | |||
| 318 | if extra_source_val is not None:  | 
            ||
| 319 | return extra_source_val  | 
            ||
| 320 | |||
| 321 | if field not in BASE_FIELDS:  | 
            ||
| 322 |             raise KeyError("Invalid column: " + str(field)) | 
            ||
| 323 | |||
| 324 | column_to_use = BASE_FIELDS[field]  | 
            ||
| 325 | |||
| 326 | if isinstance(asset, int):  | 
            ||
| 327 | asset = self._asset_finder.retrieve_asset(asset)  | 
            ||
| 328 | |||
| 329 | self._check_is_currently_alive(asset, dt)  | 
            ||
| 330 | |||
| 331 | if data_frequency == "daily":  | 
            ||
| 332 | day_to_use = dt  | 
            ||
| 333 | day_to_use = normalize_date(day_to_use)  | 
            ||
| 334 | return self._get_daily_data(asset, column_to_use, day_to_use)  | 
            ||
| 335 | else:  | 
            ||
| 336 | if isinstance(asset, Future):  | 
            ||
| 337 | return self._get_minute_spot_value_future(  | 
            ||
| 338 | asset, column_to_use, dt)  | 
            ||
| 339 | else:  | 
            ||
| 340 | return self._get_minute_spot_value(  | 
            ||
| 341 | asset, column_to_use, dt)  | 
            ||
| 342 | |||
| 343 | def _get_adjusted_value(self, asset, field, dt,  | 
            ||
| 344 | perspective_dt,  | 
            ||
| 345 | data_frequency):  | 
            ||
| 346 | """  | 
            ||
| 347 | Private method that returns a scalar value representing the value  | 
            ||
| 348 | of the desired asset's field at the given dt with adjustments applied.  | 
            ||
| 349 | |||
| 350 | Parameters  | 
            ||
| 351 | ---------  | 
            ||
| 352 | asset : Asset  | 
            ||
| 353 | The asset whose data is desired.  | 
            ||
| 354 | |||
| 355 | field: string  | 
            ||
| 356 | The desired field of the asset. Valid values are "open",  | 
            ||
| 357 | "open_price", "high", "low", "close", "close_price", "volume", and  | 
            ||
| 358 | "price".  | 
            ||
| 359 | |||
| 360 | dt: pd.Timestamp  | 
            ||
| 361 | The timestamp for the desired value.  | 
            ||
| 362 | |||
| 363 | perspective_dt : pd.Timestamp  | 
            ||
| 364 | The timestamp from which the data is being viewed back from.  | 
            ||
| 365 | |||
| 366 | data_frequency: string  | 
            ||
| 367 | The frequency of the data to query; i.e. whether the data is  | 
            ||
| 368 | 'daily' or 'minute' bars  | 
            ||
| 369 | |||
| 370 | Returns  | 
            ||
| 371 | -------  | 
            ||
| 372 | The value of the desired field at the desired time.  | 
            ||
| 373 | """  | 
            ||
| 374 | if isinstance(asset, int):  | 
            ||
| 375 | asset = self._asset_finder.retrieve_asset(asset)  | 
            ||
| 376 | |||
| 377 | spot_value = self.get_spot_value(asset, field, dt, data_frequency)  | 
            ||
| 378 | |||
| 379 | if isinstance(asset, Equity):  | 
            ||
| 380 | adjs = []  | 
            ||
| 381 | split_adjustments = self._get_adjustment_list(  | 
            ||
| 382 | asset, self._splits_dict, "SPLITS"  | 
            ||
| 383 | )  | 
            ||
| 384 | for adj_dt, adj in split_adjustments:  | 
            ||
| 385 | if adj_dt < dt:  | 
            ||
| 386 | if field != 'volume':  | 
            ||
| 387 | adjs.append(adj)  | 
            ||
| 388 | else:  | 
            ||
| 389 | adjs.append(1.0 / adj)  | 
            ||
| 390 | if adj_dt >= perspective_dt:  | 
            ||
| 391 | break  | 
            ||
| 392 | |||
| 393 | if field != 'volume':  | 
            ||
| 394 | merger_adjustments = self._get_adjustment_list(  | 
            ||
| 395 | asset, self._mergers_dict, "MERGERS"  | 
            ||
| 396 | )  | 
            ||
| 397 | for adj_dt, adj in merger_adjustments:  | 
            ||
| 398 | if adj_dt < dt:  | 
            ||
| 399 | adjs.append(adj)  | 
            ||
| 400 | if adj_dt >= perspective_dt:  | 
            ||
| 401 | break  | 
            ||
| 402 | div_adjustments = self._get_adjustment_list(  | 
            ||
| 403 | asset, self._dividends_dict, "DIVIDENDS",  | 
            ||
| 404 | )  | 
            ||
| 405 | for adj_dt, adj in div_adjustments:  | 
            ||
| 406 | if adj_dt < dt:  | 
            ||
| 407 | adjs.append(adj)  | 
            ||
| 408 | if adj_dt >= perspective_dt:  | 
            ||
| 409 | break  | 
            ||
| 410 | |||
| 411 | ratio = reduce(mul, adjs, 1.0)  | 
            ||
| 412 | |||
| 413 | spot_value *= ratio  | 
            ||
| 414 | |||
| 415 | return spot_value  | 
            ||
| 416 | |||
| 417 | def _get_minute_spot_value_future(self, asset, column, dt):  | 
            ||
| 418 | # Futures bcolz files have 1440 bars per day (24 hours), 7 days a week.  | 
            ||
| 419 | # The file attributes contain the "start_dt" and "last_dt" fields,  | 
            ||
| 420 | # which represent the time period for this bcolz file.  | 
            ||
| 421 | |||
| 422 | # The start_dt is midnight of the first day that this future started  | 
            ||
| 423 | # trading.  | 
            ||
| 424 | |||
| 425 | # figure out the # of minutes between dt and this asset's start_dt  | 
            ||
| 426 | start_date = self._get_asset_start_date(asset)  | 
            ||
| 427 | minute_offset = int((dt - start_date).total_seconds() / 60)  | 
            ||
| 428 | |||
| 429 | if minute_offset < 0:  | 
            ||
| 430 | # asking for a date that is before the asset's start date, no dice  | 
            ||
| 431 | return 0.0  | 
            ||
| 432 | |||
| 433 | # then just index into the bcolz carray at that offset  | 
            ||
| 434 | carray = self._open_minute_file(column, asset)  | 
            ||
| 435 | result = carray[minute_offset]  | 
            ||
| 436 | |||
| 437 | # if there's missing data, go backwards until we run out of file  | 
            ||
| 438 | while result == 0 and minute_offset > 0:  | 
            ||
| 439 | minute_offset -= 1  | 
            ||
| 440 | result = carray[minute_offset]  | 
            ||
| 441 | |||
| 442 | if column != 'volume':  | 
            ||
| 443 | return result * self.MINUTE_PRICE_ADJUSTMENT_FACTOR  | 
            ||
| 444 | else:  | 
            ||
| 445 | return result  | 
            ||
| 446 | |||
| 447 | def _get_minute_spot_value(self, asset, column, dt):  | 
            ||
| 448 | # if dt is before the first market minute, minute_index  | 
            ||
| 449 | # will be 0. if it's after the last market minute, it'll  | 
            ||
| 450 | # be len(minutes_for_day)  | 
            ||
| 451 | minute_offset_to_use = \  | 
            ||
| 452 | self._equity_minute_reader._find_position_of_minute(dt)  | 
            ||
| 453 | |||
| 454 | carray = self._equity_minute_reader._open_minute_file(column, asset)  | 
            ||
| 455 | result = carray[minute_offset_to_use]  | 
            ||
| 456 | |||
| 457 | if result == 0:  | 
            ||
| 458 | # if the given minute doesn't have data, we need to seek  | 
            ||
| 459 | # backwards until we find data. This makes the data  | 
            ||
| 460 | # forward-filled.  | 
            ||
| 461 | |||
| 462 | # get this asset's start date, so that we don't look before it.  | 
            ||
| 463 | start_date = self._get_asset_start_date(asset)  | 
            ||
| 464 | start_date_idx = self._equity_minute_reader.trading_days.\  | 
            ||
| 465 | searchsorted(start_date)  | 
            ||
| 466 | start_day_offset = start_date_idx * 390  | 
            ||
| 467 | |||
| 468 | original_start = minute_offset_to_use  | 
            ||
| 469 | |||
| 470 | while result == 0 and minute_offset_to_use > start_day_offset:  | 
            ||
| 471 | minute_offset_to_use -= 1  | 
            ||
| 472 | result = carray[minute_offset_to_use]  | 
            ||
| 473 | |||
| 474 | # once we've found data, we need to check whether it needs  | 
            ||
| 475 | # to be adjusted.  | 
            ||
| 476 | if result != 0:  | 
            ||
| 477 | minutes = self.env.market_minute_window(  | 
            ||
| 478 | start=dt,  | 
            ||
| 479 | count=(original_start - minute_offset_to_use + 1),  | 
            ||
| 480 | step=-1  | 
            ||
| 481 | ).order()  | 
            ||
| 482 | |||
| 483 | # only need to check for adjustments if we've gone back  | 
            ||
| 484 | # far enough to cross the day boundary.  | 
            ||
| 485 | if minutes[0].date() != minutes[-1].date():  | 
            ||
| 486 | # create a np array of size minutes, fill it all with  | 
            ||
| 487 | # the same value. and adjust the array.  | 
            ||
| 488 | arr = np.array([result] * len(minutes),  | 
            ||
| 489 | dtype=np.float64)  | 
            ||
| 490 | self._apply_all_adjustments(  | 
            ||
| 491 | data=arr,  | 
            ||
| 492 | asset=asset,  | 
            ||
| 493 | dts=minutes,  | 
            ||
| 494 | field=column  | 
            ||
| 495 | )  | 
            ||
| 496 | |||
| 497 | # The first value of the adjusted array is the value  | 
            ||
| 498 | # we want.  | 
            ||
| 499 | result = arr[0]  | 
            ||
| 500 | |||
| 501 | if column != 'volume':  | 
            ||
| 502 | return result * self.MINUTE_PRICE_ADJUSTMENT_FACTOR  | 
            ||
| 503 | else:  | 
            ||
| 504 | return result  | 
            ||
| 505 | |||
| 506 | def _get_daily_data(self, asset, column, dt):  | 
            ||
| 507 | while True:  | 
            ||
| 508 | try:  | 
            ||
| 509 | value = self._equity_daily_reader.spot_price(asset, dt, column)  | 
            ||
| 510 | if value != -1:  | 
            ||
| 511 | return value  | 
            ||
| 512 | else:  | 
            ||
| 513 | dt -= tradingcalendar.trading_day  | 
            ||
| 514 | except NoDataOnDate:  | 
            ||
| 515 | return 0  | 
            ||
| 516 | |||
| 517 | def _get_history_daily_window(self, assets, end_dt, bar_count,  | 
            ||
| 518 | field_to_use):  | 
            ||
| 519 | """  | 
            ||
| 520 | Internal method that returns a dataframe containing history bars  | 
            ||
| 521 | of daily frequency for the given sids.  | 
            ||
| 522 | """  | 
            ||
| 523 | day_idx = tradingcalendar.trading_days.searchsorted(end_dt.date())  | 
            ||
| 524 | days_for_window = tradingcalendar.trading_days[  | 
            ||
| 525 | (day_idx - bar_count + 1):(day_idx + 1)]  | 
            ||
| 526 | |||
| 527 | if len(assets) == 0:  | 
            ||
| 528 | return pd.DataFrame(None,  | 
            ||
| 529 | index=days_for_window,  | 
            ||
| 530 | columns=None)  | 
            ||
| 531 | |||
| 532 | data = []  | 
            ||
| 533 | |||
| 534 | for asset in assets:  | 
            ||
| 535 | if isinstance(asset, Future):  | 
            ||
| 536 | data.append(self._get_history_daily_window_future(  | 
            ||
| 537 | asset, days_for_window, end_dt, field_to_use  | 
            ||
| 538 | ))  | 
            ||
| 539 | else:  | 
            ||
| 540 | data.append(self._get_history_daily_window_equity(  | 
            ||
| 541 | asset, days_for_window, end_dt, field_to_use  | 
            ||
| 542 | ))  | 
            ||
| 543 | |||
| 544 | return pd.DataFrame(  | 
            ||
| 545 | np.array(data).T,  | 
            ||
| 546 | index=days_for_window,  | 
            ||
| 547 | columns=assets  | 
            ||
| 548 | )  | 
            ||
| 549 | |||
| 550 | def _get_history_daily_window_future(self, asset, days_for_window,  | 
            ||
| 551 | end_dt, column):  | 
            ||
| 552 | # Since we don't have daily bcolz files for futures (yet), use minute  | 
            ||
| 553 | # bars to calculate the daily values.  | 
            ||
| 554 | data = []  | 
            ||
| 555 | data_groups = []  | 
            ||
| 556 | |||
| 557 | # get all the minutes for the days NOT including today  | 
            ||
| 558 | for day in days_for_window[:-1]:  | 
            ||
| 559 | minutes = self.env.market_minutes_for_day(day)  | 
            ||
| 560 | |||
| 561 | values_for_day = np.zeros(len(minutes), dtype=np.float64)  | 
            ||
| 562 | |||
| 563 | for idx, minute in enumerate(minutes):  | 
            ||
| 564 | minute_val = self._get_minute_spot_value_future(  | 
            ||
| 565 | asset, column, minute  | 
            ||
| 566 | )  | 
            ||
| 567 | |||
| 568 | values_for_day[idx] = minute_val  | 
            ||
| 569 | |||
| 570 | data_groups.append(values_for_day)  | 
            ||
| 571 | |||
| 572 | # get the minutes for today  | 
            ||
| 573 | last_day_minutes = pd.date_range(  | 
            ||
| 574 | start=self.env.get_open_and_close(end_dt)[0],  | 
            ||
| 575 | end=end_dt,  | 
            ||
| 576 | freq="T"  | 
            ||
| 577 | )  | 
            ||
| 578 | |||
| 579 | values_for_last_day = np.zeros(len(last_day_minutes), dtype=np.float64)  | 
            ||
| 580 | |||
| 581 | for idx, minute in enumerate(last_day_minutes):  | 
            ||
| 582 | minute_val = self._get_minute_spot_value_future(  | 
            ||
| 583 | asset, column, minute  | 
            ||
| 584 | )  | 
            ||
| 585 | |||
| 586 | values_for_last_day[idx] = minute_val  | 
            ||
| 587 | |||
| 588 | data_groups.append(values_for_last_day)  | 
            ||
| 589 | |||
| 590 | for group in data_groups:  | 
            ||
| 591 | if len(group) == 0:  | 
            ||
| 592 | continue  | 
            ||
| 593 | |||
| 594 | if column == 'volume':  | 
            ||
| 595 | data.append(np.sum(group))  | 
            ||
| 596 | elif column == 'open':  | 
            ||
| 597 | data.append(group[0])  | 
            ||
| 598 | elif column == 'close':  | 
            ||
| 599 | data.append(group[-1])  | 
            ||
| 600 | elif column == 'high':  | 
            ||
| 601 | data.append(np.amax(group))  | 
            ||
| 602 | elif column == 'low':  | 
            ||
| 603 | data.append(np.amin(group))  | 
            ||
| 604 | |||
| 605 | return data  | 
            ||
| 606 | |||
| 607 | def _get_history_daily_window_equity(self, asset, days_for_window,  | 
            ||
| 608 | end_dt, field_to_use):  | 
            ||
| 609 | sid = int(asset)  | 
            ||
| 610 | ends_at_midnight = end_dt.hour == 0 and end_dt.minute == 0  | 
            ||
| 611 | |||
| 612 | # get the start and end dates for this sid  | 
            ||
| 613 | end_date = self._get_asset_end_date(asset)  | 
            ||
| 614 | |||
| 615 | if ends_at_midnight or (days_for_window[-1] > end_date):  | 
            ||
| 616 | # two cases where we use daily data for the whole range:  | 
            ||
| 617 | # 1) the history window ends at midnight utc.  | 
            ||
| 618 | # 2) the last desired day of the window is after the  | 
            ||
| 619 | # last trading day, use daily data for the whole range.  | 
            ||
| 620 | return self._get_daily_window_for_sid(  | 
            ||
| 621 | asset,  | 
            ||
| 622 | field_to_use,  | 
            ||
| 623 | days_for_window,  | 
            ||
| 624 | extra_slot=False  | 
            ||
| 625 | )  | 
            ||
| 626 | else:  | 
            ||
| 627 | # for the last day of the desired window, use minute  | 
            ||
| 628 | # data and aggregate it.  | 
            ||
| 629 | all_minutes_for_day = self.env.market_minutes_for_day(  | 
            ||
| 630 | pd.Timestamp(end_dt.date()))  | 
            ||
| 631 | |||
| 632 | last_minute_idx = all_minutes_for_day.searchsorted(end_dt)  | 
            ||
| 633 | |||
| 634 | # these are the minutes for the partial day  | 
            ||
| 635 | minutes_for_partial_day =\  | 
            ||
| 636 | all_minutes_for_day[0:(last_minute_idx + 1)]  | 
            ||
| 637 | |||
| 638 | daily_data = self._get_daily_window_for_sid(  | 
            ||
| 639 | sid,  | 
            ||
| 640 | field_to_use,  | 
            ||
| 641 | days_for_window[0:-1]  | 
            ||
| 642 | )  | 
            ||
| 643 | |||
| 644 | minute_data = self._get_minute_window_for_equity(  | 
            ||
| 645 | sid,  | 
            ||
| 646 | field_to_use,  | 
            ||
| 647 | minutes_for_partial_day  | 
            ||
| 648 | )  | 
            ||
| 649 | |||
| 650 | if field_to_use == 'volume':  | 
            ||
| 651 | minute_value = np.sum(minute_data)  | 
            ||
| 652 | elif field_to_use == 'open':  | 
            ||
| 653 | minute_value = minute_data[0]  | 
            ||
| 654 | elif field_to_use == 'close':  | 
            ||
| 655 | minute_value = minute_data[-1]  | 
            ||
| 656 | elif field_to_use == 'high':  | 
            ||
| 657 | minute_value = np.amax(minute_data)  | 
            ||
| 658 | elif field_to_use == 'low':  | 
            ||
| 659 | minute_value = np.amin(minute_data)  | 
            ||
| 660 | |||
| 661 | # append the partial day.  | 
            ||
| 662 | daily_data[-1] = minute_value  | 
            ||
| 663 | |||
| 664 | return daily_data  | 
            ||
| 665 | |||
| 666 | def _get_history_minute_window(self, assets, end_dt, bar_count,  | 
            ||
| 667 | field_to_use):  | 
            ||
| 668 | """  | 
            ||
| 669 | Internal method that returns a dataframe containing history bars  | 
            ||
| 670 | of minute frequency for the given sids.  | 
            ||
| 671 | """  | 
            ||
| 672 | # get all the minutes for this window  | 
            ||
| 673 | minutes_for_window = self.env.market_minute_window(  | 
            ||
| 674 | end_dt, bar_count, step=-1)[::-1]  | 
            ||
| 675 | |||
| 676 | first_trading_day = self._equity_minute_reader.first_trading_day  | 
            ||
| 677 | |||
| 678 | # but then cut it down to only the minutes after  | 
            ||
| 679 | # the first trading day.  | 
            ||
| 680 | modified_minutes_for_window = minutes_for_window[  | 
            ||
| 681 | minutes_for_window.slice_indexer(first_trading_day)]  | 
            ||
| 682 | |||
| 683 | modified_minutes_length = len(modified_minutes_for_window)  | 
            ||
| 684 | |||
| 685 | if modified_minutes_length == 0:  | 
            ||
| 686 |             raise ValueError("Cannot calculate history window that ends" | 
            ||
| 687 | "before 2002-01-02 14:31 UTC!")  | 
            ||
| 688 | |||
| 689 | data = []  | 
            ||
| 690 | bars_to_prepend = 0  | 
            ||
| 691 | nans_to_prepend = None  | 
            ||
| 692 | |||
| 693 | if modified_minutes_length < bar_count:  | 
            ||
| 694 | first_trading_date = first_trading_day.date()  | 
            ||
| 695 | if modified_minutes_for_window[0].date() == first_trading_date:  | 
            ||
| 696 | # the beginning of the window goes before our global trading  | 
            ||
| 697 | # start date  | 
            ||
| 698 | bars_to_prepend = bar_count - modified_minutes_length  | 
            ||
| 699 | nans_to_prepend = np.repeat(np.nan, bars_to_prepend)  | 
            ||
| 700 | |||
| 701 | if len(assets) == 0:  | 
            ||
| 702 | return pd.DataFrame(  | 
            ||
| 703 | None,  | 
            ||
| 704 | index=modified_minutes_for_window,  | 
            ||
| 705 | columns=None  | 
            ||
| 706 | )  | 
            ||
| 707 | |||
| 708 | for asset in assets:  | 
            ||
| 709 | asset_minute_data = self._get_minute_window_for_asset(  | 
            ||
| 710 | asset,  | 
            ||
| 711 | field_to_use,  | 
            ||
| 712 | modified_minutes_for_window  | 
            ||
| 713 | )  | 
            ||
| 714 | |||
| 715 | if bars_to_prepend != 0:  | 
            ||
| 716 | asset_minute_data = np.insert(asset_minute_data, 0,  | 
            ||
| 717 | nans_to_prepend)  | 
            ||
| 718 | |||
| 719 | data.append(asset_minute_data)  | 
            ||
| 720 | |||
| 721 | return pd.DataFrame(  | 
            ||
| 722 | np.array(data).T,  | 
            ||
| 723 | index=minutes_for_window,  | 
            ||
| 724 | columns=assets  | 
            ||
| 725 | )  | 
            ||
| 726 | |||
| 727 | def get_history_window(self, assets, end_dt, bar_count, frequency, field,  | 
            ||
| 728 | ffill=True):  | 
            ||
| 729 | """  | 
            ||
| 730 | Public API method that returns a dataframe containing the requested  | 
            ||
| 731 | history window. Data is fully adjusted.  | 
            ||
| 732 | |||
| 733 | Parameters  | 
            ||
| 734 | ---------  | 
            ||
| 735 | assets : list of zipline.data.Asset objects  | 
            ||
| 736 | The assets whose data is desired.  | 
            ||
| 737 | |||
| 738 | bar_count: int  | 
            ||
| 739 | The number of bars desired.  | 
            ||
| 740 | |||
| 741 | frequency: string  | 
            ||
| 742 | "1d" or "1m"  | 
            ||
| 743 | |||
| 744 | field: string  | 
            ||
| 745 | The desired field of the asset.  | 
            ||
| 746 | |||
| 747 | ffill: boolean  | 
            ||
| 748 | Forward-fill missing values. Only has effect if field  | 
            ||
| 749 | is 'price'.  | 
            ||
| 750 | |||
| 751 | Returns  | 
            ||
| 752 | -------  | 
            ||
| 753 | A dataframe containing the requested data.  | 
            ||
| 754 | """  | 
            ||
| 755 | try:  | 
            ||
| 756 | field_to_use = BASE_FIELDS[field]  | 
            ||
| 757 | except KeyError:  | 
            ||
| 758 |             raise ValueError("Invalid history field: " + str(field)) | 
            ||
| 759 | |||
| 760 | # sanity check in case sids were passed in  | 
            ||
| 761 | assets = np.array([  | 
            ||
| 762 | (self.env.asset_finder.retrieve_asset(asset) if  | 
            ||
| 763 | isinstance(asset, int) else asset) for asset in assets])  | 
            ||
| 764 | |||
| 765 | if frequency == "1d":  | 
            ||
| 766 | df = self._get_history_daily_window(assets, end_dt, bar_count,  | 
            ||
| 767 | field_to_use)  | 
            ||
| 768 | elif frequency == "1m":  | 
            ||
| 769 | df = self._get_history_minute_window(assets, end_dt, bar_count,  | 
            ||
| 770 | field_to_use)  | 
            ||
| 771 | else:  | 
            ||
| 772 |             raise ValueError("Invalid frequency: {0}".format(frequency)) | 
            ||
| 773 | |||
| 774 | # forward-fill if needed  | 
            ||
| 775 | if field == "price" and ffill:  | 
            ||
| 776 | assets_with_nan_index = np.where(pd.isnull(df.iloc[0, :]))[0]  | 
            ||
| 777 | assets_with_leading_nan = assets[assets_with_nan_index]  | 
            ||
| 778 | |||
| 779 | if frequency == "1m":  | 
            ||
| 780 | data_frequency = 'minute'  | 
            ||
| 781 | elif frequency == "1d":  | 
            ||
| 782 | data_frequency = 'daily'  | 
            ||
| 783 | else:  | 
            ||
| 784 | raise Exception(  | 
            ||
| 785 | "Only 1d and 1m are supported for forward-filling.")  | 
            ||
| 786 | |||
| 787 | dt_to_fill = df.index[0]  | 
            ||
| 788 | |||
| 789 | perspective_dt = df.index[-1]  | 
            ||
| 790 | assets_with_leading_nan = np.where(pd.isnull(df.iloc[0]))[0]  | 
            ||
| 791 | for missing_loc in assets_with_leading_nan:  | 
            ||
| 792 | asset = assets[missing_loc]  | 
            ||
| 793 | previous_dt = self.get_last_traded_dt(  | 
            ||
| 794 | asset, dt_to_fill, data_frequency)  | 
            ||
| 795 | previous_value = self._get_adjusted_value(  | 
            ||
| 796 | asset,  | 
            ||
| 797 | field,  | 
            ||
| 798 | previous_dt,  | 
            ||
| 799 | perspective_dt,  | 
            ||
| 800 | data_frequency,  | 
            ||
| 801 | )  | 
            ||
| 802 | df.iloc[0, missing_loc] = previous_value  | 
            ||
| 803 | |||
| 804 | df.fillna(method='ffill', inplace=True)  | 
            ||
| 805 | |||
| 806 | return df  | 
            ||
| 807 | |||
| 808 | def _get_minute_window_for_asset(self, asset, field, minutes_for_window):  | 
            ||
| 809 | """  | 
            ||
| 810 | Internal method that gets a window of adjusted minute data for an asset  | 
            ||
| 811 | and specified date range. Used to support the history API method for  | 
            ||
| 812 | minute bars.  | 
            ||
| 813 | |||
| 814 | Missing bars are filled with NaN.  | 
            ||
| 815 | |||
| 816 | Parameters  | 
            ||
| 817 | ----------  | 
            ||
| 818 | asset : Asset  | 
            ||
| 819 | The asset whose data is desired.  | 
            ||
| 820 | |||
| 821 | field: string  | 
            ||
| 822 | The specific field to return. "open", "high", "close_price", etc.  | 
            ||
| 823 | |||
| 824 | minutes_for_window: pd.DateTimeIndex  | 
            ||
| 825 | The list of minutes representing the desired window. Each minute  | 
            ||
| 826 | is a pd.Timestamp.  | 
            ||
| 827 | |||
| 828 | Returns  | 
            ||
| 829 | -------  | 
            ||
| 830 | A numpy array with requested values.  | 
            ||
| 831 | """  | 
            ||
| 832 | if isinstance(asset, int):  | 
            ||
| 833 | asset = self.env.asset_finder.retrieve_asset(asset)  | 
            ||
| 834 | |||
| 835 | if isinstance(asset, Future):  | 
            ||
| 836 | return self._get_minute_window_for_future(asset, field,  | 
            ||
| 837 | minutes_for_window)  | 
            ||
| 838 | else:  | 
            ||
| 839 | return self._get_minute_window_for_equity(asset, field,  | 
            ||
| 840 | minutes_for_window)  | 
            ||
| 841 | |||
| 842 | def _get_minute_window_for_future(self, asset, field, minutes_for_window):  | 
            ||
| 843 | # THIS IS TEMPORARY. For now, we are only exposing futures within  | 
            ||
| 844 | # equity trading hours (9:30 am to 4pm, Eastern). The easiest way to  | 
            ||
| 845 | # do this is to simply do a spot lookup for each desired minute.  | 
            ||
| 846 | return_data = np.zeros(len(minutes_for_window), dtype=np.float64)  | 
            ||
| 847 | for idx, minute in enumerate(minutes_for_window):  | 
            ||
| 848 | return_data[idx] = \  | 
            ||
| 849 | self._get_minute_spot_value_future(asset, field, minute)  | 
            ||
| 850 | |||
| 851 | # Note: an improvement could be to find the consecutive runs within  | 
            ||
| 852 | # minutes_for_window, and use them to read the underlying ctable  | 
            ||
| 853 | # more efficiently.  | 
            ||
| 854 | |||
| 855 | # Once futures are on 24-hour clock, then we can just grab all the  | 
            ||
| 856 | # requested minutes in one shot from the ctable.  | 
            ||
| 857 | |||
| 858 | # no adjustments for futures, yay.  | 
            ||
| 859 | return return_data  | 
            ||
| 860 | |||
| 861 | def _get_minute_window_for_equity(self, asset, field, minutes_for_window):  | 
            ||
| 862 | # each sid's minutes are stored in a bcolz file  | 
            ||
| 863 | # the bcolz file has 390 bars per day, starting at 1/2/2002, regardless  | 
            ||
| 864 | # of when the asset started trading and regardless of half days.  | 
            ||
| 865 | # for a half day, the second half is filled with zeroes.  | 
            ||
| 866 | |||
| 867 | # find the position of start_dt in the entire timeline, go back  | 
            ||
| 868 | # bar_count bars, and that's the unadjusted data  | 
            ||
| 869 | raw_data = self._equity_minute_reader._open_minute_file(field, asset)  | 
            ||
| 870 | |||
| 871 | try:  | 
            ||
| 872 | start_idx = self._equity_minute_reader._find_position_of_minute(  | 
            ||
| 873 | minutes_for_window[0])  | 
            ||
| 874 | except KeyError:  | 
            ||
| 875 | start_idx = 0  | 
            ||
| 876 | |||
| 877 | try:  | 
            ||
| 878 | end_idx = self._equity_minute_reader._find_position_of_minute(  | 
            ||
| 879 | minutes_for_window[-1]) + 1  | 
            ||
| 880 | except KeyError:  | 
            ||
| 881 | end_idx = 0  | 
            ||
| 882 | |||
| 883 | if end_idx == 0:  | 
            ||
| 884 | # No data to return for minute window.  | 
            ||
| 885 | return np.full(len(minutes_for_window), np.nan)  | 
            ||
| 886 | |||
| 887 | return_data = np.zeros(len(minutes_for_window), dtype=np.float64)  | 
            ||
| 888 | |||
| 889 | data_to_copy = raw_data[start_idx:end_idx]  | 
            ||
| 890 | |||
| 891 | num_minutes = len(minutes_for_window)  | 
            ||
| 892 | |||
| 893 | # data_to_copy contains all the zeros (from 1pm to 4pm of an early  | 
            ||
| 894 | # close). num_minutes is the number of actual trading minutes. if  | 
            ||
| 895 | # these two have different lengths, that means that we need to trim  | 
            ||
| 896 | # away data due to early closes.  | 
            ||
| 897 | if len(data_to_copy) != num_minutes:  | 
            ||
| 898 | # get a copy of the minutes in Eastern time, since we depend on  | 
            ||
| 899 | # an early close being at 1pm Eastern.  | 
            ||
| 900 |             eastern_minutes = minutes_for_window.tz_convert("US/Eastern") | 
            ||
| 901 | |||
| 902 | # accumulate a list of indices of the last minute of an early  | 
            ||
| 903 | # close day. For example, if data_to_copy starts at 12:55 pm, and  | 
            ||
| 904 | # there are five minutes of real data before 180 zeroes, we would  | 
            ||
| 905 | # put 5 into last_minute_idx_of_early_close_day, because the fifth  | 
            ||
| 906 | # minute is the last "real" minute of the day.  | 
            ||
| 907 | last_minute_idx_of_early_close_day = []  | 
            ||
| 908 | for minute_idx, minute_dt in enumerate(eastern_minutes):  | 
            ||
| 909 | if minute_idx == (num_minutes - 1):  | 
            ||
| 910 | break  | 
            ||
| 911 | |||
| 912 | if minute_dt.hour == 13 and minute_dt.minute == 0:  | 
            ||
| 913 | next_minute = eastern_minutes[minute_idx + 1]  | 
            ||
| 914 | if next_minute.hour != 13:  | 
            ||
| 915 | # minute_dt is the last minute of an early close day  | 
            ||
| 916 | last_minute_idx_of_early_close_day.append(minute_idx)  | 
            ||
| 917 | |||
| 918 | # spin through the list of early close markers, and use them to  | 
            ||
| 919 | # chop off 180 minutes at a time from data_to_copy.  | 
            ||
| 920 | for idx, early_close_minute_idx in \  | 
            ||
| 921 | enumerate(last_minute_idx_of_early_close_day):  | 
            ||
| 922 | early_close_minute_idx -= (180 * idx)  | 
            ||
| 923 | data_to_copy = np.delete(  | 
            ||
| 924 | data_to_copy,  | 
            ||
| 925 | range(  | 
            ||
| 926 | early_close_minute_idx + 1,  | 
            ||
| 927 | early_close_minute_idx + 181  | 
            ||
| 928 | )  | 
            ||
| 929 | )  | 
            ||
| 930 | |||
| 931 | return_data[0:len(data_to_copy)] = data_to_copy  | 
            ||
| 932 | |||
| 933 | self._apply_all_adjustments(  | 
            ||
| 934 | return_data,  | 
            ||
| 935 | asset,  | 
            ||
| 936 | minutes_for_window,  | 
            ||
| 937 | field,  | 
            ||
| 938 | self.MINUTE_PRICE_ADJUSTMENT_FACTOR  | 
            ||
| 939 | )  | 
            ||
| 940 | |||
| 941 | return return_data  | 
            ||
| 942 | |||
| 943 | def _apply_all_adjustments(self, data, asset, dts, field,  | 
            ||
| 944 | price_adj_factor=1.0):  | 
            ||
| 945 | """  | 
            ||
| 946 | Internal method that applies all the necessary adjustments on the  | 
            ||
| 947 | given data array.  | 
            ||
| 948 | |||
| 949 | The adjustments are:  | 
            ||
| 950 | - splits  | 
            ||
| 951 | - if field != "volume":  | 
            ||
| 952 | - mergers  | 
            ||
| 953 | - dividends  | 
            ||
| 954 | - * 0.001  | 
            ||
| 955 | - any zero fields replaced with NaN  | 
            ||
| 956 | - all values rounded to 3 digits after the decimal point.  | 
            ||
| 957 | |||
| 958 | Parameters  | 
            ||
| 959 | ----------  | 
            ||
| 960 | data : np.array  | 
            ||
| 961 | The data to be adjusted.  | 
            ||
| 962 | |||
| 963 | asset: Asset  | 
            ||
| 964 | The asset whose data is being adjusted.  | 
            ||
| 965 | |||
| 966 | dts: pd.DateTimeIndex  | 
            ||
| 967 | The list of minutes or days representing the desired window.  | 
            ||
| 968 | |||
| 969 | field: string  | 
            ||
| 970 | The field whose values are in the data array.  | 
            ||
| 971 | |||
| 972 | price_adj_factor: float  | 
            ||
| 973 | Factor with which to adjust OHLC values.  | 
            ||
| 974 | Returns  | 
            ||
| 975 | -------  | 
            ||
| 976 | None. The data array is modified in place.  | 
            ||
| 977 | """  | 
            ||
| 978 | self._apply_adjustments_to_window(  | 
            ||
| 979 | self._get_adjustment_list(  | 
            ||
| 980 | asset, self._splits_dict, "SPLITS"  | 
            ||
| 981 | ),  | 
            ||
| 982 | data,  | 
            ||
| 983 | dts,  | 
            ||
| 984 | field != 'volume'  | 
            ||
| 985 | )  | 
            ||
| 986 | |||
| 987 | if field != 'volume':  | 
            ||
| 988 | self._apply_adjustments_to_window(  | 
            ||
| 989 | self._get_adjustment_list(  | 
            ||
| 990 | asset, self._mergers_dict, "MERGERS"  | 
            ||
| 991 | ),  | 
            ||
| 992 | data,  | 
            ||
| 993 | dts,  | 
            ||
| 994 | True  | 
            ||
| 995 | )  | 
            ||
| 996 | |||
| 997 | self._apply_adjustments_to_window(  | 
            ||
| 998 | self._get_adjustment_list(  | 
            ||
| 999 | asset, self._dividends_dict, "DIVIDENDS"  | 
            ||
| 1000 | ),  | 
            ||
| 1001 | data,  | 
            ||
| 1002 | dts,  | 
            ||
| 1003 | True  | 
            ||
| 1004 | )  | 
            ||
| 1005 | |||
| 1006 | data *= price_adj_factor  | 
            ||
| 1007 | |||
| 1008 | # if anything is zero, it's a missing bar, so replace it with NaN.  | 
            ||
| 1009 | # we only want to do this for non-volume fields, because a missing  | 
            ||
| 1010 | # volume should be 0.  | 
            ||
| 1011 | data[data == 0] = np.NaN  | 
            ||
| 1012 | |||
| 1013 | np.around(data, 3, out=data)  | 
            ||
| 1014 | |||
| 1015 | def _get_daily_window_for_sid(self, asset, field, days_in_window,  | 
            ||
| 1016 | extra_slot=True):  | 
            ||
| 1017 | """  | 
            ||
| 1018 | Internal method that gets a window of adjusted daily data for a sid  | 
            ||
| 1019 | and specified date range. Used to support the history API method for  | 
            ||
| 1020 | daily bars.  | 
            ||
| 1021 | |||
| 1022 | Parameters  | 
            ||
| 1023 | ----------  | 
            ||
| 1024 | asset : Asset  | 
            ||
| 1025 | The asset whose data is desired.  | 
            ||
| 1026 | |||
| 1027 | start_dt: pandas.Timestamp  | 
            ||
| 1028 | The start of the desired window of data.  | 
            ||
| 1029 | |||
| 1030 | bar_count: int  | 
            ||
| 1031 | The number of days of data to return.  | 
            ||
| 1032 | |||
| 1033 | field: string  | 
            ||
| 1034 | The specific field to return. "open", "high", "close_price", etc.  | 
            ||
| 1035 | |||
| 1036 | extra_slot: boolean  | 
            ||
| 1037 | Whether to allocate an extra slot in the returned numpy array.  | 
            ||
| 1038 | This extra slot will hold the data for the last partial day. It's  | 
            ||
| 1039 | much better to create it here than to create a copy of the array  | 
            ||
| 1040 | later just to add a slot.  | 
            ||
| 1041 | |||
| 1042 | Returns  | 
            ||
| 1043 | -------  | 
            ||
| 1044 | A numpy array with requested values. Any missing slots filled with  | 
            ||
| 1045 | nan.  | 
            ||
| 1046 | |||
| 1047 | """  | 
            ||
| 1048 | bar_count = len(days_in_window)  | 
            ||
| 1049 | # create an np.array of size bar_count  | 
            ||
| 1050 | if extra_slot:  | 
            ||
| 1051 | return_array = np.zeros((bar_count + 1,))  | 
            ||
| 1052 | else:  | 
            ||
| 1053 | return_array = np.zeros((bar_count,))  | 
            ||
| 1054 | |||
| 1055 | return_array[:] = np.NAN  | 
            ||
| 1056 | |||
| 1057 | start_date = self._get_asset_start_date(asset)  | 
            ||
| 1058 | end_date = self._get_asset_end_date(asset)  | 
            ||
| 1059 | day_slice = days_in_window.slice_indexer(start_date, end_date)  | 
            ||
| 1060 | active_days = days_in_window[day_slice]  | 
            ||
| 1061 | |||
| 1062 | if active_days.shape[0]:  | 
            ||
| 1063 | data = self._equity_daily_reader.history_window(field,  | 
            ||
| 1064 | active_days[0],  | 
            ||
| 1065 | active_days[-1],  | 
            ||
| 1066 | asset)  | 
            ||
| 1067 | return_array[day_slice] = data  | 
            ||
| 1068 | self._apply_all_adjustments(  | 
            ||
| 1069 | return_array,  | 
            ||
| 1070 | asset,  | 
            ||
| 1071 | active_days,  | 
            ||
| 1072 | field,  | 
            ||
| 1073 | )  | 
            ||
| 1074 | |||
| 1075 | return return_array  | 
            ||
| 1076 | |||
| 1077 | @staticmethod  | 
            ||
| 1078 | def _apply_adjustments_to_window(adjustments_list, window_data,  | 
            ||
| 1079 | dts_in_window, multiply):  | 
            ||
| 1080 | if len(adjustments_list) == 0:  | 
            ||
| 1081 | return  | 
            ||
| 1082 | |||
| 1083 | # advance idx to the correct spot in the adjustments list, based on  | 
            ||
| 1084 | # when the window starts  | 
            ||
| 1085 | idx = 0  | 
            ||
| 1086 | |||
| 1087 | while idx < len(adjustments_list) and dts_in_window[0] >\  | 
            ||
| 1088 | adjustments_list[idx][0]:  | 
            ||
| 1089 | idx += 1  | 
            ||
| 1090 | |||
| 1091 | # if we've advanced through all the adjustments, then there's nothing  | 
            ||
| 1092 | # to do.  | 
            ||
| 1093 | if idx == len(adjustments_list):  | 
            ||
| 1094 | return  | 
            ||
| 1095 | |||
| 1096 | while idx < len(adjustments_list):  | 
            ||
| 1097 | adjustment_to_apply = adjustments_list[idx]  | 
            ||
| 1098 | |||
| 1099 | if adjustment_to_apply[0] > dts_in_window[-1]:  | 
            ||
| 1100 | break  | 
            ||
| 1101 | |||
| 1102 | range_end = dts_in_window.searchsorted(adjustment_to_apply[0])  | 
            ||
| 1103 | if multiply:  | 
            ||
| 1104 | window_data[0:range_end] *= adjustment_to_apply[1]  | 
            ||
| 1105 | else:  | 
            ||
| 1106 | window_data[0:range_end] /= adjustment_to_apply[1]  | 
            ||
| 1107 | |||
| 1108 | idx += 1  | 
            ||
| 1109 | |||
| 1110 | def _get_adjustment_list(self, asset, adjustments_dict, table_name):  | 
            ||
| 1111 | """  | 
            ||
| 1112 | Internal method that returns a list of adjustments for the given sid.  | 
            ||
| 1113 | |||
| 1114 | Parameters  | 
            ||
| 1115 | ----------  | 
            ||
| 1116 | asset : Asset  | 
            ||
| 1117 | The asset for which to return adjustments.  | 
            ||
| 1118 | |||
| 1119 | adjustments_dict: dict  | 
            ||
| 1120 | A dictionary of sid -> list that is used as a cache.  | 
            ||
| 1121 | |||
| 1122 | table_name: string  | 
            ||
| 1123 | The table that contains this data in the adjustments db.  | 
            ||
| 1124 | |||
| 1125 | Returns  | 
            ||
| 1126 | -------  | 
            ||
| 1127 | adjustments: list  | 
            ||
| 1128 | A list of [multiplier, pd.Timestamp], earliest first  | 
            ||
| 1129 | |||
| 1130 | """  | 
            ||
| 1131 | if self._adjustment_reader is None:  | 
            ||
| 1132 | return []  | 
            ||
| 1133 | |||
| 1134 | sid = int(asset)  | 
            ||
| 1135 | |||
| 1136 | try:  | 
            ||
| 1137 | adjustments = adjustments_dict[sid]  | 
            ||
| 1138 | except KeyError:  | 
            ||
| 1139 | adjustments = adjustments_dict[sid] = self._adjustment_reader.\  | 
            ||
| 1140 | get_adjustments_for_sid(table_name, sid)  | 
            ||
| 1141 | |||
| 1142 | return adjustments  | 
            ||
| 1143 | |||
| 1144 | def _check_is_currently_alive(self, asset, dt):  | 
            ||
| 1145 | sid = int(asset)  | 
            ||
| 1146 | |||
| 1147 | if sid not in self._asset_start_dates:  | 
            ||
| 1148 | self._get_asset_start_date(asset)  | 
            ||
| 1149 | |||
| 1150 | start_date = self._asset_start_dates[sid]  | 
            ||
| 1151 | if self._asset_start_dates[sid] > dt:  | 
            ||
| 1152 | raise NoTradeDataAvailableTooEarly(  | 
            ||
| 1153 | sid=sid,  | 
            ||
| 1154 | dt=normalize_date(dt),  | 
            ||
| 1155 | start_dt=start_date  | 
            ||
| 1156 | )  | 
            ||
| 1157 | |||
| 1158 | end_date = self._asset_end_dates[sid]  | 
            ||
| 1159 | if self._asset_end_dates[sid] < dt:  | 
            ||
| 1160 | raise NoTradeDataAvailableTooLate(  | 
            ||
| 1161 | sid=sid,  | 
            ||
| 1162 | dt=normalize_date(dt),  | 
            ||
| 1163 | end_dt=end_date  | 
            ||
| 1164 | )  | 
            ||
| 1165 | |||
| 1166 | def _get_asset_start_date(self, asset):  | 
            ||
| 1167 | self._ensure_asset_dates(asset)  | 
            ||
| 1168 | return self._asset_start_dates[asset]  | 
            ||
| 1169 | |||
| 1170 | def _get_asset_end_date(self, asset):  | 
            ||
| 1171 | self._ensure_asset_dates(asset)  | 
            ||
| 1172 | return self._asset_end_dates[asset]  | 
            ||
| 1173 | |||
| 1174 | def _ensure_asset_dates(self, asset):  | 
            ||
| 1175 | sid = int(asset)  | 
            ||
| 1176 | |||
| 1177 | if sid not in self._asset_start_dates:  | 
            ||
| 1178 | self._asset_start_dates[sid] = asset.start_date  | 
            ||
| 1179 | self._asset_end_dates[sid] = asset.end_date  | 
            ||
| 1180 | |||
| 1181 | def get_splits(self, sids, dt):  | 
            ||
| 1182 | """  | 
            ||
| 1183 | Returns any splits for the given sids and the given dt.  | 
            ||
| 1184 | |||
| 1185 | Parameters  | 
            ||
| 1186 | ----------  | 
            ||
| 1187 | sids : list  | 
            ||
| 1188 | Sids for which we want splits.  | 
            ||
| 1189 | |||
| 1190 | dt: pd.Timestamp  | 
            ||
| 1191 | The date for which we are checking for splits. Note: this is  | 
            ||
| 1192 | expected to be midnight UTC.  | 
            ||
| 1193 | |||
| 1194 | Returns  | 
            ||
| 1195 | -------  | 
            ||
| 1196 | list: List of splits, where each split is a (sid, ratio) tuple.  | 
            ||
| 1197 | """  | 
            ||
| 1198 | if self._adjustment_reader is None or len(sids) == 0:  | 
            ||
| 1199 |             return {} | 
            ||
| 1200 | |||
| 1201 | # convert dt to # of seconds since epoch, because that's what we use  | 
            ||
| 1202 | # in the adjustments db  | 
            ||
| 1203 | seconds = int(dt.value / 1e9)  | 
            ||
| 1204 | |||
| 1205 | splits = self._adjustment_reader.conn.execute(  | 
            ||
| 1206 | "SELECT sid, ratio FROM SPLITS WHERE effective_date = ?",  | 
            ||
| 1207 | (seconds,)).fetchall()  | 
            ||
| 1208 | |||
| 1209 | sids_set = set(sids)  | 
            ||
| 1210 | splits = [split for split in splits if split[0] in sids_set]  | 
            ||
| 1211 | |||
| 1212 | return splits  | 
            ||
| 1213 | |||
| 1214 | def get_stock_dividends(self, sid, trading_days):  | 
            ||
| 1215 | """  | 
            ||
| 1216 | Returns all the stock dividends for a specific sid that occur  | 
            ||
| 1217 | in the given trading range.  | 
            ||
| 1218 | |||
| 1219 | Parameters  | 
            ||
| 1220 | ----------  | 
            ||
| 1221 | sid: int  | 
            ||
| 1222 | The asset whose stock dividends should be returned.  | 
            ||
| 1223 | |||
| 1224 | trading_days: pd.DatetimeIndex  | 
            ||
| 1225 | The trading range.  | 
            ||
| 1226 | |||
| 1227 | Returns  | 
            ||
| 1228 | -------  | 
            ||
| 1229 | list: A list of objects with all relevant attributes populated.  | 
            ||
| 1230 | All timestamp fields are converted to pd.Timestamps.  | 
            ||
| 1231 | """  | 
            ||
| 1232 | |||
| 1233 | if self._adjustment_reader is None:  | 
            ||
| 1234 | return []  | 
            ||
| 1235 | |||
| 1236 | if len(trading_days) == 0:  | 
            ||
| 1237 | return []  | 
            ||
| 1238 | |||
| 1239 | start_dt = trading_days[0].value / 1e9  | 
            ||
| 1240 | end_dt = trading_days[-1].value / 1e9  | 
            ||
| 1241 | |||
| 1242 | dividends = self._adjustment_reader.conn.execute(  | 
            ||
| 1243 | "SELECT * FROM stock_dividend_payouts WHERE sid = ? AND "  | 
            ||
| 1244 | "ex_date > ? AND pay_date < ?", (int(sid), start_dt, end_dt,)).\  | 
            ||
| 1245 | fetchall()  | 
            ||
| 1246 | |||
| 1247 | dividend_info = []  | 
            ||
| 1248 | for dividend_tuple in dividends:  | 
            ||
| 1249 |             dividend_info.append({ | 
            ||
| 1250 | "declared_date": dividend_tuple[1],  | 
            ||
| 1251 | "ex_date": pd.Timestamp(dividend_tuple[2], unit="s"),  | 
            ||
| 1252 | "pay_date": pd.Timestamp(dividend_tuple[3], unit="s"),  | 
            ||
| 1253 | "payment_sid": dividend_tuple[4],  | 
            ||
| 1254 | "ratio": dividend_tuple[5],  | 
            ||
| 1255 | "record_date": pd.Timestamp(dividend_tuple[6], unit="s"),  | 
            ||
| 1256 | "sid": dividend_tuple[7]  | 
            ||
| 1257 | })  | 
            ||
| 1258 | |||
| 1259 | return dividend_info  | 
            ||
| 1260 | |||
| 1261 | def contains(self, asset, field):  | 
            ||
| 1262 | return field in BASE_FIELDS or \  | 
            ||
| 1263 | (field in self._augmented_sources_map and  | 
            ||
| 1264 | asset in self._augmented_sources_map[field])  | 
            ||
| 1265 | |||
| 1266 | def get_fetcher_assets(self, day):  | 
            ||
| 1267 | """  | 
            ||
| 1268 | Returns a list of assets for the current date, as defined by the  | 
            ||
| 1269 | fetcher data.  | 
            ||
| 1270 | |||
| 1271 | Notes  | 
            ||
| 1272 | -----  | 
            ||
| 1273 | Data is forward-filled. If there is no fetcher data defined for day  | 
            ||
| 1274 | N, we use day N-1's data (if available, otherwise we keep going back).  | 
            ||
| 1275 | |||
| 1276 | Returns  | 
            ||
| 1277 | -------  | 
            ||
| 1278 | list: a list of Asset objects.  | 
            ||
| 1279 | """  | 
            ||
| 1280 | # return a list of assets for the current date, as defined by the  | 
            ||
| 1281 | # fetcher source  | 
            ||
| 1282 | if self._extra_source_df is None:  | 
            ||
| 1283 | return []  | 
            ||
| 1284 | |||
| 1285 | if day in self._extra_source_df.index:  | 
            ||
| 1286 | date_to_use = day  | 
            ||
| 1287 | else:  | 
            ||
| 1288 | # current day isn't in the fetcher df, go back the last  | 
            ||
| 1289 | # available day  | 
            ||
| 1290 | idx = self._extra_source_df.index.searchsorted(day)  | 
            ||
| 1291 | if idx == 0:  | 
            ||
| 1292 | return []  | 
            ||
| 1293 | |||
| 1294 | date_to_use = self._extra_source_df.index[idx - 1]  | 
            ||
| 1295 | |||
| 1296 | asset_list = self._extra_source_df.loc[date_to_use]["sid"]  | 
            ||
| 1297 | |||
| 1298 | # make sure they're actually assets  | 
            ||
| 1299 | asset_list = [asset for asset in asset_list  | 
            ||
| 1300 | if isinstance(asset, Asset)]  | 
            ||
| 1301 | |||
| 1302 | return asset_list  | 
            ||
| 1303 |