exchange

package
v0.5.10 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2023 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	OrderTypeLimit OrderType = iota
	OrderTypeMarket
	OrderTypeStopLimit
	OrderTypeStopMarket

	//OrderStatusUnknown means order info need check with api
	OrderStatusUnknown OrderStatus = iota
	OrderStatusOpen
	OrderStatusDone
	OrderStatusCancel
	OrderStatusFailed

	//TimeInForceGTC good_till_cancel
	TimeInForceGTC = "gtc"
	//TimeInForceFOK fill_or_kill
	TimeInForceFOK = "fok"
	//TimeInForceIOC immediate_or_cancel
	TimeInForceIOC = "ioc"
)
View Source
const (
	PositionSideLong = iota
	PositionSideShort

	PositionModeFixed
	PositionModeCross
)

Variables

View Source
var (
	TimeNoExpire = time.Time{}
)

Functions

func CurrencyFormat added in v0.5.2

func CurrencyFormat(input string) string

CurrencyFormat common method which used to transfer currency to uniq format

func NewBadArg

func NewBadArg(msg string, arg interface{}) error

func NewBadExResp

func NewBadExResp(err error) error

func Round added in v0.3.0

Types

type Balance added in v0.5.2

type Balance struct {
	Currency string
	Equitity decimal.Decimal
	Total    decimal.Decimal
	Free     decimal.Decimal
	Frozen   decimal.Decimal
}

type Balances added in v0.5.2

type Balances struct {
	Balances map[string]*Balance
	Raw      interface{}
}

func NewBalances added in v0.5.2

func NewBalances() *Balances

func (*Balances) Add added in v0.5.2

func (b *Balances) Add(balance *Balance)

Add specific balance

func (*Balances) Get added in v0.5.2

func (b *Balances) Get(currency string) (*Balance, error)

Get return balance for specific token(upper case)

type BaseFutureSymbol

type BaseFutureSymbol struct {
	RawMixin
	BaseSymbolProperty
	// contains filtered or unexported fields
}

BaseFutureSymbol define common property of future symbol

func NewBaseFutureSymbol

func NewBaseFutureSymbol(index string, st time.Time, typ FutureType) *BaseFutureSymbol

func NewBaseFuturesSymbolWithCfg added in v0.3.0

func NewBaseFuturesSymbolWithCfg(index string, st time.Time, typ FutureType, cfg SymbolConfig, raw interface{}) *BaseFutureSymbol

func NewBaseFuturesSymbolWithCfgCV added in v0.5.1

func NewBaseFuturesSymbolWithCfgCV(index string, st time.Time, typ FutureType, cfg SymbolConfig, cv decimal.Decimal, raw interface{}) *BaseFutureSymbol

func (*BaseFutureSymbol) ContractVal added in v0.5.1

func (bfs *BaseFutureSymbol) ContractVal() decimal.Decimal

func (*BaseFutureSymbol) Index

func (bfs *BaseFutureSymbol) Index() string

func (*BaseFutureSymbol) SettleTime

func (bfs *BaseFutureSymbol) SettleTime() time.Time

func (*BaseFutureSymbol) Type added in v0.3.0

func (bfs *BaseFutureSymbol) Type() FutureType

type BaseMarginSymbol added in v0.3.0

type BaseMarginSymbol struct {
	*BaseSpotSymbol
	// contains filtered or unexported fields
}

func NewBaseMarginSymbol added in v0.3.0

func NewBaseMarginSymbol(base, quote string, cfg SymbolConfig, lever decimal.Decimal, raw interface{}) *BaseMarginSymbol

func (*BaseMarginSymbol) Lever added in v0.3.0

func (ms *BaseMarginSymbol) Lever() decimal.Decimal

type BaseMarket

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

func (*BaseMarket) Expire

func (m *BaseMarket) Expire() bool

func (*BaseMarket) Symbol

func (m *BaseMarket) Symbol() Symbol

type BaseOptionSymbol

type BaseOptionSymbol struct {
	RawMixin
	BaseSymbolProperty
	// contains filtered or unexported fields
}

BaseOptionSymbol define common property of option symbol

