-
Notifications
You must be signed in to change notification settings - Fork 7
/
Liquidity_Risk_Price_Impact_Based_Liquidity_Measures.py
75 lines (58 loc) · 2.66 KB
/
Liquidity_Risk_Price_Impact_Based_Liquidity_Measures.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
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
def load_raw_data(file_path):
liq_data = pd.read_csv(file_path)
liq_data = liq_data[~(liq_data == '#VALUE!').any(axis=1)]
liq_data['RET'] = pd.to_numeric(liq_data['RET'])
rolling_five = []
for i in range(len(liq_data)):
rolling_five.append(liq_data[i:i+5].agg({'BIDLO': 'min',
'ASKHI': 'max',
'Volume': 'sum',
'SHROUT': 'mean',
'PRC': 'mean'}))
rolling_five_df = pd.DataFrame(rolling_five)
rolling_five_df.columns = ['bidlo_min', 'askhi_max', 'vol_sum', 'shrout_mean', 'prc_mean']
liq_vol_all = pd.concat([liq_data, rolling_five_df], axis=1)
return liq_vol_all
def amihud_illiquidity(liq_vol_all):
dvol = []
for i in range(len(liq_vol_all)):
dvol.append((liq_vol_all['PRC'][i:i+5] * liq_vol_all['Volume'][i:i+5]).sum())
dvol = pd.DataFrame(dvol)
amihud = []
for i in range(len(liq_vol_all)):
amihud.append((1 / liq_vol_all['RET'].count()) *
(np.sum(np.abs(liq_vol_all['RET'][i:i+1])) /
np.sum(dvol[i:i+1])))
amihud = pd.DataFrame(amihud)
return amihud
def return_to_turnover(liq_vol_all):
turnover_ratio = []
for i in range(len(liq_vol_all)):
turnover_ratio.append((1 / liq_vol_all['Volume'].count()) *
(liq_vol_all['Volume'][i:i + 1].sum() /
liq_vol_all['SHROUT'][i:i + 1].sum()))
florackis = []
for i in range(len(liq_vol_all)):
florackis.append((1 / liq_vol_all['RET'].count()) *
(np.sum(np.abs(liq_vol_all['RET'][i:i+1]) /
turnover_ratio[i:i+1])))
florackis = pd.DataFrame(florackis)
return florackis
def coefficient_of_elasticity(liq_vol_all):
vol_diff_pct = liq_vol_all['Volume'].diff().pct_change()
price_diff_pct = liq_vol_all['PRC'].diff().pct_change()
cet = []
for i in range(len(liq_vol_all)):
cet.append(np.sum(vol_diff_pct[i:i+1]) /
np.sum(price_diff_pct[i:i+1]))
cet = pd.DataFrame(cet)
return cet
if __name__ == '__main__':
file_path = 'D:/PyCharm Community Edition 2023.1.2/Python_Project/Finance/ml4frm/AMD.csv'
liq_vol_all_ = load_raw_data(file_path)
amihud_ = amihud_illiquidity(liq_vol_all_)
florackis_ = return_to_turnover(liq_vol_all_)
cet_ = coefficient_of_elasticity(liq_vol_all_)