Elaborating a bit on the security issue here: as_strided
in full generality allows access to raw (and therefore potentially unsafe) memory addresses.
Zipline does currently implement a few utility functions in terms of safe usages ofas_strided
in zipline.utils.numpy_utils
.
It looks like you're looking to implement rolling-window calculations in terms of as_strided
, which isn't something that we currently have implemented, but which would likely be a welcome contribution.
I took a stab at implementing what looks like your particular use-case in this Zipline PR.
Sample output:
In [1]: import numpy as np
In [2]: from zipline.utils.numpy_utils import rolling_window
In [3]: data = np.arange(15).reshape(5, 3)
In [4]: data
Out[4]:
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11],
[12, 13, 14]])
In [5]: window = rolling_window(data, 3)
In [6]: window[0]
Out[6]:
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
In [7]: window[1]
Out[7]:
array([[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
In [8]: window[2]
Out[8]:
array([[ 6, 7, 8],
[ 9, 10, 11],
[12, 13, 14]])