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 defget_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
defget_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
defget_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
defget_mar(fro,to,N):
ma = []
for i in range(fro,to+1):
ma.append(get_ma(i,N))
return ma
#Date\Open\High\Low\Closedefget_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
defget_volume(fro,to):
res = []
for d in range(fro,to+1):
num = 1try:
if get_price(d,'close')<get_price(d-1,'close'):
num = -1except:
pass
res.append(num*get_price(d,'volume'))
return res
#STO实现defget_STO(D,N):
a = 1.0/3defget_H_L(D,N):
high = 0
low = 0for i in range(N):
high = max(high,get_price(D-i,'high'))
low = min(low,get_price(D-i,'low'))
return [high,low]
defget_K(D,N):
high = get_H_L(D,N)[0]
low = get_H_L(D,N)[1]
RSV = 100*(get_price(D,'close')-low)/(high-low)
if D==N:
return a*RSV+(1-a)*50else:
return a*RSV+(1-a)*get_K(D-1,N)
defget_D(D,N):if D==N:
return a*get_K(D,N)+(1-a)*50else:
return a*get_K(D,N)+(1-a)*get_D(D-1,N)
K = get_K(D,N)
D = get_D(D,N)
return [K,D]
defget_sto(fro,to):
res = [[],[]]
for d in range(fro,to+1):
res[0].append(get_STO(d,9)[0])
res[1].append(get_STO(d,9)[1])
return res
defget_STO_r(fro,to,N):
a = 1.0/3defget_H_L(D,N):
high = 0
low = 0for i in range(N):
high = max(high,get_price(D-i,'high'))
low = min(low,get_price(D-i,'low'))
return [high,low]
defget_K(D,N):if D<=N:
return50
high = get_H_L(D,N)[0]
low = get_H_L(D,N)[1]
RSV = 100*(get_price(D,'close')-low)/(high-low)
return a*RSV+(1-a)*get_K(D-1,N)
K_res = []
for d in range(1,to+1):
K_res.append(get_K(d,N))
D_res = []
for i in range(to):
if i==0:
D_res.append(50)
else:
D_res.append(a*K_res[i]+(1.0-a)*D_res[-1])
K = K_res[fro-1:]
D = D_res[fro-1:]
return [K,D]
绘制k线图及STO指标
绘图代码:
defplot_STO(fro,to):
volume = get_volume(fro,to)
tmp = get_STO(fro,to,9)
K9 = tmp[0]
D9 = tmp[1]
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("Stochastic Oscillator(STO)")
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.set_ylabel("STO")
p3.set_xlabel("Dates")
p3.plot(date,K9, 'r-')
p3.plot(date,D9, 'g-')
p3.legend(('K9','D9'),loc='upper left')
plt.subplots_adjust(hspace=0)
plt.show()# show the plot on the screen
stock_hist = get_stock_hist(18)
plot_STO(200,800)