Documentation
¶
Overview ¶
Package tinvest provides a Go client for the T-Invest (Tinkoff Investments) gRPC API.
Use NewConn to create a configured gRPC connection and NewClient to wrap it with typed service sub-clients. Financial values are converted between proto representations (pb.Quotation, pb.MoneyValue) and udecimal.Decimal via helper functions.
Index ¶
- Constants
- Variables
- func DecimalToMoneyValue(d udecimal.Decimal, currency string) (*pb.MoneyValue, error)
- func DecimalToQuotation(d udecimal.Decimal) (*pb.Quotation, error)
- func FormatMoney(moneyValue *pb.MoneyValue) (string, error)
- func FormatQuotation(quotation *pb.Quotation) (string, error)
- func MoneyValueToDecimal(moneyValue *pb.MoneyValue) (udecimal.Decimal, error)
- func NewConn(ctx context.Context, connConfig *ConnConfig) (*grpc.ClientConn, error)
- func QuotationToDecimal(quotation *pb.Quotation) (udecimal.Decimal, error)
- type Client
- type ClientConfig
- type ConnConfig
Constants ¶
const ( // EndpointProduction is the T-Invest live trading API endpoint. EndpointProduction = "invest-public-api.tinkoff.ru:443" // EndpointSandbox is the T-Invest sandbox API endpoint for testing without real money. EndpointSandbox = "sandbox-invest-public-api.tinkoff.ru:443" // AppName is the default x-app-name header value identifying this client library. AppName = "github.com/acidsailor/tinvest" )
Variables ¶
var ( // ErrNil indicates a required argument was nil. ErrNil = errors.New("nil") // ErrInvalidConfig indicates a configuration value is missing or invalid. ErrInvalidConfig = errors.New("invalid config") // ErrOverflow indicates a value does not fit the target representation // (int64 units, int32 nano, or 9-digit fractional precision). ErrOverflow = errors.New("overflow") // ErrConversion indicates a failure in decimal ↔ units/nano conversion. ErrConversion = errors.New("conversion") )
Sub-sentinel errors for finer-grained errors.Is matching. All wrap ErrClient, so errors.Is(err, ErrClient) still works.
var ErrClient = errors.New("tinvest client")
ErrClient is the sentinel error for the tinvest package. Errors returned by this package's own validation and conversion functions wrap it, enabling callers to detect them with errors.Is(err, ErrClient). Errors from gRPC RPC calls are passed through unwrapped.
Functions ¶
func DecimalToMoneyValue ¶
DecimalToMoneyValue converts a udecimal.Decimal and currency to a proto MoneyValue.
func DecimalToQuotation ¶
DecimalToQuotation converts a udecimal.Decimal to a proto Quotation.
func FormatMoney ¶ added in v0.3.0
func FormatMoney(moneyValue *pb.MoneyValue) (string, error)
FormatMoney formats a MoneyValue as "250.50 RUB". Shows 2 decimal places (nano is truncated to centesimal precision). Returns an error if moneyValue is nil or units and nano have mixed signs.
func FormatQuotation ¶ added in v0.3.0
FormatQuotation formats a Quotation as a decimal string. Shows 2 decimal places only when Nano is non-zero (truncated to centesimal precision). Returns an error if quotation is nil or units and nano have mixed signs.
func MoneyValueToDecimal ¶
func MoneyValueToDecimal(moneyValue *pb.MoneyValue) (udecimal.Decimal, error)
MoneyValueToDecimal converts a proto MoneyValue to udecimal.Decimal. The currency field is dropped. Returns an error if moneyValue is nil.
func NewConn ¶
func NewConn( ctx context.Context, connConfig *ConnConfig, ) (*grpc.ClientConn, error)
NewConn creates a *grpc.ClientConn configured for the T-Invest API. The connection is lazy (grpc.NewClient default) — no actual TCP dial until the first RPC. The caller owns the connection lifecycle and is responsible for calling conn.Close(). ctx is used for structured logging and trace propagation.
Types ¶
type Client ¶
type Client struct {
// Instruments provides reference data for bonds, shares, ETFs, futures, etc.
Instruments pb.InstrumentsServiceClient
// MarketData provides current prices, order books, and candles.
MarketData pb.MarketDataServiceClient
// MarketDataStream provides real-time market data via server-side streaming.
MarketDataStream pb.MarketDataStreamServiceClient
// Operations provides trade and portfolio operation history.
Operations pb.OperationsServiceClient
// OperationsStream provides real-time portfolio updates via server-side streaming.
OperationsStream pb.OperationsStreamServiceClient
// Orders provides order placement and management.
Orders pb.OrdersServiceClient
// OrdersStream provides real-time order state updates via server-side streaming.
OrdersStream pb.OrdersStreamServiceClient
// StopOrders provides stop-order placement and management.
StopOrders pb.StopOrdersServiceClient
// Sandbox provides a paper-trading environment that mirrors the production API.
Sandbox pb.SandboxServiceClient
// Users provides account and tariff information.
Users pb.UsersServiceClient
// Signals provides trading signals and strategies.
Signals pb.SignalServiceClient
}
Client provides access to all T-Invest API services. It wraps an existing *grpc.ClientConn — use NewConn to create one with standard T-Invest configuration, or pass your own. Does not own the connection — caller is responsible for conn.Close().
func NewClient ¶
func NewClient(conn *grpc.ClientConn, config *ClientConfig) (*Client, error)
NewClient wraps an existing gRPC connection with T-Invest service sub-clients. Does not own the connection — caller is responsible for conn.Close(). Returns an error if conn is nil or if config fails validation.
type ClientConfig ¶
type ClientConfig struct{}
ClientConfig holds configuration for the Client created by NewClient. Build with NewClientConfig.
func NewClientConfig ¶
func NewClientConfig() *ClientConfig
NewClientConfig creates a ClientConfig with default values.
func (*ClientConfig) Validate ¶
func (c *ClientConfig) Validate() error
Validate checks that the ClientConfig is valid before use.
type ConnConfig ¶
type ConnConfig struct {
// contains filtered or unexported fields
}
ConnConfig holds configuration for the gRPC connection created by NewConn. Build with NewConnConfig and use WithAppName to override the default app name.
func NewConnConfig ¶
func NewConnConfig(endpoint, token string) *ConnConfig
NewConnConfig creates a ConnConfig with the required endpoint and API token. Use EndpointProduction or EndpointSandbox as the endpoint value.
func (*ConnConfig) Validate ¶
func (c *ConnConfig) Validate() error
Validate checks that the ConnConfig is valid before use.
func (*ConnConfig) WithAppName ¶
func (c *ConnConfig) WithAppName(name string) *ConnConfig
WithAppName sets the x-app-name header sent with every request. T-Invest uses this to identify the client application in their logs.