stream

package
v2.0.0-beta.3 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2021 License: Apache-2.0 Imports: 13 Imported by: 15

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrBadAuthResponse = errors.New("did not receive authenticated message")

ErrBadAuthResponse is returned when the client could not successfully authenticate

View Source
var ErrConnectCalledMultipleTimes = errors.New("tried to call Connect multiple times")

ErrConnectCalledMultipleTimes is returned when Connect has been called multiple times on a single client

View Source
var ErrConnectionLimitExceeded = errors.New("connection limit exceeded")

ErrConnectionLimitExceeded is returned when the client has exceeded their connection limit

View Source
var ErrInvalidCredentials = errors.New("invalid credentials")

ErrInvalidCredentials is returned when invalid credentials have been sent by the user

View Source
var ErrNoConnected = errors.New("did not receive connected message")

ErrNoConnected is returned when the client did not receive the welcome message from the server

View Source
var ErrSlowClient = errors.New("slow client")

ErrSlowClient is returned when the server has detected a slow client. In this case there's no guarantee that all prior messages are sent to the server so a subscription acknowledgement may not arrive

View Source
var ErrSubResponse = errors.New("did not receive subscribed message")

ErrSubResponse is returned when the client's subscription request was not acknowledged

View Source
var ErrSubscriptionChangeAfterTerminated = errors.New("subscription change after client termination")

ErrSubscriptionChangeAfterTerminated is returned when client attempts to change subscriptions after the client has been terminated

View Source
var ErrSubscriptionChangeAlreadyInProgress = errors.New("subscription change already in progress")

ErrSubscriptionChangeAlreadyInProgress is returned when a subscription change is called concurrently with another

View Source
var ErrSubscriptionChangeBeforeConnect = errors.New("subscription change attempted before calling Connect")

ErrSubscriptionChangeBeforeConnect is returned when the client attempts to change subscriptions before calling Connect

View Source
var ErrSubscriptionChangeInterrupted = errors.New("subscription change interrupted by client termination")

ErrSubscriptionChangeInterrupted is returned when a subscription change was in progress when the client has terminated

View Source
var ErrSymbolLimitExceeded = errors.New("symbol limit exceeded")

ErrSymbolLimitExceeded is returned when the client has subscribed to too many symbols

Functions

This section is empty.

Types

type Bar

type Bar struct {
	Symbol    string
	Open      float64
	High      float64
	Low       float64
	Close     float64
	Volume    uint64
	Timestamp time.Time
}

Bar is an aggregate of trades

type CryptoBar

type CryptoBar struct {
	Symbol    string
	Open      float64
	High      float64
	Low       float64
	Close     float64
	Volume    float64
	Timestamp time.Time
}

type CryptoClient

type CryptoClient interface {
	StreamClient
	SubscribeToTrades(handler func(trade CryptoTrade), symbols ...string) error
	UnsubscribeFromTrades(symbols ...string) error
	SubscribeToQuotes(handler func(quote CryptoQuote), symbols ...string) error
	UnsubscribeFromQuotes(symbols ...string) error
	SubscribeToBars(handler func(bar CryptoBar), symbols ...string) error
	UnsubscribeFromBars(symbols ...string) error
	SubscribeToDailyBars(handler func(bar CryptoBar), symbols ...string) error
	UnsubscribeFromDailyBars(symbols ...string) error
}

CryptoClient is a client that connects to an Alpaca Data V2 stream server and handles communication both ways.

After constructing, Connect() must be called before any subscription changes are called. Connect keeps the connection alive and reestablishes it until a configured number of retries has not been exceeded.

Terminated() returns a channel that the client sends an error to when it has terminated. A client can not be reused once it has terminated!

SubscribeTo... and UnsubscribeFrom... can be used to modify subscriptions and the handler used to process incoming trades/quotes/bars. These block until an irrecoverable error occurs or if they succeed.

Note that subscription changes can not be called concurrently.

func NewCryptoClient

func NewCryptoClient(opts ...CryptoOption) CryptoClient

NewCryptoClient returns a new CryptoClient that will connect to the crypto feed and whose default configurations are modified by opts.

type CryptoOption

type CryptoOption interface {
	// contains filtered or unexported methods
}

CryptoOption is a configuration option for the CryptoClient

func WithCryptoBars

func WithCryptoBars(handler func(CryptoBar), symbols ...string) CryptoOption

WithCryptoBars configures inital bar symbols to subscribe to and the handler

func WithCryptoDailyBars

func WithCryptoDailyBars(handler func(CryptoBar), symbols ...string) CryptoOption

WithCryptoDailyBars configures inital daily bar symbols to subscribe to and the handler

func WithCryptoQuotes

func WithCryptoQuotes(handler func(CryptoQuote), symbols ...string) CryptoOption

WithCryptoQuotes configures inital quote symbols to subscribe to and the handler

func WithCryptoTrades

func WithCryptoTrades(handler func(CryptoTrade), symbols ...string) CryptoOption

WithCryptoTrades configures inital trade symbols to subscribe to and the handler

type CryptoQuote

type CryptoQuote struct {
	Symbol    string
	BidPrice  float64
	AskPrice  float64
	Timestamp time.Time
}

