ellipxobj

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2024 License: MIT Imports: 12 Imported by: 0

README

GoDoc

EllipX objects

These structures are shared among various parts of the system in order to ensure consistent structure, and can be used to communicate with APIs.

Pairs

When a pair is mentioned, it comes in a specific order, and typically written with a _ between the two elements as unlike standard pairs, there is no guarantee that elements of a given pair will be 3 characters long.

If the order is a buy order, it means exchanging the first element for the second. Sell order works the other way around.

Documentation

Index

Constants

View Source
const TimeIdDataLen = 16 // number of bytes in a timeid

Variables

View Source
var (
	ErrOrderIdMissing      = errors.New("order id is required")
	ErrBrokerIdMissing     = errors.New("broker id is required")
	ErrOrderTypeNotValid   = errors.New("order type is not valid")
	ErrOrderStatusNotValid = errors.New("order status is not valid")
	ErrOrderNeedsAmount    = errors.New("order amount or spend limit is required")
	ErrAmountParseFailed   = errors.New("failed to parse provided amount")
)

Functions

This section is empty.

Types

type Amount

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

Amount is a fixed point value

func NewAmount

func NewAmount(value int64, decimals int) *Amount

NewAmount returns a new Amount object set to the specific value and decimals

func NewAmountFromFloat

func NewAmountFromFloat(f *big.Float, decimals int) (*Amount, big.Accuracy)

NewAmountFromFloat return a new Amount initialized with the value f stored with the specified number of decimals

func NewAmountFromFloat64

func NewAmountFromFloat64(f float64, exp int) (*Amount, big.Accuracy)

NewAmountFromFloat64 return a new Amount initialized with the value f stored with the specified number of decimals

func NewAmountFromString

func NewAmountFromString(s string, decimals int) (*Amount, error)

NewAmountFromString return a new Amount initialized with the passed string value

func NewAmountRaw added in v0.0.26

func NewAmountRaw(v *big.Int, decimals int) *Amount

NewAmountRaw returns a new Amount initialized with the passed values as is

func (*Amount) Add added in v0.0.11

func (a *Amount) Add(x, y *Amount) *Amount

Add sets a=x+y and returns a

func (*Amount) Bytes added in v0.0.23

func (a *Amount) Bytes() []byte

func (Amount) Cmp added in v0.0.11

func (a Amount) Cmp(b *Amount) int

Cmp compares two amounts, assuming both have the same exponent

func (*Amount) Div added in v0.0.11

func (a *Amount) Div(x, y *Amount) *Amount

Div sets a=x/y and returns a

func (*Amount) Dup added in v0.0.11

func (a *Amount) Dup() *Amount

Dup returns a copy of the Amount object so that modifying one won't affect the other

func (Amount) Exp added in v0.0.19

func (a Amount) Exp() int

Exp returns the amount's exp value

func (Amount) Float

func (a Amount) Float() *big.Float

func (Amount) IsZero added in v0.0.11

func (a Amount) IsZero() bool

func (*Amount) MarshalBinary added in v0.0.23

func (a *Amount) MarshalBinary() ([]byte, error)

func (*Amount) MarshalJSON

func (a *Amount) MarshalJSON() ([]byte, error)

func (*Amount) Mul

func (a *Amount) Mul(x, y *Amount) (*Amount, big.Accuracy)

Mul sets a=x*y and returns a

func (Amount) Neg added in v0.0.35

func (a Amount) Neg() *Amount

Neg returns -a in a newly allocated Amount

func (Amount) Reciprocal

func (a Amount) Reciprocal() (*Amount, big.Accuracy)

Reciprocal returns 1/a in a newly allocated Amount

func (*Amount) Scan added in v0.0.28

func (a *Amount) Scan(v any) error

func (*Amount) SetExp added in v0.0.2

func (a *Amount) SetExp(e int) *Amount

SetExp sets the number of decimals (exponent) of the amount, truncating or adding zeroes as needed

func (Amount) Sign added in v0.0.11

func (a Amount) Sign() int

func (Amount) String

func (a Amount) String() string

func (*Amount) Sub added in v0.0.11

func (a *Amount) Sub(x, y *Amount) *Amount

Sub sets a=x-y and returns a

func (*Amount) UnmarshalBinary added in v0.0.23

func (a *Amount) UnmarshalBinary(data []byte) error

func (*Amount) UnmarshalJSON added in v0.0.2

