[032量化交易] python互交式显示 bokeh绘制K线图

该博客展示了如何利用Python的tushare和bokeh库来获取并可视化股票数据。首先,通过tushare接口获取股票历史数据,然后将日期转换为datetime类型,确定数据的最高和最低点。接着,使用bokeh创建图表,设置了x轴为日期类型,并绘制了股票的高低点以及升跌情况。最后,通过vbar方法显示了每日收盘价与开盘价的关系,并展示可视化结果。
摘要由CSDN通过智能技术生成
from bokeh.models import LinearAxis, Range1d
from bokeh.plotting import figure, show
from bokeh.layouts import row, column
from bokeh.layouts import gridplot
import pandas as pd
import tushare as ts

# 初始化pro接口
pro = ts.pro_api('')

print("获取股票数据.")
example01 = pro.daily(ts_code='000006.SZ', start_date='20190101', end_date='20190301')

# print(example01)
print(type(example01))

example01['date'] = pd.to_datetime(example01['trade_date'])
print(example01['date'])
example01_max = max(example01['high'].tolist())
example01_min = min(example01['low'].tolist())
# 设置条件
inc01 = example01.close > example01.open
dec01 = example01.open > example01.close

print(example01)
w = 12 * 60 * 60 * 1000
bond_code = '123120'
stock_code = '300263'

TOOLS = "pan,wheel_zoom,box_zoom,reset,save"
# 通过传入exmple01de min,max控制figure的展示
p = figure(x_axis_type="datetime", tools=TOOLS,
           y_range=(example01_min, example01_max),
           width=500, title="{}+{}".format(bond_code, stock_code))
p.xaxis.major_label_orientation = 3.14 / 4
p.grid.grid_line_alpha = 0.3

p.segment(example01.date, example01.high, example01.date, example01.low, color="black")
p.vbar(example01.date[inc01], w, example01.open[inc01], example01.close[inc01],
       fill_color="red",
       line_color="black")
p.vbar(example01.date[dec01], w, example01.open[dec01], example01.close[dec01],
       fill_color="green",
       line_color="black")

show(p)

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Bokeh绘制K线图,您可以按照以下步骤进行操作: 1. 首先,确保您已经安装了Bokeh库。可以通过运行以下命令来安装它: ``` pip install bokeh ``` 2. 导入必要的库: ```python import pandas as pd from bokeh.plotting import figure, show from bokeh.io import output_notebook from bokeh.sampledata.stocks import AAPL # 示例数据中包含了一个股票数据集,这里使用AAPL(苹果公司)作为示例 ``` 3. 将示例数据加载到Pandas的DataFrame中: ```python # 将示例数据加载到DataFrame中 df = pd.DataFrame(AAPL) # 将时间列设置为索引列,并按照升序排序 df['date'] = pd.to_datetime(df['date']) df = df.set_index('date').sort_index() ``` 4. 创建一个Bokeh图表,并将K线图数据添加到图表中: ```python # 设置输出为Notebook output_notebook() # 创建一个新的Bokeh图表 p = figure(title='AAPL Candlestick Chart', x_axis_type='datetime', plot_width=800, plot_height=400) # 绘制K线图 p.segment(df.index, df['high'], df.index, df['low'], color='black') p.vbar(df.index[df['close'] > df['open']], width=0.5, bottom=df['open'][df['close'] > df['open']], top=df['close'][df['close'] > df['open']], fill_color='green', line_color='black') p.vbar(df.index[df['close'] < df['open']], width=0.5, bottom=df['close'][df['close'] < df['open']], top=df['open'][df['close'] < df['open']], fill_color='red', line_color='black') # 显示图表 show(p) ``` 这样,您就可以在Bokeh绘制K线图了。请注意,上述示例使用了示例数据,您可以根据需要替换为自己的股票数据。希望对您有所帮助!如有任何问题,请随时向我提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值