func NewBaseOptionSymbol

func NewBaseOptionSymbol(index string, st time.Time, strike decimal.Decimal, typ OptionType, prop SymbolConfig, raw interface{}) *BaseOptionSymbol

func (*BaseOptionSymbol) Index

func (bos *BaseOptionSymbol) Index() string

func (*BaseOptionSymbol) SettleTime

func (bos *BaseOptionSymbol) SettleTime() time.Time

func (*BaseOptionSymbol) Strike

func (bos *BaseOptionSymbol) Strike() decimal.Decimal

func (*BaseOptionSymbol) Type

func (bos *BaseOptionSymbol) Type() OptionType

type BaseSpotSymbol

type BaseSpotSymbol struct {
	RawMixin
	BaseSymbolProperty
	// contains filtered or unexported fields
}

BaseSpotSymbol define common property of spot symbol

func NewBaseSpotSymbol

func NewBaseSpotSymbol(base, quote string, cfg SymbolConfig, raw interface{}) *BaseSpotSymbol

func (*BaseSpotSymbol) Base

func (bss *BaseSpotSymbol) Base() string

func (*BaseSpotSymbol) Quote

func (bss *BaseSpotSymbol) Quote() string

type BaseSwapSymbol

type BaseSwapSymbol struct {
	RawMixin
	BaseSymbolProperty
	// contains filtered or unexported fields
}

func NewBaseSwapSymbol

func NewBaseSwapSymbol(index string) *BaseSwapSymbol

func NewBaseSwapSymbolWithCfg added in v0.3.0

func NewBaseSwapSymbolWithCfg(index string, cf decimal.Decimal, cfg SymbolConfig, raw interface{}) *BaseSwapSymbol

func (*BaseSwapSymbol) ContractVal added in v0.3.0

func (bsv *BaseSwapSymbol) ContractVal() decimal.Decimal

func (*BaseSwapSymbol) Index

func (bsw *BaseSwapSymbol) Index() string

type BaseSymbolProperty added in v0.3.0

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

BaseSymbolProperty define common property of all kind symbol

func (*BaseSymbolProperty) AmountExponent added in v0.5.4

func (p *BaseSymbolProperty) AmountExponent() int32

func (*BaseSymbolProperty) AmountMax added in v0.3.0

func (p *BaseSymbolProperty) AmountMax() decimal.Decimal

AmountMax minum order amount zero means no limit

func (*BaseSymbolProperty) AmountMin added in v0.3.0

func (p *BaseSymbolProperty) AmountMin() decimal.Decimal

AmountMin minium order amount

func (*BaseSymbolProperty) AmountPrecision added in v0.3.0

func (p *BaseSymbolProperty) AmountPrecision() decimal.Decimal

AmountPrecision return amount precision value

func (*BaseSymbolProperty) PriceExponent added in v0.5.4

func (p *BaseSymbolProperty) PriceExponent() int32

func (*BaseSymbolProperty) PricePrecision added in v0.3.0

func (p *BaseSymbolProperty) PricePrecision() decimal.Decimal

PricePrecision return price precision value

func (*BaseSymbolProperty) RoundAmount added in v0.5.4

func (p *BaseSymbolProperty) RoundAmount(amt decimal.Decimal) decimal.Decimal

func (*BaseSymbolProperty) RoundPrice added in v0.5.4

func (p *BaseSymbolProperty) RoundPrice(price decimal.Decimal) decimal.Decimal

func (*BaseSymbolProperty) ValueMin added in v0.3.0

func (p *BaseSymbolProperty) ValueMin() decimal.Decimal

ValueMin return minium amount * price value zero means no limit

func (*BaseSymbolProperty) ValuePrecision added in v0.5.0

func (p *BaseSymbolProperty) ValuePrecision() decimal.Decimal

type Channel

type Channel interface {
	String() string
}

Channel a subscribe channel

type Client

type Client struct {
	NewConn ConnCB
	Conn    rpc.Conn
	Addr    string
	Key     string
	Secret  string
	Timeout time.Duration
	//subscribe struct will be updated via rpc Conn notify message
	Sub   map[string]interface{}
	SubMu sync.Mutex
}

