1

Is it possible to define custom color for candlesticks? We would like to color them according to our own business rules, not "increasing" and "decreasing" rules, which Plotly applies by default.

vestland
  • 55,229
  • 37
  • 187
  • 305
vemikhaylov
  • 53
  • 1
  • 7
  • @vestland Currently we are using "increasing" and "decreasing" attributes of the Candlestick class, but they use clearly defined rules for coloring; namely, comparison between close and open prices within the same candle. So we cannot, for instance, set some color if the difference between these prices is greater than some threshold or something like that. Unfortunately, I have not found any other options to customize candlestick colors. – vemikhaylov Apr 08 '21 at 07:00
  • Does that mean that you'd like different colors for different levels of increasing and decreasing? – vestland Apr 08 '21 at 07:06
  • That's just an example of a possible custom way to color candlesticks different from the default algorithm, which Plotly uses. – vemikhaylov Apr 08 '21 at 07:20

1 Answers1

6

To answer you initial question directly wrt:

Is it possible to define custom color for candlesticks?

From the information you've provided so far, and the lack of data and code provided, I can only interpret your question as:

How to apply different colors to different candlestick sizes or ranges between open and close.

Here's a plot that sets different thresholds for what is considered extreme upwards and downwards movements through:

df['change'] = df['AAPL.Close'] - df['AAPL.Open']
df_hi = df[df['change']>1.5]
df_lo = df[df['change']<-0.3]

Then, a "base" trace is set up from data that are not considered extreme with:

fig = go.Figure(go.Candlestick(x=df['Date'],
                open=df['AAPL.Open'], high=df['AAPL.High'],
                low=df['AAPL.Low'], close=df['AAPL.Close']))

And then two additional traces are added to include the movements that are considered extreme with:

# set up trace with extreme highs
fig.add_traces(go.Candlestick(x=df_hi['Date'],
                open=df_hi['AAPL.Open'], high=df_hi['AAPL.High'],
                low=df_hi['AAPL.Low'], close=df_hi['AAPL.Close']))

I will provide further explanations on how the colors are set if this is in fact what you're looking for. With this setup, you can also subset the different traces through the legend.

Plot:

enter image description here

Complete code:

import plotly.graph_objects as go
from plotly.subplots import make_subplots
import pandas as pd

# data
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
df=df.tail(15)

df['change'] = df['AAPL.Close'] - df['AAPL.Open']
df_hi = df[df['change']>1.5]
df_lo = df[df['change']<-0.3]

not_hi = df[df.index.isin(df_hi.index)].index
not_lo = df[df.index.isin(df_lo.index)].index
df = df.drop(not_hi)
df = df.drop(not_lo)

# set up figure with values not high and not low
# include candlestick with rangeselector
fig = go.Figure(go.Candlestick(x=df['Date'],
                open=df['AAPL.Open'], high=df['AAPL.High'],
                low=df['AAPL.Low'], close=df['AAPL.Close']))

# set up trace with extreme highs
fig.add_traces(go.Candlestick(x=df_hi['Date'],
                open=df_hi['AAPL.Open'], high=df_hi['AAPL.High'],
                low=df_hi['AAPL.Low'], close=df_hi['AAPL.Close']))

# set up traces with extreme lows
fig.add_traces(go.Candlestick(x=df_lo['Date'],
                open=df_lo['AAPL.Open'], high=df_lo['AAPL.High'],
                low=df_lo['AAPL.Low'], close=df_lo['AAPL.Close']))


color_hi_fill = 'black'
color_hi_line = 'blue'

color_lo_fill = 'yellow'
color_lo_line = 'purple'

fig.data[1].increasing.fillcolor = color_hi_fill
fig.data[1].increasing.line.color = color_hi_line
fig.data[1].decreasing.fillcolor = 'rgba(0,0,0,0)'
fig.data[1].decreasing.line.color = 'rgba(0,0,0,0)'

fig.data[2].increasing.fillcolor = 'rgba(0,0,0,0)'
fig.data[2].increasing.line.color = 'rgba(0,0,0,0)'
fig.data[2].decreasing.fillcolor = color_lo_fill
fig.data[2].decreasing.line.color = color_lo_line

fig.show()
vestland
  • 55,229
  • 37
  • 187
  • 305
  • 1
    Thanks a lot. This worked for me and solved the problem. – vemikhaylov Apr 12 '21 at 07:58
  • But my graphs are separated by multiple timestamp base, it doesn't using one timestamp base. – neikel Aug 21 '21 at 13:05
  • @neikel And...? – vestland Aug 21 '21 at 14:20
  • @vestland I meant, my graph is separated into "three zones" based on timestamp. For example, using your method, I got a graph begins with the first range include candlesticks from 9am-14:29pm, all candlesticks have same red; and the second range include candlesticks from 9am-14:29pm, all candlesticks have same green; and so on. However I need candlesticks all mix color in one range of time from 9am-14:29pm. My output https://imgur.com/a/ZsZqt50 – neikel Aug 23 '21 at 09:40
  • 1
    @neikel I see. I would consider writing that up as a new question. If you do then I'm sure you'll quickly get a few suggestions. – vestland Aug 23 '21 at 09:46
  • @vestland thanks, I created a new question here https://stackoverflow.com/questions/68890717/plotly-candlesticks-customize-color – neikel Aug 23 '21 at 10:09