【问题标题】:Intraday candlestick charts using Matplotlib使用 Matplotlib 的日内烛台图表
【发布时间】:2012-03-12 20:03:42
【问题描述】:

我在使用 Matplotlib 的财务图表时遇到了一些困难。似乎他们的烛台图最适合处理每日数据,但我很难让它们处理日内(每 5 分钟一次,在 9:30 到 4 点之间)数据。

我已将示例数据粘贴到 pastebin 中。顶部是我从数据库中得到的,底部是元组,日期格式为有序浮点数,用于 Matplotlib。

Link to sample data

当我绘制图表时,其中有巨大的空白,轴很糟糕,缩放也同样可怕。 http://imgur.com/y7O8A

如何从这些数据中制作出可读性好的图表?我的最终目标是得到一个看起来像这样的图表:

http://i.imgur.com/EnrTW.jpg

数据点的增量可以从 5 分钟到 30 分钟不等。


我还制作了数据的 Pandas 数据框,但我不确定 pandas 是否具有烛台功能。

【问题讨论】:

  • 从您提供的数据来看,您的数据似乎每天从上午 9:30 到下午 4:00 每隔 30 分钟收集一次。该差距可能仅反映未获取数据的日期之间的下午 4:00 到上午 9:30 之间的时间。顺便说一句,使用pandas 库,您可以直接处理和分析您的原始数据以及绘制它们。
  • Pandas 似乎无法绘制 ohlc/烛台数据...
  • pastebin 链接不再可用
  • 能否再次更新 pastebin 数据?

标签: python charts matplotlib


【解决方案1】:

如果我理解得很好,您最关心的问题之一是每日数据之间的差距。 要摆脱它们,一种方法是人为地“均匀分布”您的数据(但当然您会在一天内失去任何时间指示)。

无论如何,这样做,您将能够获得一个看起来像您作为示例提出的图表。

注释代码和结果图如下。

import numpy as np
import matplotlib.pyplot as plt
import datetime

from matplotlib.finance import candlestick
from matplotlib.dates import num2date

# data in a text file, 5 columns: time, opening, close, high, low
# note that I'm using the time you formated into an ordinal float
data = np.loadtxt('finance-data.txt', delimiter=',')

# determine number of days and create a list of those days
ndays = np.unique(np.trunc(data[:,0]), return_index=True)
xdays =  []
for n in np.arange(len(ndays[0])):
    xdays.append(datetime.date.isoformat(num2date(data[ndays[1],0][n])))

# creation of new data by replacing the time array with equally spaced values.
# this will allow to remove the gap between the days, when plotting the data
data2 = np.hstack([np.arange(data[:,0].size)[:, np.newaxis], data[:,1:]])

# plot the data
fig = plt.figure(figsize=(10, 5))
ax = fig.add_axes([0.1, 0.2, 0.85, 0.7])
    # customization of the axis
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.tick_params(axis='both', direction='out', width=2, length=8,
               labelsize=12, pad=8)
ax.spines['left'].set_linewidth(2)
ax.spines['bottom'].set_linewidth(2)
    # set the ticks of the x axis only when starting a new day
ax.set_xticks(data2[ndays[1],0])
ax.set_xticklabels(xdays, rotation=45, horizontalalignment='right')

ax.set_ylabel('Quote ($)', size=20)
ax.set_ylim([177, 196])

candlestick(ax, data2, width=0.5, colorup='g', colordown='r')

plt.show()

【讨论】:

  • 非常感谢先生,我上周一直没有编程,否则我会早点给你赏金的。再次感谢。
【解决方案2】:

我厌倦了 matplotlib(和 plotly)糟糕的性能以及缺少您要求的此类功能,因此实现了 one of my own。以下是它的工作原理:

import finplot as fplt
import yfinance
df = yfinance.download('AAPL')
fplt.candlestick_ochl(df[['Open', 'Close', 'High', 'Low']])
fplt.show()

不仅排除了交易所自动关闭的日子,而且具有更好的性能和更好的api。对于更类似于您最终要寻找的东西:

import finplot as fplt
import yfinance

symbol = 'AAPL'
df = yfinance.download(symbol)

ax = fplt.create_plot(symbol)

fplt.candlestick_ochl(df[['Open', 'Close', 'High', 'Low']], ax=ax)
fplt.plot(df['Close'].rolling(200).mean(), ax=ax, legend='SMA 200')
fplt.plot(df['Close'].rolling(50).mean(), ax=ax, legend='SMA 50')
fplt.plot(df['Close'].rolling(20).mean(), ax=ax, legend='SMA 20')

fplt.volume_ocv(df[['Open', 'Close', 'Volume']], ax=ax.overlay())

fplt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多