I have a dataframe that consist of k-line data.
open_time,open,high,low,close,volume,close_time
2022-01-20 01:00:00,41824.64,41880.62,41814.27,41818.71,214.285,1642640459999
2022-01-20 01:01:00,41818.71,41827.12,41802.49,41805.11,98.679,1642640519999
2022-01-20 01:02:00,41805.11,41820.99,41805.11,41813.83,30.419,1642640579999
2022-01-20 01:03:00,41813.84,41850.0,41813.84,41841.6,61.493,1642640639999
What I need to do is calculating the volatility and create a new dataframe with daily time interval. Thus, the volatility must be calculated based on intraday (1min) high en lows for a time period of 1 day.
The exact formula for volatility is:
np.sqrt(((np.square(np.log(vola['high']) / np.log(vola['low']))) / 4 * np.log(2)))
What I tried to do is using the groupby function in combination with lambda. However, I do not know how to get new column with the volatility using this function. Does anyone have an idea?
The function that I tried to use:
vola.groupby([pd.Grouper(key='open_time', freq= 'D')]).apply(lambda x: np.sqrt(((np.square(np.log(vola['open']) / np.log(vola['close']))) / 4 * np.log(2))))
I know how to make a new column with .aggregate for example but when I use the following code:
vola['vola'] = vola.groupby([pd.Grouper(key='open_time', freq= 'D'), 'high', 'low']).aggregate(vola = lambda x: np.sqrt(((np.square(np.log(vola['high']) / np.log(vola['low']))) / 4 * np.log(2))))
I get the error:
TypeError: Must provide 'func' or tuples of '(column, aggfunc).