type CryptoTrade

type CryptoTrade struct {
	Symbol    string
	Price     float64
	Size      float64
	Timestamp time.Time
}

type Logger

type Logger interface {
	Infof(format string, v ...interface{})
	Warnf(format string, v ...interface{})
	Errorf(format string, v ...interface{})
}

type Option

type Option interface {
	StockOption
	CryptoOption
}

Option is a configuration option that can be used for both StockClient and CryptoClient

func WithBaseURL

func WithBaseURL(url string) Option

WithBaseURL configures the base URL

func WithBufferSize

func WithBufferSize(size int) Option

WithBufferSize sets the size for the buffer that is used for messages received from the server

func WithCredentials

func WithCredentials(key, secret string) Option

WithCredentials configures the key and secret to use

func WithLogger

func WithLogger(logger Logger) Option

WithLogger configures the logger

func WithProcessors

func WithProcessors(count int) Option

WithProcessors configures how many goroutines should process incoming messages. Increasing this past 1 means that the order of processing is not necessarily the same as the order of arrival the from server.

func WithReconnectSettings

func WithReconnectSettings(limit int, delay time.Duration) Option

WithReconnectSettings configures how many consecutive connection errors should be accepted and the delay (that is multipled by the number of consecutive errors) between retries. limit = 0 means the client will try restarting indefinitely unless it runs into an irrecoverable error (such as invalid credentials).

type Quote

type Quote struct {
	Symbol      string
	BidExchange string
	BidPrice    float64
	BidSize     uint32
	AskExchange string
	AskPrice    float64
	AskSize     uint32
	Timestamp   time.Time
	Conditions  []string
	Tape        string
}

Quote is a stock quote from the market

type StockOption

type StockOption interface {
	// contains filtered or unexported methods
}

StockOption is a configuration option for the StockClient

func WithBars

func WithBars(handler func(Bar), symbols ...string) StockOption

WithBars configures inital bar symbols to subscribe to and the handler

func WithDailyBars

func WithDailyBars(handler func(Bar), symbols ...string) StockOption

WithDailyBars configures inital daily bar symbols to subscribe to and the handler

func WithQuotes

func WithQuotes(handler func(Quote), symbols ...string) StockOption

WithQuotes configures inital quote symbols to subscribe to and the handler

func WithStatuses

func WithStatuses(handler func(TradingStatus), symbols ...string) StockOption

WithStatuses configures inital trading status symbols to subscribe to and the handler

func WithTrades

func WithTrades(handler func(Trade), symbols ...string) StockOption

WithTrades configures inital trade symbols to subscribe to and the handler

type StocksClient

type StocksClient interface {
	StreamClient
	SubscribeToTrades(handler func(trade Trade), symbols ...string) error
	UnsubscribeFromTrades(symbols ...string) error
	SubscribeToQuotes(handler func(quote Quote), symbols ...string) error
	UnsubscribeFromQuotes(symbols ...string) error
	SubscribeToBars(handler func(bar Bar), symbols ...string) error
	UnsubscribeFromBars(symbols ...string) error
	SubscribeToDailyBars(handler func(bar Bar), symbols ...string) error
	UnsubscribeFromDailyBars(symbols ...string) error
	SubscribeToStatuses(handler func(ts TradingStatus), symbols ...string) error
	UnsubscribeFromStatuses(symbols ...string) error
}

StocksClient is a client that connects to an Alpaca Data V2 stream server and handles communication both ways.

After constructing, Connect() must be called before any subscription changes are called. Connect keeps the connection alive and reestablishes it until a configured number of retries has not been exceeded.

Terminated() returns a channel that the client sends an error to when it has terminated. A client can not be reused once it has terminated!

SubscribeTo... and UnsubscribeFrom... can be used to modify subscriptions and the handler used to process incoming trades/quotes/bars. These block until an irrecoverable error occurs or if they succeed.

Note that subscription changes can not be called concurrently.

func NewStocksClient

func NewStocksClient(feed string, opts ...StockOption) StocksClient

NewStocksClient returns a new StocksClient that will connect to feed data feed and whose default configurations are modified by opts.

type StreamClient

type StreamClient interface {
	// Connect establishes a connection and **reestablishes it when errors occur**
	// as long as the configured number of retires has not been exceeded.
	//
	// It blocks until the connection has been established for the first time (or it failed to do so).
	//
	// **Should only be called once!**
	Connect(ctx context.Context) error
	// Terminated returns a channel that the client sends an error to when it has terminated.
	// The channel is also closed upon termination.
	Terminated() <-chan error
}

type Trade

type Trade struct {
	ID         int64
	Symbol     string
	Exchange   string
	Price      float64
	Size       uint32
	Timestamp  time.Time
	Conditions []string
	Tape       string
}

Trade is a stock trade that happened on the market

type TradingStatus

type TradingStatus struct {
	Symbol     string
	StatusCode string
	StatusMsg  string
	ReasonCode string
	ReasonMsg  string
	Timestamp  time.Time
	Tape       string
}

TradingStatus is a halt or resume status for a security

Jump to

Keyboard shortcuts

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