-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
174 lines (119 loc) · 4.72 KB
/
utils.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
import pandas as pd
import numpy as np
import tarfile
import logging
import re
import datetime
from pathlib import Path
logger = logging.getLogger(__name__)
def get_cache_dir():
pth = Path('~/Documents/temp/dtkcache')
if not pth.exists():
pth.mkdir(parents=True)
return pth
def df_info(df):
display(df.info(verbose=True))
display(df.describe(include='all'))
display(df.sample(10))
def display_dfs_inline(dfs, captions=None, margin=5):
from functools import reduce
from IPython.display import display_html
captions = [''] * len(dfs) if captions is None else captions
stylers = [D(df).style.set_table_attributes(f'style="display:inline; margin:{margin}px;"').set_caption(c) for df, c in zip(dfs, captions)]
display_html(reduce(lambda x, y: x + y, (s._repr_html_() for s in stylers)), raw=True)
def smart_convert_df(df, infer_objects=True):
if infer_objects:
df = df.infer_objects().copy()
else:
df = df.copy()
for c in df.columns:
try:
if df[c].dtype == object:
df[c] = df[c].fillna(np.nan).astype(float)
except (ValueError, TypeError):
pass
return df
def extract_targz(fpath):
extracted_fpath = str(fpath) + '_extracted'
# https://stackoverflow.com/questions/30887979/i-want-to-create-a-script-for-unzip-tar-gz-file-via-python
if (fpath.endswith("tar.gz")):
tar = tarfile.open(fpath, "r:gz")
tar.extractall(extracted_fpath)
tar.close()
elif (fpath.endswith("tar")):
tar = tarfile.open(fpath, "r:")
tar.extractall(extracted_fpath)
tar.close()
logger.info('extracted : {} -> {}'.format(fpath, extracted_fpath))
return extracted_fpath
def to_posix_ms(ts, unit='ms'):
mul_fac = {
's': 1,
'ms': 1e3,
'ns': 1e6
}
return str(int(np.round(pd.Timestamp(ts).timestamp() * mul_fac[unit], decimals=0)))
def make_column_name_sql_pd_safe(s):
return re.sub(r'[^0-9a-zA-Z\s]+', '', s).lower().lstrip().rstrip().replace(' ', '_')
def make_column_value_numeric_safe(s):
return re.sub(r'[^0-9.]+', '', s)
def validate_date(date, as_str=True):
if isinstance(date, (pd.Timestamp, datetime.date)):
date = date.strftime('%Y%m%d')
elif isinstance(date, int):
date = (pd.Timestamp.now() - pd.offsets.Day(date)).strftime('%Y%m%d')
elif isinstance(date, str):
date = pd.to_datetime(date).strftime('%Y%m%d')
else:
raise ValueError(f'unable to validate date: {date}')
if as_str:
return date
return pd.Timestamp(date)
def utctoday():
return validate_date(pd.Timestamp.utcnow(), as_str=False)
def today():
return validate_date(pd.Timestamp.now(), as_str=False)
def zscore(df, wdw, closed_interval=False, min_periods_thresh=.5):
if wdw < 2:
return df
# mp = min_periods_thresh *
if closed_interval:
return (df - df.rolling(wdw, min_periods=2).mean()) / df.rolling(wdw, min_periods=2).std()
else:
return (df - df.rolling(wdw, min_periods=2).mean().shift(1)) / df.rolling(wdw, min_periods=2).std().shift(1)
def get_volatility_data(klines_df, standardize_periods=1440):
wdws = [2**i for i in range(1, 10)]
prc = klines_df[['close']]
ret = prc.close.pct_change()
up_ret = ret.copy()
up_ret.loc[ret < 0] = np.nan
down_ret = ret.copy()
down_ret.loc[ret > 0] = np.nan
vol_df = prc.copy()
for wdw in tqdm(wdws):
tot_vol = ret.rolling(wdw).std() * np.sqrt(standardize_periods / wdw)
vol_df[f'tot_vol_{wdw}'] = tot_vol
up_vol = up_ret.rolling(wdw, min_periods=2).std() * np.sqrt(standardize_periods / wdw)
vol_df[f'up_vol_{wdw}'] = up_vol
down_vol = down_ret.rolling(wdw, min_periods=2).std() * np.sqrt(standardize_periods / wdw)
vol_df[f'down_vol_{wdw}'] = down_vol
vol_df[f'ud_vol_ratio_{wdw}'] = up_vol / down_vol
return vol_df
def loc_around(df, loc, wdw=5, highlight=True):
loc_idx = df.index.get_loc(loc)
subdf = df.iloc[loc_idx - wdw: loc_idx + wdw + 1].copy()
if highlight:
def row_style(row):
if row.name == loc:
return pd.Series('background-color: mint', row.index)
else:
return pd.Series('', row.index)
# def index_style(idx):
# if idx == loc:
# return pd.Series('background-color: yellow', row.index)
# else:
# return pd.Series('', row.index)
subdf = subdf.style.apply(row_style, axis=1)
# .apply_index(index_style, axis=1)
# subdf = subdf.style.apply_index(index_style, axis=1)
return subdf