Hi, I’m the guy that made this example:
Example Candlestick Chart From CSV File
I’m trying to add vertical lines AFTER the chart is drawn, from the output of another function that does some computation, then creates a list of x index numbers.
I can do it on a static example, like the one I made above – but when I want to add lines, even when I pass the list into my abstracted function for the figure:
def myCandleChart(myInputData, myResults): # Adding results to pass final result list...
myFig = make_subplots(rows=2, row_heights=[0.70,0.30], cols=1, shared_xaxes=True, vertical_spacing=0.02) # Heights must add up to 1
# Plotly Chart
myFig.add_trace(
go.Candlestick(
x=myInputData['BarTimestamp'],
open=myInputData['Open'],
high=myInputData['High'],
low=myInputData['Low'],
close=myInputData['Close'],
name="Price"),
row=1, col=1
)
# Add Indicator subplot with go.Scatter - works, Line is deprecated
myFig.add_trace(go.Scatter(x=myInputData['BarTimestamp'], y=myInputData['Indicator'], name="Indicator", line=dict(color='rgb(235,140,52)')), row=2, col=1) # Dark orange
# Remove default rangeslider
myFig.update_layout(xaxis_rangeslider_visible=False)
# Background color
myFig.update_layout(plot_bgcolor='rgb(0,0,0)')
# Axes line color
myFig.update_xaxes(showline=True, linewidth=1, linecolor='dimgray')
myFig.update_yaxes(showline=True, linewidth=1, linecolor='dimgray')
# Tick value display format - yaxis<num> is for subplots
myFig.update_layout(yaxis={"tickformat" : ','}, yaxis2={"tickformat" : '.2f'}) # Displays thousands,hundreds without scientific 'K' notation
# Grid line color
myFig.update_xaxes(showgrid=True, gridwidth=1, gridcolor='dimgray')
myFig.update_yaxes(showgrid=True, gridwidth=1, gridcolor='dimgray')
# Adjusting font sizes
myFig.update_layout(
font=dict(
family="Microsoft Sans Serif", # Font needs to be installed on system to be accessible
size=12, color='rgb(160,166,184)' # Light Gray
)
)
# Chart title
myFig.update_layout(title="BTCUSD 5min", title_x=0.5, yaxis_title="Price", yaxis2_title="Indicator")
# Legend Background Transparent Color, 'Paper' background color - plot canvas, sets pan enabled on load
myFig.update_layout(legend=dict(bgcolor='rgba(0,0,0,0)'), paper_bgcolor='rgb(12,12,12)', dragmode='pan')
# Got hits? Draw lines...
if len(myResults) > 0:
# Debug
print("< Candle Chart Func > Inside result status IF block - firing line drawing FOR loop... \n")
# Add vertical lines for index matches
for myLine in myResults:
myFig.add_vline(x=myLine, line=dict(color='rgb(52,183,235)', width=2, dash='dot')) # Light blue
# Debug
print("< Candle Chart Func > Length of result status: " + str(len(myResults)))
return myFig
I have no issues with the chart being drawn the first time - works great like in my example post, just not sure why it won’t add the lines when the myResults list is populated with x axis values… I know that the IF block is working because of the debug output:
So I’m a bit stumped as to why it won’t do this… I’d appreciate any simple example in reply as I’m still learning.
Thanks!