在Python中使用图表库在Volumestick图表上覆盖Volume Profile

时间:2019-10-23 07:05:53

标签: python matplotlib plotly bokeh plotly-dash

我想在python的烛台图上绘制Volume Profile,这将导致类似这样的情况。

CandleStickwithVolumeProfile

我的主要ohlc数据将位于pandas数据框中。

Date,       Open,  High,  Low,   Close
2019-10-18, 54.09, 54.62, 53.35, 53.78
2019-10-17, 52.99, 54.16, 52.62, 53.93
2019-10-16, 52.92, 53.74, 52.51, 53.36

然后我的Volume信息将位于另一个这样的数据框中。

Price, Volume
54.75, 150
54.50, 135
54.25, 140
54.00, 140
53.75, 125
53.50, 145
53.25, 130
53.00, 135
52.75, 155
52.50, 150

我尝试了我所知道的每个库,例如Matplotlib,Plotly和Bokeh。我试图简单地在烛台旁边绘制条形图,但是缩放比例通常是关闭的。我会对使用python中的任何标准图表库感兴趣,这些库会以相当简单的方式产生此结果。希望这里的某人知道某种方式。

2 个答案:

答案 0 :(得分:1)

好吧,我决定深入研究可扩展的文档,看看是否可以找到解决方法。事实证明这没什么大不了的。我开始越来越喜欢Plotly。

user@mac:[/]: df -h
Filesystem      Size   Used  Avail Capacity iused      ifree %iused  Mounted on
/dev/disk1s6   466Gi  9.9Gi   12Gi    46%  481819 4881971061    0%   /
devfs          205Ki  205Ki    0Bi   100%     717          0  100%   /dev
/dev/disk1s5   466Gi  141Gi   12Gi    93% 1410288 4881042592    0%   /System/Volumes/Data
/dev/disk1s4   466Gi  9.0Gi   12Gi    44%       6 4882452874    0%   /private/var/vm
map auto_home    0Bi    0Bi    0Bi   100%       0          0  100%   /System/Volumes/Data/home

不确定公开发布股票数据的法律是什么,所以我构建了一个简单的生成器来生成OHLC数据和交易量。实际的库存数据会使图表看起来更混乱。

VolumeProfileOnCandlestick

我还没弄清楚的地方是横条的一面。当前它们在左侧,但是将它们放在右侧会很好。应该是一件容易的事。

好吧,希望这对某人有所帮助。祝你有美好的一天!

答案 1 :(得分:0)

这对我有用:

def custom_round(x, base=5):
    return int(base * round(float(x)/base))

def round_and_group(data,base=5):
    df = data[['Last', 'Volume']].copy()
    #Round to nearest X
    df['Last'] = df['Last'].apply(lambda x: custom_round(x, base=base))
    # Remove the date index
    df = df.set_index('Last')
    df = df.groupby(['Last']).sum()
    return df

df=round_and_group(TTF,base=1)
df.reset_index(inplace=True)
plt.figure(figsize=(10,4), dpi=120) # 10 is width, 4 is height
ax1=plt.subplot(1,2,1)
plt.barh(df['Last'],df['Volume'], 2)
ax2= ax1.twiny()
candlestick2_ohlc(ax2,TTF['Open'],
                      TTF['High'],
                      TTF['Low'],
                      TTF['Last'],width = 0.6,colorup='g')

Fig: