Пример #1
0
    def test_cached_object(self):
        expiry = pd.Timestamp("2014")
        before = expiry - pd.Timedelta("1 minute")
        after = expiry + pd.Timedelta("1 minute")

        obj = CachedObject(1, expiry)

        assert obj.unwrap(before) == 1
        assert obj.unwrap(expiry) == 1  # Unwrap on expiry is allowed.
        with pytest.raises(Expired, match=str(expiry)):
            obj.unwrap(after)
Пример #2
0
    def test_cached_object(self):
        expiry = Timestamp('2014')
        before = expiry - Timedelta('1 minute')
        after = expiry + Timedelta('1 minute')

        obj = CachedObject(1, expiry)

        self.assertEqual(obj.unwrap(before), 1)
        self.assertEqual(obj.unwrap(expiry), 1)  # Unwrap on expiry is allowed.
        with self.assertRaises(Expired) as e:
            obj.unwrap(after)
        self.assertEqual(e.exception.args, (expiry,))
Пример #3
0
    def test_cached_object(self):
        expiry = Timestamp("2014")
        before = expiry - Timedelta("1 minute")
        after = expiry + Timedelta("1 minute")

        obj = CachedObject(1, expiry)

        self.assertEqual(obj.unwrap(before), 1)
        self.assertEqual(obj.unwrap(expiry), 1)  # Unwrap on expiry is allowed.
        with self.assertRaises(Expired) as e:
            obj.unwrap(after)
        self.assertEqual(e.exception.args, (expiry, ))
Пример #4
0
    def test_expired(self):
        always_expired = CachedObject.expired()

        for dt in Timestamp.min, Timestamp.now(), Timestamp.max:
            with self.assertRaises(Expired):
                always_expired.unwrap(dt)
Пример #5
0
    def _ensure_sliding_window(self, assets, dts, field):
        """
        Ensure that there is a Float64Multiply window that can provide data
        for the given parameters.
        If the corresponding window for the (assets, len(dts), field) does not
        exist, then create a new one.
        If a corresponding window does exist for (assets, len(dts), field), but
        can not provide data for the current dts range, then create a new
        one and replace the expired window.

        WARNING: A simulation with a high variance of assets, may cause
        unbounded growth of floating windows stored in `_window_blocks`.
        There should be some regular clean up of the cache, if stale windows
        prevent simulations from completing because of memory constraints.

        Parameters
        ----------
        assets : iterable of Assets
            The assets in the window
        dts : iterable of datetime64-like
            The datetimes for which to fetch data.
            Makes an assumption that all dts are present and contiguous,
            in the calendar.
        field : str
            The OHLCV field for which to retrieve data.

        Returns
        -------
        out : Float64Window with sufficient data so that the window can
        provide `get` for the index corresponding with the last value in `dts`
        """
        end = dts[-1]
        size = len(dts)
        assets_key = frozenset(assets)
        try:
            block_cache = self._window_blocks[(assets_key, field, size)]
            try:
                return block_cache.unwrap(end)
            except Expired:
                pass
        except KeyError:
            pass

        start = dts[0]

        offset = 0
        start_ix = self._calendar.get_loc(start)
        end_ix = self._calendar.get_loc(end)

        cal = self._calendar
        prefetch_end_ix = min(end_ix + self._prefetch_length, len(cal) - 1)
        prefetch_end = cal[prefetch_end_ix]
        prefetch_dts = cal[start_ix:prefetch_end_ix + 1]
        array = self._array(prefetch_dts, assets, field)
        if self._adjustments_reader:
            adjs = self._get_adjustments_in_range(assets, prefetch_dts, field)
        else:
            adjs = {}
        if field == 'volume':
            array = array.astype('float64')
        dtype_ = dtype('float64')

        window = Float64Window(array, dtype_, adjs, offset, size)
        block = SlidingWindow(window, size, start_ix, offset)
        self._window_blocks[(assets_key, field,
                             size)] = CachedObject(block, prefetch_end)
        return block
Пример #6
0
 def test_expired(self, date):
     always_expired = CachedObject.expired()
     with pytest.raises(Expired):
         always_expired.unwrap(date)
Пример #7
0
    def test_expired(self):
        always_expired = CachedObject.expired()

        for dt in Timestamp.min, Timestamp.now(), Timestamp.max:
            with self.assertRaises(Expired):
                always_expired.unwrap(dt)