Documentation ¶
Overview ¶
Package broker provides an API for interacting with 3rd party exchanges, and a simulated dealer for backtesting in the child package backtest.
Index ¶
- Constants
- func SortMapValues[K constraints.Ordered, V any](m map[K]V) []V
- type AccountBalance
- type BracketOrder
- type DealID
- type Dealer
- type EquitySeries
- type MakeSimulatedDealer
- type MockDealer
- func (d *MockDealer) CancelOrders(ctx context.Context) (*web.Response, error)
- func (d *MockDealer) GetBalance(ctx context.Context) (*AccountBalance, *web.Response, error)
- func (d *MockDealer) ListPositions(ctx context.Context, opts *web.ListOpts) ([]Position, *web.Response, error)
- func (d *MockDealer) ListTrades(ctx context.Context, opts *web.ListOpts) ([]Trade, *web.Response, error)
- func (d *MockDealer) PlaceOrder(ctx context.Context, order Order) (*Order, *web.Response, error)
- type Order
- type OrderSide
- type OrderState
- type OrderType
- type Position
- type PositionState
- type SimulatedDealer
- type StubDealer
- func (d *StubDealer) CancelOrders(ctx context.Context) (*web.Response, error)
- func (d *StubDealer) EquityHistory() EquitySeries
- func (d *StubDealer) GetBalance(ctx context.Context) (*AccountBalance, *web.Response, error)
- func (d *StubDealer) ListPositions(ctx context.Context, opts *web.ListOpts) ([]Position, *web.Response, error)
- func (d *StubDealer) ListTrades(ctx context.Context, opts *web.ListOpts) ([]Trade, *web.Response, error)
- func (d *StubDealer) PlaceOrder(ctx context.Context, order Order) (*Order, *web.Response, error)
- func (d *StubDealer) ReceivePrice(ctx context.Context, price market.Kline) error
- func (d *StubDealer) SetInitialCapital(amount decimal.Decimal)
- type TimeSeries
- type Timestamp
- type Trade
Constants ¶
const ( // OrderPending represents an order that has not been processed by a dealer. OrderPending = iota // OrderOpen represents an order that has been opened by a dealer but not yet filled. OrderOpen // OrderFilled represents an order that has been filled by a dealer at a price level. OrderFilled // OrderClosed represents an order that has been closed by a dealer. OrderClosed )
const ( // PositionPending represents a position that has not been processed by a dealer. PositionPending = iota // PositionOpen represents a position that has been opened by a dealer. PositionOpen // PositionClosed represents a position that has been closed by a dealer. PositionClosed )
Variables ¶
This section is empty.
Functions ¶
func SortMapValues ¶
func SortMapValues[K constraints.Ordered, V any](m map[K]V) []V
SortMapValues sorts the values of the map.
Types ¶
type AccountBalance ¶
type AccountBalance struct { // Trade is the balance amount available for trading. Trade decimal.Decimal // Equity is the total notional account value including unrealized gains on open positions. Equity decimal.Decimal }
AccountBalance is a representation of a broker's account balance.
type BracketOrder ¶
BracketOrder groups together a set of dependent orders to open and manage a new position.
type DealID ¶
type DealID string
DealID is a unique identifier for a dealer data entity.
func NewIDWithTime ¶
NewIDWithTime returns a new DealID seeded with the given time.
type Dealer ¶
type Dealer interface { GetBalance(context.Context) (*AccountBalance, *web.Response, error) PlaceOrder(context.Context, Order) (*Order, *web.Response, error) CancelOrders(context.Context) (*web.Response, error) ListPositions(context.Context, *web.ListOpts) ([]Position, *web.Response, error) ListTrades(context.Context, *web.ListOpts) ([]Trade, *web.Response, error) }
Dealer is an interface for interacting with a 3rd party exchange and placing orders in the market.
type EquitySeries ¶
type EquitySeries TimeSeries[decimal.Decimal]
EquitySeries is a time series of equity values.
func (EquitySeries) SortKeys ¶
func (es EquitySeries) SortKeys() []Timestamp
SortKeys returns a sorted slice of keys in ascending chronological order.
func (EquitySeries) SortValuesByTime ¶
func (es EquitySeries) SortValuesByTime() []decimal.Decimal
SortValuesByTime returns a sorted slice of values in ascending chronological order.
type MakeSimulatedDealer ¶
type MakeSimulatedDealer func() (SimulatedDealer, error)
MakeSimulatedDealer returns a new SimulatedDealer
type MockDealer ¶
MockDealer is a mock implementation of the Dealer interface using testify/mock.
func (*MockDealer) CancelOrders ¶
CancelOrders cancels an order.
func (*MockDealer) GetBalance ¶
func (d *MockDealer) GetBalance(ctx context.Context) (*AccountBalance, *web.Response, error)
GetBalance returns the balance of the account.
func (*MockDealer) ListPositions ¶
func (d *MockDealer) ListPositions(ctx context.Context, opts *web.ListOpts) ([]Position, *web.Response, error)
ListPositions returns the positions of the account.
func (*MockDealer) ListTrades ¶
func (d *MockDealer) ListTrades(ctx context.Context, opts *web.ListOpts) ([]Trade, *web.Response, error)
ListTrades returns the trades of the account.
func (*MockDealer) PlaceOrder ¶
PlaceOrder places an order.
type Order ¶
type Order struct { ID DealID OpenedAt time.Time FilledAt time.Time ClosedAt time.Time Asset market.Asset Side OrderSide Type OrderType LimitPrice decimal.Decimal Size decimal.Decimal ReduceOnly bool FilledPrice decimal.Decimal FilledSize decimal.Decimal }
Order represents an order to be placed using a dealer.
func (*Order) State ¶
func (o *Order) State() OrderState
State returns the state of the order based on the order timestamps.
type OrderSide ¶
type OrderSide int
OrderSide represents the side of an order: Buy (long) or Sell (short).
func (OrderSide) MarshalText ¶ added in v0.0.20
MarshalText is used to output as a string for CSV rendering.
type OrderState ¶
type OrderState int
OrderState represents the state of an order as it is processed by a dealer.
func (OrderState) String ¶
func (s OrderState) String() string
type OrderType ¶
type OrderType int
OrderType represents an order type
func (OrderType) MarshalText ¶ added in v0.0.20
MarshalText is used to output as a string for CSV rendering.
type Position ¶
type Position struct { ID DealID OpenedAt time.Time ClosedAt time.Time Asset market.Asset Side OrderSide Price decimal.Decimal Size decimal.Decimal LiquidationPrice decimal.Decimal }
Position represents a position in a market for a given asset.
func (*Position) State ¶
func (p *Position) State() PositionState
State returns the state of the position based on the position timestamps.
type PositionState ¶
type PositionState int
PositionState represents the state of a position as it is processed by a dealer.
func (PositionState) String ¶
func (s PositionState) String() string
type SimulatedDealer ¶
type SimulatedDealer interface { Dealer market.Receiver EquityHistory() EquitySeries SetInitialCapital(amount decimal.Decimal) }
SimulatedDealer is a Dealer that can be used for backtesting.
type StubDealer ¶
type StubDealer struct { }
StubDealer is a test double for a simulated dealer that does nothing.
func (*StubDealer) CancelOrders ¶
CancelOrders not implemented.
func (*StubDealer) EquityHistory ¶
func (d *StubDealer) EquityHistory() EquitySeries
EquityHistory not implemented.
func (*StubDealer) GetBalance ¶
func (d *StubDealer) GetBalance(ctx context.Context) (*AccountBalance, *web.Response, error)
GetBalance not implemented.
func (*StubDealer) ListPositions ¶
func (d *StubDealer) ListPositions(ctx context.Context, opts *web.ListOpts) ([]Position, *web.Response, error)
ListPositions not implemented.
func (*StubDealer) ListTrades ¶
func (d *StubDealer) ListTrades(ctx context.Context, opts *web.ListOpts) ([]Trade, *web.Response, error)
ListTrades not implemented.
func (*StubDealer) PlaceOrder ¶
PlaceOrder not implemented.
func (*StubDealer) ReceivePrice ¶
ReceivePrice not implemented.
func (*StubDealer) SetInitialCapital ¶
func (d *StubDealer) SetInitialCapital(amount decimal.Decimal)
SetInitialCapital not implemented.
type TimeSeries ¶
TimeSeries represents a time series of values.
func (TimeSeries[V]) SortKeys ¶
func (ts TimeSeries[V]) SortKeys() []Timestamp
SortKeys sorts the keys of the time series by time in ascending order.
func (TimeSeries[V]) SortValuesByTime ¶
func (ts TimeSeries[V]) SortValuesByTime() []V
SortValuesByTime sorts the values of the time series by time in ascending order.
type Trade ¶
type Trade struct { ID DealID `csv:"id"` CreatedAt time.Time `csv:"created_at"` Asset market.Asset `csv:",inline"` Side OrderSide `csv:"side"` Size decimal.Decimal `csv:"size"` Profit decimal.Decimal `csv:"profit"` HoldPeriod time.Duration `csv:"hold_period"` }
Trade is the result of opening and closing a position i.e. a roundtrip / roundturn.