Plotting Stock Financials with Yfinance
Mix.install([
{:yfinance, "~> 0.5.0"},
{:kino_vega_lite, "~> 0.1.10"},
{:kino, "~> 0.12.0"}
])
Introduction
Yahoo! Finance® is a great place to download financial data like stock ticker prices. While they don’t offer an API from which you can download this data, there is a Python library that makes the same calls as their web app so that you can fetch the time series data. This library was written to allow readers of Elixir For Finance to collect, analyze and visualize economic data from Yahoo! Finance®, but it can be used outside the context of the book.
To learn how you can analyze and visualize the financial markets using Livebook, Explorer, Scholar and Nx, be sure to pick up a copy of our book:

Get Financial Statement Data
To get historical financial statement data for a stock from Yahoo! Finance®, you can use the Yfinance.Ticker module and pass in the stock that you are interested in along with the data frequency:
{:ok, data_frame} = Yfinance.Ticker.balance_sheet("aapl", :quarterly)
Now that you have the time series price data in an Explorer.DataFrame struct, you can render the tabular data in Livebook using Kino.DataTable:
Kino.DataTable.new(data_frame)
Lastly, you can plot the time series data using VegaLite:
import Explorer.DataFrame
alias VegaLite, as: Vl
plottable_data_frame =
data_frame
|> Explorer.DataFrame.select(["date", "net_debt", "total_assets"])
|> Explorer.DataFrame.pivot_longer(["net_debt", "total_assets"],
names_to: "series",
values_to: "value"
)
|> Explorer.DataFrame.to_rows()
Vl.new(width: 700, height: 400, title: "Apple Financials")
|> VegaLite.data_from_values(plottable_data_frame)
|> Vl.mark(:line)
|> Vl.encode_field(:x, "date", type: :temporal, title: "Date")
|> Vl.encode_field(:y, "value", type: :quantitative, title: "Dollar amount")
|> VegaLite.encode_field(:color, "series", type: :nominal)