-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathContainers.py
75 lines (56 loc) · 2.15 KB
/
Containers.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
from collections import namedtuple
def unstack_df(df, column, level=0):
return df[column].unstack(level=level)
# Your New Python File
class Data():
def __init__(self, df, ohlcv):
'''
Args:
df: pandas dataframe of historical data
ohlcv: string specifying whether to use open, close, high, low, volume
for the timeseries
'''
self.raw = df
self._ohlcv = ohlcv
self._timeseries = unstack_df(df,ohlcv) # unstack so columns are instrument tickers
def __len__(self):
return len(self._timeseries)
@property
def index(self):
return self._timeseries.index
@property
def returns(self):
return self._timeseries.diff().dropna() # we always get a nan for the first value
@property
def columns(self):
return list(self._timeseries.columns)
def shape(self):
return self._timeseries.shape()
def get_correlations(self, type="kendall"):
return self.returns.corr(method=type)
def get_returns(self, instrument):
return (instrument, self.returns[instrument].to_numpy())
class ModelContainer():
'''
Container object for storing copula models according to the instrument pair
tickers.
Expects to receive a tuple of size two containing the ticker
for the instruments being modelled, and its corresponding
model.
'''
def __init__(self):
self._container_dict = {}
def __getitem__(self, key:tuple):
std_key = self._enforce_key_structure(key)
return self._container_dict[std_key]
def __setitem__(self, key:tuple, model):
std_key = self._enforce_key_structure(key)
self._container_dict[std_key] = model
def _enforce_key_structure(self, pair:tuple):
return tuple(sorted(pair))
def update(self, new_dict):
raise NotImplementedError("This method should not be used as it doesn't enforce Key ordering")
def keys(self):
return self._container_dict.keys()
def values(self):
return self._container_dict.values()