Quantcast
Channel: 🎛️ Dash - Plotly Community Forum
Viewing all articles
Browse latest Browse all 6271

What format is required by the extenddata property with candlestick plots?

$
0
0

I’m trying to plot a candlestick from a live stream of data by extending it updon receiving new data and its not working. Currently by inspecting chrome’s DevTool console it displays the following error

Error: attribute type must be an array of length equal to indices array length

I think this suggest that my update_graph’s data dictionary format is incorrect, but i cannot find information regarding the format and how to fix it.

below is my code.

import json
import dash_html_components as html
import dash_core_components as dcc
import plotly.graph_objects as go
from gevent import sleep
from dash import Dash
from dash.dependencies import Input, Output, State
from dash_extensions import WebSocket
from dash_extensions.websockets import SocketPool, run_server

import threading, queue, time
from datetime import datetime
from pytz import timezone
x = 0
def genData(q):
    global x
    while True:
        values = {}
        values['high'] = 10
        values['low'] = 5 
        values['open'] = 6
        values['close'] = 9
        
        values['CHANGE'] = 0
        values['MARKET_STATE'] = 'edit'
        values['UPDATE_TIME'] = datetime.now(timezone('Europe/London')).time().strftime('%H:%M:%S')
        item = {}
        item['values'] = values
        q.put(item)
        x += 0.1
        sleep(1)

# This block runs asynchronously.
def ws_handler(ws, q):
    # for data in data_feed():
    #     ws.send(json.dumps(data))  # send data
    while True:
        while len(list(q.queue)) != 0:
            data = q.get()
            #print(data)
            sleep(0.1)  # delay between data events
            ws.send(json.dumps(data))
            q.task_done()


q = queue.Queue()
# Create example app.
app = Dash(prevent_initial_callbacks=True)
socket_pool = SocketPool(app, handler=lambda x: ws_handler(x, q))
app.layout = html.Div([
    dcc.Graph(id="graph", figure=go.Figure(go.Candlestick(x=[],open=[],close=[],high=[],low=[]))),#go.Scatter(x=[], y=[]))),
    WebSocket(id="ws")
])


@app.callback(Output("graph", "extendData"), [Input("ws", "message")], [State("graph", "figure")])
def update_graph(msg, figure):    
    data = json.loads(msg['data'])['values']
    return (
        dict(
            x = [[ datetime.strptime(data['UPDATE_TIME'],'%H:%M:%S') ]],
            open  = [[ data['open'] ]],
            close = [[ data['close']]],
            high  = [[ data['high']]],
            low   = [[ data['low']]],
            type='candlestick'
        ),
        [0],
        10 
    )

if __name__ == '__main__':
    threading.Thread(target=genData, daemon=True, args=(q,)).start()
    threading.Thread(target=run_server,daemon=True,args=(app,),kwargs={'port':5000}).start()

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 6271

Trending Articles