plotly Streaming 不支持烛台图 API (python)

Candlestick chart not supported by plotly Streaming API (python)

问题

我正在尝试使用 plotly 和 python 制作显示实时数据的烛台图表。因此,我正在使用 plotly 的 Streaming API。作为示例,我简单地改编了提供的示例 at this link.

工作代码

import plotly.plotly as py
import plotly.tools as tls
from plotly.graph_objs import *

import numpy as np  
import datetime
import time

stream_ids = tls.get_credentials_file()['stream_ids']

# Get stream id from stream id list
stream_id = stream_ids[0]

# Make instance of stream id object
stream = Stream(
    token=stream_id,  
    maxpoints=80      
)

# Initialize trace of streaming plot by embedding the unique stream_id
trace1 = Candlestick(
    open=[],
    high=[],
    low=[],
    close=[],
    x=[],
    stream=stream         
)

data = Data([trace1])

# Add title to layout object
layout = Layout(title='CANDLE STICK CHART')

# Make a figure object
fig = Figure(data=data, layout=layout)

# Send fig to Plotly, initialize streaming plot, open new tab
unique_url = py.plot(fig, filename='s7_first-stream')

# Make instance of the Stream link object,
s = py.Stream(stream_id)

# Open the stream
s.open()

i = 0    # a counter
k = 5    # some shape parameter
N = 200  # number of points to be plotted

# Delay start of stream by 5 sec (time to switch tabs)
time.sleep(5)

while i<N:
    i += 1   # add to counter

    # Current time on x-axis, dummy data for the candlestick chart
    x = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
    low = np.cos(k*i/50.)*np.cos(i/50.)+np.random.randn(1)
    close = low+1
    open = low +2
    high = low+3

    # Write to Plotly stream
    s.write(dict(x=x,
                 low=low,
                 close=close,
                 open=open,
                 high=high))

    # Write numbers to stream to append current data on plot,
    # Write lists to overwrite existing data on plot 

    time.sleep(0.08)  

# Close the stream when done plotting
s.close()

输出

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/plotly/plotly/plotly.py", line 632, in write
    tools.validate(stream_object, stream_object['type'])
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/plotly/tools.py", line 1323, in validate
    cls(obj)  # this will raise on invalid keys/items
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/plotly/graph_objs/graph_objs.py", line 377, in __init__
    self.__setitem__(key, val, _raise=_raise)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/plotly/graph_objs/graph_objs.py", line 428, in __setitem__
    raise exceptions.PlotlyDictKeyError(self, path)
plotly.exceptions.PlotlyDictKeyError: 'open' is not allowed in 'scatter'

Path To Error: ['open']

Valid attributes for 'scatter' at path [] under parents []:

    ['cliponaxis', 'connectgaps', 'customdata', 'customdatasrc', 'dx',
    'dy', 'error_x', 'error_y', 'fill', 'fillcolor', 'hoverinfo',
    'hoverinfosrc', 'hoverlabel', 'hoveron', 'hovertext', 'hovertextsrc',
    'ids', 'idssrc', 'legendgroup', 'line', 'marker', 'mode', 'name',
    'opacity', 'r', 'rsrc', 'showlegend', 'stream', 't', 'text',
    'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'tsrc',
    'type', 'uid', 'visible', 'x', 'x0', 'xaxis', 'xcalendar', 'xsrc', 'y',
    'y0', 'yaxis', 'ycalendar', 'ysrc']

Run `<scatter-object>.help('attribute')` on any of the above.
'<scatter-object>' is the object at []

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/Louis/PycharmProjects/crypto_dashboard/Data_gathering/candlestick_stream_test.py", line 79, in <module>
    high=high))
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/plotly/plotly/plotly.py", line 640, in write
    "invalid:\n\n{1}".format(stream_object['type'], err)
plotly.exceptions.PlotlyError: Part of the data object with type, 'scatter', is invalid. This will default to 'scatter' if you do not supply a 'type'. If you do not want to validate your data objects when streaming, you can set 'validate=False' in the call to 'your_stream.write()'. Here's why the object is invalid:

'open' is not allowed in 'scatter'

Path To Error: ['open']

Valid attributes for 'scatter' at path [] under parents []:

    ['cliponaxis', 'connectgaps', 'customdata', 'customdatasrc', 'dx',
    'dy', 'error_x', 'error_y', 'fill', 'fillcolor', 'hoverinfo',
    'hoverinfosrc', 'hoverlabel', 'hoveron', 'hovertext', 'hovertextsrc',
    'ids', 'idssrc', 'legendgroup', 'line', 'marker', 'mode', 'name',
    'opacity', 'r', 'rsrc', 'showlegend', 'stream', 't', 'text',
    'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'tsrc',
    'type', 'uid', 'visible', 'x', 'x0', 'xaxis', 'xcalendar', 'xsrc', 'y',
    'y0', 'yaxis', 'ycalendar', 'ysrc']

Run `<scatter-object>.help('attribute')` on any of the above.
'<scatter-object>' is the object at []

我的跟踪似乎已被解释为 'Scatter' 类型的数据对象,而不是 'Candlestick'。这可以解释为什么不允许使用 'open' 属性。

Candlestick 对象支持流式传输 API 还是我遗漏了什么?

谢谢!

在你的 while 循环中,你应该将键 type='candlestick' 添加到传递给 write 的字典中:

s.write(dict(type='candlestick',
    x=x,
    low=low,
    close=close,
    open=open,
    high=high))

对于所有接受 stream 属性的 plotly.graph_objs 跟踪,它的工作方式类似。