├── .idea └── vcs.xml ├── LICENSE ├── README.md ├── in └── IBM_cts.h5 ├── main.py └── out └── realized_quantities_IBM_cts.csv /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Sebastian Bayer 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Realized Quantities 2 | 3 | The main.py file contains a large variety of realized quantities. 4 | To date, the following are implemented: 5 | 6 | * Realized Variance 7 | * Realized Semivariance 8 | * Realized Skewness 9 | * Realized Kurtosis 10 | * Realized Absolute Variation 11 | * Realized Bipower Variation 12 | * Standardized Tri-Power Quarticity 13 | * Signed Jump Variation 14 | 15 | This file uses the data generated from the 16 | [ResampleHF](https://github.com/BayerSe/ResampleHF) repo. -------------------------------------------------------------------------------- /in/IBM_cts.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BayerSe/RealizedQuantities/663b81ffb82d11315012a08076b63a2a0485a879/in/IBM_cts.h5 -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import numpy as np 4 | import pandas as pd 5 | from scipy import stats 6 | from scipy.special import gamma 7 | 8 | 9 | def realized_quantity(fun): 10 | """Applies the function 'fun' to each day separately""" 11 | return intraday_returns.groupby(pd.Grouper(freq="1d")).apply(fun)[index] 12 | 13 | 14 | if __name__ == "__main__": 15 | 16 | # Settings 17 | asset = "IBM" 18 | sampling = "cts" 19 | trading_seconds = 23400 20 | avg_sampling_frequency = 300 21 | original_sampling_frequency = 60 # From the process_data.py file 22 | M = trading_seconds / original_sampling_frequency 23 | 24 | # Load data and store the intraday returns 25 | data = pd.read_hdf("in/" + asset + "_" + sampling + ".h5", "table") 26 | intraday_returns = data.groupby(pd.Grouper(freq="1d")).apply(lambda x: np.log(x / x.shift(1))).dropna() 27 | 28 | # Index of all days 29 | index = data.groupby(pd.Grouper(freq="1d")).first().dropna().index 30 | 31 | # Some constants 32 | mu_1 = np.sqrt((2 / np.pi)) 33 | mu_43 = 2 ** (2 / 3) * gamma(7 / 6) * gamma(1 / 2) ** (-1) 34 | 35 | # First and last price of each day 36 | prices_open = data.resample('D').first()[index] 37 | prices_close = data.resample('D').last()[index] 38 | 39 | # Return (close-to-close and open-to-close 40 | r_cc = pd.Series(np.log(prices_close / prices_close.shift(1)), name='r_cc') 41 | r_oc = pd.Series(np.log(prices_close / prices_open), name='r_oc') 42 | 43 | # Realized Variance (Andersen and Bollerslev, 1998) 44 | rv = realized_quantity(lambda x: (x ** 2).sum()) 45 | 46 | # Realized absolute variation (Forsberg and Ghysels, 2007) 47 | rav = mu_1 ** (-1) * M ** (-.5) * realized_quantity(lambda x: x.abs().sum()) 48 | 49 | # Realized bipower variation (Barndorff-Nielsen and Shephard; 2004, 2006) 50 | bv = mu_1 ** (-2) * realized_quantity(lambda x: (x.abs() * x.shift(1).abs()).sum()) 51 | 52 | # Standardized tri-power quarticity (see e.g. Forsberg & Ghysels, 2007) 53 | tq = M * mu_43 ** (-3) * realized_quantity( 54 | lambda x: (x.abs() ** (4 / 3) * x.shift(1).abs() ** (4 / 3) * x.shift(2).abs() ** (4 / 3)).sum()) 55 | 56 | # Jump test by Huang and Tauchen (2005) 57 | j = (np.log(rv) - np.log(bv)) / \ 58 | ((mu_1 ** -4 + 2 * mu_1 ** -2 - 5) / (M * tq * bv ** -2)) ** 0.5 59 | jump = j.abs() >= stats.norm.ppf(0.999) 60 | 61 | # Separate continuous and discontinuous parts of the quadratic variation 62 | iv = pd.Series(0, index=index) 63 | iv[jump] = bv[jump] ** 0.5 64 | iv[~jump] = rv[~jump] ** 0.5 65 | 66 | jv = pd.Series(0, index=index) 67 | jv[jump] = rv[jump] ** 0.5 - bv[jump] ** 0.5 68 | jv[jv < 0] = 0 69 | 70 | # Realized Semivariance (Barndorff-Nielsen, Kinnebrock and Shephard, 2010) 71 | rv_m = realized_quantity(lambda x: (x ** 2 * (x < 0)).sum()) 72 | rv_p = realized_quantity(lambda x: (x ** 2 * (x > 0)).sum()) 73 | 74 | # Signed jump variation (Patton and Sheppard, 2015) 75 | sjv = rv_p - rv_m 76 | sjv_p = sjv * (sjv > 0) 77 | sjv_m = sjv * (sjv < 0) 78 | 79 | # Realized Skewness and Kurtosis (see, e.g. Amaya, Christoffersen, Jacobs and Vasquez, 2015) 80 | rm3 = realized_quantity(lambda x: (x ** 3).sum()) 81 | rm4 = realized_quantity(lambda x: (x ** 4).sum()) 82 | rs = np.sqrt(M) * rm3 / rv ** (3 / 2) 83 | rk = M * rm4 / rv ** 2 84 | 85 | # Export data 86 | out = pd.concat([r_cc, r_oc, rav, rv ** .5, bv ** .5, rv_m ** 0.5, rv_p ** 0.5, 87 | iv, jv, sjv, sjv_p, sjv_m, rs, rk], axis=1) 88 | out.columns = ['r_cc', 'r_oc', 'rav', 'rvol', 'bvol', 'rvol_m', 'rvol_p', 89 | 'ivol', 'jvol', 'sjv', 'sjv_p', 'sjv_m', 'rs', 'rk'] 90 | out.to_csv('out/realized_quantities_' + asset + "_" + sampling + ".csv") 91 | --------------------------------------------------------------------------------