func NewClient

func NewClient(cb ConnCB, addr, key, secret string, timeout time.Duration) *Client

NewClient got a new client instance

func (*Client) Close

func (c *Client) Close() error

Close the running loop

func (*Client) Done

func (c *Client) Done() <-chan struct{}

Done get notify if running loop closed

func (*Client) Error

func (c *Client) Error() error

Error return error if running loop closed due to error

func (*Client) Handle

func (c *Client) Handle(_ context.Context, notify *rpc.Notify)

Handler handle notify message

func (*Client) Index

func (c *Client) Index(sym Symbol) (*Index, error)

func (*Client) OrderBook

func (c *Client) OrderBook(symbol Symbol) (*OrderBook, error)

func (*Client) Run

func (c *Client) Run(ctx context.Context) error

Run create wsconn and start conn running loop

type CodeC

type CodeC struct {
}

CodeC define base encode method

func NewCodeC

func NewCodeC() *CodeC

func (*CodeC) Encode

func (cc *CodeC) Encode(req rpc.Request) ([]byte, error)

Encode encode req with json.Marshal

type ConnCB

type ConnCB func(addr string) (rpc.Conn, error)

type ErrBadArg

type ErrBadArg struct {
	Arg interface{}
	Msg string
}

ErrBadArg means func argument is incorrect

func (*ErrBadArg) Error

func (eba *ErrBadArg) Error() string

func (*ErrBadArg) Is

func (eba *ErrBadArg) Is(target error) bool

type ErrBadExResp

type ErrBadExResp struct {
	Err error
}

ErrBadResp means exchange response message error

func (*ErrBadExResp) Error

func (ebe *ErrBadExResp) Error() string

func (*ErrBadExResp) Is

func (ebe *ErrBadExResp) Is(target error) bool

type Finance added in v0.3.0

type Finance struct {
	ID       string
	Time     time.Time
	Amount   decimal.Decimal
	Currency string
	Type     FinanceType
	Symbol   Symbol
	Raw      interface{}
}

type FinanceProp added in v0.3.0

type FinanceProp struct {
	MaxDuration time.Duration
	SuportID    bool
	SupportTime bool
}

type FinanceReqParam added in v0.3.0

type FinanceReqParam struct {
	TradeReqParam
	Type FinanceType
}

type FinanceType added in v0.3.0

type FinanceType int
const (
	FinanceTypeOther FinanceType = iota
	FinanceTypeFunding
	FinanceTypeInterest
)

type FundingRate added in v0.5.4

type FundingRate struct {
	Symbol          Symbol
	FundingRate     decimal.Decimal
	NextFundingTime time.Time
	Time            time.Time
	Raw             interface{}
}

type FutureType added in v0.3.0

type FutureType int
const (

	//FutureTypeCW current week settle future
	FutureTypeCW FutureType = iota
	//FutureTypeNW next week settle future
	FutureTypeNW
	//FutureTypeCQ current quarter settle future
	FutureTypeCQ
	//FutureTypeNQ next quarter settle future
	FutureTypeNQ
	//FutureTypeNNQ next next quart settle future (deribit only)
	FutureTypeNNQ
)

type FuturesSymbol

type FuturesSymbol interface {
	Symbol
	Index() string
	SettleTime() time.Time
	Type() FutureType
	ContractVal() decimal.Decimal
}

type Index

type Index struct {
	Price   decimal.Decimal
	Created time.Time
	Symbol  Symbol
}

Index price

type IndexNotify

type IndexNotify Index

func (*IndexNotify) Key

func (i *IndexNotify) Key() string

func (*IndexNotify) Snapshot

func (i *IndexNotify) Snapshot() *Index

type IntID

type IntID struct {
	ID int64
}

func NewIntID

func NewIntID(id int64) IntID

func (IntID) Int added in v0.5.5

func (sid IntID) Int() int64

func (IntID) String

func (sid IntID) String() string

type Kline added in v0.5.1

