Hello :3, I’m trying to plot live bitcoin price but I keep getting a callback error that says
updated_btc_2h() takes 1 positional argument but 2 were given
If I’m making an error elsewhere when trying to plot the candles live, please feel free to let me know!
import json
from datetime import datetime
import requests
import pathlib
import time
import pandas as pd
import plotly.graph_objects as go
import plotly.express as px
from plotly.graph_objs import *
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Output, Input, State
app = dash.Dash(__name__)
# Requesting JSON
BTC2H = requests.get(
'https://poloniex.com/public?command=returnChartData¤cyPair=USDT_BTC&start=1571803200&end=9999999999&period=7200'
)
jsonBTC2H = BTC2H.json()
data = pd.DataFrame(jsonBTC2H)
# Converting dates (newdate1 for callback)
newDate = data['date'].apply(lambda x: time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(x)))
newDate1 = data.date.apply(lambda x: time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(x)))
# Candlesticks for BTC
chartBTC = go.Candlestick(x=newDate,
open=data['open'],
high=data['high'],
low=data['low'],
close=data['close'],
xaxis='x1',
yaxis='y1',
increasing=dict(line=dict(color="#00E676")),
decreasing=dict(line=dict(color="#FF5252")),
name="Candlesticks")
# Layout
layout = Layout(
showlegend=False,
xaxis=dict(
domain=[0, 1]
),
yaxis=dict(
domain=[0, 1],
)
)
# Plot
btcplot = go.Figure(data=[chartBTC], layout=layout)
# Colours of the candles
cs = btcplot.data[0]
cs.increasing.fillcolor = '#131722'
cs.increasing.line.color = '#00E676'
cs.decreasing.fillcolor = '#FF5252'
cs.decreasing.line.color = '#FF5252'
# Removes rangeslider
btcplot.update(layout_xaxis_rangeslider_visible=False)
# Sets the automatic x-axis range when you first open it (plots lasts 250 candles)
btcplot.update_xaxes(range=[newDate.iloc[-250], newDate.max()])
# Sets the automatic y-axis range to the limit of
btcplot.update_yaxes(range=[(data['low'].iloc[-250:].min()) - 100, (data['high'].iloc[-250:].max()) + 100])
# Frequency of readings on both axis
btcplot.update_xaxes(nticks=20)
btcplot.update_yaxes(nticks=30)
# Configurations
config = dict({
'showTips': False
})
# Dash interface
app.layout = html.Div(children=[
dcc.Graph(id='btc_chart',
figure=btcplot,
config=config,
animate=True,
style={"height" : "99vh"}), # Without height 99vh, height of dash app is small
dcc.Interval(
id='btc_update',
interval=1000,
n_intervals=0
)
])
@app.callback(
Output('btc_chart', 'figure'),
[Input('btc_update', 'n_intervals')],
[State('btc_update', 'figure')]
)
def updated_btc_2h(n_interval):
global data
newbtcplot= go.Candlestick(
x=newDate1,
open=data.open,
high=data.high,
low=data.low,
close=data.close,
increasing=dict(line=dict(color="#00E676")),
decreasing=dict(line=dict(color="#FF5252")),
showlegend=False,
name="Candlestick Updated")
newbtcplot1 = go.Figure(data=[newbtcplot], layout=layout)
return newbtcplot1
# return Figure(data= figure, layout = layout)
# To run the app
if __name__ == '__main__':
app.run_server(debug=True)