How do I make special candles a different color?

Hi,

I am new to plotly so please bear with me. I currently have an algorithm that determines the special candles (swing_points) for a set of candle stick data. Currently, I can plot the candles using the example shown on the website.

Is there a way highlight (make them a different color given certain criteria) or do I have to use annotations?

path = 'candles.json'
swing_path = 'swing_candles.json'
df = pd.read_json(path, orient='columns')
# anyway to use the swing_df to be special???
# if df['open'] == swing_df ['open']:
#      something like ....line=dict(color= '#17BECF'))
swing_df = pd.read_json(swing_path, orient='columns') 
trace = go.Ohlc(x=df['timestamp'],
            open=df['open'],
            high=df['high'],
            low=df['low'],
            close=df['close'])
data = [trace]
py.offline.plot(data, filename='candles')

Hi @llamagiii,

This isn’t possible with a single candlestick trace, but you could layer on a second trace to hold the highlighted data. Here’s an example

import plotly.graph_objs as go
from plotly.offline import init_notebook_mode, iplot
from datetime import datetime
init_notebook_mode()


open_data = [33.0, 33.3, 33.5, 33.0, 34.1]
high_data = [33.1, 33.3, 33.6, 33.2, 34.8]
low_data = [32.7, 32.7, 32.8, 32.6, 32.8]
close_data = [33.0, 32.9, 33.3, 33.1, 33.1]
dates = [datetime(year=2013, month=10, day=10),
         datetime(year=2013, month=11, day=10),
         datetime(year=2013, month=12, day=10),
         datetime(year=2014, month=1, day=10),
         datetime(year=2014, month=2, day=10)]

trace = go.Candlestick(
    x=dates,
    open=open_data,
    high=high_data,
    low=low_data,
    close=close_data,
    name='all data'
)

highlight_inds = [1, 3]
track_highlight = go.Candlestick(
    x=[dates[i] for i in highlight_inds],
    open=[open_data[i] for i in highlight_inds],
    high=[high_data[i] for i in highlight_inds],
    low=[low_data[i] for i in highlight_inds],
    close=[close_data[i] for i in highlight_inds],
    increasing={'line': {'color': 'yellow'}},
    decreasing={'line': {'color': 'purple'}},
    name='highlight'
)

fig = go.Figure(data = [trace, track_highlight])
iplot(fig)

Hope that helps!
-Jon

that worked beautifully. Appreciate the help!

1 Like