go
Last Update: March 1, 2024

Getting Started With The Golang Stock API

Get financial data in Go using the Golang Stock API. The SDK includes a variety of methods for accessing financial data such as real-time stock quotes.

Although stock data APIs are becoming more commonplace, it is rare to find one that supports Go. This is why Market Data set out to build our own Golang Stock API with a focus on rapid development and ease of use. Our Go SDK’s API has extensive documentation, examples for every endpoint, and is easy to get up and running.

Setting Up Your Environment

To set up your environment for using the Market Data Go SDK, follow these steps:

  1. Sign Up for a Market Data Account: Visit Market Data to sign up for a free account. This account gives you access to the API, allowing you to explore its capabilities without any cost. You can also opt for a paid plan with a 30-day free trial to explore premium features.
  2. Set Up Authentication: After signing up, you’ll receive an API token via email. Set this token as an environment variable named MARKETDATA_TOKEN to authenticate your requests to the Market Data API. This can be done in your terminal:
    • For macOS/Linux: export MARKETDATA_TOKEN="<your_api_token>"
    • For Windows: setx MARKETDATA_TOKEN "<your_api_token>"
  3. Install the SDK: Use the Go package manager to install the SDK:
    go get github.com/MarketDataApp/sdk-go
  4. Import the SDK in Your Project: Once installed, you can import the SDK into your Go project:
    import api "github.com/MarketDataApp/sdk-go"

By following these steps, you’ll have your environment set up and ready to get financial data in Go by making requests to the SDK.

Fetching a Real-Time Stock Quote With The Golang Stock API

If you’ve used our API before, you’re probably already familiar with getting live stock quotes from our stock quote endpoint. Using the Go SDK is just as easy. To make your first stock quote request using the Market Data Go SDK, follow these steps:

// Start by initializing a new request for the data you're interested in.
// For example, to get stock quotes for Apple Inc. (AAPL),
// you would set the symbol parameter and make the request as shown below:
quotes, err := api.StockQuote().Symbol("AAPL").Get()

// Handle errors using Go's standard error handling pattern.
if err != nil {
  log.Fatalf("Failed to get stock quotes: %v", err)
}

// Loop over the returned quotes and print them out.
// Each quote in the quotes slice represents a stock quote for the specified symbol.
// For a single quote there will be just a single entry in the slice, but once you
// begin to work with BulkQuotesRequest, you'll be able to get thousands of quotes at once.
for _, quote := range quotes {
  fmt.Println(quote)
}

Use the code above to make your first stock request. Just update the AAPL symbol with the symbol you are interested in. Although it may seem excessive at first to loop over a single-item slice, we recommend using this pattern. All our endpoints return slices and most of our endpoints include the ability to return multiple objects in the response.

Working with Stock Quotes in Go

Understanding the stock data returned by the Go SDK involves getting familiar with the StockQuote struct, which is central to handling stock data in the Market Data Go SDK. This struct encapsulates the details of a full level 1 stock quote.

type StockQuote struct {
    Symbol    string    // Symbol is the stock symbol.
    Ask       float64   // Ask is the asking price for the stock.
    AskSize   int64     // AskSize is the size (quantity) of the ask.
    Bid       float64   // Bid is the bidding price for the stock.
    BidSize   int64     // BidSize is the size (quantity) of the bid.
    Mid       float64   // Mid is the mid price calculated between the ask and bid prices.
    Last      float64   // Last is the last traded price for the stock.
    Change    *float64  // Change is the price change, can be nil if not applicable.
    ChangePct *float64  // ChangePct is the percentage change in price, can be nil if not applicable.
    High52    *float64  // High52 is the 52-week high price, can be nil if not applicable.
    Low52     *float64  // Low52 is the 52-week low price, can be nil if not applicable.
    Volume    int64     // Volume is the trading volume for the stock.
    Updated   time.Time // Updated is the time when the quote was last updated.
}

Interpreting the StockQuote Struct

When interpreting the data fields, it’s important to consider the context of the stock market and the specific stock you’re analyzing. For example, a significant change in the Ask and Bid prices could indicate a shift in market sentiment, while the Volume can give insights into the stock’s liquidity. The High52 and Low52 provide a perspective on the stock’s performance over the past year, and the Change and ChangePct offer immediate feedback on recent price movements.

Additional Resources

You can consult the Market Data Go SDK on Github as well as access the SDK’s documentation on pkg.go.dev. However, we recommend using our own Go SDK Documentation Portal for detailed information and examples.

On this page
    Add a header to begin generating the table of contents
    Share this article
    Comments & Questions
    Shopping Basket
    //