Coder Social home page Coder Social logo

alpaca-trade-api-go's Introduction

alpaca-trade-api-go

GitHub Status Go Report Card

alpaca-trade-api-go is a Go library for the Alpaca trade and marketdata 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 -u github.com/alpacahq/alpaca-trade-api-go/v3/alpaca

Examples

In order to call Alpaca's trade API, you need to obtain an API key pair from the web console.

Trading REST example

package main

import (
	"fmt"

	"github.com/alpacahq/alpaca-trade-api-go/v3/alpaca"
)

func main() {
	client := alpaca.NewClient(alpaca.ClientOpts{
		// Alternatively you can set your key and secret using the
		// APCA_API_KEY_ID and APCA_API_SECRET_KEY environment variables
		APIKey:    "YOUR_API_KEY",
		APISecret: "YOUR_API_SECRET",
		BaseURL:   "https://paper-api.alpaca.markets",
	})
	acct, err := client.GetAccount()
	if err != nil {
		panic(err)
	}
	fmt.Printf("%+v\n", *acct)
}

Trade updates stream example

The following example shows how you can stream your own trade updates. First we register a handler function that simply prints the received trade updates, then we submit a single AAPL buy order. You should see two updates, a "new" event as soon as you submit the order, and a "fill" event soon after that, provided that the market is open.

// Listen to trade updates in the background (with unlimited reconnect)
alpaca.StreamTradeUpdatesInBackground(context.TODO(), func(tu alpaca.TradeUpdate) {
	log.Printf("TRADE UPDATE: %+v\n", tu)
})

// Send a single AAPL order
qty := decimal.NewFromInt(1)
if _, err := alpaca.PlaceOrder(alpaca.PlaceOrderRequest{
	Symbol:      "AAPL",
	Qty:         &qty,
	Side:        "buy",
	Type:        "market",
	TimeInForce: "day",
}); err != nil {
	log.Fatalf("failed place order: %v", err)
}
log.Println("order sent")

select {}

Further examples

See the examples directory for further examples:

  • algo-trading examples
    • long-short
    • martingale
    • mean-reversion
  • marketdata examples
    • crypto-stream
    • data-stream
    • marketdata

API Document

The HTTP API document is located here.

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 or set the BaseURL option when constructing the client.

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

Documentation

For a more in-depth look at the SDK, see the package documentation.

alpaca-trade-api-go's People

Contributors

gnvk avatar ttt733 avatar andrewkim316 avatar umitanuki avatar kalmant avatar shlomiku avatar gjtorikian avatar itsankoff avatar 117 avatar notargets avatar jrenk avatar rocketbitz avatar ccnlui avatar leki75 avatar gliptak avatar timwatson avatar noramehesz avatar alexandroskyriakakis avatar cksidharthan avatar ducille avatar matteosantama avatar drew887 avatar thetulga avatar dylanmctiernan-alpaca avatar dependabot[bot] avatar zipkr13 avatar zippeykeys12 avatar tibkiss avatar rmccorm4 avatar r4stl1n avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.