PlotCandlestick
PlotCandlestick(price_df,title,bar_name,begin_date,ma,ma_func,trade_data)
K线图是投资者常用的分析工具。在K线图中,除了画出K线和均线之外,还可以自己定义想画的曲线(如一些技术指标)。而且在K线图中也可以标记买卖点。
- 参数说明
| 参数 | 类型 | 说明 |
|---|---|---|
| prie_df | DataFrame | columns必须包含'open','high','low','close','volume' 5列数据,index必须为日期和时间类型,不能为str |
| title | str | 图表正标题 |
| bar_name | str | k线图例名称 |
| begin_date | str | 由于计算均线前序期,故只展示此日期后的数据及均线 |
| ma | list | 计算均线的窗口列表,最大为6根均线 |
| ma_func | 自定义均线函数 | 可以为talib.EMA,talib.SMA等 |
| trade_data | DataFrame | columns必须包含'side','date','price'3列数据,用于标记交易点 |
- trade_data参数详情说明 传入的DataFrame中dated的数据类型必须为datetime.datetime
| date | side | price | |
|---|---|---|---|
| 0 | 2014-04-14 | Buy | 11.89 |
| 1 | 2014-05-09 | Sell | 11.28 |
| 2 | 2014-06-06 | Buy | 11.83 |
| 3 | 2014-11-03 | Sell | 11.39 |
| 4 | 2014-11-14 | Buy | 11.62 |
| 5 | 2015-09-02 | Sell | 15.20 |
返回结果为:封面图
以下依赖聚宽数据
from jqdata import *
# 引入常用库
import talib # 技术分析
import pandas as pd
import datetime as dt
import plotly.graph_objs as go
import plotly.offline as offline
offline.init_notebook_mode(connected=True)
# 显示K先图
## 适用plotly 版本为3.4.2
def PlotCandlestick(price_df: pd.DataFrame, title: str, bar_name: str,
begin_date: str, ma: list, ma_func,
trade_data: pd.DataFrame):
'''
prace_df:含有完整数据的表列名必须包含OHLC index-date
title:图表名称
begin_date:从什么时候开始,因为计算均线有前序期
ma:均线计算的周期列表,最大显示6根均线
ma_func:计算均线的方法函数
trade_data:交易点 列名必须包含date,side,price
===============================================
return 可视化图表
'''
trade_data = trade_data.copy()
trade_data['date'] = trade_data['date'].dt.strftime('%Y-%m-%d')
begin = get_trade_days(end_date=begin_date, count=10)[0]
slice_df = price_df.loc[begin:]
fig = go.Figure(data=[
go.Candlestick(
x=slice_df.index,
open=slice_df['open'],
high=slice_df['high'],
low=slice_df['low'],
close=slice_df['close'],
name=bar_name)
])
# 计算均线
ma_df = pd.DataFrame({i: ma_func(price_df['close'], i) for i in ma[:6]},
index=price_df.index)
ma_df.columns = ma
# 添加均线
for i in ma:
fig.add_trace(
go.Scatter(
x=ma_df.reindex(slice_df.index).index,
y=ma_df[i].reindex(slice_df.index),
name='MA' + str(i),
line=dict(width=1.5),
))
# 买点
fig.add_trace(
go.Scatter(
mode='markers',
x=trade_data.query('side=="Buy"')['date'].values.tolist(),
y=trade_data.query('side=="Buy"')['price'].values.tolist(),
marker=dict(symbol=5, size=10, color="Crimson", line=dict(width=2)),
name='Buy'))
# 买点
fig.add_trace(
go.Scatter(
mode='markers',
x=trade_data.query('side=="Sell"')['date'].values.tolist(),
y=trade_data.query('side=="Sell"')['price'].values.tolist(),
marker=dict(
symbol=6, size=10, color="ForestGreen", line=dict(width=2)),
name='Sell'))
fig.layout.update(
title=title,
legend=dict(x=0.1, y=1.1, orientation="h"),
xaxis=dict(rangeslider=dict(visible=False), tickformat='%Y-%m-%d'))
offline.iplot(fig)
symbol = '000651.XSHE'
start = '2014-04-14'
end = '2015-09-02'
dis_name = get_security_info(symbol).display_name
# 获取数据
begin_date = get_trade_days(end_date=start,count=250)[0]
price_df = get_price(symbol,start_date=begin_date,end_date=end,fields=['close','open','high','low'],panel=False)
# 交易点数据 (我这里是举例自己构建的交易点)
trade_data = pd.DataFrame(
[[dt.datetime(2014, 4, 14), 'Buy', 11.89
], [dt.datetime(2014, 5, 9), 'Sell', '11.28'],
[dt.datetime(2014, 6, 6), 'Buy', 11.83], [dt.date(2014, 11, 3), 'Sell', 11.39],
[dt.datetime(2014, 11, 14), 'Buy', 11.62], [dt.date(2015, 9, 2), 'Sell', 15.2]
],
columns=['date', 'side', 'price'])
# 可视化
PlotCandlestick(price_df,dis_name+'行情',dis_name,start,[5,10,20,60,120,250],talib.SMA,trade_data)