本篇博客是量化笔记系列的最后一篇了,之后会停更一段时间量化内容,后面会写什么还待定
OVB主要计算累计成交量,将股价上涨时的成交量进行正累加,对股价下跌时的成交量进行负累加,计算公式是:
O B V n = ± V n + O B V n − 1 OBV_n=\pm V_n + OBV_{n-1} OBVn=±Vn+OBVn−1
当本期股价上涨时, V n V_n Vn的符号为正,
当本期股价下跌时, V n V_n Vn的符号为负
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# 以青岛啤酒为例
TsingTao=pd.read_csv('TsingTao.csv',index_col='Date')
TsingTao.index=pd.to_datetime(TsingTao.index)
TsingTao.Volume=TsingTao.Volume.replace(0,np.nan)
TsingTao=TsingTao.dropna()
close=TsingTao.Close
Volume=TsingTao.Volume
# 计算OBV
difClose=close.diff()
difClose[0]=0
OBV=(((difClose>=0)*2-1)*Volume).cumsum()
OBV.name='OBV'
OBV.head()
OBV.describe()
count 3.230000e+02
mean -1.104009e+07
std 3.572700e+07
min -6.132070e+07
25% -3.602440e+07
50% -2.539030e+07
75% 1.912695e+07
max 9.652380e+07
Name: OBV, dtype: float64