quandl

package module
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 16, 2016 License: MIT Imports: 9 Imported by: 2

README

Go Quandl

Build Status GoDoc


This library provides easy access to the Quandl API using the Go programming language.

The full documentation is at:
godoc.org/github.com/DannyBen/quandl


Install

$ go get github.com/DannyBen/quandl

Features

  • Supports 3 call types to Quandl: GetSymbol, GetList and GetSearch.
  • Returns either a native Go object, or a raw (CSV/JSON/XML) response.
  • Built in cache handling.

Usage

Basic usage looks like this:

quandl.APIKey = "YOUR KEY"
data, err := quandl.GetSymbol("WIKI/AAPL", nil)

and will return a native Go object. To use the data in the response, iterate through its Data property:

for i, item := range data.Data {
  fmt.Println(i, item[0], item[2])
}

To receive a raw response from Quandl (CSV, JSON, XML) you can use:

data, err := quandl.GetSymbolRaw("WIKI/AAPL", "csv", nil)

To pass options to the Quandl API, use something like this:

v := quandl.Options{}
v.Set("trim_start", "2014-01-01")
v.Set("trim_end", "2014-02-02")
data, err := quandl.GetSymbol("WIKI/AAPL", v)

More examples are in the quandl_test file or in the official godoc documentation

Development

Before running tests, set your API key in an environment variable.

$ export QUANDL_KEY=your_key_here
$ go test -v

Documentation

Overview

Package quandl provides easy access to the Quandl API

It provides methods for getting response from Quandl in several formats.

Basic usage looks like this:

quandl.ApiKey = "YOUR KEY"
data, err := quandl.GetSymbol("WIKI/AAPL", nil)

and will return a native Go object. To use the data in the response, iterate through its Data property:

for i, item := range data.Data {
    fmt.Println(i, item[0], item[2])
}

To receive a raw response from Quandl (CSV, JSON, XML) you can use:

data, err := quandl.GetSymbolRaw("WIKI/AAPL", "csv", nil)

To pass options to the Quandl API, use something like this:

v := quandl.Options{}
v.Set("trim_start", "2014-01-01")
v.Set("trim_end", "2014-02-02")
data, err := quandl.GetSymbol("WIKI/AAPL", v)

Index

Examples

Constants

This section is empty.

Variables

View Source
var APIKey = ""

APIKey is used to set your api key before you make any call

View Source
var LastURL = ""

LastURL will hold the last requested URL after each call

Functions

func FloatColumn

func FloatColumn(s []interface{}) []float64

FloatColumn converts a column of interface{} to a column of floats

Example
CacheHandler = filecache.Handler{Life: 60}

opts := NewOptions(
	"start_date", "2014-01-01",
	"end_date", "2014-01-06",
	"column_index", "4",
)

data, err := GetSymbol("WIKI/AAPL", opts)
if err != nil {
	panic(err)
}

columns := data.ToColumns()
var prices = FloatColumn(columns[1])
fmt.Println(prices)
Output:

[543.93 540.98 553.13]

func GetListRaw

func GetListRaw(source string, format string, page int, perPage int) ([]byte, error)

GetListRaw returns a list of symbols for a source as CSV, JSON or XML

func GetSearchRaw

func GetSearchRaw(query string, format string, page int, perPage int) ([]byte, error)

GetSearchRaw returns search results as JSON or XML

func GetSymbolRaw

func GetSymbolRaw(symbol string, format string, params Options) ([]byte, error)

GetSymbolRaw returns CSV, JSON or XML data for a given symbol

Example
// This block is optional
CacheHandler = filecache.Handler{Life: 60}
v := Options{}
v.Set("start_date", "2014-01-01")
v.Set("end_date", "2014-01-06")
v.Set("column_index", "4") // Close price only
// ---

data, err := GetSymbolRaw("WIKI/AAPL", "csv", v)
if err != nil {
	panic(err)
}
fmt.Println(string(data))
Output:

Date,Close
2014-01-06,543.93
2014-01-03,540.98
2014-01-02,553.13

func StringColumn

func StringColumn(s []interface{}) []string

StringColumn converts a column of interface{} to a column of string

func TimeColumn

func TimeColumn(s []interface{}) []time.Time

TimeColumn converts a column of interface{} to a column of time

Example
CacheHandler = filecache.Handler{Life: 60}

opts := NewOptions(
	"start_date", "2014-01-01",
	"end_date", "2014-01-06",
	"column_index", "4",
)

data, err := GetSymbol("WIKI/AAPL", opts)
if err != nil {
	panic(err)
}

columns := data.ToColumns()
var dates = TimeColumn(columns[0])
fmt.Println(dates)
Output:

[2014-01-06 00:00:00 +0000 UTC 2014-01-03 00:00:00 +0000 UTC 2014-01-02 00:00:00 +0000 UTC]

func ToColumns

func ToColumns(src [][]interface{}) (out [][]interface{})

ToColumns converts a rows array to a columns array

