I’ve been able to animate plotly candlesticks which works fine but when I try to add a line trace on it, I get a flickering candlestick but the line trace animates just fine.
Is there any work around?
Here’s my code which is a modification of a response from @matiaslee found here
### my attempt
play_button = {
"args": [
None,
{
"frame": {"duration": 300, "redraw": True},
"fromcurrent": True,
"transition": {"duration": 100,"easing": "quadratic-in-out"}
}
],
"label": "Play",
"method": "animate"
}
pause_button = {
"args": [
[None],
{
"frame": {"duration": 0, "redraw": False},
"mode": "immediate",
"transition": {"duration": 0}
}
],
"label": "Pause",
"method": "animate"
}
sliders_steps = [
{"args": [
[0, i], # 0, in order to reset the image, i in order to plot frame i
{"frame": {"duration": 300, "redraw": True},
"mode": "immediate",
"transition": {"duration": 300}}
],
"label": i,
"method": "animate"}
for i in range(len(raw_candles))
]
sliders_dict = {
"active": 0,
"yanchor": "top",
"xanchor": "left",
"currentvalue": {
"font": {"size": 20},
"prefix": "frame:",
"visible": True,
"xanchor": "right"
},
"transition": {"duration": 300, "easing": "cubic-in-out"},
"pad": {"b": 10, "t": 50},
"len": 0.9,
"x": 0.1,
"y": 0,
"steps": sliders_steps,
}
fig = go.Figure(go.Candlestick(
x=raw_candles.index,
open=raw_candles.open,
high=raw_candles.high,
low=raw_candles.low,
close=raw_candles.close,
name='candles'
))
fig.add_trace(go.Scatter(x=raw_candles.index, y= raw_candles.ema21,
line={'color':'red', 'width':1},
mode='lines',
name='ema21'))
#Store Names and their corresponding order of the
nameDataindexdict={}
for dt in fig.data:
nameDataindexdict[dt['name']]=fig.data.index(dt)
frames = []
for i in range(len(raw_candles)):
frames.append(go.Frame(data=[go.Candlestick(
x=raw_candles.index[:i],
open=raw_candles.open[:i],
high=raw_candles.high[:i],
low=raw_candles.low[:i],
close=raw_candles.close[:i]
),
go.Scatter(x=raw_candles.index, y= raw_candles.ema21[:i],
line={'color':'red', 'width':1},
mode='lines',
name='ema21'
)],
traces=[nameDataindexdict['candles'],nameDataindexdict['ema21']],
name=f"fr{i}"
))
fig.update(frames=frames)
fig.update_layout(
updatemenus=[dict(type="buttons", buttons=[play_button, pause_button])],
sliders=[sliders_dict])
fig.show()