…接上文,简单的一个买点信号:
今天:ma5 > ma20 且 前天:ma5 < ma20 则 买入
# -*- coding: utf-8 -*- class maimaixinhao: def __init__(self, date, ma5, ma20): self.date = date self.ma5 = ma5 self.ma20 = ma20 def bspoint(self): bdate = [] bma5 = [] for i in range(2, len(self.ma5)): if self.ma5[i] > self.ma20[i] and self.ma5[i-2] < self.ma20[i-2]: bdate.append(self.date[i]) bma5.append(self.ma5[i]) return bdate, bma5
在图上显示出来
# -*- coding: utf-8 -*- import maimaixinhao import os import tushare import pandas import matplotlib.pyplot as plot from matplotlib.finance import candlestick_ochl # 清屏 clea = os.system('cls') # 获取上证指数历史数据 sh = tushare.get_hist_data('sh') # 根据需要选择数据 date = pandas.Series([i for i in range(0, len(sh))]) o = sh['open'] h = sh['high'] c = sh['close'] l = sh['low'] volume = sh['volume'] ma5 = sh['ma5'] ma10 = sh['ma10'] ma20 = sh['ma20'] # 按列合并成一个 quotes = [] for i in range(0, len(sh)): quotes.append((date[i], o[i], c[i], h[i], l[i], volume[i])) # 获取图表实例 figure = plot.figure('Figure') # 上图 subf1 = figure.add_subplot(211, title='Index of Shanghai', xlabel='date', ylabel='index', xlim=[min(date), max(date)]) candlestick_ochl(subf1, quotes, colorup='r', colordown='g') l11 = subf1.plot(date, ma5) l12 = subf1.plot(date, ma10) l13 = subf1.plot(date, ma20) b11 = subf1.bar(left=date, height=[i*100/min(volume) for i in volume], bottom=0, width=0.8, color='c', edgecolor='c') plot.grid(True, axis='both') # 下图 subf2 = figure.add_subplot(212, title='Singal of Buy or Sell', xlabel='date', ylabel='index', xlim=[min(date), max(date)]) l21 = subf2.plot(date, ma5, 'g-') l22 = subf2.plot(date, ma20, 'r-') b21 = subf2.bar(left=date, height=[ma5[i]-ma20[i] for i in range(0, len(date))], bottom=0, color='c', edgecolor='c') buy = maimaixinhao.maimaixinhao(date, ma5, ma20) bdate, bma5 = buy.bspoint() subf2.plot(bdate, bma5, 'ro') plot.grid(True, axis='both') plot.legend(('ma5', 'ma20', 'buy'))