/polygon-go

Polygon.io API wrapper for Go

Primary LanguageGoMozilla Public License 2.0MPL-2.0

polygon

godoc goreportcard

Installation

go get -u github.com/117/polygon@latest

Client

There are two ways to interact with the client. The first is using an environment variable with the DefaultClient.

$ export POLYGON_KEY yourKeyGoesHere

The second way is by creating your own client, this has the benefit of using multiple keys.

func main() {
    client, err := polygon.NewClient(&polygon.Credentials{Key: "yourKeyHere"})

    // an error occurs if they key is unauthorized
    if err != nil {
        panic(err)
    }
}

Streaming

Your API key allows 1 simultaneous connection to each cluster.

Cluster URL Enum
Stocks wss://socket.polygon.io/stocks polygon.Stocks
Forex wss://socket.polygon.io/forex polygon.Forex
Crypto wss://socket.polygon.io/crypto polygon.Crypto

Connecting to these servers is easy.

// this is a blocking method, wrap a for{} loop to reconnect on error
err := polygon.Stream(polygon.Stocks, []string{"Q.SPY"}, func(event polygon.StreamEvent) {
    switch event.String("ev") {
    case "Q":
        fmt.Println(fmt.Sprintf("received quote for %q symbol", event.String("sym")))
    }
})

Methods

These are all the Polygon.io REST API methods supported by the wrapper.

Method Returns Example
polygon.Tickers(*polygon.Parameters) ResponseTickers See Example
polygon.TickerTypes() ResponseTickerTypes See Example
polygon.TickerDetails(*polygon.Parameters) ResponseTickerDetails See Example
polygon.TickerNews() ResponseTickerNews See Example
polygon.Markets() ResponseMarkets See Example
polygon.Locales() ResponseLocales See Example
polygon.StockSplits() ResponseStockSplits See Example
polygon.StockDividends() ResponseStockDividends See Example
polygon.StockFinancials() ResponseStockFinancials See Example
polygon.MarketStatus() ResponseMarketStatus See Example
polygon.MarketHolidays() ResponseMarketHolidays See Example
polygon.Exchanges() ResponseExchanges See Example
polygon.HistoricTrades() ResponseHistoricTrades See Example
polygon.HistoricQuotes() ResponseHistoricQuotes See Example
polygon.LastTradeForATicker() ResponseLastTradeForATicker See Example
polygon.DailyOpenClose() ResponseDailyOpenClose See Example
polygon.ConditionMappings() map[string]string See Example
polygon.SnapshotAllTickers() ResponseSnapshotMultipleTickers See Example
polygon.SnapshotSingleTicker() ResponseSnapshotSingleTicker See Example
polygon.SnapshotGainersLosers() ResponseSnapshotMultipleTickers See Example
polygon.PreviousClose() ResponsePreviousClose See Example
polygon.Aggregates() ResponseAggregates See Example
polygon.GroupedDaily() ResponseAggregates See Example
more coming soon...

Tickers

Query all ticker symbols which are supported by Polygon.io. This API includes Indices, Crypto, FX, and Stocks/Equities.

tickers, err := polygon.Tickers(&polygon.Parameters{
    Market: "stocks",
    // possibly more, check polygon docs or hover in VSC
})

TickerTypes

Get the mapping of ticker types to descriptions / long names.

types, err := polygon.TickerTypes()

TickerDetails

Get the details of the symbol company/entity. These are important details which offer an overview of the entity. Things like name, sector, description, logo and similar companies.

details, err := polygon.TickerDetails(&polygon.Parameters{
    Ticker: "AAPL",
    // possibly more, check polygon docs or hover in VSC
})

TickerNews

Get news articles for this ticker.

news, err := polygon.TickerNews(&polygon.Parameters{
    Ticker: "AAPL",
    // possibly more, check polygon docs or hover in VSC
})

Markets

Get the list of currently supported markets.

markets, err := polygon.Markets()

Locales

Get the list of currently supported locales.

locales, err := polygon.Locales()

StockSplits