type Kline struct {
	Symbol Symbol
	Open   float64
	Close  float64
	High   float64
	Low    float64
	Volume float64
	Time   time.Time
	Raw    interface{}
}

type KlineReq added in v0.5.1

type KlineReq struct {
	Symbol     Symbol
	StartTime  time.Time
	EndTime    time.Time
	Limit      int
	Resolution KlineResolution
}

func NewKlineReq added in v0.5.1

func NewKlineReq(symbol Symbol, resolution KlineResolution) *KlineReq

func (*KlineReq) SetEndTime added in v0.5.1

func (kr *KlineReq) SetEndTime(et time.Time) *KlineReq

func (*KlineReq) SetLimit added in v0.5.1

func (kr *KlineReq) SetLimit(l int) *KlineReq

func (*KlineReq) SetStartTime added in v0.5.1

func (kr *KlineReq) SetStartTime(st time.Time) *KlineReq

type KlineResolution added in v0.5.1

type KlineResolution int
const (
	KlineResolution1m KlineResolution = iota
	KlineResolution5m
	KlineResolution15m
	KlineResolution30m
	KlineResolution1h
	KlineResolution4h
	KlineResolution1D
	KlineResolution1W
)

func (KlineResolution) Secs added in v0.5.1

func (kr KlineResolution) Secs() int

func (KlineResolution) String added in v0.5.1

func (kr KlineResolution) String() string

type MarginSymbol added in v0.3.0

type MarginSymbol interface {
	Symbol
	Lever() decimal.Decimal
	Base() string
	Quote() string
}

type OptionSymbol

type OptionSymbol interface {
	Symbol
	Strike() decimal.Decimal
	Index() string
	SettleTime() time.Time
	Type() OptionType
}

type OptionType

type OptionType int
const (
	OptionTypeCall OptionType = iota
	OptionTypePut
)

func (OptionType) String

func (ot OptionType) String() string

type Order

type Order struct {
	ID          OrderID
	ClientID    OrderID
	Symbol      Symbol
	Amount      decimal.Decimal
	Filled      decimal.Decimal
	Price       decimal.Decimal
	AvgPrice    decimal.Decimal
	Fee         decimal.Decimal
	FeeCurrency string
	Created     time.Time
	Updated     time.Time
	Side        OrderSide
	Status      OrderStatus
	Type        OrderType
	Raw         interface{} `json:"-"`
}

func (*Order) Equal

func (o *Order) Equal(o2 *Order) bool

Equal check whether o equal o2 mainly used for test

type OrderBook

type OrderBook struct {
	Symbol  Symbol
	Bids    []OrderElem
	Asks    []OrderElem
	Created time.Time
	Raw     interface{}
}

OrderBook get via OrderBookDS.Snapshot

type OrderBookDS

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

OrderBookDS is the ds which hold orderbook info

func NewOrderBookDS

func NewOrderBookDS(notify *OrderBookNotify) *OrderBookDS

func (*OrderBookDS) Snapshot

func (ds *OrderBookDS) Snapshot() *OrderBook

func (*OrderBookDS) Update

func (ds *OrderBookDS) Update(notify *OrderBookNotify)

type OrderBookNotify

type OrderBookNotify struct {
	Symbol Symbol
	Bids   []OrderElem
	Asks   []OrderElem
	Raw    interface{}
}

OrderBookNotify change of current orderbook OrderElem.Amount == 0 means delete

func (*OrderBookNotify) Key

func (notify *OrderBookNotify) Key() string

type OrderElem

type OrderElem struct {
	Price  float64
	Amount float64
}

type OrderID

type OrderID interface {
	String() string
}

type OrderReqOption

type OrderReqOption interface {
}

OrderReqOption specific option to create order each exchange support different options.

func NewPostOnlyOption

func NewPostOnlyOption(postOnly bool) OrderReqOption

func NewTimeInForceOption

func NewTimeInForceOption(flag TimeInForceFlag) OrderReqOption

type OrderRequest

type OrderRequest struct {
	Symbol   Symbol
	ClientID OrderID
	Side     OrderSide
	Type     OrderType
	Price    decimal.Decimal
	Amount   decimal.Decimal
}

