0

我正在尝试使用mplfinance绘制 OHLCV 数据

注意:mplfinance是最新的。它已经过时matplotlib.financempl_finance.

该示例(https://github.com/matplotlib/mplfinance#usage)具有:

daily = pd.read_csv('examples/data/SP500_NOV2019_Hist.csv',index_col=0,parse_dates=True)
daily.index.name = 'Date'

但是我的数据不是 .csv 格式。

相反,我得到以下 JSON(来自 CoinMarketCap API):

[
   {
      "open":0.005846639252602948,
      "high":0.0062519524982233106,
      "low":0.005457133404836241,
      "close":0.006138245355734082,
      "volume":43409.74,
      "market_cap":1401259.98,
      "timestamp":"2021-10-02T23:59:59.999Z"
   },
   {
      "open":0.006134242128329189,
      "high":0.0063679709676487005,
      "low":0.006059241124035879,
      "close":0.006222796245534013,
      "volume":8918.02,
      "market_cap":1420561.55,
      "timestamp":"2021-10-03T23:59:59.999Z"
   },
   {
      "open":0.006223039734629446,
      "high":0.0062474505752106915,
      "low":0.005435646019963704,
      "close":0.005665330087500413,
      "volume":61171.43,
      "market_cap":1293301.24,
      "timestamp":"2021-10-04T23:59:59.999Z"
   }
]

我现在尝试对其进行转换,以使输出看起来与示例中的相同:

df_quote_data = pd.DataFrame(quote_data)
df_quote_data = df_quote_data['timestamp open high low close volume'.split()]
df_quote_data = df_quote_data.rename(
    columns={
        'timestamp' : 'Date',
        'open'      : 'Open',
        'high'      : 'High',
        'low'       : 'Low',
        'close'     : 'Close',
        'volume'    : 'Volume',
    },
    inplace=False,
    errors='raise'
)
# dates = df_quote_data['Date']
# timestamps = pd.to_datetime(dates)
# ts = (timestamps - pd.Timestamp("1970-01-01T00:00:00.000Z")) // pd.Timedelta('1s')
# df_quote_data['Date'] = ts

df_quote_data = df_quote_data.set_index('Date')

在此处输入图像描述

但尝试绘图失败:

%matplotlib inline
import mplfinance as mpf
mpf.plot(df_quote_data)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/var/folders/nj/rqdlt_hs4k5dwr5xrpgmn0kh0000gn/T/ipykernel_22891/1573098417.py in <module>
      1 import mplfinance as mpf
----> 2 mpf.plot(df_quote_data)

~/Desktop/CMC/.venv/lib/python3.10/site-packages/mplfinance/plotting.py in plot(data, **kwargs)
    296     config['type'] = _get_valid_plot_types(config['type'])
    297 
--> 298     dates,opens,highs,lows,closes,volumes = _check_and_prepare_data(data, config)
    299 
    300     config['xlim'] = _check_and_convert_xlim_configuration(data, config)

~/Desktop/CMC/.venv/lib/python3.10/site-packages/mplfinance/_arg_validators.py in _check_and_prepare_data(data, config)
     28 
     29     if not isinstance(data.index,pd.core.indexes.datetimes.DatetimeIndex):
---> 30         raise TypeError('Expect data.index as DatetimeIndex')
     31 
     32     if (len(data.index) > config['warn_too_much_data'] and

TypeError: Expect data.index as DatetimeIndex

怎么修?(我的管道可以改进吗?)

好的,我想通了。这些日期只是字符串,需要在设置索引之前进行转换:

df_quote_data['Date'] = pd.to_datetime(df_quote_data['Date'])

现在它起作用了:

mpf.plot(df_quote_data, type='candle', volume=True, show_nontrading=True)

在此处输入图像描述

无论如何,我都会发布这个问题,因为我认为它可能对某人有用。如果有人可以提出一种更清洁的方法来完成此任务,请发帖,我会接受答案。

4

0 回答 0