alpaca-trade-api-go

module
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2020 License: Apache-2.0

README

alpaca-trade-api-go

This fork is based on github.com/alpacahq/alpaca-trade-api-go. It adds some examples in the cmd directory on how to call the APIs and I will also be working on adding and updating missing or out-of-date structs as I discover them.

alpaca-trade-api-go is a Go library for the Alpaca trade API. It allows rapid trading algo development easily, with support for the both REST and streaming interfaces. For details of each API behavior, please see the online API document.

Installation

$ go get github.com/gmlewis/alpaca-trade-api-go/common
$ go get github.com/gmlewis/alpaca-trade-api-go/polygon
$ go get github.com/gmlewis/alpaca-trade-api-go/stream
$ go get github.com/gmlewis/alpaca-trade-api-go/alpaca

Example

In order to call Alpaca's trade API, you need to obtain an API key pair. Replace <key_id> and <secret_key> with what you get from the web console.

REST example
import (
    "os"
    "fmt"

    "github.com/gmlewis/alpaca-trade-api-go/alpaca"
    "github.com/gmlewis/alpaca-trade-api-go/common"
)

func init() {
    os.Setenv(common.EnvApiKeyID, "<key_id>")
    os.Setenv(common.EnvApiSecretKey, "<secret_key>")

    fmt.Printf("Running w/ credentials [%v %v]\n", common.Credentials().ID, common.Credentials().Secret)

    alpaca.SetBaseUrl("https://paper-api.alpaca.markets")
}

func main() {
    alpacaClient := alpaca.NewClient(common.Credentials())
    acct, err := alpacaClient.GetAccount()
    if err != nil {
        panic(err)
    }

    fmt.Println(*acct)
}
Streaming example

The SDK provides a unified streaming interface for both data updates (from Alpaca or Polygon), and Alpaca's trade/account updates. The following example subscribes to trade updates, and prints any messages received, and subscribes to live quotes for AAPL, and prints any quotes received. The main function also ends with an empty select{} statement which causes the program to run indefinitely.

In order to use Polygon streaming, you need to call stream.SetDataStream("polygon"). This requires your Alpaca account to be eligible for Polygon integration (for details of the setup, please read Alpaca API document).

package main

import (
	"fmt"
	"os"

	"github.com/gmlewis/alpaca-trade-api-go/alpaca"
	"github.com/gmlewis/alpaca-trade-api-go/common"
	"github.com/gmlewis/alpaca-trade-api-go/stream"
)

func main() {
	os.Setenv(common.EnvApiKeyID, "your_key_id")
	os.Setenv(common.EnvApiSecretKey, "your_secret_key")

	if err := stream.Register(alpaca.TradeUpdates, tradeHandler); err != nil {
		panic(err)
	}

	if err := stream.Register("Q.AAPL", quoteHandler); err != nil {
		panic(err)
	}

	select {}
}

func tradeHandler(msg interface{}) {
	tradeupdate := msg.(alpaca.TradeUpdate)
	fmt.Printf("%s event received for order %s.\n", tradeupdate.Event, tradeupdate.Order.ID)
}

func quoteHandler(msg interface{}) {
	quote := msg.(alpaca.StreamQuote)

	fmt.Println(quote.Symbol, quote.BidPrice, quote.BidSize, quote.AskPrice, quote.AskSize)
}

API Document

The HTTP API document is located at https://docs.alpaca.markets/

Authentication

The Alpaca API requires API key ID and secret key, which you can obtain from the web console after you sign in. This key pair can then be applied to the SDK either by setting environment variables (APCA_API_KEY_ID=<key_id> and APCA_API_SECRET_KEY=<secret_key>), or hardcoding them into the Go code directly as shown in the examples above.

$ export APCA_API_KEY_ID=xxxxx
$ export APCA_API_SECRET_KEY=yyyyy

Endpoint

For paper trading, set the environment variable APCA_API_BASE_URL.

$ export APCA_API_BASE_URL=https://paper-api.alpaca.markets

You can also instead use the function alpaca.SetBaseUrl("https://paper-api.alpaca.markets") to configure the endpoint.

GoDoc

For a more in-depth look at the SDK, see the GoDoc

Directories