OrderRequest carry field which used to create order

func NewOrderRequest

func NewOrderRequest(sym Symbol, cid OrderID, side OrderSide, typ OrderType,
	price float64, amount float64) *OrderRequest

NewOrderRequest create a order request with given param, the price and amount field will be formatted according to symbol precision config

type OrderSide

type OrderSide int
const (
	OrderSideBuy OrderSide = iota
	OrderSideSell
	OrderSideCloseLong
	OrderSideCloseShort
)

func (OrderSide) String

func (s OrderSide) String() string

type OrderStatus

type OrderStatus int

type OrderType

type OrderType int

func (OrderType) String

func (t OrderType) String() string

type Position

type Position struct {
	Symbol           Symbol
	Mode             PositionMode
	Side             PositionSide
	LiquidationPrice decimal.Decimal
	AvgOpenPrice     decimal.Decimal
	CreateTime       time.Time
	Margin           decimal.Decimal
	MarginMaintRatio decimal.Decimal
	Position         decimal.Decimal
	AvailPosition    decimal.Decimal
	RealizedPNL      decimal.Decimal
	UNRealizedPNL    decimal.Decimal
	Leverage         decimal.Decimal
	Raw              interface{}
}

Position info

type PositionMode

type PositionMode int

func (PositionMode) String

func (pm PositionMode) String() string

type PositionSide

type PositionSide int

func (PositionSide) String

func (ps PositionSide) String() string

type PostOnlyOption

type PostOnlyOption struct {
	PostOnly bool
}

PostOnlyOption wether the order ensure maker

type Property added in v0.3.0

type Property struct {
	Trades  *TradesProp
	Finance *FinanceProp
}

type PublicTrade added in v0.5.1

type PublicTrade struct {
	Symbol Symbol
	Price  decimal.Decimal
	Amount decimal.Decimal
	Side   OrderSide
	ID     string
	Time   time.Time
	Raw    interface{}
}

type RawMixin added in v0.3.0

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

func (*RawMixin) Raw added in v0.3.0

func (r *RawMixin) Raw() interface{}

type RestReq added in v0.5.4

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

RestReq base requeuest struct used by other

func NewRestReq added in v0.5.4

func NewRestReq() *RestReq

func (*RestReq) AddFields added in v0.5.4

func (rr *RestReq) AddFields(key string, val interface{}) *RestReq

func (*RestReq) MarshalJSON added in v0.5.4

func (rr *RestReq) MarshalJSON() ([]byte, error)

func (*RestReq) Values added in v0.5.4

func (rr *RestReq) Values() (url.Values, error)

type SpotSymbol

type SpotSymbol interface {
	Symbol
	Base() string
	Quote() string
}

type StrID

type StrID string

func NewStrID

func NewStrID(id string) StrID

func (StrID) String

func (sid StrID) String() string

type SubType

type SubType int
const (
	SubTypeOrderBook SubType = iota
	SubTypeIndex
	SubTypeTrade
	SubTypePrivateOrder
	SubTypePrivateTrade
)

type SwapSymbol

type SwapSymbol interface {
	Symbol
	Index() string
	ContractVal() decimal.Decimal
}

type Symbol

type Symbol interface {
	Raw() interface{}
	AmountPrecision() decimal.Decimal
	PricePrecision() decimal.Decimal
	ValuePrecision() decimal.Decimal
	AmountExponent() int32
	PriceExponent() int32
	AmountMax() decimal.Decimal
	AmountMin() decimal.Decimal
	ValueMin() decimal.Decimal
	RoundAmount(decimal.Decimal) decimal.Decimal
	RoundPrice(decimal.Decimal) decimal.Decimal
	String() string
}

Symbol is used to unit different exchange markets symbol serialize

type SymbolConfig added in v0.3.0

type SymbolConfig struct {
	PricePrecision  decimal.Decimal //symbol price precision in decimal
	AmountPrecision decimal.Decimal //symbol amount precision in decimal
	ValuePrecision  decimal.Decimal
	AmountMin       decimal.Decimal
	AmountMax       decimal.Decimal
	ValueMin        decimal.Decimal
}

