我正在绘制 5、15、30 和 60 分钟蜡烛的堆叠图。
我想请:
- 让所有价格图表都右对齐(
y_on_right=True
似乎没有使用) - 对于 5 分钟图表上的时间/网格,每小时每 60 分钟
- 对于所有其他图表使用与上述相同的,每 60 分钟,全部对齐
- 如果可能的话,可以选择删除左侧和右侧的空间(因此第一个栏靠左边缘,最后一个栏靠右边缘)
到目前为止,这是我的输出:
代码如下:
import mplfinance as mpf
import pandas as pd
from polygon import RESTClient
def main():
key = "key"
with RESTClient(key) as client:
start = "2019-02-01"
end = "2019-02-02"
ticker = "TVIX"
resp5 = client.stocks_equities_aggregates(ticker, 5, "minute", start, end, unadjusted=False)
resp15 = client.stocks_equities_aggregates(ticker, 15, "minute", start, end, unadjusted=False)
resp30 = client.stocks_equities_aggregates(ticker, 30, "minute", start, end, unadjusted=False)
resp60 = client.stocks_equities_aggregates(ticker, 60, "minute", start, end, unadjusted=False)
print(f'5 min data is {len(resp5.results)} long')
print(f'15 min data is {len(resp15.results)} long')
print(f'30 min data is {len(resp30.results)} long')
print(f'60 min data is {len(resp60.results)} long')
df5 = pd.DataFrame(resp5.results)
df5.index = pd.DatetimeIndex( pd.to_datetime(df5['t']/1000, unit='s') )
df15 = pd.DataFrame(resp15.results)
df15.index = pd.DatetimeIndex( pd.to_datetime(df15['t']/1000, unit='s') )
df30 = pd.DataFrame(resp30.results)
df30.index = pd.DatetimeIndex( pd.to_datetime(df30['t']/1000, unit='s') )
df60 = pd.DataFrame(resp60.results)
df60.index = pd.DatetimeIndex( pd.to_datetime(df60['t']/1000, unit='s') )
df60.index.name = df30.index.name = df15.index.name = df5.index.name = 'Timestamp'
# mpf expects a dataframe containing Open, High, Low, and Close data with a Pandas TimetimeIndex
df60.columns = df30.columns = df15.columns = df5.columns = ['Volume', 'Volume Weighted', 'Open', 'Close', 'High', 'Low', 'Time', 'Num Items']
fig = mpf.figure(figsize=(32, 32))
ax1 = fig.add_subplot(4, 1, 1)
ax2 = fig.add_subplot(4, 1, 2)
ax3 = fig.add_subplot(4, 1, 3)
ax4 = fig.add_subplot(4, 1, 4)
ap = [
mpf.make_addplot(df15, type='candle', ax=ax2, y_on_right=True),
mpf.make_addplot(df30, type='candle', ax=ax3, y_on_right=True),
mpf.make_addplot(df60, type='candle', ax=ax4, y_on_right=True)
]
s = mpf.make_mpf_style(base_mpf_style='default',y_on_right=True)
mpf.plot(df5, style=s, ax=ax1, addplot=ap, xrotation=0, datetime_format='%H:%M', type='candlestick')
if __name__ == '__main__':
main()