DEV Community

Eli Holderness for Anvil

Posted on • Originally published at anvil.works

Day 13 of the Anvil Advent Calendar: Gold, Frankincense and Myrrh

This post is part of Anvil's Advent calendar - we're building a web app every day for 24 days, using nothing but Python! For day 13, we take a look at some very traditional Christmas gifts.

Financial data at Christmas

Why do governments have underground vaults full of gold, but not frankincense or myrrh? What makes some things commodities and other things not? Fundamentally, why is gold so much more popular than tree resin?

We learnt the answers to these questions when we built today's app, which visualises the prices of these commodities:

Click here to clone the app, see how it works, and edit the code yourself!

It calculates the price for a basket of gold, frankincense and myrrh. By adjusting the three sliders, you can set how much of each item you plan to purchase. The app calculates the total cost and breaks it down into the separate costs of each item.

We also track the historic price of gold, using data from Quandl's financial and economic data API.
This isn't possible for frankincense or myrrh, because they're not traded on the commodities markets. But why?

Why you care more about gold than frankincense and myrrh

What is myrrh, anyway? It's the resin of a thorny tree found in Oman, Yemen and Somalia. Frankincense is the resin of another tree from the same area. There is an ongoing shortage of frankincense
as a result of drought, so stocking up might prove wise!

But how do you buy frankincense? What does it even look like? Should you get frankincense oil or solid frankincense resin?
What is a good price? What is a bad price? Buying frankincense wisely is not easy.

Gold is a lot simpler. It's an element. One gold bar is the same as any other gold bar, for the sake of exchange - it's fungible.
In fact, there's a strict standard that defines whether your gold bar can be traded or not (the LBMA Good Delivery specification.)
The gold price is set by an online auction that takes place twice a day. Everybody who wants to buy some standard-issue gold takes part in the auction, and the balance of supply and demand dictates what the price is that morning
or afternoon.

That's why it's easy to get the 'price of gold' over time, but no API endpoint for frankincense or myrrh. We pull the historical record of this data, which you can use to decide when to buy your Christmas gold.

To plot the gold price in our Anvil app, we simply make a request to the Quandl API like so:

@anvil.server.callable
def get_gold_chart():
  # Construct date strings for the API request
  now = datetime.now()
  today = now.strftime('%Y-%m-%d')
  a_year_ago = (now - timedelta(days=365)).strftime('%Y-%m-%d')

  # Our Quandl API key is stored in an Anvil Secret
  api_key = anvil.secrets.get_secret("quandl_key")

  # Construct the URL to request the data from
  url = f'https://www.quandl.com/api/v3/datasets/LBMA/GOLD/data.json?api_key={api_key}&start_date={a_year_ago}&end_date={today}'

  # Make the request
  response = anvil.http.request(url, json=True)

  # Get the afternoon price in GBP out of the data structure
  return [{'date': x[0], 'afternoon_price': x[5]} for x in response['dataset_data']['data']]
Enter fullscreen mode Exit fullscreen mode

Then we can plot the gold price in a Plotly plot on the client side like so:

    self.gold_chart = anvil.server.call('get_gold_chart')

    self.plot_1.data = go.Scatter(
      x=[p['date'] for p in self.gold_chart],
      y=[p['afternoon_price'] for p in self.gold_chart],
    )
Enter fullscreen mode Exit fullscreen mode

And that's all there is to fetching and manipulating historical commodities data! If you want to use Python to process financial and economic data, the Quandl API is free to use and offers data for equities, currencies, derivatives and all sorts of other assets.

As always, you can clone this app and build upon it by clicking here.

You could use it to build a stock portfolio app, like this one that we built in an hour (and captured on video!)

Come back tomorrow for another Christmas-themed app build.

Top comments (0)