def sid_day_index(self, sid, day): """ all data for all assets is stored sequentially. to get the right values we must find the index for this sid and this day. so we calculate the offset in this long array. Parameters ---------- sid : int The asset identifier. day : datetime64-like Midnight of the day for which data is requested. Returns ------- int Index into the data tape for the given sid and day. Raises a NoDataOnDate exception if the given day and sid is before or after the date range of the equity. """ try: day_loc = self.sessions.get_loc(day) except Exception: raise NoDataOnDate("day={0} is outside of calendar={1}".format( day, self.sessions)) offset = day_loc - self._calendar_offsets[sid] if offset < 0: raise NoDataBeforeDate( "No data on or before day={0} for sid={1}".format( day, sid)) ix = self._first_rows[sid] + offset if ix > self._last_rows[sid]: raise NoDataAfterDate( "No data on or after day={0} for sid={1}".format( day, sid)) return ix
def sid_day_index(self, sid, day): """ Parameters ---------- sid : int The asset identifier. day : datetime64-like Midnight of the day for which data is requested. Returns ------- int Index into the data tape for the given sid and day. Raises a NoDataOnDate exception if the given day and sid is before or after the date range of the equity. """ try: day_loc = self.sessions.get_loc(day) except: raise NoDataOnDate("day={0} is outside of calendar={1}".format( day, self.sessions)) if sid not in self._calendar_offsets: raise NoDataOnDate("day={0} is outside of calendar={1}".format( day, self.sessions)) offset = day_loc - self._calendar_offsets[sid] if offset < 0: raise NoDataBeforeDate( "No data on or before day={0} for sid={1}".format(day, sid)) ix = self._first_rows[sid] + offset if ix > self._last_rows[sid]: raise NoDataAfterDate( "No data on or after day={0} for sid={1}".format(day, sid)) return ix
def get_value(self, sid, dt, field): day = self._fmt_date(dt) sql = "SELECT %s FROM prices WHERE sid = %d and date = '%s'" % (field, sid, day) res = self._query(sql) if len(res) == 0: if self._exist_sid(sid): raise NoDataBeforeDate("No data on or before day={0} for sid={1}".format(dt, sid)) else: raise KeyError(sid) return res[0][0]
def get_value(self, sid, dt, field): """ Retrieve the value at the given coordinates. Parameters ---------- sid : int The asset identifier. dt : pd.Timestamp The timestamp for the desired data point. field : string The OHLVC name for the desired data point. Returns ------- value : float|int The value at the given coordinates, ``float`` for OHLC, ``int`` for 'volume'. Raises ------ NoDataOnDate If the given dt is not a valid market minute (in minute mode) or session (in daily mode) according to this reader's tradingcalendar. """ self._validate_assets([sid]) self._validate_timestamp(dt) sid_ix = self.sids.searchsorted(sid) dt_ix = self.dates.searchsorted(dt.asm8) value = self._postprocessors[field]( self._country_group[DATA][field][sid_ix, dt_ix] ) # When the value is nan, this dt may be outside the asset's lifetime. # If that's the case, the proper NoDataOnDate exception is raised. # Otherwise (when there's just a hole in the middle of the data), the # nan is returned. if np.isnan(value): if dt.asm8 < self.asset_start_dates[sid_ix]: raise NoDataBeforeDate() if dt.asm8 > self.asset_end_dates[sid_ix]: raise NoDataAfterDate() return value