go-iex
A Go library for accessing the IEX Developer API.
go-iex is a library to access the IEX Developer API from Go.
It provides a thin wrapper for working with the JSON REST endpoints and IEXTP1 pcap data.
IEX is a fair, simple and transparent stock exchange dedicated to investor protection.
IEX provides realtime and historical market data for free through the IEX Developer API.
By using the IEX API, you agree to the Terms of Use. IEX is not affiliated
and does not endorse or recommend this library.
Usage
pcap2json
If you just need a tool to convert the provided pcap data files into JSON, you can use the included pcap2json
tool:
$ go install github.com/timpalpant/go-iex/pcap2json
$ pcap2json < input.pcap > output.json
Fetch real-time top-of-book quotes
package main
import (
"fmt"
"net/http"
"github.com/timpalpant/go-iex"
)
func main() {
client := iex.NewClient(&http.Client{})
quotes, err := client.GetTOPS([]string{"AAPL", "SPY"})
if err != nil {
panic(err)
}
for _, quote := range quotes {
fmt.Printf("%v: bid $%.02f (%v shares), ask $%.02f (%v shares) [as of %v]\n",
quote.Symbol, quote.BidPrice, quote.BidSize,
quote.AskPrice, quote.AskSize, quote.LastUpdated)
}
}
Fetch historical top-of-book quote (L1 tick) data.
Historical tick data (TOPS and DEEP) can be parsed using the PcapScanner
.
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"time"
"github.com/timpalpant/go-iex"
"github.com/timpalpant/go-iex/iextp/tops"
)
func main() {
client := iex.NewClient(&http.Client{})
histData, err := client.GetHIST(time.Date(2016, time.December, 12, 0, 0, 0, 0, time.UTC))
if err != nil {
panic(err)
} else if len(histData) == 0 {
panic(fmt.Errorf("Found %v available data feeds", len(histData)))
}
resp, err := http.Get(histData[0].Link)
if err != nil {
panic(err)
}
defer resp.Body.Close()
packetDataSource, err := iex.NewPacketDataSource(resp.Body)
if err != nil {
panic(err)
}
pcapScanner := iex.NewPcapScanner(packetDataSource)
enc := json.NewEncoder(os.Stdout)
for {
msg, err := pcapScanner.NextMessage()
if err != nil {
if err == io.EOF {
break
}
panic(err)
}
switch msg := msg.(type) {
case *tops.QuoteUpdateMessage:
enc.Encode(msg)
default:
}
}
}
Contributing
Pull requests and issues are welcomed!
License
go-iex is released under the GNU Lesser General Public License, Version 3.0