欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

量化交易——传统技术分析能量潮指标OBV的原理及实现

程序员文章站 2022-07-13 17:15:06
...

能量潮指标OBV

股市分析中有四个要素,分别是价、量、时、空。其中OBV便是从成交量作为分析的突破口。它反映的是在股市起伏波动时相关的市场人气变化,可以用来判断股市是否处于有较强的想上冲的牛市中还是即将要踏空。成交量越大,反映的是市场判断不一致程度越强,而这会如何影响股价走势则是研究的重点。

实现

分析过程可以参考前面的博文,量化交易——传统技术分析相对强弱指数RSI的原理及实现
相关代码如下:

import numpy as np
import math
import random
import json
import matplotlib.pyplot as plt
import sys
sys.setrecursionlimit(10000)

#date|open|high|low|close|volume|adjsuted 

def get_stock_hist(num):
    s_his=np.genfromtxt('C:/Users/Haipeng/Desktop/python/Korea/Korea_{:03d}.csv'.format(num), delimiter=',')
    s_hi=s_his[1:][:]
    days=s_hi.shape[0]
    this_stock = []
    for i in range(1,days,1):
        this_day = [i]
        for k in range(1,7):
            this_day.append(s_hi[i][k])
        this_stock.append(this_day)
    print 'Maximum date is ',len(this_stock)
    return this_stock

def get_price(D, p_tpe):
    if p_tpe=='close':
        pos=4;
    elif p_tpe=='open':
        pos=1;
    elif p_tpe=='high':
        pos=2;
    elif p_tpe=='low':
        pos=3;
    else:
        pos=5
    price=stock_hist[D-1][pos];
    return price

def get_ma(D, N):
    p_used=np.zeros(N);
    for i in range(1,N+1,1):
        p_used[i-1]=stock_hist[(D-1)-(i-1)][4];
    ma=np.mean(p_used);
    return ma

def get_mar(fro,to,N):
    ma = []
    for i in range(fro,to+1):
        ma.append(get_ma(i,N))
    return ma
#Date\Open\High\Low\Close
def get_tuples(fro,to):
    res =[]
    for d in range(fro,to+1):
        tmp = []
        tmp.append(d)
        tmp.append(get_price(d,'open'))
        tmp.append(get_price(d,'high'))
        tmp.append(get_price(d,'low'))
        tmp.append(get_price(d,'close'))        
        res.append(tmp)
    return res

def get_volume(fro,to):
    res = []
    for d in range(fro,to+1):
        num = 1
        try:
            if get_price(d,'close')<get_price(d-1,'close'):
                num = -1
        except:
            pass
        res.append(num*get_price(d,'volume'))
    return res  
# OBV 
def get_OBV(D):
    if D == 1:
        return 0
    else:
        if get_price(D,'close')>get_price(D-1,'close'):
            return get_OBV(D-1)+get_price(D,'volume')
        elif get_price(D,'close')<get_price(D-1,'close'):
            return get_OBV(D-1)-get_price(D,'volume')
        else:
            return get_OBV(D-1)
def get_obv(fro,to):
    res = []
    for d in range(fro,to+1):
        res.append(get_OBV(d))
    return res

绘制K线图及OBV曲线

代码:

def plot_OBV(fro,to):
    volume = get_volume(fro,to)
    obv =get_obv(fro,to)
    ma5 = get_mar(fro,to,5)
    ma10 = get_mar(fro,to,10)
    ma20 = get_mar(fro,to,20)
    tuples = get_tuples(fro,to)
    date = [d for d in range(fro,to+1)] 

    fig = plt.figure(figsize=(8,5))
    p1 = plt.subplot2grid((5,4),(0,0),rowspan=3,colspan=4,axisbg='k') 
    p1.set_title("On Balance Volume(OBV)")
    p1.set_ylabel("Price")
    p1.plot(date,ma5,'m')
    p1.plot(date,ma10,'b')
    p1.plot(date,ma20,'y')
    p1.legend(('MA5','MA10','MA20'))
    p1.grid(True,color='w')
    candlestick_ohlc(p1, tuples, width=0.7,colorup='r',colordown="g")

    p2 = plt.subplot2grid((5,4),(3,0),colspan=4,axisbg='c') 
    p2.set_ylabel("Volume")
    colors = []
    for i in range(len(volume)):
        if volume[i]<0:
            colors.append('green')
            volume[i]=-volume[i]
        else:
            colors.append('red')
    p2.bar(date,volume,color=colors)

    p3 = plt.subplot2grid((5,4),(4,0),colspan=4,axisbg='m') 
    p3.plot(date,obv,'b')
    p3.set_ylabel("OBV")
    p3.set_xlabel("Dates")
    plt.subplots_adjust(hspace=0)
    plt.show()# show the plot on the screen
stock_hist = get_stock_hist(17)
plot_OBV(888,999)

图像1:
量化交易——传统技术分析能量潮指标OBV的原理及实现
图像2:

stock_hist = get_stock_hist(18)
plot_OBV(123,789)

量化交易——传统技术分析能量潮指标OBV的原理及实现

欢迎交流~