func (a *Amount) UnmarshalJSON(b []byte) error

func (Amount) Value

func (a Amount) Value() *big.Int

Value returns the amount's value *big.Int

type Order

type Order struct {
	OrderId     string      `json:"id"`                    // order ID assigned by the broker
	BrokerId    string      `json:"iss"`                   // id of the broker
	RequestTime uint64      `json:"iat"`                   // unix timestamp when the order was placed
	Unique      *TimeId     `json:"uniq,omitempty"`        // unique ID allocated on order igress
	Target      *TimeId     `json:"target,omitempty"`      // target order to be updated, instead of creating a new order
	Version     uint64      `json:"ver"`                   // value starting at zero incremented each time an order is modified
	Pair        PairName    `json:"pair"`                  // the name of the pair the order is on
	Type        OrderType   `json:"type"`                  // type of order (buy or sell)
	Status      OrderStatus `json:"status"`                // new orders will always be in "pending" state
	Flags       OrderFlags  `json:"flags,omitempty"`       // order flags
	Amount      *Amount     `json:"amount,omitempty"`      // optional amount, if nil SpendLimit must be set
	Price       *Amount     `json:"price,omitempty"`       // price, if nil this will be a market order
	SpendLimit  *Amount     `json:"spend_limit,omitempty"` // optional spending limit, if nil Amount must be set
	StopPrice   *Amount     `json:"stop_price,omitempty"`  // ignored if flag Stop is not set
}

func NewOrder

func NewOrder(pair PairName, typ OrderType) *Order

func (*Order) Deduct added in v0.0.11

func (o *Order) Deduct(t *Trade) bool

Deduct deducts the trade's value from the order, and return true if this order is fully consumed. Note that even if an order isn't fully consumed, it might be too low to be executed.

func (*Order) Dup added in v0.0.14

func (o *Order) Dup() *Order

Dup returns a copy of Order including objects such as Amount duplicated

func (*Order) IsValid

func (o *Order) IsValid() error

func (*Order) Matches added in v0.0.11

func (a *Order) Matches(b *Order) *Trade

Matches returns a Trade if a can consume b Because b is assumed to be an open order, it must have a Price

func (*Order) Meta

func (o *Order) Meta() *OrderMeta

func (*Order) NominalAmount added in v0.0.12

func (a *Order) NominalAmount(amountExp int) *Amount

NominalAmount returns the order's maximum amount if one can be computed, or nil

func (*Order) Reverse

func (o *Order) Reverse() *Order

Reverse reverses an order's pair, updating Amount and Price accordingly

func (*Order) SetId

func (o *Order) SetId(orderId, brokerId string) *Order

func (*Order) String

func (o *Order) String() string

func (*Order) TradeAmount added in v0.0.11

func (a *Order) TradeAmount(b *Order) *Amount

TradeAmount returns the order's trade amount in case it matches against b

type OrderFlags

type OrderFlags int
const (
	FlagImmediateOrCancel OrderFlags = 1 << iota // do not create an open order after execution
	FlagFillOrKill                               // if order can't be fully executed, cancel
	FlagStop
)

func (OrderFlags) Has added in v0.1.1

func (f OrderFlags) Has(chk OrderFlags) bool

Has returns true if the flags contain all the check flags

func (OrderFlags) MarshalJSON

func (f OrderFlags) MarshalJSON() ([]byte, error)

func (*OrderFlags) UnmarshalJSON

func (f *OrderFlags) UnmarshalJSON(j []byte) error

type OrderMeta

type OrderMeta struct {
	OrderId  string  `json:"id"`
	BrokerId string  `json:"iss"`
	Unique   *TimeId `json:"uniq,omitempty"`
}

type OrderStatus

type OrderStatus int
const (
	OrderInvalid OrderStatus = -1
	OrderPending OrderStatus = iota
	OrderRunning
	OrderOpen
	OrderStop // pending for a trigger
	OrderDone
	OrderCancel // cancelled or overwritten order
)

func OrderStatusByString

func OrderStatusByString(s string) OrderStatus

func (OrderStatus) IsValid

func (s OrderStatus) IsValid() bool

func (OrderStatus) MarshalJSON added in v0.0.4

func (s OrderStatus) MarshalJSON() ([]byte, error)

func (OrderStatus) String

func (s OrderStatus) String() string

func (*OrderStatus) UnmarshalJSON added in v0.0.4

