我曾经在图书馆里看到一本书,在那本书里作者提出了一种股票预测算法,即历史与未来中心对称于当下,我当时就花了一天时间写下了这个代码。
# -*- coding: utf-8 -*-
"""
Created on Fri Apr 7 16:41:01 2017
@author: Administrator
"""
import matplotlib.pyplot as plt
import numpy as np
import numpy
from matplotlib.dates import DateFormatter, WeekdayLocator, DayLocator, MONDAY,YEARLY
from matplotlib.finance import quotes_historical_yahoo_ohlc, candlestick_ohlc
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
ticker = '600023' # 600028 是"中国石化"的股票代码
ticker += '.ss' # .ss 表示上证 .sz表示深证
date1 = (2016, 6, 5) # 起始日期,格式:(年,月,日)元组
date2 = (2017, 6,28) # 结束日期,格式:(年,月,日)元组
mondays = WeekdayLocator(MONDAY) # 主要刻度
alldays = DayLocator() # 次要刻度
#weekFormatter = DateFormatter('%b %d') # 如:Jan 12
mondayFormatter = DateFormatter('%m-%d-%Y') # 如:2-29-2015
dayFormatter = DateFormatter('%d') # 如:12
quotes = quotes_historical_yahoo_ohlc(ticker, date1, date2)
#np_qu=[]
#for x in range(len(quotes)):
# np_qu.append()
mid=np.array(quotes)
mm=np.ones((len(mid),6))
av=(mid[len(mid)-1,1]+mid[len(mid)-1,2]+mid[len(mid)-1,3]+mid[len(mid)-1,4])*0.25
for x in range(len(mid)):
mm[x,0]=2*mid[len(mid)-1,0]-mid[x,0]
mm[x,1]=2*av-mid[x,4]
mm[x,2]=2*av-mid[x,3]
mm[x,3]=2*av-mid[x,2]
mm[x,4]=2*av-mid[x,1]
mm[x,5]=2*mid[len(mid)-1,5]-mid[x,5]
merge=np.concatenate((mid,mm))
if len(merge) == 0:
raise SystemExit
fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.2)
ax.xaxis.set_major_locator(mondays)
ax.xaxis.set_minor_locator(alldays)
ax.xaxis.set_major_formatter(mondayFormatter)
#ax.xaxis.set_minor_formatter(dayFormatter)
#plot_day_summary(ax, quotes, ticksize=3)
candlestick_ohlc(ax, merge, width=0.6, colorup='r', colordown='g')
ax.xaxis_date()
ax.autoscale_view()
plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')
ax.grid(True)
plt.title('浙能电力 600023')
plt.show()
后来,当我再次运行这个代码时出现了bug .可能是雅虎金融不再提供数据了。我用采用tushare 库导入数据重写了这段代码。在运行之前,请安装tushare.
"""
Created on Sat Jul 1 15:57:50 2017
罗干
中国海诚
"""
import matplotlib.pyplot as plt
import numpy as np
#import numpy
from matplotlib.dates import DateFormatter, WeekdayLocator, DayLocator, MONDAY,YEARLY
from matplotlib.finance import quotes_historical_yahoo_ohlc, candlestick_ohlc
#import matplotlib
import tushare as ts
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.pylab import date2num
import datetime
df=ts.get_hist_data('000002',start='2017-04-25',end='2017-06-30')
fig = plt.gcf()
hist_data=df
# 创建子图
fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.2)
# 设置X轴刻度为日期时间
ax.xaxis_date()
plt.xticks(rotation=45)
plt.yticks()
plt.title("股票代码:000002")
plt.xlabel("时间")
plt.ylabel("股价(元)")
#candlestick_ohlc(ax,data_list,width=1.5,colorup='r',colordown='g')
plt.grid()
hist_data=df .sort_index()
# 对tushare获取到的数据转换成candlestick_ohlc()方法可读取的格式
data_list = []
for dates,row in hist_data.iterrows():
# 将时间转换为数字
date_time = datetime.datetime.strptime(dates,'%Y-%m-%d')
t = date2num(date_time)
open,high,close,low = row[:4]
datas = (t,open,high,low,close)
data_list.append(datas)
mondays = WeekdayLocator(MONDAY) # 主要刻度
alldays = DayLocator() # 次要刻度
#weekFormatter = DateFormatter('%b %d') # 如:Jan 12
mondayFormatter = DateFormatter('%m-%d-%Y') # 如:2-29-2015
dayFormatter = DateFormatter('%d') # 如:12
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
candlestick_ohlc(ax,data_list,width=1.5,colorup='r',colordown='g')
mid=np.array(data_list)
mm=np.ones((len(mid),5))
mm=np.ones((len(mid),5))
av=(mid[len(mid)-1,1]+mid[len(mid)-1,2]+mid[len(mid)-1,3]+mid[len(mid)-1,4])*0.25
for x in range(len(mid)):
mm[x,0]=2*mid[len(mid)-1,0]-mid[x,0]
mm[x,1]=2*av-mid[x,4]
mm[x,2]=2*av-mid[x,3]
mm[x,3]=2*av-mid[x,2]
mm[x,4]=2*av-mid[x,1]
merge=np.concatenate((mm,mid))
if len(merge) == 0:
raise SystemExit
fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.2)
ax.xaxis.set_major_locator(mondays)
ax.xaxis.set_minor_locator(alldays)
ax.xaxis.set_major_formatter(mondayFormatter)
#ax.xaxis.set_minor_formatter(dayFormatter)
#plot_day_summary(ax, quotes, ticksize=3)
candlestick_ohlc(ax, merge, width=0.6, colorup='r', colordown='g')
ax.xaxis_date()
ax.autoscale_view()
plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')
ax.grid(True)
plt.title('000002')
plt.show()
预测下万科吧,祝没有王石的万科好运!
代码下载