-
Notifications
You must be signed in to change notification settings - Fork 5
/
_util.py
195 lines (155 loc) · 5.49 KB
/
_util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import warnings
from numbers import Number
from typing import Dict, List, Optional, Sequence, Union, cast
import numpy as np
import pandas as pd
def try_(lazy_func, default=None, exception=Exception):
try:
return lazy_func()
except exception:
return default
def _as_str(value) -> str:
if isinstance(value, (Number, str)):
return str(value)
if isinstance(value, pd.DataFrame):
return 'df'
name = str(getattr(value, 'name', '') or '')
if name in ('Open', 'High', 'Low', 'Close', 'Volume'):
return name[:1]
if callable(value):
name = getattr(value, '__name__', value.__class__.__name__).replace('<lambda>', 'λ')
if len(name) > 10:
name = name[:9] + '…'
return name
def _as_list(value) -> List:
if isinstance(value, Sequence) and not isinstance(value, str):
return list(value)
return [value]
def _data_period(index) -> Union[pd.Timedelta, Number]:
"""Return data index period as pd.Timedelta"""
values = pd.Series(index[-100:])
return values.diff().dropna().median()
class _Array(np.ndarray):
"""
ndarray extended to supply .name and other arbitrary properties
in ._opts dict.
"""
def __new__(cls, array, *, name=None, **kwargs):
obj = np.asarray(array).view(cls)
obj.name = name or array.name
obj._opts = kwargs
return obj
def __array_finalize__(self, obj):
if obj is not None:
self.name = getattr(obj, 'name', '')
self._opts = getattr(obj, '_opts', {})
# Make sure properties name and _opts are carried over
# when (un-)pickling.
def __reduce__(self):
value = super().__reduce__()
return value[:2] + (value[2] + (self.__dict__,),)
def __setstate__(self, state):
self.__dict__.update(state[-1])
super().__setstate__(state[:-1])
def __bool__(self):
try:
return bool(self[-1])
except IndexError:
return super().__bool__()
def __float__(self):
try:
return float(self[-1])
except IndexError:
return super().__float__()
def to_series(self):
warnings.warn("`.to_series()` is deprecated. For pd.Series conversion, use accessor `.s`")
return self.s
@property
def s(self) -> pd.Series:
values = np.atleast_2d(self)
index = self._opts['index'][:values.shape[1]]
return pd.Series(values[0], index=index, name=self.name)
@property
def df(self) -> pd.DataFrame:
values = np.atleast_2d(np.asarray(self))
index = self._opts['index'][:values.shape[1]]
df = pd.DataFrame(values.T, index=index, columns=[self.name] * len(values))
return df
class _Indicator(_Array):
pass
class _Data:
"""
A data array accessor. Provides access to OHLCV "columns"
as a standard `pd.DataFrame` would, except it's not a DataFrame
and the returned "series" are _not_ `pd.Series` but `np.ndarray`
for performance reasons.
"""
def __init__(self, df: pd.DataFrame):
self.__df = df
self.__i = len(df)
self.__pip: Optional[float] = None
self.__cache: Dict[str, _Array] = {}
self.__arrays: Dict[str, _Array] = {}
self._update()
def __getitem__(self, item):
return self.__get_array(item)
def __getattr__(self, item):
try:
return self.__get_array(item)
except KeyError:
raise AttributeError(f"Column '{item}' not in data") from None
def _set_length(self, i):
self.__i = i
self.__cache.clear()
def _update(self):
index = self.__df.index.copy()
self.__arrays = {col: _Array(arr, index=index)
for col, arr in self.__df.items()}
# Leave index as Series because pd.Timestamp nicer API to work with
self.__arrays['__index'] = index
def __repr__(self):
i = min(self.__i, len(self.__df)) - 1
index = self.__arrays['__index'][i]
items = ', '.join(f'{k}={v}' for k, v in self.__df.iloc[i].items())
return f'<Data i={i} ({index}) {items}>'
def __len__(self):
return self.__i
@property
def df(self) -> pd.DataFrame:
return (self.__df.iloc[:self.__i]
if self.__i < len(self.__df)
else self.__df)
@property
def pip(self) -> float:
if self.__pip is None:
self.__pip = float(10**-np.median([len(s.partition('.')[-1])
for s in self.__arrays['Close'].astype(str)]))
return self.__pip
def __get_array(self, key) -> _Array:
arr = self.__cache.get(key)
if arr is None:
arr = self.__cache[key] = cast(_Array, self.__arrays[key][:self.__i])
return arr
@property
def Open(self) -> _Array:
return self.__get_array('Open')
@property
def High(self) -> _Array:
return self.__get_array('High')
@property
def Low(self) -> _Array:
return self.__get_array('Low')
@property
def Close(self) -> _Array:
return self.__get_array('Close')
@property
def Volume(self) -> _Array:
return self.__get_array('Volume')
@property
def index(self) -> pd.DatetimeIndex:
return self.__get_array('__index')
# Make pickling in Backtest.optimize() work with our catch-all __getattr__
def __getstate__(self):
return self.__dict__
def __setstate__(self, state):
self.__dict__ = state