布林通道线是根据统计学的标准差来计算的,其具体可由上中下三条曲线展示。其中上下两线分别代表上升压力线和下降支撑线,故而可以根据K线图是否突破布林曲线来判断较好的买卖节点。三条曲线计算方法如下:
中轨线(MID)=收盘价的M日移动平均线;
上轨线(UPER)=中轨线+N倍的标准差;
下轨线(LOWER)=中轨线-N倍的标准差.、
某些分析过程可以参考前面的博文,量化交易——传统技术分析相对强弱指数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')1,'close'):
num = -1
except:
pass
res.append(num*get_price(d,'volume'))
return res
# BB Band实现
def get_BB(D,N):
MD = 0.0
UP = 0.0
DN = 0.0
SD = 0.0
for i in range(N):
MD += get_price(D-i,'close')
MD = MD/N
for i in range(N):
SD += math.pow(MD-get_price(D-i,'close'),2)
SD = math.sqrt(SD/N)
UP = MD + 2*SD
DN = MD - 2*SD
return [UP,MD,DN]
def get_bb(fro,to,N):
res = [[],[],[]]
for d in range(fro,to+1):
if dprint 'Date number is not larger than N!'
break
tmp = get_BB(d,N)
res[0].append(tmp[0])
res[1].append(tmp[1])
res[2].append(tmp[2])
return res
绘图代码:
def plot_BB(fro,to):
volume = get_volume(fro,to)
tmp = get_bb(fro,to,20)
up = tmp[0]
md = tmp[1]
dn = tmp[2]
tuples = get_tuples(fro,to)
date = [d for d in range(fro,to+1)]
fig = plt.figure(figsize=(18,10))
p1 = plt.subplot2grid((4,4),(0,0),rowspan=3,colspan=4,axisbg='k')
p1.set_title("Bollinger Bands:(20-Day Moving Average)")
p1.set_ylabel("Price")
p1.plot(date,up,'m')
p1.plot(date,md,'b')
p1.plot(date,dn,'y')
p1.legend(('UP','MD','DN'))
p1.grid(True,color='w')
candlestick_ohlc(p1, tuples, width=0.7,colorup='r',colordown="g")
p2 = plt.subplot2grid((4,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)
p2.set_xlabel("Dates")
plt.subplots_adjust(hspace=0)
plt.show()# show the plot on the screen
stock_hist = get_stock_hist(18)
plot_BB(1250,1520)
#得图2
plot_BB(220,320)
我们发现,上图有较大的不合理的跳跃。这是什么原因呢?仔细观察发现图像是在第278天发生了突变,应该就是数据出了问题。现在打开对应股票的CSV数据如下,果然:
可知,当天数据丢失了一位数,这才导致了以上的突变。所以在接下来深入挖掘数据时要考虑减少数据错误带来的影响。