Get the historical splits for this symbol.

splits, err := polygon.StockSplits(&polygon.Parameters{
    Ticker: "AAPL",
    // possibly more, check polygon docs or hover in VSC
})

StockDividends

Get the historical divdends for this ticker.

dividends, err := polygon.StockDividends(&polygon.Parameters{
    Ticker: "AAPL",
    // possibly more, check polygon docs or hover in VSC
})

StockFinancials

Get the historical financials for this ticker.

financials, err := polygon.StockFinancials(&polygon.Parameters{
    Ticker: "AAPL",
    // possibly more, check polygon docs or hover in VSC
})

MarketStatus

Current status of each market.

status, err := polygon.MarketStatus()

MarketHolidays

Get upcoming market holidays and their open/close times.

holidays, err := polygon.MarketHolidays()

Exchanges

List of stock exchanges which are supported by Polygon.io.

exchanges, err := polygon.Exchanges()

HistoricTrades

Get historic trades for a ticker.

trades, err := polygon.HistoricTrades(&polygon.Parameters{
    Ticker: "AAPL",
    Date:   "2020-01-01",
    Limit:  100,
    // possibly more, check polygon docs or hover in VSC
})

HistoricQuotes

Get historic NBBO quotes for a ticker.

quotes, err := polygon.HistoricQuotes(&polygon.Parameters{
    Ticker: "AAPL",
    Date:   "2020-01-01",
    Limit:  100,
    // possibly more, check polygon docs or hover in VSC
})

LastTradeForATicker

Get the last trade for a given stock.

trade, err := polygon.LastTradeForATicker(&polygon.Parameters{
    Ticker: "AAPL",
    // possibly more, check polygon docs or hover in VSC
})

LastQuoteForATicker

Get the last quote tick for a given stock.

quote, err := polygon.LastQuoteForATicker(&polygon.Parameters{
    Ticker: "AAPL",
    // possibly more, check polygon docs or hover in VSC
})

DailyOpenClose

Get the open, close and afterhours prices of a symbol on a certain date.

quotes, err := polygon.DailyOpenClose(&polygon.Parameters{
    Ticker: "AAPL",
    Date:   "2020-01-01",
    // possibly more, check polygon docs or hover in VSC
})

ConditionMappings

The mappings for conditions on trades and quotes.

mappings, err := polygon.ConditionMappings(&polygon.Parameters{
    TickType: "trades",
    // possibly more, check polygon docs or hover in VSC
})

SnapshotAllTickers

Snapshot allows you to see all tickers current minute aggregate, daily aggregate and last trade. As well as previous days aggregate and calculated change for today.

snapshots, err := polygon.SnapshotAllTickers()

SnapshotSingleTicker

See the current snapshot of a single ticker.

snapshot, err := polygon.SnapshotSingleTicker(&polygon.Parameters{
    Ticker: "AAPL",
    // possibly more, check polygon docs or hover in VSC
})

SnapshotGainersLosers

See the current snapshot of the top 20 gainers or losers of the day at the moment.

snapshots, err := polygon.SnapshotGainersLosers(&polygon.Parameters{
    Direction: "gainers",
    // possibly more, check polygon docs or hover in VSC
})

PreviousClose

Get the previous day close for the specified ticker.

aggregate, err := polygon.PreviousClose(&polygon.Parameters{
    Ticker: "AAPL",
    // possibly more, check polygon docs or hover in VSC
})

Aggregates

Get the previous day close for the specified ticker.

aggregate, err := polygon.Aggregates(&polygon.Parameters{
    Ticker:     "AAPL",
    Timespan:   "day",
    From:       "2019-01-01",
    To:         "2019-02-01",
    Multiplier: 1,
    // possibly more, check polygon docs or hover in VSC
})

GroupedDaily

Get the daily OHLC for entire markets.

aggregates, err := polygon.GroupedDaily(&polygon.Parameters{
    Locale: "US",
    Market: "stocks",
    Date:   "2019-02-01",
    // possibly more, check polygon docs or hover in VSC
})