def plot(self, code=None): if code is None: data=[] axis=[] for dates,row in self.data.iterrows(): open,high,low,close=row[1:5] datas=[open,close,low,high] axis.append(dates[0]) data.append(datas) path_name='.\QA_'+self.type+'_'+self.code[0]+'_'+self.if_fq+'.html' kline=Kline(self.code[0]+'__'+self.if_fq+'__'+self.type,width=1360,height=700) kline.add(self.code[0],axis,data,mark_point=["max","min"], is_datazoom_show=True,datazoom_orient='horizontal') kline.render(path_name) QA_util_log_info('The Pic has been saved to your path: %s'%path_name) else: data=[] axis=[] for dates,row in self.select_code(code).data.iterrows(): open,high,low,close=row[1:5] datas=[open,close,low,high] axis.append(dates[0]) data.append(datas) path_name='.\QA_'+self.type+'_'+code+'_'+self.if_fq+'.html' kline=Kline(code+'__'+self.if_fq+'__'+self.type,width=1360,height=700) kline.add(code,axis,data,mark_point=["max","min"], is_datazoom_show=True,datazoom_orient='horizontal') kline.render(path_name) QA_util_log_info('The Pic has been saved to your path: %s'%path_name)
def candLestick_mode(): v1 = [[2320.26, 2320.26, 2287.3, 2362.94], [2300, 2291.3, 2288.26, 2308.38], [2295.35, 2346.5, 2295.35, 2345.92], [2347.22, 2358.98, 2337.35, 2363.8], [2360.75, 2382.48, 2347.89, 2383.76], [2383.43, 2385.42, 2371.23, 2391.82], [2377.41, 2419.02, 2369.57, 2421.15], [2425.92, 2428.15, 2417.58, 2440.38], [2411, 2433.13, 2403.3, 2437.42], [2432.68, 2334.48, 2427.7, 2441.73], [2430.69, 2418.53, 2394.22, 2433.89], [2416.62, 2432.4, 2414.4, 2443.03], [2441.91, 2421.56, 2418.43, 2444.8], [2420.26, 2382.91, 2373.53, 2427.07], [2383.49, 2397.18, 2370.61, 2397.94], [2378.82, 2325.95, 2309.17, 2378.82], [2322.94, 2314.16, 2308.76, 2330.88], [2320.62, 2325.82, 2315.01, 2338.78], [2313.74, 2293.34, 2289.89, 2340.71], [2297.77, 2313.22, 2292.03, 2324.63], [2322.32, 2365.59, 2308.92, 2366.16], [2364.54, 2359.51, 2330.86, 2369.65], [2332.08, 2273.4, 2259.25, 2333.54], [2274.81, 2326.31, 2270.1, 2328.14], [2333.61, 2347.18, 2321.6, 2351.44], [2340.44, 2324.29, 2304.27, 2352.02], [2326.42, 2318.61, 2314.59, 2333.67], [2314.68, 2310.59, 2296.58, 2320.96], [2309.16, 2286.6, 2264.83, 2333.29], [2282.17, 2263.97, 2253.25, 2286.33], [2255.77, 2270.28, 2253.31, 2276.22]] kline = Kline("K 线图示例") kline.add("日K", ["2019/1/{}".format(i + 1) for i in range(31)], v1) kline.render('kline.html')
def draw_kline_pic(title: str, labels: list, data_package: list, y_min: int = 0, y_max: int = 100, y_formatter: str = "元人民币", path: str = './charts/k_line.html'): """ K线图 :param title: 画图标题 :param labels: 图例 :param data_package: 数据包 :param y_min: y轴最小值 :param y_max: y轴最大值 :param y_formatter: y轴的格式化 :param path: 保存的路径 """ style = Style(title_top="#fff", title_pos="left", width=1920, height=900) kline = Kline(title=title, renderer='svg', **style.init_style) kline.add('日K', labels, data_package, yaxis_min=y_min, yaxis_max=y_max, yaxis_formatter=y_formatter, mark_line=["min", "max"], mark_point=["min", "max"], is_datazoom_show=True, datazoom_type="both", datazoom_range=[80, 100]) kline.render(path=path)
def draw(code): import pandas as pd stock = pd.read_csv(code + '.csv', usecols=[0, 1, 2, 3, 4, 5, 6], encoding='gbk') stock.head() stock_new = stock.iloc[:180, :] stock_new_sorted = stock_new.sort_values('日期', ascending=True) stock_new_sorted.head() from pyecharts import Kline stock_code = stock_new_sorted['股票代码'][0] stock_name = stock_new_sorted['名称'][0] index = stock_new_sorted['日期'] v = [[o, close, lowest, highest] for o, close, lowest, highest in zip( stock_new_sorted['开盘价'], stock_new_sorted['收盘价'], stock_new_sorted['最低价'], stock_new_sorted['最高价'])] kline = Kline() kline.add(stock_name + '(' + stock_code + ')' + '日K线图', index, v, mark_point=["max"], is_datazoom_show=True) kline.render()
def test_kline_datazoom_horizontal(): kline = Kline("K 线图示例 - dataZoom 水平布局") kline.add("日K", ["2017/7/{}".format(i + 1) for i in range(31)], data, mark_point=["max"], is_datazoom_show=True) kline.render()
def test_kline_user_define_markline_style(): kline = Kline("K 线图-自定义标记点风格") kline.add("日K", DATE, data, mark_point=["min", "max"], mark_point_symbolsize=80, datazoom_orient='vertical', mark_line_valuedim=['lowest', 'highest']) kline.show_config() kline.render()
def test_kline_user_define_markline_style(): kline = Kline("K 线图示例 - 自定义标记线风格") kline.add("日K", ["2017/7/{}".format(i + 1) for i in range(31)], data, mark_line=["max"], mark_line_symbolsize=0, datazoom_orient='vertical', mark_line_valuedim='close') kline.render()
def plot(self, code=None): 'plot the market_data' if code is None: path_name = '.' + os.sep + 'QA_' + self.type + \ '_codepackage_' + self.if_fq + '.html' kline = Kline('CodePackage_' + self.if_fq + '_' + self.type, width=1360, height=700, page_title='QUANTAXIS') data_splits = self.splits() for i_ in range(len(data_splits)): data = [] axis = [] for dates, row in data_splits[i_].data.iterrows(): open, high, low, close = row[1:5] datas = [open, close, low, high] axis.append(dates[0]) data.append(datas) kline.add(self.code[i_], axis, data, mark_point=["max", "min"], is_datazoom_show=True, datazoom_orient='horizontal') kline.render(path_name) webbrowser.open(path_name) QA_util_log_info('The Pic has been saved to your path: %s' % path_name) else: data = [] axis = [] for dates, row in self.select_code(code).data.iterrows(): open, high, low, close = row[1:5] datas = [open, close, low, high] axis.append(dates[0]) data.append(datas) path_name = '.{}QA_{}_{}_{}.html'.format(os.sep, self.type, code, self.if_fq) kline = Kline('{}__{}__{}'.format(code, self.if_fq, self.type), width=1360, height=700, page_title='QUANTAXIS') kline.add(code, axis, data, mark_point=["max", "min"], is_datazoom_show=True, datazoom_orient='horizontal') kline.render(path_name) webbrowser.open(path_name) QA_util_log_info( 'The Pic has been saved to your path: {}'.format(path_name))
def plot_datastruct(__stock_hq_base, code=None): if code is None: path_name = '.' + os.sep + 'QA_' + __stock_hq_base.type + \ '_codepackage_' + __stock_hq_base.if_fq + '.html' kline = Kline('CodePackage_' + __stock_hq_base.if_fq + '_' + __stock_hq_base.type, width=1360, height=700, page_title='QUANTAXIS') data_splits = __stock_hq_base.splits() for i_ in range(len(data_splits)): data = [] axis = [] for dates, row in data_splits[i_].data.iterrows(): open, high, low, close = row[1:5] datas = [open, close, low, high] axis.append(dates[0]) data.append(datas) kline.add(__stock_hq_base.code[i_], axis, data, mark_point=["max", "min"], is_datazoom_show=True, datazoom_orient='horizontal') kline.render(path_name) webbrowser.open(path_name) QA_util_log_info('The Pic has been saved to your path: %s' % path_name) else: data = [] axis = [] for dates, row in __stock_hq_base.select_code(code).data.iterrows(): open, high, low, close = row[1:5] datas = [open, close, low, high] axis.append(dates[0]) data.append(datas) path_name = '.' + os.sep + 'QA_' + __stock_hq_base.type + \ '_' + code + '_' + __stock_hq_base.if_fq + '.html' kline = Kline(code + '__' + __stock_hq_base.if_fq + '__' + __stock_hq_base.type, width=1360, height=700, page_title='QUANTAXIS') kline.add(code, axis, data, mark_point=["max", "min"], is_datazoom_show=True, datazoom_orient='horizontal') kline.render(path_name) webbrowser.open(path_name) QA_util_log_info('The Pic has been saved to your path: %s' % path_name)
def test_kline_datazoom_vertical(): kline = Kline("K 线图-dataZoom 垂直布局") kline.add( "日K", DATE, data, mark_line=["max"], is_datazoom_show=True, datazoom_orient="vertical", ) kline.render()
def test_kline_datazoom_vertical(): kline = Kline("K 线图-dataZoom 垂直布局") kline.add( "日K", DATE, data, mark_line=["max"], is_datazoom_show=True, datazoom_orient='vertical', ) kline.render()
def test_kline_datazoom_vertical(): kline = Kline("K 线图示例 - dataZoom 垂直布局") kline.add( "日K", ["2017/7/{}".format(i + 1) for i in range(31)], data, mark_line=["max"], is_datazoom_show=True, datazoom_orient='vertical', ) kline.render()
def test_kline_user_define_markline_style(): title = "K 线图-自定义标记点风格" kline = Kline(title) kline.add("日K", DATE, data, mark_point=["min", "max"], mark_point_symbolsize=80, datazoom_orient='vertical', mark_line_valuedim=['lowest', 'highest']) kline.render() actual_content = get_default_rendering_file_content() assert 'lowest' in actual_content assert 'highest' in actual_content assert json.dumps(title) in actual_content
def create(self, code): rsbasic = bs.query_stock_basic(code) if len(rsbasic.data) > 0: data_basic_list = [] while (rsbasic.error_code == '0') & rsbasic.next(): data_basic_list.append(rsbasic.get_row_data()) result_basic = pd.DataFrame(data_basic_list, columns=rsbasic.fields) # print(result_basic.code) # print(result_basic.to_dict()['code_name'][0]) fields = "date,code,open,high,low,close" rs = bs.query_history_k_data_plus(code, fields, start_date=self.start_date, end_date=self.end_date, frequency="d", adjustflag="2") # frequency="d"取日k线,adjustflag="3"默认不复权, # 1:后复权;2:前复权 data_list = [] while (rs.error_code == '0') & rs.next(): # 获取一条记录,将记录合并在一起 data_list.append(rs.get_row_data()) result = pd.DataFrame(data_list, columns=rs.fields) result.index = pd.to_datetime(result.date) y = list(result.loc[:, ['open', 'close', 'low', 'high']].values) x = list(result.index.strftime('%Y%m%d')) kline = Kline(result_basic.to_dict()['code_name'][0], title_text_size=15) kline.add("", x, y, is_datazoom_show=False, mark_line=["average"], mark_point=["max", "min"], mark_point_symbolsize=60, mark_line_valuedim=['highest', 'lowest']) kline.render("baostock/%s.gif" % (code)) kline else: print("empty ----> " + rsbasic.code)
def test_kline_user_define_markline_style(): title = "K 线图-自定义标记点风格" kline = Kline(title) kline.add( "日K", DATE, data, mark_point=["min", "max"], mark_point_symbolsize=80, datazoom_orient="vertical", mark_line_valuedim=["lowest", "highest"], ) kline.render() actual_content = get_default_rendering_file_content() assert "lowest" in actual_content assert "highest" in actual_content assert json.dumps(title) in actual_content
def draw_kline(self, start, end, pro): # 输入起始时间、终止时间绘制K线图,并将K线图保存到本地 if self.code == 'None': return '' elif len(start) != 8 or len(end) != 8: return '' elif int(end[0:4]) > 2020: return '' elif int(start[4:6]) > 12 or int(end[4:6]) > 12: return '' elif int(start[6:8]) > 31 or ( int(start[6:8]) > 30 and (int(start[4:6]) == 2 or int(start[4:6]) == 4 or int(start[4:6]) == 6 or int(start[4:6]) == 9 or int(start[4:6]) == 11) or (int(start[6:8]) > 28 and int(start[4:6]) == 2)): return '' elif int(start) > int(end): return '' price = self.get_price(start, end, pro) price.index = pd.to_datetime(price.trade_date) price = price.sort_index() v1 = list(price.loc[:, ['open', 'close', 'low', 'high']].values) v0 = list(price.index.strftime('%Y%m%d')) kline = Kline(self.name + 'K线图', title_text_size=15) kline.add("", v0, v1, is_datazoom_show=True, mark_line=["average"], mark_point=["max", "min"], mark_point_symbolsize=60, mark_line_valuedim=['highest', 'lowest']) # 将K线图保存到本地 root_path = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) #kline.render(os.path.join(root_path, 'static', 'klines', self.code + '-' + start + '-' + end + '.html')) #尝试一下写到一个固定的临时文件,每次调用查看股票信息时删除,然后新建,以此来防止文件冲突和储存堆满 #临时文件为static/klines/temp_custom_date.html target_file = os.path.join(root_path, 'static', 'klines', 'temp_custom' + '.html') if os.path.exists(target_file): os.remove(target_file) kline.render(target_file) return kline
def plot(self, code=None): """plot the market_data""" if code is None: path_name = '.' + os.sep + 'QA_' + self.type + \ '_codepackage_' + self.if_fq + '.html' kline = Kline('CodePackage_' + self.if_fq + '_' + self.type, width=1360, height=700, page_title='QUANTAXIS') data_splits = self.splits() for i_ in range(len(data_splits)): data = [] axis = [] for dates, row in data_splits[i_].data.iterrows(): open, high, low, close = row[1:5] datas = [open, close, low, high] axis.append(dates[0]) data.append(datas) kline.add(self.code[i_], axis, data, mark_point=[ "max", "min"], is_datazoom_show=True, datazoom_orient='horizontal') kline.render(path_name) webbrowser.open(path_name) QA_util_log_info( 'The Pic has been saved to your path: %s' % path_name) else: data = [] axis = [] for dates, row in self.select_code(code).data.iterrows(): open, high, low, close = row[1:5] datas = [open, close, low, high] axis.append(dates[0]) data.append(datas) path_name = '.{}QA_{}_{}_{}.html'.format( os.sep, self.type, code, self.if_fq) kline = Kline('{}__{}__{}'.format(code, self.if_fq, self.type), width=1360, height=700, page_title='QUANTAXIS') kline.add(code, axis, data, mark_point=[ "max", "min"], is_datazoom_show=True, datazoom_orient='horizontal') kline.render(path_name) webbrowser.open(path_name) QA_util_log_info( 'The Pic has been saved to your path: {}'.format(path_name))
def plot_datastruct(_quotation_base, code=None): if code is None: path_name = '.' + os.sep + 'QA_' + _quotation_base.type + \ '_codepackage_' + _quotation_base.if_fq + '.html' kline = Kline('CodePackage_' + _quotation_base.if_fq + '_' + _quotation_base.type, width=1360, height=700, page_title='QUANTAXIS') data_splits = _quotation_base.splits() for i_ in range(len(data_splits)): data = [] axis = [] for dates, row in data_splits[i_].data.iterrows(): open, high, low, close = row[1:5] datas = [open, close, low, high] axis.append(dates[0]) data.append(datas) kline.add(_quotation_base.code[i_], axis, data, mark_point=[ "max", "min"], is_datazoom_show=True, datazoom_orient='horizontal') kline.render(path_name) webbrowser.open(path_name) QA_util_log_info('The Pic has been saved to your path: %s' % path_name) else: data = [] axis = [] for dates, row in _quotation_base.select_code(code).data.iterrows(): open, high, low, close = row[1:5] datas = [open, close, low, high] axis.append(dates[0]) data.append(datas) path_name = '.' + os.sep + 'QA_' + _quotation_base.type + \ '_' + code + '_' + _quotation_base.if_fq + '.html' kline = Kline(code + '__' + _quotation_base.if_fq + '__' + _quotation_base.type, width=1360, height=700, page_title='QUANTAXIS') kline.add(code, axis, data, mark_point=[ "max", "min"], is_datazoom_show=True, datazoom_orient='horizontal') kline.render(path_name) webbrowser.open(path_name) QA_util_log_info('The Pic has been saved to your path: %s' % path_name)
def view(code, start_, end_): ''' 使用pyecharts展示数据 :param code: 股票代码 :param start_: 开始时间(包含)格式为'2018-01-01' :param end_: 结束时间(包含)格式为'2018-01-01' :return: ''' # 从数据库获取数据 th = get_data_from_db(code, start_, end_) ''' 官网网址:http://pyecharts.org/#/zh-cn/charts?id=klinecandlestick%ef%bc%88k%e7%ba%bf%e5%9b%be%ef%bc%89 Kline.add() 方法签名 add(name, x_axis, y_axis, **kwargs) name -> str 图例名称 x_axis -> list x 坐标轴数据 y_axis -> [list], 包含列表的列表 y 坐标轴数据。数据中,每一行是一个『数据项』,每一列属于一个『维度』。 数据项具体为 [open, close, lowest, highest] (即:[开盘值, 收盘值, 最低值, 最高值]) ''' # 此处存放的是K线数组 v1 = th.values.tolist() # 存放X轴横坐标数组,此处选用的是日期 v2 = [str(x.strftime('%Y-%m-%d')) for x in th.index] kline = Kline("{0} K线图".format(code)) ''' is_datazoom_show:是否显示放大缩小 mark_point:标记最大最小值 ''' kline.add("日K", v2, v1, is_datazoom_show=True, mark_point=["min", "max"]) # 打印配置信息 kline.show_config() try: kline.render('股票[{0}]K线图.html'.format(code)) except Exception as e: print('生成图形出错啦[{0}]'.format(e))
def create_view(): # open, close, lowest, highest] v1 = [] kline = Kline("日K线图") rows = SQLiteUtil.select_all() down_flag = [] for row in rows: down_flag.append(row[0]) v1.append([row[3], row[4], row[1], row[2]]) kline.add( "日K", down_flag, v1, mark_point=["max"], is_datazoom_show=True, ) kline.render(path="../view/day.html") v1 = [] kline = Kline("7天K线图") rows = SQLiteUtil.select_group_by_week() down_flag = [] for row in rows: down_flag.append(row.get("week_flag")) v1.append([ row.get("first_price"), row.get("last_price"), row.get("min_price"), row.get("max_price") ]) kline.add( "周K", down_flag, v1, mark_point=["max"], is_datazoom_show=True, ) kline.render(path="../view/week.html") v1 = [] kline = Kline("30天K线图") rows = SQLiteUtil.select_group_by_moon() down_flag = [] for row in rows: down_flag.append(row.get("moon_flag")) v1.append([ row.get("first_price"), row.get("last_price"), row.get("min_price"), row.get("max_price") ]) kline.add( "月K", down_flag, v1, mark_point=["max"], is_datazoom_show=True, ) kline.render(path="../view/moon.html")
def test_kline(): # kline_0 v1 = [[2320.26, 2320.26, 2287.3, 2362.94], [2300, 2291.3, 2288.26, 2308.38], [2295.35, 2346.5, 2295.35, 2345.92], [2347.22, 2358.98, 2337.35, 2363.8], [2360.75, 2382.48, 2347.89, 2383.76], [2383.43, 2385.42, 2371.23, 2391.82], [2377.41, 2419.02, 2369.57, 2421.15], [2425.92, 2428.15, 2417.58, 2440.38], [2411, 2433.13, 2403.3, 2437.42], [2432.68, 2334.48, 2427.7, 2441.73], [2430.69, 2418.53, 2394.22, 2433.89], [2416.62, 2432.4, 2414.4, 2443.03], [2441.91, 2421.56, 2418.43, 2444.8], [2420.26, 2382.91, 2373.53, 2427.07], [2383.49, 2397.18, 2370.61, 2397.94], [2378.82, 2325.95, 2309.17, 2378.82], [2322.94, 2314.16, 2308.76, 2330.88], [2320.62, 2325.82, 2315.01, 2338.78], [2313.74, 2293.34, 2289.89, 2340.71], [2297.77, 2313.22, 2292.03, 2324.63], [2322.32, 2365.59, 2308.92, 2366.16], [2364.54, 2359.51, 2330.86, 2369.65], [2332.08, 2273.4, 2259.25, 2333.54], [2274.81, 2326.31, 2270.1, 2328.14], [2333.61, 2347.18, 2321.6, 2351.44], [2340.44, 2324.29, 2304.27, 2352.02], [2326.42, 2318.61, 2314.59, 2333.67], [2314.68, 2310.59, 2296.58, 2320.96], [2309.16, 2286.6, 2264.83, 2333.29], [2282.17, 2263.97, 2253.25, 2286.33], [2255.77, 2270.28, 2253.31, 2276.22]] kline = Kline("K 线图示例") kline.add("日K", ["2017/7/{}".format(i + 1) for i in range(31)], v1) kline.show_config() kline.render() # kline_1 kline = Kline("K 线图示例") kline.add("日K", ["2017/7/{}".format(i + 1) for i in range(31)], v1, mark_point=["max"], is_datazoom_show=True) kline.show_config() kline.render() # kline_2 kline = Kline("K 线图示例") kline.add("日K", ["2017/7/{}".format(i + 1) for i in range(31)], v1, mark_point=["max"], is_datazoom_show=True, datazoom_orient='vertical') kline.show_config() kline.render()
#正常显示画图时出现的中文和负号 if __name__ == '__main__': token = '8b09761b8473e73a0a57bafa07ea2766dfba5d3472d527c40e03d47b' pro = ts.pro_api(token) from pylab import mpl mpl.rcParams['font.sans-serif'] = ['SimHei'] mpl.rcParams['axes.unicode_minus'] = False # 获取平安银行日行情数据 pa = pro.daily(ts_code='000001.SZ', start_date='20180101', end_date='20190106') # pa.head() # K线图可视化 pa.index = pd.to_datetime(pa.trade_date) pa = pa.sort_index() v1 = list(pa.loc[:, ['open', 'close', 'low', 'high']].values) t = pa.index v0 = list(t.strftime('%Y%m%d')) kline = Kline("平安银行K线图", title_text_size=15) kline.add("", v0, v1, is_datazoom_show=True, mark_line=["average"], mark_point=["max", "min"], mark_point_symbolsize=60, mark_line_valuedim=['highest', 'lowest']) kline.render("上证指数图.html")
def test_kline_datazoom_horizontal(): kline = Kline("K 线图-dataZoom 水平布局") kline.add("日K", DATE, data, mark_point=["max"], is_datazoom_show=True) kline.render()
def test_kline_default(): kline = Kline("K 线图-默认示例") kline.add("日K", DATE, data) kline.render()
#bs.logout() result.info() #将某些object转化numeric result=result.apply(pd.to_numeric, errors='ignore') result.info() result.close.plot(figsize=(16,8)) ax = plt.gca() ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') #plt.show() from pyecharts import Kline y=list(result.loc[:,['open','close','low','high']].values) x=list(result.index.strftime('%Y%m%d')) kline = Kline("000831K线图",title_text_size=15) kline.add("", x, y,is_datazoom_show=False, mark_line=["average"], mark_point=["max", "min"], mark_point_symbolsize=60, mark_line_valuedim=['highest', 'lowest'] ) kline.render("gif/000831_bao.gif") kline print()
import tushare as ts import pandas as pd ts.set_token('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx') pro = ts.pro_api() df1 = pro.daily(ts_code='000938.SZ', start_date='20150401', end_date='20180930') df = df1.sort_values(by=['trade_date']) df.reset_index(level=0, inplace=True) df.drop(['index'], axis=1, inplace=True) print(df) df.to_csv("aaa.csv") date = df.trade_date.tolist() data = [] for idx in df.index: row = [ df.iloc[idx]['open'], df.iloc[idx]['close'], df.iloc[idx]['low'], df.iloc[idx]['high'] ] data.append(row) kline = Kline("K 线图示例") kline.add( "日K", date, data, mark_point=["max"], is_datazoom_show=True, ) kline.render()
v1 = [[2320.26, 2320.26, 2287.3, 2362.94], [2300, 2291.3, 2288.26, 2308.38], [2295.35, 2346.5, 2295.35, 2345.92], [2347.22, 2358.98, 2337.35, 2363.8], [2360.75, 2382.48, 2347.89, 2383.76], [2383.43, 2385.42, 2371.23, 2391.82], [2377.41, 2419.02, 2369.57, 2421.15], [2425.92, 2428.15, 2417.58, 2440.38], [2411, 2433.13, 2403.3, 2437.42], [2432.68, 2334.48, 2427.7, 2441.73], [2430.69, 2418.53, 2394.22, 2433.89], [2416.62, 2432.4, 2414.4, 2443.03], [2441.91, 2421.56, 2418.43, 2444.8], [2420.26, 2382.91, 2373.53, 2427.07], [2383.49, 2397.18, 2370.61, 2397.94], [2378.82, 2325.95, 2309.17, 2378.82], [2322.94, 2314.16, 2308.76, 2330.88], [2320.62, 2325.82, 2315.01, 2338.78], [2313.74, 2293.34, 2289.89, 2340.71], [2297.77, 2313.22, 2292.03, 2324.63], [2322.32, 2365.59, 2308.92, 2366.16], [2364.54, 2359.51, 2330.86, 2369.65], [2332.08, 2273.4, 2259.25, 2333.54], [2274.81, 2326.31, 2270.1, 2328.14], [2333.61, 2347.18, 2321.6, 2351.44], [2340.44, 2324.29, 2304.27, 2352.02], [2326.42, 2318.61, 2314.59, 2333.67], [2314.68, 2310.59, 2296.58, 2320.96], [2309.16, 2286.6, 2264.83, 2333.29], [2282.17, 2263.97, 2253.25, 2286.33], [2255.77, 2270.28, 2253.31, 2276.22]] kline = Kline("K线图示例") kline.add("日K", ["2018/7/{}".format(i + 1) for i in range(31)], v1) kline.render(path="kline.html")
def plot(self, code=None): def kline_formater(param): return param.name + ':' + vars(param) """plot the market_data""" if code is None: path_name = '.' + os.sep + 'QA_' + self.type + \ '_codepackage_' + self.if_fq + '.html' kline = Kline('CodePackage_' + self.if_fq + '_' + self.type, width=1360, height=700, page_title='QUANTAXIS') bar = Bar() data_splits = self.splits() for ds in data_splits: data = [] axis = [] if ds.data_type[-3:] == 'day': datetime = np.array(ds.date.map(str)) else: datetime = np.array(ds.datetime.map(str)) ohlc = np.array(ds.data.loc[:, ['open', 'close', 'low', 'high']]) #amount = np.array(ds.amount) #vol = np.array(ds.volume) kline.add(ds.code[0], datetime, ohlc, mark_point=["max", "min"], is_datazoom_show=False, datazoom_orient='horizontal') kline.render(path_name) webbrowser.open(path_name) QA_util_log_info('The Pic has been saved to your path: %s' % path_name) else: data = [] axis = [] ds = self.select_code(code) data = [] #axis = [] if self.data_type[-3:] == 'day': datetime = np.array(ds.date.map(str)) else: datetime = np.array(ds.datetime.map(str)) ohlc = np.array(ds.data.loc[:, ['open', 'close', 'low', 'high']]) #amount = np.array(ds.amount) vol = np.array(ds.volume) kline = Kline('{}__{}__{}'.format(code, self.if_fq, self.type), width=1360, height=700, page_title='QUANTAXIS') bar = Bar() kline.add( self.code, datetime, ohlc, mark_point=["max", "min"], # is_label_show=True, is_datazoom_show=True, is_xaxis_show=False, # is_toolbox_show=True, tooltip_formatter='{b}:{c}', # kline_formater, # is_more_utils=True, datazoom_orient='horizontal') bar.add(self.code, datetime, vol, is_datazoom_show=True, datazoom_xaxis_index=[0, 1]) path_name = '.{}QA_{}_{}_{}.html'.format(os.sep, self.type, code, self.if_fq) # kline.add(code, axis, data, mark_point=[ # "max", "min"], is_datazoom_show=True, datazoom_orient='horizontal') grid = Grid(width=1360, height=700, page_title='QUANTAXIS') grid.add(bar, grid_top="80%") grid.add(kline, grid_bottom="30%") grid.render(path_name) webbrowser.open(path_name) QA_util_log_info( 'The Pic has been saved to your path: {}'.format(path_name))
basic = pro.stock_basic(list_status='L') pa = pro.daily(ts_code='000831.SZ', start_date='20190101', end_date='20190220') from pyecharts import Kline #from pyecharts.engine import create_default_environment pa.index = pd.to_datetime(pa.trade_date) pa = pa.sort_index() y = list(pa.loc[:, ['open', 'close', 'low', 'high']].values) t = pa.index x = list(t.strftime('%Y%m%d')) kline = Kline("000831K线图", title_text_size=15) kline.add("", x, y, is_datazoom_show=False, mark_line=["average"], mark_point=["max", "min"], mark_point_symbolsize=60, mark_line_valuedim=['highest', 'lowest']) # create_default_environment(filet_ype) # file_type: 'html', 'svg', 'png', 'jpeg', 'gif' or 'pdf' #env = create_default_environment('jpeg') #env.render_chart_to_file(kline, path='line.jpeg') kline.render("gif/000831.gif") kline
def test_kline_default(): kline = Kline("K 线图示例 - 默认") kline.add("日K", ["2017/7/{}".format(i + 1) for i in range(31)], data) kline.render()
#-*-coding:utf-8 from pyecharts import Kline import pandas_datareader.data as web import datetime df_stockload = web.DataReader("000001.SS", "yahoo", datetime.datetime(2018, 1, 1), datetime.datetime(2019, 1, 1)) kline = Kline("行情显示图") # 数据转换 ohlc = list( zip(df_stockload.Open, df_stockload.Close, df_stockload.Low, df_stockload.High)) dates = df_stockload.index.strftime('%Y-%m-%d') kline.add("日K", dates, ohlc, is_datazoom_show=True, mark_line=["max"], mark_point=["max"], xaxis_rotate=30, yaxis_min=0.9 * min(df_stockload["Low"])) kline.show_config() kline.render(r'k.html')
def test_custom(): # custom_0 attr = ['A', 'B', 'C', 'D', 'E', 'F'] v1 = [10, 20, 30, 40, 50, 60] v2 = [15, 25, 35, 45, 55, 65] v3 = [38, 28, 58, 48, 78, 68] bar = Bar("Line - Bar 示例") bar.add("bar", attr, v1) line = Line() line.add("line", v2, v3) bar.custom(line.get_series()) bar.show_config() bar.render() # custom_1 v1 = [10, 20, 30, 40, 50, 60] v2 = [30, 30, 30, 30, 30, 30] v3 = [50, 50, 50, 50, 50, 50] v4 = [10, 10, 10, 10, 10, 10] es = EffectScatter("Scatter - EffectScatter 示例") es.add("es", v1, v2) scatter = Scatter() scatter.add("scatter", v1, v3) es.custom(scatter.get_series()) es_1 = EffectScatter() es_1.add("es_1", v1, v4, symbol='pin', effect_scale=5) es.custom(es_1.get_series()) es.show_config() es.render() # custom_2 import random v1 = [[2320.26, 2320.26, 2287.3, 2362.94], [2300, 2291.3, 2288.26, 2308.38], [2295.35, 2346.5, 2295.35, 2345.92], [2347.22, 2358.98, 2337.35, 2363.8], [2360.75, 2382.48, 2347.89, 2383.76], [2383.43, 2385.42, 2371.23, 2391.82], [2377.41, 2419.02, 2369.57, 2421.15], [2425.92, 2428.15, 2417.58, 2440.38], [2411, 2433.13, 2403.3, 2437.42], [2432.68, 2334.48, 2427.7, 2441.73], [2430.69, 2418.53, 2394.22, 2433.89], [2416.62, 2432.4, 2414.4, 2443.03], [2441.91, 2421.56, 2418.43, 2444.8], [2420.26, 2382.91, 2373.53, 2427.07], [2383.49, 2397.18, 2370.61, 2397.94], [2378.82, 2325.95, 2309.17, 2378.82], [2322.94, 2314.16, 2308.76, 2330.88], [2320.62, 2325.82, 2315.01, 2338.78], [2313.74, 2293.34, 2289.89, 2340.71], [2297.77, 2313.22, 2292.03, 2324.63], [2322.32, 2365.59, 2308.92, 2366.16], [2364.54, 2359.51, 2330.86, 2369.65], [2332.08, 2273.4, 2259.25, 2333.54], [2274.81, 2326.31, 2270.1, 2328.14], [2333.61, 2347.18, 2321.6, 2351.44], [2340.44, 2324.29, 2304.27, 2352.02], [2326.42, 2318.61, 2314.59, 2333.67], [2314.68, 2310.59, 2296.58, 2320.96], [2309.16, 2286.6, 2264.83, 2333.29], [2282.17, 2263.97, 2253.25, 2286.33], [2255.77, 2270.28, 2253.31, 2276.22]] attr = ["2017/7/{}".format(i + 1) for i in range(31)] kline = Kline("Kline - Line 示例") kline.add("日K", attr, v1) line_1 = Line() line_1.add("line-1", attr, [random.randint(2400, 2500) for _ in range(31)]) line_2 = Line() line_2.add("line-2", attr, [random.randint(2400, 2500) for _ in range(31)]) kline.custom(line_1.get_series()) kline.custom(line_2.get_series()) kline.show_config() kline.render()
# ts.set_token(token) pro = ts.pro_api(token) # 获取当前上市的股票代码、简称、注册地、行业、上市时间等数据 basic = pro.stock_basic(list_status='L') # 查看前五行数据 # basic.head(5) # 获取日行情数据 pa = pro.daily(ts_code='000711.SZ', start_date='20180101', end_date='20190106') # pa.head() pa.index = pd.to_datetime(pa.trade_date) pa = pa.sort_index() v1 = list(pa.loc[:, ['open', 'close', 'low', 'high']].values) t = pa.index v0 = list(t.strftime('%Y%m%d')) kline = Kline("K线图", title_text_size=15) kline.add("", v0, v1, is_datazoom_show=True, mark_line=["average"], mark_point=["max", "min"], mark_point_symbolsize=60, mark_line_valuedim=['highest', 'lowest']) kline.render("K线图.html") kline