Example
// This block is optional
CacheHandler = filecache.Handler{Life: 60}
v := Options{}
v.Set("start_date", "2014-01-06")
v.Set("end_date", "2014-01-08")
v.Set("column_index", "4")
// ---

data, err := GetSymbol("WIKI/AAPL", v)
if err != nil {
	panic(err)
}

d := ToColumns(data.Data)
fmt.Println(d)
Output:

[[2014-01-08 2014-01-07 2014-01-06] [543.46 540.0375 543.93]]

func ToNamedColumns

func ToNamedColumns(src [][]interface{}, keys []string) (out map[string][]interface{})

ToNamedColumns converts a rows array to a columns map

Example
// This block is optional
CacheHandler = filecache.Handler{Life: 60}
v := Options{}
v.Set("start_date", "2014-01-06")
v.Set("end_date", "2014-01-07")
v.Set("column_index", "4")
// ---

data, err := GetSymbol("WIKI/AAPL", v)
if err != nil {
	panic(err)
}

d := ToNamedColumns(data.Data, data.ColumnNames)
fmt.Println(d["Date"], d["Close"])
Output:

[2014-01-07 2014-01-06] [540.0375 543.93]

Types

type Cacher

type Cacher interface {
	Get(key string) []byte
	Set(key string, data []byte) error
}

Cacher defines the interface for a custom cache handler

var CacheHandler Cacher

CacheHandler is a reference to a struct that implements the Cacher interface. If set, it will use it to get documents from the cache or set to it.

type Dataset

type Dataset struct {
	ID                  int
	DatasetCode         string `json:"dataset_code"`
	DatabaseCode        string `json:"database_code"`
	Name                string
	Description         string
	RefreshedAt         string   `json:"refreshed_at"`
	NewestAvailableDate string   `json:"newest_available_date"`
	OldestAvailableDate string   `json:"oldest_available_date"`
	ColumnNames         []string `json:"column_names"`
	Frequency           string
	Type                string
	Premium             bool
	Limit               int
	Transform           string
	ColumnIndex         int
	StartDate           string `json:"start_date"`
	EndDate             string `json:"end_date"`
	Collapse            string
	Order               string
	DatabaseID          uint
}

Dataset represents an entity at Quandl. It is used when requesting a symbol data, or a list of symbols supported by a data source.

type ListResponse

type ListResponse struct {
	Datasets []Dataset
	Meta     ResponseMeta
}

ListResponse represents the response received when requesting a list of supported symbol in a data source

Example
// This block is optional
CacheHandler = filecache.Handler{Life: 60}
// ---

data, err := GetList("WIKI", 2, 5)
if err != nil {
	panic(err)
}

if data.Meta.TotalCount > 3000 {
	fmt.Println("Found over 3000 results")
}
fmt.Println(data.Meta.CurrentPage)
fmt.Println(data.Meta.PerPage)
// fmt.Println(data.Docs[0].Code)
Output:

Found over 3000 results
2
5

func GetList

func GetList(source string, page int, perPage int) (*ListResponse, error)

GetList returns a list of symbols for a source

Example
// This block is optional
CacheHandler = filecache.Handler{Life: 60}
// ---

data, err := GetList("WIKI", 1, 3)
if err != nil {
	panic(err)
}

for i, doc := range data.Datasets {
	fmt.Println(i, doc.DatabaseCode, doc.ColumnNames[1])
}
Output:

0 WIKI Open
1 WIKI Open
2 WIKI Open

type Options

type Options url.Values

Options is used to send additional parameters in the Quandl request

func NewOptions

func NewOptions(s ...string) Options

NewOptions accepts any even number of arguments and returns an Options object. The odd arguments are the keys, the even arguments are the values.

Example
CacheHandler = filecache.Handler{Life: 60}

opts := NewOptions(
	"start_date", "2014-01-01",
	"end_date", "2014-01-06",
	"column_index", "4",
)

data, err := GetSymbolRaw("WIKI/AAPL", "csv", opts)
if err != nil {
	panic(err)
}
fmt.Println(string(data))
Output:

Date,Close
2014-01-06,543.93
2014-01-03,540.98
2014-01-02,553.13

func (Options) Set

func (o Options) Set(key, value string)

Set registers a key=value pair to be sent in the Quandl request

type ResponseMeta

type ResponseMeta struct {
	PerPage          int `json:"per_page"`
	Query            string
	CurrentPage      int  `json:"current_page"`
	PrevPage         int  `json:"prev_page"`
	TotalPages       uint `json:"total_pages"`
	TotalCount       uint `json:"total_count"`
	NextPage         int  `json:"next_page"`
	CurrentFirstItem int  `json:"current_first_item"`
	CurrentLastItem  int  `json:"current_last_item"`
}

ResponseMeta represents meta data in quandl

type SearchResponse

type SearchResponse struct {
	ListResponse
}

SearchResponse represents the response received when submitting a search request

Example
// This block is optional
CacheHandler = filecache.Handler{Life: 60}
// ---

