1

I found a same topic with a solution here

Custom color of Plotly candlesticks

However, using that solution my output graph is separated into "three zones" based on timestamp. For example, 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.

data = pd.DataFrame(master.intradata)
intradata_sell = data[data['oi'] == 1]
intradata_buy = data[data['oi'] == 2]
intradata_und = data[data['oi'] == 3] 

fig = go.Figure(go.Candlestick(x=intradata_sell['time'],
                open=intradata_sell['open'], high=intradata_sell['high'],
                low=intradata_sell['low'], close=intradata_sell['close']))

fig.add_traces(go.Candlestick(x=intradata_buy['time'],
            open=intradata_buy['open'], high=intradata_buy['high'],
            low=intradata_buy['low'], close=intradata_buy['close']))

fig.add_traces(go.Candlestick(x=intradata_und['time'],
            open=intradata_und['open'], high=intradata_und['high'],
            low=intradata_und['low'], close=intradata_und['close']))

fig.data[0].increasing.fillcolor = 'red'
fig.data[0].increasing.line.color = 'red'
fig.data[0].decreasing.fillcolor = 'red'
fig.data[0].decreasing.line.color = 'red'

fig.data[1].increasing.fillcolor = 'green'
fig.data[1].increasing.line.color = 'green'
fig.data[1].decreasing.fillcolor = 'green'
fig.data[1].decreasing.line.color = 'green'

fig.data[2].increasing.fillcolor = 'orange'
fig.data[2].increasing.line.color = 'orange'
fig.data[2].decreasing.fillcolor = 'orange'
fig.data[2].decreasing.line.color = 'orange'


fig.show()

My output .

neikel
  • 95
  • 1
  • 9

1 Answers1

1
  • have simulated data to be able to use your plot code
  • have added an extra step to your code
# ensure first dataframe has all times so additional traces match up on xaxis
intradata_sell = (
    intradata_sell.merge(intradata_buy["time"], on="time", how="outer")
    .merge(intradata_und["time"], on="time", how="outer")
    .sort_values("time")
)
  • as per comment - need all xaxis values in first trace, so additional traces are not appended to end of xaxis
  • remove this line and get plots like your image
import plotly.graph_objects as go
from dataclasses import dataclass
import datetime as dt
import random
import numpy as np
import pandas as pd

@dataclass
class master:
    intradata: None

# simulate data....
S = 50
df = pd.DataFrame(
    {
        "time": [
            dt.time(random.randint(9, 14), min(random.randint(0, 12)*5,59)) for i in range(S)
        ],
        "oi": np.random.randint(1, 4, S),
        "value": np.random.uniform(1, 5, S) * np.random.uniform(0.1, 1, S),
    }
).groupby(["time", "oi"]).ohlc().droplevel(0, 1).reset_index()

master = master(intradata=df.to_dict("list"))

data = pd.DataFrame(master.intradata)
intradata_sell = data[data['oi'] == 1]
intradata_buy = data[data['oi'] == 2]
intradata_und = data[data['oi'] == 3] 

# ensure first dataframe has all times so additional traces match up on xaxis
intradata_sell = (
    intradata_sell.merge(intradata_buy["time"], on="time", how="outer")
    .merge(intradata_und["time"], on="time", how="outer")
    .sort_values("time")
)

fig = go.Figure(go.Candlestick(x=intradata_sell['time'],
                open=intradata_sell['open'], high=intradata_sell['high'],
                low=intradata_sell['low'], close=intradata_sell['close']))

fig.add_traces(go.Candlestick(x=intradata_buy['time'],
            open=intradata_buy['open'], high=intradata_buy['high'],
            low=intradata_buy['low'], close=intradata_buy['close']))

fig.add_traces(go.Candlestick(x=intradata_und['time'],
            open=intradata_und['open'], high=intradata_und['high'],
            low=intradata_und['low'], close=intradata_und['close']))

fig.data[0].increasing.fillcolor = 'red'
fig.data[0].increasing.line.color = 'red'
fig.data[0].decreasing.fillcolor = 'red'
fig.data[0].decreasing.line.color = 'red'

fig.data[1].increasing.fillcolor = 'green'
fig.data[1].increasing.line.color = 'green'
fig.data[1].decreasing.fillcolor = 'green'
fig.data[1].decreasing.line.color = 'green'

fig.data[2].increasing.fillcolor = 'orange'
fig.data[2].increasing.line.color = 'orange'
fig.data[2].decreasing.fillcolor = 'orange'
fig.data[2].decreasing.line.color = 'orange'


fig.show()
Rob Raymond
  • 29,118
  • 3
  • 14
  • 30