以东方雨虹为例,计算OBV。Python代码如下:
import matplotlib.pyplot as plt
difClose = df.Close.diff()
difClose[0] = 0
OBV = (((difClose >= 0)*2-1)*Volume).cumsum()
OBV = OBV.dropna()
OBV.name = ‘OBV’
[](
)1.2 移动型OBV
移动型OBV是由累积OBV进行简单移动平均得到,一般选择9日或者12日为时间跨度,移动型OBV的计算公式为:
s m O B V t = O B V t + O B V t − 1 + ⋅ ⋅ ⋅ + O B V T − 8 9 , t = 9 , 10 , ⋅ ⋅ ⋅ \displaystyle smOBV_t = \frac{OBV_t+OBV_{t-1}+ ··· +OBV_{T-8}}{9}, t=9,10,··· smOBVt=9OBVt+OBVt−1+⋅⋅⋅+OBVT−8,t=9,10,⋅⋅⋅
用python计算东方雨虹移动型OBV
定义简单移动平均函数
def smaCal(tsPrice, k):
import pandas as pd
Sma = pd.Series(0.0, index=tsPrice.index)
for i in range(k-1, le