Code Monkey home page Code Monkey logo

gnagel / go-tradier Goto Github PK

View Code? Open in Web Editor NEW
2.0 4.0 6.0 115 KB

A Golang client for the Tradier REST API. This is a fork of the original timpalpant/go-tradier library with additional features added: Cobra CLI, Historical Data retrieval, Improved retries, and Unit tests.

Home Page: https://github.com/gnagel/go-tradier

License: GNU Lesser General Public License v3.0

Go 99.76% Makefile 0.24%
options-trading stock tradier tradier-api tradier-client api cli

go-tradier's Introduction

go-tradier

A Go library for accessing the Tradier Developer API.

GoDoc Build Status Coverage Status

go-tradier is a library to access the Tradier Developer API from Go. It provides a thin wrapper for working with the JSON REST endpoints.

Tradier is the first Brokerage API company, powering the world's leading trading, investing and digital advisor platforms. Tradier is not affiliated and does not endorse or recommend this library.

Usage

tcli

The tcli tool is a small command-line interface for making requests.

$ go install github.com/gnagel/go-tradier/tcli
$ tcli -tradier.account XXXXX -tradier.apikey XXXXX -command positions

Fetch real-time top-of-book quotes

package main

import (
  "fmt"

  "github.com/gnagel/go-tradier"
)

func main() {
  params := tradier.DefaultParams("your-api-key-here")
  client := tradier.NewClient(params)

  quotes, err := client.GetQuotes([]string{"AAPL", "SPY"})
  if err != nil {
    panic(err)
  }

  for _, quote := range quotes {
    fmt.Printf("%v: bid $%.02f (%v shares), ask $%.02f (%v shares)\n",
      quote.Symbol, quote.Bid, quote.BidSize, quote.Ask, quote.AskSize)
  }
}

Stream real-time top-of-book trades and quotes (L1 TAQ) data.

package main

import (
	"fmt"

	"github.com/gnagel/go-tradier"
)

func main() {
	params := tradier.DefaultParams("your-api-key-here")
	client := tradier.NewClient(params)

	eventsReader, err := client.StreamMarketEvents(
		[]string{"AAPL", "SPY"},
		[]tradier.Filter{tradier.FilterQuote, tradier.FilterTrade})
	if err != nil {
		panic(err)
	}

	eventsCh := make(chan *tradier.StreamEvent)
	eventStream := tradier.NewMarketEventStream(eventsReader, eventsCh)
	defer eventStream.Stop()

	demuxer := tradier.StreamDemuxer{
		Quotes: func(quote *tradier.QuoteEvent) {
			fmt.Printf("QUOTE %v: bid $%.02f (%v shares), ask $%.02f (%v shares)\n",
				quote.Symbol, quote.Bid, quote.BidSize, quote.Ask, quote.AskSize)
		},
		Trades: func(trade *tradier.TradeEvent) {
			fmt.Printf("TRADE %v: $%.02f (%v shares) at %v\n",
				trade.Symbol, trade.Price, trade.Size, trade.DateMs)
		},
	}

	demuxer.HandleChan(eventsCh)
}

Place and then cancel an order for SPY.

package main

import (
	"fmt"
	"time"

	"github.com/gnagel/go-tradier"
)

func main() {
	params := tradier.DefaultParams("your-api-key-here")
	client := tradier.NewClient(params)
	client.SelectAccount("your-account-id-here")

	// Place a limit order for 1 share of SPY at $1.00.
	orderId, err := client.PlaceOrder(tradier.Order{
		Class:    tradier.Equity,
		Type:     tradier.LimitOrder,
		Symbol:   "SPY",
		Side:     tradier.Buy,
		Quantity: 1,
		Price:    1.00,
		Duration: tradier.Day,
	})
	if err != nil {
		panic(err)
	}
	fmt.Printf("Placed order: %v\n", orderId)

	time.Sleep(2 * time.Second)
	order, err := client.GetOrderStatus(orderId)
	if err != nil {
		panic(err)
	}
	fmt.Printf("Order status: %v\n", order.Status)

	// Cancel the order.
	fmt.Printf("Canceling order: %v\n", orderId)
	if err := client.CancelOrder(orderId); err != nil {
		panic(err)
	}
}

Contributing

Pull requests and issues are welcomed!

License

go-tradier is released under the GNU Lesser General Public License, Version 3.0

go-tradier's People

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

go-tradier's Issues

Single result causes JSON Marshalling Crash

I'm sure you're well aware of this issue as it's 'suppose to be fixed in a future version of the API', but for others who don't know..

The Tradier API returns a single item as a key:value pair whereas multiple items are returned as a JSON array containing multiple key:value pairs. This poorly formed single JSON object response is not handled by this library and causes an unmarshalling error. It would be great if this could be handled.

The best solution I've found is to store each level as a json.RawMessage and then test unmarshalling into either an item or []item struct as appropriate. Annoying, but here we are.

ps. thank you for creating this library, it has saved me a lot of time.

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.