SymbolConfig used to specific symbol property

func (*SymbolConfig) Property added in v0.3.0

func (p *SymbolConfig) Property() BaseSymbolProperty

type Ticker added in v0.3.0

type Ticker struct {
	Symbol      Symbol
	BestBid     decimal.Decimal
	BestBidSize decimal.Decimal
	BestAsk     decimal.Decimal
	BestAskSize decimal.Decimal
	MarkPrice   decimal.Decimal
	Time        time.Time
	LastPrice   decimal.Decimal
	Raw         interface{}
}

type TimeInForceFlag

type TimeInForceFlag string

TimeInForceFlag specific TimeInForceOption value

type TimeInForceOption

type TimeInForceOption struct {
	Flag TimeInForceFlag
}

TimeInForceOption specific how long the order remains in effect

type Trade added in v0.3.0

type Trade struct {
	ID          string
	OrderID     string
	Symbol      Symbol
	Price       decimal.Decimal
	Amount      decimal.Decimal
	Fee         decimal.Decimal
	FeeCurrency string
	Time        time.Time
	Side        OrderSide
	IsMaker     bool
	Raw         interface{}
}

Trade private trade

type TradeDS added in v0.5.4

type TradeDS struct {
	Price       string
	Size        string
	Side        string
	Liquidation bool
	Time        string
	// contains filtered or unexported fields
}

func NewTradeDS added in v0.5.4

func NewTradeDS(notify *TradeNotify) *TradeDS

func (*TradeDS) Snapshot added in v0.5.4

func (ts *TradeDS) Snapshot() *Trade

func (*TradeDS) Update added in v0.5.4

func (ts *TradeDS) Update(notify *TradeNotify)

type TradeFee

type TradeFee struct {
	Symbol Symbol
	Maker  decimal.Decimal
	Taker  decimal.Decimal
	Raw    interface{}
}

TradeFee for the symbol

type TradeNotify added in v0.5.4

type TradeNotify struct {
	Symbol      Symbol
	Price       string `json:"price"`
	Size        string `json:"size"`
	Side        string `json:"side"`
	Liquidation bool   `json:"liquidation"`
	Time        string `json:"time"`
}

type TradeReqParam added in v0.3.0

type TradeReqParam struct {
	Symbol    Symbol
	StartTime time.Time
	EndTime   time.Time
	StartID   string
	EndID     string
	Limit     int
}

func NewTradeReqParam added in v0.5.1

func NewTradeReqParam() *TradeReqParam

func (*TradeReqParam) SetEndID added in v0.5.1

func (trp *TradeReqParam) SetEndID(eid string) *TradeReqParam

func (*TradeReqParam) SetEndTime added in v0.5.1

func (trp *TradeReqParam) SetEndTime(et time.Time) *TradeReqParam

func (*TradeReqParam) SetLimit added in v0.5.1

func (trp *TradeReqParam) SetLimit(l int) *TradeReqParam

func (*TradeReqParam) SetStartID added in v0.5.1

func (trp *TradeReqParam) SetStartID(sid string) *TradeReqParam

func (*TradeReqParam) SetStartTime added in v0.5.1

func (trp *TradeReqParam) SetStartTime(st time.Time) *TradeReqParam

func (*TradeReqParam) SetSymbol added in v0.5.1

func (trp *TradeReqParam) SetSymbol(s Symbol) *TradeReqParam

type TradesProp added in v0.3.0

type TradesProp struct {
	MaxDuration time.Duration
	SuportID    bool
	SupportTime bool
}

TradesProp specific property which used to build Trades request

type WSClient

type WSClient struct {
	rpc.Conn
	// contains filtered or unexported fields
}

func NewWSClient

func NewWSClient(addr string, codec rpc.Codec, handler rpc.Handler) *WSClient

func (*WSClient) Close added in v0.5.0

func (ws *WSClient) Close() error

func (*WSClient) Run

func (wc *WSClient) Run(ctx context.Context) error

type WSNotify

type WSNotify struct {
	Exchange string
	Chan     string
	Data     interface{}
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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