We have previously studied how to create candlestick charts using OHLC data. We can do this with tabular data as well.
Converting OHLC data to DataFrame
object:
from pandas import DataFrame
ohlc_data = [
{"date": "2030-03-16", "open": 236.2800, "high": 240.0550, "low": 235.9400, "close": 237.7100, "volume": 28092196},
{"date": "2030-03-15", "open": 234.9600, "high": 235.1850, "low": 231.8100, "close": 234.8100, "volume": 26042669},
{"date": "2030-03-12", "open": 234.0100, "high": 235.8200, "low": 233.2300, "close": 235.7500, "volume": 22653662},
{"date": "2030-03-11", "open": 234.9600, "high": 239.1700, "low": 234.3100, "close": 237.1300, "volume": 29907586},
{"date": "2030-03-10", "open": 237.0000, "high": 237.0000, "low": 232.0400, "close": 232.4200, "volume": 29746812}
]
df = DataFrame(ohlc_data)
df.head()
0 |
2030-03-16 |
236.28 |
240.055 |
235.94 |
237.71 |
28092196 |
1 |
2030-03-15 |
234.96 |
235.185 |
231.81 |
234.81 |
26042669 |
2 |
2030-03-12 |
234.01 |
235.820 |
233.23 |
235.75 |
22653662 |
3 |
2030-03-11 |
234.96 |
239.170 |
234.31 |
237.13 |
29907586 |
4 |
2030-03-10 |
237.00 |
237.000 |
232.04 |
232.42 |
29746812 |
Because the Candlestick
class comes from the Graph Objects sub-library, instead of passing the DataFrame
object, we pass the columns directly:
from plotly.graph_objects import Figure, Candlestick
stick = Candlestick(x=df["date"],
open=df["open"],
high=df["high"],
low=df["low"],
close=df["close"]
)
fig = Figure(data=[stick])
fig.update_layout(title="Example Candlestick Chart")
fig.show()