I have these two functions here giving different results, but I do not understand why, and assuming the second one is the correct one for Parkinson volatility, how to modify the first one to obtain the same results for the second one. This is the original formula: Parkinson volatility
This is the code:
def Parkinson1(price, window):
sigmasqrd = (1.0 / (4.0 * math.log(2.0))) * ((price['High'] / price['Low']).apply(np.log))**2.0
sigmasqrd_mean=sigmasqrd.rolling(window).mean()
sigma=(252*sigmasqrd)**0.5
sigma=sigma.rolling(window).mean()
return sigma
def Parkinson2(price, window, trading_periods=252, clean=True):
rs = (1.0 / (4.0 * math.log(2.0))) * ((price['High'] / price['Low']).apply(np.log))**2.0
def f(v):
return (trading_periods * v.mean())**0.5
result = rs.rolling(
window=window,
center=False
).apply(func=f)
if clean:
return result.dropna()
else:
return result
df['Parkinson1']=Parkinson1(df, window=22)
df['Parkinson2']=Parkinson2(df, window=22)
df.tail(10)
Date Open High Low Close Adj Close Volume vwap Pct Returns Parkinson1 Parkinson2
1249 2020-03-16 241.179993 256.899994 237.360001 239.850006 233.172028 297240000 241.179993 -0.109424 1.151741 0.347854 0.413066
1250 2020-03-17 245.039993 256.170013 237.070007 252.800003 245.761459 262070500 245.039993 0.053992 1.213926 0.378139 0.441803
1251 2020-03-18 236.250000 248.369995 228.020004 240.000000 233.317871 327597100 236.250000 -0.050633 1.152461 0.413217 0.474653
1252 2020-03-19 239.250000 247.380005 232.220001 240.509995 233.813660 289322000 239.250000 0.002125 1.154910 0.437453 0.491526
1253 2020-03-20 242.529999 244.470001 228.500000 228.800003 223.737640 347158800 242.529999 -0.048688 1.098679 0.464679 0.510255
1254 2020-03-23 228.190002 229.679993 218.259995 222.949997 218.017059 326025200 228.190002 -0.025568 1.070588 0.480385 0.519814
1255 2020-03-24 234.419998 244.100006 233.800003 243.149994 237.770142 235494500 234.419998 0.090603 1.167587 0.494879 0.526782
1256 2020-03-25 244.869995 256.350006 239.750000 246.789993 241.329590 299430300 244.869995 0.014970 1.185066 0.507582 0.538669
1257 2020-03-26 249.520004 262.799988 249.050003 261.200012 255.420761 257632800 249.520004 0.058390 1.254262 0.513269 0.543397
1258 2020-03-27 253.270004 260.809998 251.050003 253.419998 247.812912 224341200 253.270004 -0.029786 1.216903 0.519583 0.546804
It seems that you are using price
in Parkinson1
.
While you're using price_data
in Parkinson2
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.