Path Synopsis
Package API defines the Alpaca v2 and Polygon v2 APIs interface.
Package API defines the Alpaca v2 and Polygon v2 APIs interface.
Client is a simple client for testing out the Alpaca and Polygon APIs.
Client is a simple client for testing out the Alpaca and Polygon APIs.
cmd
alpaca-cancel-all-orders
alpaca-cancel-all-orders cancels all orders for the authenticated user.
alpaca-cancel-all-orders cancels all orders for the authenticated user.
alpaca-cancel-order
alpaca-cancel-order cancels an order for the authenticated user.
alpaca-cancel-order cancels an order for the authenticated user.
alpaca-close-all-positions
alpaca-close-all-positions closes all positions for the authenticated user.
alpaca-close-all-positions closes all positions for the authenticated user.
alpaca-close-position
alpaca-close-position closes a position for the authenticated user.
alpaca-close-position closes a position for the authenticated user.
alpaca-get-account
alpaca-get-account gets account information for the authenticated user.
alpaca-get-account gets account information for the authenticated user.
alpaca-get-account-activities
alpaca-get-account-activities gets account activities information for the authenticated user.
alpaca-get-account-activities gets account activities information for the authenticated user.
alpaca-get-account-configurations
alpaca-get-account-configurations gets account configuration information for the authenticated user.
alpaca-get-account-configurations gets account configuration information for the authenticated user.
alpaca-get-aggregates
alpaca-get-aggregate gets aggregate information for the authenticated user.
alpaca-get-aggregate gets aggregate information for the authenticated user.
alpaca-get-asset
alpaca-get-asset gets asset information for the authenticated user.
alpaca-get-asset gets asset information for the authenticated user.
alpaca-get-calendar
alpaca-get-calendar gets calendar information for the authenticated user.
alpaca-get-calendar gets calendar information for the authenticated user.
alpaca-get-clock
alpaca-get-clock gets clock information for the authenticated user.
alpaca-get-clock gets clock information for the authenticated user.
alpaca-get-last-quote
alpaca-get-last-quote gets last quote information for the authenticated user.
alpaca-get-last-quote gets last quote information for the authenticated user.
alpaca-get-last-trade
alpaca-get-last-trade gets last trade information for the authenticated user.
alpaca-get-last-trade gets last trade information for the authenticated user.
alpaca-get-order
alpaca-get-order gets order information for the authenticated user.
alpaca-get-order gets order information for the authenticated user.
alpaca-get-portfolio-history
alpaca-get-portfolio-history gets portfolio history information for the authenticated user.
alpaca-get-portfolio-history gets portfolio history information for the authenticated user.
alpaca-get-position
alpaca-get-position gets position information for the authenticated user.
alpaca-get-position gets position information for the authenticated user.
alpaca-get-symbol-bars
alpaca-get-symbol-bars gets symbol bars for the authorized account.
alpaca-get-symbol-bars gets symbol bars for the authorized account.
alpaca-list-assets
alpaca-list-assets lists assets information for the authenticated user.
alpaca-list-assets lists assets information for the authenticated user.
alpaca-list-bars
alpaca-list-bars lists bar lists for the named symbol(s).
alpaca-list-bars lists bar lists for the named symbol(s).
alpaca-list-orders
alpaca-list-orders lists orders information for the authenticated user.
alpaca-list-orders lists orders information for the authenticated user.
alpaca-list-positions
alpaca-list-positions lists positions information for the authenticated user.
alpaca-list-positions lists positions information for the authenticated user.
alpaca-place-order
alpaca-place-order places an order for the authenticated user.
alpaca-place-order places an order for the authenticated user.
alpaca-replace-order
alpaca-replace-order replaces an order for the authenticated user.
alpaca-replace-order replaces an order for the authenticated user.
alpaca-stream-market
alpaca-stream-market streams Trades, Quotes, and Minute-Bars for the named symbol(s).
alpaca-stream-market streams Trades, Quotes, and Minute-Bars for the named symbol(s).
polygon-get-exchanges
polygon-get-exchanges gets exchanges information for the authenticated user.
polygon-get-exchanges gets exchanges information for the authenticated user.
polygon-get-previous-close
polygon-get-previous-close gets previous day close information for the authenticated user.
polygon-get-previous-close gets previous day close information for the authenticated user.
polygon-get-snapshot
polygon-get-snapshot gets snapshot information for the authenticated user.
polygon-get-snapshot gets snapshot information for the authenticated user.
view-candles
view-candles copies the provided JSON candlestick data file to a /tmp dir and installs the trading-vue-js app, then runs "go-wasm" on it to display the chart.
view-candles copies the provided JSON candlestick data file to a /tmp dir and installs the trading-vue-js app, then runs "go-wasm" on it to display the chart.
examples

Jump to

Keyboard shortcuts

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