Monday, February 29th, 2016

racket-quandl: Racket for fun and profit

programming, racket, Quandl, finance

Today I was extracting some commodity spot prices from Quandl with both python and Matlab. I knew it couldn’t be hard to obtain the same data via racket so I threw together a very small library called racket-quandl to do just that.

Listing 1 shows a basic example of using racket-quandl to pull down historic commodity prices. It’s as simple as calling get with the code for the required data set. Using racket’s fantastic plot library these data can be shown graphically. If you have a Quandl key, this can be set by passing it to set-auth-token before calling get.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#lang racket

(require racket-quandl)
(require plot)
(require plot/utils)

; Set Quandl auth token from file
(set-auth-token (string-normalize-spaces (file->string "quandlkey.txt")))

; Grab daily spot prices from Quandl (EIA dataset)
(define crude (get "EIA/PET_RWTC_D")) ; WTI crude
(define gasoline (get "EIA/PET_EER_EPMRU_PF4_Y35NY_DPG_D")) ; RBOB gasoline
(define nat-gas (get "EIA/NG_RNGWHHD_D")) ; Henry hub natural gas

; Plot the historic price
(parameterize
      ([plot-x-ticks (date-ticks)]
       [plot-x-label "Date"]
       [plot-y-label "Price ($/unit)"])
  (plot (list
         (lines
          (map vector (map datetime->real (map car crude)) (map cadr crude))
          #:color 6 #:label "WTI crude")
         (lines
          (map vector (map datetime->real (map car gasoline)) (map cadr gasoline))
          #:color 7 #:label "RBOB gasoline")
         (lines
          (map vector (map datetime->real (map car nat-gas)) (map cadr nat-gas))
          #:color 8 #:label "Natural gas"))))
Listing 1: racket-quandl example

Output from the code is shown in Figure 1.

Figure 1: Commodity spot prices pulled form Quandl with racket

Comments

comments powered by Disqus