注意: pyecharts本身存在bug , 如果绘图出现空白 , ctrl shift p 搜索 "信任notebook" ,点击信任 , 如果网页一直处于加载状态可以多刷新几次页面试试
还有问题的话可以重新建立一个notebook执行上述步骤
用于在研究中获取回测信息,并将买卖点绘制在K线图中, 使用了pyecharts库 , 动手能力强的小伙伴们可以自己使用返回的overlip添加技术指标等显示在图上
回测ID(backtestId):每个回测的唯一标识
TRADE_PLOT(ID = "回测ID")
初始化后将获得 trade_data 属性 , 是回测的所有交易记录详情
set_security('601866.XSHG' ,fq_date="last_day" ,forward_day=20 )
overlap = trade_plot.plot_base_bar() #绘制K线图
overlap
后续您也也可以自己添加副图或者主图指标到K线图中去 , 同时上传了py文件 plot_k.py , 可以直接上传到研究中 ,使用时 from plot_k import * 就可以直接调用了
import pandas as pd
from jqdata import *
import talib
from pyecharts import *
class TRADE_PLOT(object):
def __init__(self,ID,security = None):
online('https://cdn.bootcss.com/echarts/4.1.0.rc2') #可能因为网络/pyecharts问题加载不出图像 , 网页没有加载的话可能需要多刷新几次
self.ID = ID #回测ID
self.security = security #绘制K图的标的选择
self.trade_data = self.get_trade_info(self.ID,self.security) #交易详情
self.price_data = None
def get_trade_info(self,ID,security):
'''获取交易数据'''
data = pd.DataFrame(get_backtest(ID).get_orders())
data['trade_date'] = pd.to_datetime(data.time).apply(lambda x : pd.to_datetime(x).date())
return data
def set_security(self , security,forward_day=20,fq_date="last_day"):
""" 设置需要查看的标的
security = None , #需要查看的标的代码
forward_day = 20 , #K线图第一次交易及最后一次交易前后回溯的交易日个数(剔除停牌)
fq_date="last_day #复权基准日, 默认K线的结束日期 ,其他可选参数详见API , get_bars"""
self.security = security
self.trade_data_security = self.trade_data[self.trade_data.security==security]
if len(self.trade_data_security) ==0:
print ('交易记录中没有该标的'.format(self.security))
self.price_data = self.get_price_data(self.security,self.trade_data_security,forward_day,fq_date)
def get_price_data(self,security,trade_data,forward_day,fq_date):
'''获取价格数据'''
# start_date= get_trade_days(end_date = trade_data.trade_date.iloc[0],count=forward_day)[0]
last_date = trade_data.trade_date.iloc[-1] + datetime.timedelta(days = (forward_day*2 + 1))
_after_days = get_trade_days(start_date=trade_data.trade_date.iloc[-1], end_date=last_date)
end_date = _after_days[:forward_day][-1]
day_range = get_trade_days( trade_data.trade_date.iloc[0] ,end_date)
count = len(day_range)+forward_day
if fq_date == 'last_day':
fq_date = end_date
p_df = pd.DataFrame(get_bars(security,end_dt=end_date,count=count,include_now=True,fq_ref_date=fq_date,
fields=['date','open', 'high', 'low', 'close','volume'])).set_index('date')
# get_price(security,end_date =end_date ,start_date= start_date ,fq=fq )
return p_df
def plot_base_bar(self):
'''K线买卖点图'''
security = self.security
trade_data_security = self.trade_data_security
price_data = self.price_data
kline = Kline(get_security_info(security).display_name,)
kline.add(security,price_data.index , price_data.loc[:,['open','close','low','high']].values,
is_datazoom_show=True,datazoom_range=[0,len(price_data)*2], datazoom_type= "both")
kline._option['series'][0]['itemStyle']={'normal':{'color':'#ef232a', 'color0': '#14b143','borderColor': '#ef232a', 'borderColor0': '#14b143'}}
es = EffectScatter("trade")
buy_data = trade_data_security[trade_data_security.action== 'open']
#symbol: 'circle', 'rect', 'roundRect', 'triangle', 'diamond', 'pin', 'arrow'
es.add('buy',buy_data.trade_date , buy_data.price,symbol='triangle')#symbol_size=20,is_label_show=True)
buy_data = trade_data_security[trade_data_security.action== 'close']
es.add('sell',buy_data.trade_date , buy_data.price,symbol='diamond')#,is_label_show=True)
es._option['series'][0]['itemStyle']={'normal':{'color':['#1d18ad']}}#, 'color0': '#140012'}}
es._option['series'][1]['itemStyle']={'normal':{'color':['#140012']}}
bar = Bar()
bar.add("volume", price_data.index, price_data["volume"].values,is_datazoom_show=True,
tooltip_tragger="axis", is_legend_show=True, is_yaxis_show=False, yaxis_max=10*(price_data["volume"].max()))
es2 = EffectScatter("除权标识")
factor = get_price(security,start_date=price_data.index[0],end_date=price_data.index[-1],
fields='factor',fq='post').factor.drop_duplicates(keep='first')[1:]
factor.index = factor.index.date
# print (factor)
es2.add('除权标识',factor.index , factor.values ,is_datazoom_show=True,
tooltip_tragger="axis", is_legend_show=True, is_yaxis_show=True, yaxis_max=factor.max()*10)
overlap = Overlap(height=400,width=990)
overlap.add(kline) #K线主图
overlap.add(es2,yaxis_index=1, is_add_yaxis=True) #除权标记
overlap.add(es) # 买卖点信息
overlap.add(bar,yaxis_index=2, is_add_yaxis=True) #成交量图
return overlap
def plot_m_k(self,end_date,count,unit='5m',fq_ref_date=None):
'''绘制分钟/bar线图'''
security = self.security
price_data = pd.DataFrame(get_bars(security,end_dt=end_date,unit=unit,fq_ref_date=fq_ref_date,count=count,
fields=['date','open','close','low','high','volume'],include_now=True)).set_index('date')
kline = Kline(get_security_info(security).display_name+' {}线'.format(unit))
kline.add(security,price_data.index , price_data.loc[:,['open','close','low','high']].values,
is_datazoom_show=True,datazoom_range=[0,len(price_data)], datazoom_type= "both")
kline._option['series'][0]['itemStyle']={'normal':{'color':'#ef232a', 'color0': '#14b143','borderColor': '#ef232a', 'borderColor0': '#14b143'}}
overlap = Overlap(height=400,width=990)
bar = Bar()
bar.add("volume", price_data.index, price_data["volume"].values,is_datazoom_show=True,
tooltip_tragger="axis", is_legend_show=True, is_yaxis_show=False, yaxis_max=10*max(price_data["volume"]))
overlap.add(kline)
overlap.add(bar,yaxis_index=1, is_add_yaxis=True)
return overlap
回测ID(backtestId):每个回测的唯一标识
初始化后将获得 trade_data 属性 , 是回测的所有交易记录详情
trade_plot = TRADE_PLOT(ID = "d3f8331cc9591c2d8d85cf2aaa348a23") #回测ID , backtestID
trade_plot.trade_data #全部交易详情
# trade_plot.trade_data.groupby("security")['security'].count().sort_values(ascending=False) #查看各个标的的交易次数
action | amount | commission | filled | gains | limit_price | match_time | price | security | security_name | side | status | time | type | trade_date | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | open | 8300 | 13.32 | 8300 | 0.0 | 0 | 2016-06-01 09:30:00 | 5.35 | 600350.XSHG | 山东高速 | long | done | 2016-06-01 09:30:00 | market | 2016-06-01 |
1 | open | 8500 | 13.39 | 8500 | 0.0 | 0 | 2016-06-01 09:30:00 | 5.25 | 600018.XSHG | 上港集团 | long | done | 2016-06-01 09:30:00 | market | 2016-06-01 |
2 | open | 6500 | 13.28 | 6500 | 0.0 | 0 | 2016-06-01 09:30:00 | 6.81 | 601158.XSHG | 重庆水务 | long | done | 2016-06-01 09:30:00 | market | 2016-06-01 |
3 | open | 6900 | 13.31 | 6900 | 0.0 | 0 | 2016-06-01 09:30:00 | 6.43 | 601699.XSHG | 潞安环能 | long | done | 2016-06-01 09:30:00 | market | 2016-06-01 |
4 | open | 7000 | 13.40 | 7000 | 0.0 | 0 | 2016-06-01 09:30:00 | 6.38 | 000540.XSHE | 中天城投 | long | done | 2016-06-01 09:30:00 | market | 2016-06-01 |
5 | open | 3500 | 13.11 | 3500 | 0.0 | 0 | 2016-06-01 09:30:00 | 12.49 | 000686.XSHE | 东北证券 | long | done | 2016-06-01 09:30:00 | market | 2016-06-01 |
6 | open | 600 | 11.76 | 600 | 0.0 | 0 | 2016-06-01 09:30:00 | 65.32 | 000963.XSHE | 华东医药 | long | done | 2016-06-01 09:30:00 | market | 2016-06-01 |
7 | open | 2000 | 12.85 | 2000 | 0.0 | 0 | 2016-06-01 09:30:00 | 21.41 | 000895.XSHE | 双汇发展 | long | done | 2016-06-01 09:30:00 | market | 2016-06-01 |
8 | open | 4900 | 13.42 | 4900 | 0.0 | 0 | 2016-06-01 09:30:00 | 9.13 | 600016.XSHG | 民生银行 | long | done | 2016-06-01 09:30:00 | market | 2016-06-01 |
9 | open | 8400 | 13.36 | 8400 | 0.0 | 0 | 2016-06-01 09:30:00 | 5.30 | 000937.XSHE | 冀中能源 | long | done | 2016-06-01 09:30:00 | market | 2016-06-01 |
10 | open | 2100 | 13.06 | 2100 | 0.0 | 0 | 2016-06-01 09:30:00 | 20.73 | 600703.XSHG | 三安光电 | long | done | 2016-06-01 09:30:00 | market | 2016-06-01 |
11 | open | 1800 | 13.08 | 1800 | 0.0 | 0 | 2016-06-01 09:30:00 | 24.23 | 601238.XSHG | 广汽集团 | long | done | 2016-06-01 09:30:00 | market | 2016-06-01 |
12 | open | 5000 | 13.40 | 5000 | 0.0 | 0 | 2016-06-01 09:30:00 | 8.93 | 600717.XSHG | 天津港 | long | done | 2016-06-01 09:30:00 | market | 2016-06-01 |
13 | open | 2400 | 12.98 | 2400 | 0.0 | 0 | 2016-06-01 09:30:00 | 18.03 | 601009.XSHG | 南京银行 | long | done | 2016-06-01 09:30:00 | market | 2016-06-01 |
14 | open | 10400 | 13.45 | 10400 | 0.0 | 0 | 2016-06-01 09:30:00 | 4.31 | 601866.XSHG | 中海集运 | long | done | 2016-06-01 09:30:00 | market | 2016-06-01 |
15 | open | 9200 | 13.36 | 9200 | 0.0 | 0 | 2016-06-01 09:30:00 | 4.84 | 600028.XSHG | 中国石化 | long | done | 2016-06-01 09:30:00 | market | 2016-06-01 |
16 | open | 2800 | 13.20 | 2800 | 0.0 | 0 | 2016-06-01 09:30:00 | 15.72 | 002500.XSHE | 山西证券 | long | done | 2016-06-01 09:30:00 | market | 2016-06-01 |
17 | open | 10800 | 13.41 | 10800 | 0.0 | 0 | 2016-06-01 09:30:00 | 4.14 | 600157.XSHG | 永泰能源 | long | done | 2016-06-01 09:30:00 | market | 2016-06-01 |
18 | open | 3000 | 13.36 | 3000 | 0.0 | 0 | 2016-06-01 09:30:00 | 14.84 | 002142.XSHE | 宁波银行 | long | done | 2016-06-01 09:30:00 | market | 2016-06-01 |
19 | open | 5200 | 13.24 | 5200 | 0.0 | 0 | 2016-06-01 09:30:00 | 8.49 | 600089.XSHG | 特变电工 | long | done | 2016-06-01 09:30:00 | market | 2016-06-01 |
20 | open | 800 | 12.01 | 800 | 0.0 | 0 | 2016-06-01 09:30:00 | 50.06 | 002007.XSHE | 华兰生物 | long | done | 2016-06-01 09:30:00 | market | 2016-06-01 |
21 | open | 1300 | 12.76 | 1300 | 0.0 | 0 | 2016-06-01 09:30:00 | 32.73 | 601318.XSHG | 中国平安 | long | done | 2016-06-01 09:30:00 | market | 2016-06-01 |
22 | open | 5200 | 13.23 | 5200 | 0.0 | 0 | 2016-06-01 09:30:00 | 8.48 | 601992.XSHG | 金隅股份 | long | done | 2016-06-01 09:30:00 | market | 2016-06-01 |
23 | open | 1100 | 12.38 | 1100 | 0.0 | 0 | 2016-06-01 09:30:00 | 37.53 | 002252.XSHE | 上海莱士 | long | done | 2016-06-01 09:30:00 | market | 2016-06-01 |
24 | open | 1700 | 13.06 | 1700 | 0.0 | 0 | 2016-06-01 09:30:00 | 25.60 | 300024.XSHE | 机器人 | long | done | 2016-06-01 09:30:00 | market | 2016-06-01 |
25 | open | 2900 | 13.18 | 2900 | 0.0 | 0 | 2016-06-01 09:30:00 | 15.15 | 600111.XSHG | 北方稀土 | long | done | 2016-06-01 09:30:00 | market | 2016-06-01 |
26 | open | 1700 | 12.72 | 1700 | 0.0 | 0 | 2016-06-01 09:30:00 | 24.95 | 600867.XSHG | 通化东宝 | long | done | 2016-06-01 09:30:00 | market | 2016-06-01 |
27 | open | 3500 | 13.37 | 3500 | 0.0 | 0 | 2016-06-01 09:30:00 | 12.73 | 601555.XSHG | 东吴证券 | long | done | 2016-06-01 09:30:00 | market | 2016-06-01 |
28 | open | 5500 | 13.36 | 5500 | 0.0 | 0 | 2016-06-01 09:30:00 | 8.10 | 600369.XSHG | 西南证券 | long | done | 2016-06-01 09:30:00 | market | 2016-06-01 |
29 | open | 10200 | 13.37 | 10200 | 0.0 | 0 | 2016-06-01 09:30:00 | 4.37 | 600578.XSHG | 京能电力 | long | done | 2016-06-01 09:30:00 | market | 2016-06-01 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
946 | open | 400 | 5.00 | 400 | 0.0 | 0 | 2016-06-30 09:30:00 | 32.65 | 002230.XSHE | 科大讯飞 | long | done | 2016-06-30 09:30:00 | market | 2016-06-30 |
947 | open | 1200 | 5.00 | 1200 | 0.0 | 0 | 2016-06-30 09:30:00 | 11.92 | 600153.XSHG | 建发股份 | long | done | 2016-06-30 09:30:00 | market | 2016-06-30 |
948 | open | 1400 | 5.00 | 1400 | 0.0 | 0 | 2016-06-30 09:30:00 | 10.35 | 600021.XSHG | 上海电力 | long | done | 2016-06-30 09:30:00 | market | 2016-06-30 |
949 | open | 3400 | 5.00 | 3400 | 0.0 | 0 | 2016-06-30 09:30:00 | 4.33 | 600578.XSHG | 京能电力 | long | done | 2016-06-30 09:30:00 | market | 2016-06-30 |
950 | open | 6500 | 5.00 | 6500 | 0.0 | 0 | 2016-06-30 09:30:00 | 2.31 | 000725.XSHE | 京东方A | long | done | 2016-06-30 09:30:00 | market | 2016-06-30 |
951 | open | 2100 | 5.00 | 2100 | 0.0 | 0 | 2016-06-30 09:30:00 | 6.95 | 600583.XSHG | 海油工程 | long | done | 2016-06-30 09:30:00 | market | 2016-06-30 |
952 | open | 1400 | 5.00 | 1400 | 0.0 | 0 | 2016-06-30 09:30:00 | 10.68 | 601800.XSHG | 中国交建 | long | done | 2016-06-30 09:30:00 | market | 2016-06-30 |
953 | open | 3800 | 5.00 | 3800 | 0.0 | 0 | 2016-06-30 09:30:00 | 3.90 | 601991.XSHG | 大唐发电 | long | done | 2016-06-30 09:30:00 | market | 2016-06-30 |
954 | open | 300 | 5.00 | 300 | 0.0 | 0 | 2016-06-30 09:30:00 | 40.67 | 601336.XSHG | 新华保险 | long | done | 2016-06-30 09:30:00 | market | 2016-06-30 |
955 | open | 200 | 5.00 | 200 | 0.0 | 0 | 2016-06-30 09:30:00 | 67.45 | 300017.XSHE | 网宿科技 | long | done | 2016-06-30 09:30:00 | market | 2016-06-30 |
956 | open | 800 | 5.00 | 800 | 0.0 | 0 | 2016-06-30 09:30:00 | 17.96 | 601607.XSHG | 上海医药 | long | done | 2016-06-30 09:30:00 | market | 2016-06-30 |
957 | open | 2300 | 5.00 | 2300 | 0.0 | 0 | 2016-06-30 09:30:00 | 6.47 | 601006.XSHG | 大秦铁路 | long | done | 2016-06-30 09:30:00 | market | 2016-06-30 |
958 | open | 1400 | 5.00 | 1400 | 0.0 | 0 | 2016-06-30 09:30:00 | 10.19 | 601186.XSHG | 中国铁建 | long | done | 2016-06-30 09:30:00 | market | 2016-06-30 |
959 | open | 800 | 5.00 | 800 | 0.0 | 0 | 2016-06-30 09:30:00 | 16.96 | 600816.XSHG | 安信信托 | long | done | 2016-06-30 09:30:00 | market | 2016-06-30 |
960 | open | 600 | 5.00 | 600 | 0.0 | 0 | 2016-06-30 09:30:00 | 24.07 | 600637.XSHG | 东方明珠 | long | done | 2016-06-30 09:30:00 | market | 2016-06-30 |
961 | open | 2700 | 5.00 | 2700 | 0.0 | 0 | 2016-06-30 09:30:00 | 5.51 | 601118.XSHG | 海南橡胶 | long | done | 2016-06-30 09:30:00 | market | 2016-06-30 |
962 | open | 2800 | 5.00 | 2800 | 0.0 | 0 | 2016-06-30 09:30:00 | 5.26 | 601106.XSHG | 中国一重 | long | done | 2016-06-30 09:30:00 | market | 2016-06-30 |
963 | open | 1600 | 5.00 | 1600 | 0.0 | 0 | 2016-06-30 09:30:00 | 9.34 | 600704.XSHG | 物产中大 | long | done | 2016-06-30 09:30:00 | market | 2016-06-30 |
964 | open | 1000 | 5.00 | 1000 | 0.0 | 0 | 2016-06-30 09:30:00 | 13.86 | 600959.XSHG | 江苏有线 | long | done | 2016-06-30 09:30:00 | market | 2016-06-30 |
965 | open | 2100 | 5.00 | 2100 | 0.0 | 0 | 2016-06-30 09:30:00 | 7.09 | 601390.XSHG | 中国中铁 | long | done | 2016-06-30 09:30:00 | market | 2016-06-30 |
966 | open | 800 | 5.00 | 800 | 0.0 | 0 | 2016-06-30 09:30:00 | 17.90 | 600485.XSHG | 信威集团 | long | done | 2016-06-30 09:30:00 | market | 2016-06-30 |
967 | close | 900 | 11.01 | 900 | -144.0 | 0 | 2016-06-30 14:35:00 | 6.68 | 002146.XSHE | 荣盛发展 | long | done | 2016-06-30 14:35:00 | market | 2016-06-30 |
968 | close | 400 | 10.70 | 400 | 12.0 | 0 | 2016-06-30 14:35:00 | 14.24 | 001979.XSHE | 招商蛇口 | long | done | 2016-06-30 14:35:00 | market | 2016-06-30 |
969 | close | 9700 | 410.58 | 9700 | 679.0 | 0 | 2016-06-30 14:35:00 | 32.56 | 000858.XSHE | 五粮液 | long | done | 2016-06-30 14:35:00 | market | 2016-06-30 |
970 | close | 700 | 10.78 | 700 | 28.0 | 0 | 2016-06-30 14:35:00 | 8.25 | 600674.XSHG | 川投能源 | long | done | 2016-06-30 14:35:00 | market | 2016-06-30 |
971 | close | 600 | 9.99 | 600 | -144.0 | 0 | 2016-06-30 14:35:00 | 8.31 | 000876.XSHE | 新希望 | long | done | 2016-06-30 14:35:00 | market | 2016-06-30 |
972 | close | 1000 | 10.86 | 1000 | 22.5 | 0 | 2016-06-30 14:35:00 | 5.86 | 600705.XSHG | 中航资本 | long | done | 2016-06-30 14:35:00 | market | 2016-06-30 |
973 | close | 300 | 10.10 | 300 | -195.0 | 0 | 2016-06-30 14:35:00 | 17.01 | 002450.XSHE | 康得新 | long | done | 2016-06-30 14:35:00 | market | 2016-06-30 |
974 | close | 1000 | 14.37 | 1000 | 20.0 | 0 | 2016-06-30 14:35:00 | 9.37 | 601009.XSHG | 南京银行 | long | done | 2016-06-30 14:35:00 | market | 2016-06-30 |
975 | close | 1200 | 12.67 | 1200 | -48.0 | 0 | 2016-06-30 14:35:00 | 6.39 | 000069.XSHE | 华侨城A | long | done | 2016-06-30 14:35:00 | market | 2016-06-30 |
976 rows × 15 columns
设定股票后可以获取的属性 :
set_security(self , security,p_f_day=20,fq_date="last_day")
""" 设置需要查看的标的
security = None , #需要查看的标的代码
p_f_day = 20 , #K线图第一次交易及最后一次交易前后回溯的交易日个数(剔除停牌)
fq_date="last_day #复权基准日, 默认K线的结束日期 ,其他可选参数详见API , get_bars"""
trade_plot.set_security('601866.XSHG' ,fq_date="last_day" ,forward_day=20 ) #设置需要查看的标的,复权基准日及K线回溯天数,None为不复权
# trade_plot.price_data #查看当前证券的行情数据
trade_plot.trade_data_security #当前证券交易记录
action | amount | commission | filled | gains | limit_price | match_time | price | security | security_name | side | status | time | type | trade_date | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
14 | open | 10400 | 13.45 | 10400 | 0.0 | 0 | 2016-06-01 09:30:00 | 4.31 | 601866.XSHG | 中海集运 | long | done | 2016-06-01 09:30:00 | market | 2016-06-01 |
345 | close | 10400 | 55.43 | 10400 | -2184.0 | 0 | 2016-06-13 14:35:00 | 4.10 | 601866.XSHG | 中海集运 | long | done | 2016-06-13 14:35:00 | market | 2016-06-13 |
overlap = trade_plot.plot_base_bar() #绘制K线图
overlap
trade_plot.plot_base_bar() 返回一个pyecharts 的 overlap , 大家也可以按照需求增加各种指标或副图上去 ,下边以最简单的ma为例
需要注意 xaxis_index 需要和K线使用的索引数据类型(datetime.date)保持一致,否则无法显示
def plot_ma(data,short,long):
ma_short= price_data.close.rolling(window=short).mean().round(2)
ma_long =price_data.close.rolling(window=long).mean().round(2)
line = Line()
line.add("ma{}".format(short),ma_short.index, ma_short.values)
line.add("ma{}".format(long),ma_long.index, ma_long.values)
return line
trade_plot.set_security('002385.XSHE',forward_day = 30,fq_date=None) #变更标的,检查 fq_date 参数及除权标记是否生效
overlap2 = trade_plot.plot_base_bar()
price_data = trade_plot.price_data
overlap2.add(plot_ma(price_data,5,10)) # 添加MA图
本社区仅针对特定人员开放
查看需注册登录并通过风险意识测评
5秒后跳转登录页面...
移动端课程