Powered by AppSignal & Oban Pro

Simple OHLC Chart

24_simple_ohlc_chart.livemd

Simple OHLC Chart

Mix.install([
  {:plotly_ex, "~> 0.3"},
  {:kino, "~> 0.18"},
  {:req, "~> 0.5"}
])

Section

Ohlc.new(x:, open:, high:, low:, close:) works identically to Candlestick but renders as open/close tick stubs on a vertical high–low line rather than filled bodies. x contains ISO 8601 date strings. A rangeslider appears below the chart by default.

alias Plotly.{Figure, Ohlc}

csv =
  Req.get!("https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv").body

[header | rows] = String.split(csv, "\n", trim: true)
headers = String.split(header, ",")

parsed =
  Enum.map(rows, fn row -> Enum.zip(headers, String.split(row, ",")) |> Map.new() end)

dates = Enum.map(parsed, & &1["Date"])
open  = Enum.map(parsed, &(elem(Float.parse(&1["AAPL.Open"]), 0)))
high  = Enum.map(parsed, &(elem(Float.parse(&1["AAPL.High"]), 0)))
low   = Enum.map(parsed, &(elem(Float.parse(&1["AAPL.Low"]), 0)))
close = Enum.map(parsed, &(elem(Float.parse(&1["AAPL.Close"]), 0)))

Figure.new()
|> Figure.add_trace(Ohlc.new(x: dates, open: open, high: high, low: low, close: close))
|> Figure.update_layout(title: "Apple OHLC Chart")
|> Plotly.show()