Candlestick Charts with plotly

In financial applications, we often have access to OHLC data (containing the open, high, low, and close price on each day). We can use a candlestick chart can help us see the movement of the price within each day.

To implement a candlestick chart, we can use the Candlestick class from plotly’s Graph Objects sub-library.

We start with some OHLC data:

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}
]

Mapping the data to get into a format the chart likes (separate lists):

dates = []
opens = []
highs = []
lows = []
closes = []

for item in ohlc_data:
    dates.append(item["date"])
    opens.append(item["open"])
    highs.append(item["high"])
    lows.append(item["low"])
    closes.append(item["close"])

print(dates)
print(opens)
print(closes)
print(highs)
print(lows)
['2030-03-16', '2030-03-15', '2030-03-12', '2030-03-11', '2030-03-10']
[236.28, 234.96, 234.01, 234.96, 237.0]
[237.71, 234.81, 235.75, 237.13, 232.42]
[240.055, 235.185, 235.82, 239.17, 237.0]
[235.94, 231.81, 233.23, 234.31, 232.04]

Finally, creating the chart:

from plotly.graph_objects import Figure, Candlestick

stick = Candlestick(x=dates, open=opens, high=highs, low=lows, close=closes)

fig = Figure(data=[stick])
fig.update_layout(title="Example Candlestick Chart")
fig.show()
Foreshadowing

Later when we learn how to calculate our own moving averages, we can add more objects, such as the moving averages, to the chart data as well.