data, err := GetSearch("twitter", 2, 5)
if err != nil {
	panic(err)
}

if data.Meta.TotalCount > 1000 {
	fmt.Println("Found more than 1000 results")
}
fmt.Println(data.Meta.CurrentPage)
fmt.Println(data.Meta.PerPage)

doc := data.Datasets[0]
fmt.Println(doc.ID)
fmt.Println(doc.DatasetCode)
fmt.Println(doc.DatabaseCode)
fmt.Println(doc.Name)
fmt.Println(doc.Description[:20], "...")
fmt.Println(doc.RefreshedAt)
fmt.Println(doc.NewestAvailableDate)
fmt.Println(doc.OldestAvailableDate)
fmt.Println(doc.ColumnNames[0], "...")
fmt.Println(doc.Frequency)
fmt.Println(doc.Type)
fmt.Println(doc.Premium)
fmt.Println(doc.Limit)
fmt.Println(doc.Transform)
fmt.Println(doc.ColumnIndex)
fmt.Println(doc.StartDate)
fmt.Println(doc.EndDate)
fmt.Println(doc.Collapse)
fmt.Println(doc.Order)
fmt.Println(doc.DatabaseID)
Output:

func GetSearch

func GetSearch(query string, page int, perPage int) (*SearchResponse, error)

GetSearch returns search results

Example
// This block is optional
CacheHandler = filecache.Handler{Life: 60}
// ---

data, err := GetSearch("google stock", 1, 3)
if err != nil {
	panic(err)
}

fmt.Printf("Found %v results", len(data.Datasets))
Output:

Found 3 results

type SymbolResponse

type SymbolResponse struct {
	Dataset
	Data [][]interface{}
}

SymbolResponse represents the response from Quandl when requesting a single symbol

Example
// This block is optional
CacheHandler = filecache.Handler{Life: 60}
v := Options{}
v.Set("start_date", "2014-01-01")
v.Set("end_date", "2014-02-02")
// ---

data, err := GetSymbol("WIKI/MSFT", v)
if err != nil {
	panic(err)
}
fmt.Println(data.ID)
fmt.Println(data.DatasetCode)
fmt.Println(data.DatabaseCode)
fmt.Println(data.Name)
fmt.Println(data.Description[:20], "...")
fmt.Println(data.RefreshedAt)
fmt.Println(data.NewestAvailableDate)
fmt.Println(data.OldestAvailableDate)
fmt.Println(data.ColumnNames[0], "...")
fmt.Println(data.Frequency)
fmt.Println(data.Type)
fmt.Println(data.Premium)
fmt.Println(data.Limit)
fmt.Println(data.Transform)
fmt.Println(data.ColumnIndex)
fmt.Println(data.StartDate)
fmt.Println(data.EndDate)
fmt.Println(data.Collapse)
fmt.Println(data.Order)
fmt.Println(data.DatabaseID)
Output:

9775827
MSFT
WIKI
Microsoft Corporation (MSFT) Prices, Dividends, Splits and Trading Volume
End of day open, hig ...
2016-06-15T21:49:38.409Z
2016-06-15
1986-03-13
Date ...
daily
Time Series
false
0

0
2014-01-01
2014-02-02

desc
0

func GetSymbol

func GetSymbol(symbol string, params Options) (*SymbolResponse, error)

GetSymbol returns data for a given symbol

Example

Main Functions

// This block is optional
CacheHandler = filecache.Handler{Life: 60}
v := Options{}
v.Set("start_date", "2014-01-01")
v.Set("end_date", "2014-02-02")
// ---

data, err := GetSymbol("WIKI/AAPL", v)
if err != nil {
	panic(err)
}
fmt.Printf("Symbol: %v, Row Count: %v\n", data.DatasetCode, len(data.Data))
fmt.Printf("Fifth column is named %v\n", data.ColumnNames[4])
fmt.Printf("On %v the close price was %v\n", data.Data[1][0], data.Data[1][4])
Output:

Symbol: AAPL, Row Count: 21
Fifth column is named Close
On 2014-01-30 the close price was 499.782

func (*SymbolResponse) ToColumns

func (s *SymbolResponse) ToColumns() [][]interface{}

ToColumns converts the data array to a columns array

Example
// This block is optional
CacheHandler = filecache.Handler{Life: 60}
v := Options{}
v.Set("start_date", "2014-01-06")
v.Set("end_date", "2014-01-08")
v.Set("column_index", "4")
// ---

data, err := GetSymbol("WIKI/AAPL", v)
if err != nil {
	panic(err)
}

d := data.ToColumns()
fmt.Println(d)
Output:

[[2014-01-08 2014-01-07 2014-01-06] [543.46 540.0375 543.93]]

func (*SymbolResponse) ToNamedColumns

func (s *SymbolResponse) ToNamedColumns(keys []string) map[string][]interface{}

ToNamedColumns converts the rows array to a columns map. You may provide it with an array of strings for the map keys (header columns), or nil to use the column names as returned from Quandl.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL