account

package
v0.2.20 Latest Latest
Warning

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

Go to latest
Published: May 27, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type OrderFlow

type OrderFlow struct {
	// contains filtered or unexported fields
}

func (*OrderFlow) C

func (f *OrderFlow) C() <-chan *exchanges.Order

func (*OrderFlow) Close

func (f *OrderFlow) Close()

func (*OrderFlow) Fills added in v0.2.11

func (f *OrderFlow) Fills() <-chan *exchanges.Fill
Example
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

adp := &accountRuntimeStubExchange{
	placeResp: &exchanges.Order{
		ClientOrderID: "cli-1",
		Symbol:        "ETH",
		Side:          exchanges.OrderSideBuy,
		Type:          exchanges.OrderTypeLimit,
		Quantity:      decimal.RequireFromString("0.1"),
		Price:         decimal.RequireFromString("100"),
		Status:        exchanges.OrderStatusPending,
	},
}

acct := account.NewTradingAccount(adp, nil)
if err := acct.Start(ctx); err != nil {
	panic(err)
}
defer acct.Close()

flow, err := acct.Place(ctx, &exchanges.OrderParams{
	Symbol:   "ETH",
	Side:     exchanges.OrderSideBuy,
	Type:     exchanges.OrderTypeLimit,
	Quantity: decimal.RequireFromString("0.1"),
	Price:    decimal.RequireFromString("100"),
})
if err != nil {
	panic(err)
}
defer flow.Close()

adp.EmitOrder(&exchanges.Order{
	OrderID:       "exch-1",
	ClientOrderID: "cli-1",
	Symbol:        "ETH",
	Side:          exchanges.OrderSideBuy,
	Type:          exchanges.OrderTypeLimit,
	Quantity:      decimal.RequireFromString("0.1"),
	Price:         decimal.RequireFromString("100"),
	Status:        exchanges.OrderStatusNew,
})
adp.EmitFill(&exchanges.Fill{
	TradeID:       "trade-1",
	OrderID:       "exch-1",
	ClientOrderID: "cli-1",
	Symbol:        "ETH",
	Side:          exchanges.OrderSideBuy,
	Price:         decimal.RequireFromString("101"),
	Quantity:      decimal.RequireFromString("0.04"),
	Timestamp:     1,
})

fill := <-flow.Fills()

order, err := flow.Wait(ctx, func(o *exchanges.Order) bool {
	return o.LastFillQuantity.Equal(decimal.RequireFromString("0.04"))
})
if err != nil {
	panic(err)
}

fmt.Println(fill.TradeID, order.Status, order.LastFillPrice)
Output:
trade-1 PARTIALLY_FILLED 101

func (*OrderFlow) Latest

func (f *OrderFlow) Latest() *exchanges.Order

func (*OrderFlow) Wait

func (f *OrderFlow) Wait(ctx context.Context, predicate func(*exchanges.Order) bool) (*exchanges.Order, error)

type StreamHealth added in v0.2.20

type StreamHealth struct {
	Name          StreamName   `json:"name"`
	Status        StreamStatus `json:"status"`
	Supported     bool         `json:"supported"`
	Ready         bool         `json:"ready"`
	Events        uint64       `json:"events"`
	DroppedEvents uint64       `json:"dropped_events"`
	LastEventAt   time.Time    `json:"last_event_at,omitempty"`
	LastError     string       `json:"last_error,omitempty"`
	LastErrorAt   time.Time    `json:"last_error_at,omitempty"`
}

StreamHealth is a copyable snapshot of one runtime stream's state.

type StreamName added in v0.2.20

type StreamName string

StreamName identifies a TradingAccount runtime stream.

const (
	StreamOrders    StreamName = "orders"
	StreamFills     StreamName = "fills"
	StreamPositions StreamName = "positions"
)

type StreamStatus added in v0.2.20

type StreamStatus string

StreamStatus describes the current support/health state of a stream.

const (
	StreamStatusUnknown     StreamStatus = "unknown"
	StreamStatusStarting    StreamStatus = "starting"
	StreamStatusReady       StreamStatus = "ready"
	StreamStatusUnsupported StreamStatus = "unsupported"
	StreamStatusError       StreamStatus = "error"
	StreamStatusStopped     StreamStatus = "stopped"
)

type Subscription

type Subscription[T any] struct {
	C <-chan *T // Read-only channel for the consumer
	// contains filtered or unexported fields
}

Subscription represents a single subscriber's channel for receiving events. Call Unsubscribe() to stop receiving events and release resources.

func (*Subscription[T]) Unsubscribe

func (s *Subscription[T]) Unsubscribe()

Unsubscribe removes this subscription and closes the channel.

type TradingAccount

type TradingAccount struct {
	// contains filtered or unexported fields
}

func NewTradingAccount

func NewTradingAccount(adp exchanges.Exchange, logger exchanges.Logger, _ ...TradingAccountOption) *TradingAccount

func (*TradingAccount) Balance

func (a *TradingAccount) Balance() decimal.Decimal

func (*TradingAccount) Cancel

func (a *TradingAccount) Cancel(ctx context.Context, orderID, symbol string) error

func (*TradingAccount) CancelWS

func (a *TradingAccount) CancelWS(ctx context.Context, orderID, symbol string) error

func (*TradingAccount) Close

func (a *TradingAccount) Close()

func (*TradingAccount) Health added in v0.2.20

Health returns a copy of the current account runtime health snapshot.

func (*TradingAccount) OpenOrder

func (a *TradingAccount) OpenOrder(orderID string) (*exchanges.Order, bool)

func (*TradingAccount) OpenOrders

func (a *TradingAccount) OpenOrders() []exchanges.Order

func (*TradingAccount) Place

func (a *TradingAccount) Place(ctx context.Context, params *exchanges.OrderParams) (*OrderFlow, error)

func (*TradingAccount) PlaceWS

func (a *TradingAccount) PlaceWS(ctx context.Context, params *exchanges.OrderParams) (*OrderFlow, error)

func (*TradingAccount) Position

func (a *TradingAccount) Position(symbol string) (*exchanges.Position, bool)

func (*TradingAccount) Positions

func (a *TradingAccount) Positions() []exchanges.Position

func (*TradingAccount) Start

func (a *TradingAccount) Start(ctx context.Context) (err error)

func (*TradingAccount) SubscribeOrders

func (a *TradingAccount) SubscribeOrders() *Subscription[exchanges.Order]

func (*TradingAccount) SubscribePositions

func (a *TradingAccount) SubscribePositions() *Subscription[exchanges.Position]

func (*TradingAccount) Track

func (a *TradingAccount) Track(orderID, clientOrderID string) (*OrderFlow, error)

type TradingAccountHealth added in v0.2.20

type TradingAccountHealth struct {
	Started        bool                        `json:"started"`
	Starting       bool                        `json:"starting"`
	Closing        bool                        `json:"closing"`
	SnapshotLoaded bool                        `json:"snapshot_loaded"`
	LastSnapshotAt time.Time                   `json:"last_snapshot_at,omitempty"`
	Streams        map[StreamName]StreamHealth `json:"streams"`
}

TradingAccountHealth is a copyable snapshot of account runtime health.

type TradingAccountOption

type TradingAccountOption func(*TradingAccount)

Jump to

Keyboard shortcuts

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