func (s *OrderStatus) UnmarshalJSON(b []byte) error

type OrderType

type OrderType int
const (
	TypeInvalid OrderType = -1
	TypeBid     OrderType = iota // buy
	TypeAsk                      // sell
)

func OrderTypeByString

func OrderTypeByString(v string) OrderType

func (OrderType) IsValid

func (t OrderType) IsValid() bool

func (OrderType) MarshalJSON added in v0.0.3

func (t OrderType) MarshalJSON() ([]byte, error)

func (OrderType) Reverse

func (t OrderType) Reverse() OrderType

func (OrderType) String

func (t OrderType) String() string

func (*OrderType) UnmarshalJSON added in v0.0.3

func (t *OrderType) UnmarshalJSON(b []byte) error

type PairName

type PairName [2]string

func Pair

func Pair(a, b string) PairName

func ParsePairName added in v0.0.13

func ParsePairName(s string) (PairName, error)

func (PairName) Hash added in v0.0.9

func (p PairName) Hash() []byte

Hash returns a 32 bytes hash (sha256) representing the pair name with a nil character between the two names

func (PairName) String added in v0.0.2

func (p PairName) String() string

func (*PairName) UnmarshalJSON added in v0.0.8

func (p *PairName) UnmarshalJSON(v []byte) error

type TimeId

type TimeId struct {
	Type  string `json:"type"` // order | trade
	Unix  uint64 `json:"unix"` // unix timestamp in seconds
	Nano  uint32 `json:"nano"` // [0, 999999999]
	Index uint32 `json:"idx"`  // index if multiple ids are generated with the same unix/nano values
}

func NewTimeId added in v0.0.2

func NewTimeId() *TimeId

NewTimeId returns a new TimeId for now

func NewUniqueTimeId added in v0.0.2

func NewUniqueTimeId() *TimeId

NewUniqueTimeId returns a unique (in the local process) TimeId

func (TimeId) Bytes

func (t TimeId) Bytes(buf []byte) []byte

Bytes returns a 128bits (TimeIdDataLen bytes) bigendian sortable version of this TimeId. If buf is not nil, the data is appended to it.

func (TimeId) Cmp added in v0.0.10

func (a TimeId) Cmp(b TimeId) int

Cmp returns an integer comparing two TimeId time point. The result will be 0 if a == b, -1 if a < b, and +1 if a > b.

func (TimeId) MarshalBinary added in v0.0.25

func (t TimeId) MarshalBinary() ([]byte, error)

func (TimeId) MarshalJSON added in v0.0.6

func (t TimeId) MarshalJSON() ([]byte, error)

func (TimeId) String added in v0.0.2

func (t TimeId) String() string

String returns a string matching this TimeId

func (TimeId) Time

func (t TimeId) Time() time.Time

Time returns the TimeId timestamp, which may be when the ID was generated

func (*TimeId) UnmarshalBinary added in v0.0.25

func (t *TimeId) UnmarshalBinary(v []byte) error

UnmarshalBinary will convert a binary value back to TimeId. Type will not be kept

func (*TimeId) UnmarshalJSON added in v0.0.6

func (t *TimeId) UnmarshalJSON(b []byte) error

type TimeIdUnique added in v0.0.2

type TimeIdUnique struct {
	Last TimeId
}

func (*TimeIdUnique) New added in v0.0.20

func (u *TimeIdUnique) New() *TimeId

New returns a new TimeId that is unique within the scope of this TimeIdUnique

func (*TimeIdUnique) Unique added in v0.0.2

func (u *TimeIdUnique) Unique(t *TimeId)

Unique ensures the provided TimeId is always higher than the latest one and will update it if not the case

type Trade

type Trade struct {
	Id     *TimeId    `json:"id"` // trade id
	Pair   PairName   `json:"pair"`
	Bid    *OrderMeta `json:"bid"`
	Ask    *OrderMeta `json:"ask"`
	Type   OrderType  `json:"type"` // taker's order type
	Amount *Amount    `json:"amount"`
	Price  *Amount    `json:"price"`
}

Trade represents a trade that happened, where two orders matched

func (*Trade) MarshalJSON added in v0.0.34

func (t *Trade) MarshalJSON() ([]byte, error)

func (*Trade) Spent added in v0.0.11

func (t *Trade) Spent() *Amount

Spent returns the amount spent in that trade

Jump to

Keyboard shortcuts

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