help(T.plot)
# 样例数据准备
# 以 平安银行 (000001.SZA) 的股票数据为例
df = DataSource('bar1d_CN_STOCK_A').read('000001.SZA', start_date='2015-01-01', end_date='2017-02-01',
fields=['open', 'high', 'low', 'close', 'adjust_factor', 'amount'])
# 日收益
df['return_1'] = df['close'] / df['close'].shift(1) - 1
# 5日收益
df['return_5'] = df['close'] / df['close'].shift(5) - 1
# # 历史数据默认为后复权价格,除以 adjust_factor 得到实际价格
# df['open'] /= df['adjust_factor']
# df['high'] /= df['adjust_factor']
# df['low'] /= df['adjust_factor']
# df['close'] /= df['adjust_factor']
# 设置 date 为index,index将被用作x轴
df.set_index('date', inplace=True)
# 设置index的名字为None,在x轴将不显示x轴数据的名字
df.index.name = None
df.tail()
# 显示股票收盘价
T.plot(df[['close']], title='收盘价', chart_type='line')
T.plot(df[['amount']], title='成交额', chart_type='column')
T.plot(df[['low', 'high']], title='最高最低价格', chart_type='area', stock=False)
T.plot(df[['open', 'high', 'low', 'close', 'return_1', 'return_5']],
# high、low显示在第一栏,高度40%,open、close显示在第二栏,其他的在最后一栏
panes=[['high', 'low', '40%'], ['open', 'close', '40%']],
# height=500,设置高度为500
options={'chart':{'height': 500}})
T.plot(df[['open', 'high', 'low', 'close', 'amount']],
# 设置图表title和高度;'series': [{},{'type': 'column'}] 设置第二个数据系列(即 amount)显示类型为柱状图(column)
options={'chart': {'title': '股票数据分析','height': 600}, 'series': [{'color': 'green','lineColor': 'green','upColor': 'red','upLineColor': 'red'},{'type': 'column'}]},
stock=True, candlestick=True)
用options参数来自己实现分栏显示
# 参考 文档 http://api.highcharts.com/highstock/yAxis
y_axis = [
{
'labels': {'align': 'right', 'x': -3 },
'title': {'text': '价格'},
'height': '50%', 'lineWidth': 2
},
{
'labels': {'align': 'right','x': -3},
'title': {'text': '日收益'},
'top': '55%', 'height': '25%', 'offset': 0, 'lineWidth': 2
},
{
'labels': {'align': 'right', 'x': -3},
'title': {'text': '交易额'},
'top': '85%', 'height': '25%', 'offset': 0, 'lineWidth': 2,
}
]
# series:只覆盖我们需要修改的字段,这里是 yAxis
options = {
'chart': {'type': 'spline', 'height': 500},
'title': {'text': '股票数据分析'},
'yAxis': y_axis,
'series': [{},{'yAxis': 1},{'yAxis': 2, 'type': 'column'}]
}
T.plot(df[['close', 'return_1', 'amount']], options=options, stock=True)
def candlestick_processor(df_options):
# df_options:为df转化后的数据
# print(df_options)
# 蜡烛图
# 生成ohlc:从series中读取OHLC数据(前四列)合并
ohlc_series = {}
sma_series={}
other_series = []
series = df_options['series']
for s in series:
if s['name'] in {'open', 'high', 'low', 'close'}:
ohlc_series[s['name']] = s
elif s['name'] in {'sma_10'}:
sma_series[s['name']]=s
else:
other_series.append(s)
if len(ohlc_series) != 4:
print('【错误】蜡烛图,没有找到open, high, low, close数据,请确保输入的数据有这四列')
return None
ohlc_data = []
for i in range(0, len(ohlc_series['open']['data'])):
row = [ohlc_series['open']['data'][i][0]]
for j in ['open', 'high', 'low', 'close']:
row.append(ohlc_series[j]['data'][i][1])
ohlc_data.append(row)
sma_data=[]
for i in range(0,len(sma_series['sma_10']['data'])):
xrow = [sma_series['sma_10']['data'][i][0]]
for j in ['sma_10']:
xrow.append(sma_series[j]['data'][i][1])
sma_data.append(xrow)
df_options['series'] = [{'data': ohlc_data, 'name': 'OHLC', 'yAxis': 0, 'type': 'candlestick','color': 'green','lineColor': 'green','upColor': 'red','upLineColor': 'red'},
{'data':sma_data,'name':'sma_10','yAxis':0,'type':'spline',}] + other_series
df_options['chart'] = {'height': 800}
if other_series:
# 设置其他数据使用第二个y轴
for s in other_series:
s.update({'yAxis':1, 'type': 'column'})
y_axis = [
{
'labels': {'align': 'right', 'x': -3},
'title': {'text': 'OHLC'},
'height': '60%', 'lineWidth': 2
},
{
'labels': {'align': 'right', 'x': -3},
'top': '65%', 'height': '35%', 'offset': 0, 'lineWidth': 2,
}
]
df_options = T.deep_update(df_options, {'yAxis': y_axis})
return df_options
df['sma_10'] = talib.SMA(np.array(df['close'].astype('double')), 10)
T.plot(df, options=candlestick_processor, title='股票数据分析', stock=True)
# output可取值 object / display,默认为display,表示直接显示。object对应highchart/highstock的输入数据,用于高级需求自定义。
T.plot(df[['close']].head(3), title='收盘价', chart_type='line', output='object')