exchange

package
v0.0.0-...-ac30242 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2020 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	BUY = 1 + iota
	SELL
)
View Source
const (
	LIMIT = 1 + iota
	MARKET
	FOK
	IOC
	POST_ONLY
)
View Source
const (
	SUBMITTED = iota
	PARTIAL_FILLED
	PARTIAL_CANCELED
	FILLED
	CANCELED
	PENDING_CANCEL
)
View Source
const (
	KLINE_PERIOD_1MIN = 1 + iota
	KLINE_PERIOD_3MIN
	KLINE_PERIOD_5MIN
	KLINE_PERIOD_15MIN
	KLINE_PERIOD_30MIN
	KLINE_PERIOD_60MIN
	KLINE_PERIOD_1H
	KLINE_PERIOD_2H
	KLINE_PERIOD_4H
	KLINE_PERIOD_6H
	KLINE_PERIOD_8H
	KLINE_PERIOD_12H
	KLINE_PERIOD_1DAY
	KLINE_PERIOD_3DAY
	KLINE_PERIOD_1WEEK
	KLINE_PERIOD_1MONTH
	KLINE_PERIOD_1YEAR
)
View Source
const (
	FCOIN     = "fcoin.com"
	FCOIN_PRO = "fcoin.pro"
)

Variables

View Source
var (
	API_ERR                     = ApiError{ErrCode: "EX_ERR_0000", ErrMsg: "unknown error"}
	HTTP_ERR_CODE               = ApiError{ErrCode: "HTTP_ERR_0001", ErrMsg: "http request error"}
	EX_ERR_API_LIMIT            = ApiError{ErrCode: "EX_ERR_1000", ErrMsg: "api limited"}
	EX_ERR_SIGN                 = ApiError{ErrCode: "EX_ERR_0001", ErrMsg: "signature error"}
	EX_ERR_NOT_FIND_SECRETKEY   = ApiError{ErrCode: "EX_ERR_0002", ErrMsg: "not find secretkey"}
	EX_ERR_NOT_FIND_APIKEY      = ApiError{ErrCode: "EX_ERR_0003", ErrMsg: "not find apikey"}
	EX_ERR_INSUFFICIENT_BALANCE = ApiError{ErrCode: "EX_ERR_0004", ErrMsg: "Insufficient Balance"}
	EX_ERR_PLACE_ORDER_FAIL     = ApiError{ErrCode: "EX_ERR_0005", ErrMsg: "place order failure"}
	EX_ERR_CANCEL_ORDER_FAIL    = ApiError{ErrCode: "EX_ERR_0006", ErrMsg: "cancel order failure"}
	EX_ERR_INVALID_SYMBOL       = ApiError{ErrCode: "EX_ERR_0007", ErrMsg: "invalid symbol"}
	EX_ERR_NOT_FIND_ORDER       = ApiError{ErrCode: "EX_ERR_0008", ErrMsg: "not find order"}
	EX_ERR_SYMBOL_ERR           = ApiError{ErrCode: "EX_ERR_0009", ErrMsg: "symbol error"}
)
View Source
var (
	UNKNOWN = Currency{"UNKNOWN", ""}

	USDT = Currency{"USDT", ""}
	PAX  = Currency{"PAX", ""}
	TUSD = Currency{"TUSD", ""}
	USDC = Currency{"USDC", ""}
	GUSD = Currency{"GUSD", ""}
	BTC  = Currency{"BTC", ""}
	BCH  = Currency{"BCH", ""}
	BSV  = Currency{"BSV", ""}
	ETH  = Currency{"ETH", ""}
	ETC  = Currency{"ETC", ""}
	LTC  = Currency{"LTC", ""}
	EOS  = Currency{"EOS", ""}
	XRP  = Currency{"XRP", ""}
	FT   = Currency{"FT", "FCoin Token"}
	FMEX = Currency{"FMEX", "FCoin Contract Token"}

	PAX_USDT  = Symbol{PAX, USDT}
	TUSD_USDT = Symbol{TUSD, USDT}
	USDC_USDT = Symbol{USDC, USDT}
	GUSD_USDT = Symbol{GUSD, USDT}
	BTC_USDT  = Symbol{BTC, USDT}
	BCH_USDT  = Symbol{BCH, USDT}
	BSV_USDT  = Symbol{BSV, USDT}
	ETH_USDT  = Symbol{ETH, USDT}
	ETC_USDT  = Symbol{ETC, USDT}
	LTC_USDT  = Symbol{LTC, USDT}
	EOS_USDT  = Symbol{EOS, USDT}
	XRP_USDT  = Symbol{XRP, USDT}
	FT_USDT   = Symbol{FT, USDT}
	FMEX_USDT = Symbol{FMEX, USDT}

	UNKNOWN_SYMBOL = Symbol{UNKNOWN, UNKNOWN}
)

Functions

func CancelAllUnfinishedOrders

func CancelAllUnfinishedOrders(api API, symbol Symbol) int

*

  • call all unfinished orders

func FlateUnCompress

func FlateUnCompress(data []byte) ([]byte, error)

func FloatToString

func FloatToString(v float64, n int) string

func FloatToStringForEx

func FloatToStringForEx(v float64) string

func FmtBalance

func FmtBalance(balance float64, usdt float64, usdtFrozen float64, currency float64, currencyFrozen float64, ft float64, ftFrozen float64) string

func FmtBalanceExt

func FmtBalanceExt(balance float64, usdt float64, usdtFrozen float64, fmex float64, fmexFrozen float64, ft float64, ftFrozen float64) string

func FmtOrder

func FmtOrder(symbol string, price float64, amount float64, state string, filledAmount float64) string

func FmtPaxMemoryStates

func FmtPaxMemoryStates(curBuyPrice float64, curSellPrice float64, lastBuyPrice float64, lastSellPrice float64, maxBuyPrice float64, minSellPrice float64) string

func FmtTicker

func FmtTicker(ticker *Ticker) string

func GetParamHmacMD5Sign

func GetParamHmacMD5Sign(secret, params string) (string, error)

func GetParamHmacSHA1Sign

func GetParamHmacSHA1Sign(secret, params string) (string, error)

func GetParamHmacSHA256Base64Sign

func GetParamHmacSHA256Base64Sign(secret, params string) (string, error)

func GetParamHmacSHA256Sign

func GetParamHmacSHA256Sign(secret, params string) (string, error)

func GetParamHmacSHA512Base64Sign

func GetParamHmacSHA512Base64Sign(hmac_key string, hmac_data string) string

func GetParamHmacSHA512Sign

func GetParamHmacSHA512Sign(secret, params string) (string, error)

func GetParamHmacSha384Sign

func GetParamHmacSha384Sign(secret, params string) (string, error)

func GetParamMD5Sign

func GetParamMD5Sign(secret, params string) (string, error)

func GetSHA

func GetSHA(text string) (string, error)

func GzipUnCompress

func GzipUnCompress(data []byte) ([]byte, error)

func HttpDelete

func HttpDelete(client *http.Client, reqUrl string, postData url.Values, headers map[string]string) ([]byte, error)

func HttpGet

func HttpGet(client *http.Client, reqUrl string) (map[string]interface{}, error)

func HttpGet2

func HttpGet2(client *http.Client, reqUrl string, headers map[string]string) (map[string]interface{}, error)

func HttpGet3

func HttpGet3(client *http.Client, reqUrl string, headers map[string]string) ([]interface{}, error)

func HttpGet4

func HttpGet4(client *http.Client, reqUrl string, headers map[string]string, result interface{}) error

func HttpGet5

func HttpGet5(client *http.Client, reqUrl string, headers map[string]string) ([]byte, error)

func HttpPost

func HttpPost(client *http.Client, reqUrl string, postData map[string]string, headers map[string]string) ([]byte, error)

func HttpPost2

func HttpPost2(client *http.Client, reqUrl string, postData url.Values) ([]byte, error)

func HttpPost3

func HttpPost3(client *http.Client, reqUrl string, postData url.Values, headers map[string]string) ([]byte, error)

func HttpPost4

func HttpPost4(client *http.Client, reqUrl string, postData string, headers map[string]string) ([]byte, error)

func NewHttpRequest

func NewHttpRequest(client *http.Client, reqType string, reqUrl string, postData string, requstHeaders map[string]string) ([]byte, error)

func Retry

func Retry(retry int, delay time.Duration, method interface{}, params ...interface{}) interface{}

*

Retry API: one error param
@retry  重试次数
@delay  每次重试延迟时间间隔
@method 调用的函数,比如:api.GetTicker,注意:不是 api.GetTicker(...)
@params 参数、顺序一定要按照实际调用函数入参顺序一样
@return

func ToFloat64

func ToFloat64(v interface{}) float64

func ToInt

func ToInt(v interface{}) int

func ToInt64

func ToInt64(v interface{}) int64

func ToUint64

func ToUint64(v interface{}) uint64

func ValuesToJson

func ValuesToJson(v url.Values) ([]byte, error)

Types

type API

type API interface {
	GetExchangeName() string

	GetAccount() (*Account, error)
	GetSubAccount(currency Currency) (*SubAccount, error)

	LimitBuy(amount, price string, symbol Symbol) (*NewOrder, error)
	LimitSell(amount, price string, symbol Symbol) (*NewOrder, error)
	MarketBuy(amount, price string, symbol Symbol) (*NewOrder, error)
	MarketSell(amount, price string, symbol Symbol) (*NewOrder, error)

	GetActiveOrders(symbol Symbol) ([]NewOrder, error)
	CancelOrder(orderId string, symbol Symbol) (bool, error)

	GetOrder(orderId string, symbol Symbol) (*NewOrder, error)
	GetOrderHistorys(symbol Symbol, currentPage, pageSize int) ([]NewOrder, error)

	GetTicker(symbol Symbol) (*Ticker, error)
	GetDepth(size int, symbol Symbol) (*Depth, error)
	GetKlines(symbol Symbol) ([]Kline, error)
	IsOrderable(symbol Symbol) (bool, error)

	GetTrades(symbol Symbol, since int64) ([]Trade, error)
}

type Account

type Account struct {
	Exchange    string
	Asset       float64 //总资产
	NetAsset    float64 //净资产
	SubAccounts map[Currency]SubAccount
}

type ApiError

type ApiError struct {
	ErrCode,
	ErrMsg,
	OriginErrMsg string
}

func (ApiError) Error

func (e ApiError) Error() string

func (ApiError) OriginErr

func (e ApiError) OriginErr(err string) ApiError

type Currency

type Currency struct {
	Name string
	Desc string
}

func NewCurrency

func NewCurrency(name, desc string) Currency

func (Currency) Eq

func (c Currency) Eq(c2 Currency) bool

func (Currency) String

func (c Currency) String() string

type Depth

type Depth struct {
	Symbol  string
	UTime   time.Time
	AskList DepthRecords //Descending order
	BidList DepthRecords //Descending order
}

type DepthRecord

type DepthRecord struct {
	Price  float64
	Amount float64
}

type DepthRecords

type DepthRecords []DepthRecord

func (DepthRecords) Len

func (dr DepthRecords) Len() int

func (DepthRecords) Less

func (dr DepthRecords) Less(i, j int) bool

func (DepthRecords) Swap

func (dr DepthRecords) Swap(i, j int)

type Kline

type Kline struct {
	Symbol    Symbol
	Timestamp int64
	Open      float64
	Close     float64
	High      float64
	Low       float64
	Vol       float64
}

type NewOrder

type NewOrder struct {
	ID           string
	Symbol       string
	Side         string
	OrderType    string
	Price        float64
	Amount       float64
	State        string
	FilledAmount float64
	FillFee      float64
	Source       string
	CreatedAt    uint64
}

type Order

type Order struct {
	Price      float64
	Amount     float64
	AvgPrice   float64
	DealAmount float64
	Fee        float64
	Cid        string //clientId
	OrderID2   string
	OrderID    int //deprecated
	OrderTime  int
	Status     OrderState
	Symbol     Symbol
	Side       TradeSide
}

type OrderState

type OrderState int

func (OrderState) String

func (state OrderState) String() string

type OrderType

type OrderType int

func (OrderType) String

func (ot OrderType) String() string

type SubAccount

type SubAccount struct {
	Currency      string
	Available     float64
	Frozen        float64
	DemandDeposit float64
	LockDeposit   float64
	Balance       float64
}

type Symbol

type Symbol struct {
	BaseCurrency  Currency
	QuoteCurrency Currency
}

func NewSymbol

func NewSymbol(baseCurrency Currency, quoteCurrency Currency) Symbol

func NewSymbol2

func NewSymbol2(symbolName string) Symbol

func (Symbol) Eq

func (c Symbol) Eq(c2 Symbol) bool

func (Symbol) Reverse

func (symbol Symbol) Reverse() Symbol

func (Symbol) String

func (c Symbol) String() string

func (Symbol) ToLower

func (symbol Symbol) ToLower() Symbol

for to symbol lower, Not practical '==' operation method

func (Symbol) ToSymbol

func (symbol Symbol) ToSymbol(joinChar string) string

func (Symbol) ToSymbol2

func (symbol Symbol) ToSymbol2(joinChar string) string

type Ticker

type Ticker struct {
	Symbol  string  `json:"symbol"`
	Last    float64 `json:"last,string"`     //最新成交价
	LastVol float64 `json:"last_vol,string"` //最近一笔成交的成交量
	Buy     float64 `json:"buy,string"`      //最大买一价
	BuyVol  float64 `json:"buy_vol,string"`  //最大买一量
	Sell    float64 `json:"sell,string"`     //最小卖一价
	SellVol float64 `json:"sell_vol,string"` //最小卖一量
	High    float64 `json:"high,string"`     //24小时内最高价
	Low     float64 `json:"low,string"`      //24小时内最低价
	BaseVol float64 `json:"base_vol,string"` //24小时内基准货币成交量
}

type Trade

type Trade struct {
	Tid    int64     `json:"tid"`
	Type   TradeSide `json:"type"`
	Amount float64   `json:"amount,string"`
	Price  float64   `json:"price,string"`
	Date   int64     `json:"date_ms"`
	Symbol Symbol    `json:"omitempty"`
}

type TradeSide

type TradeSide int

func (TradeSide) String

func (ts TradeSide) String() string

type WsBuilder

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

func NewWsBuilder

func NewWsBuilder() *WsBuilder

func (*WsBuilder) Build

func (b *WsBuilder) Build() *WsConn

func (*WsBuilder) Dump

func (b *WsBuilder) Dump() *WsBuilder

func (*WsBuilder) ErrorHandleFunc

func (b *WsBuilder) ErrorHandleFunc(f func(err error)) *WsBuilder

func (*WsBuilder) Heartbeat

func (b *WsBuilder) Heartbeat(data []byte, t time.Duration) *WsBuilder

func (*WsBuilder) Heartbeat2

func (b *WsBuilder) Heartbeat2(heartbeat func() interface{}, t time.Duration) *WsBuilder

func (*WsBuilder) ProtoHandleFunc

func (b *WsBuilder) ProtoHandleFunc(f func([]byte) error) *WsBuilder

func (*WsBuilder) ProxyUrl

func (b *WsBuilder) ProxyUrl(proxyUrl string) *WsBuilder

func (*WsBuilder) ReconnectIntervalTime

func (b *WsBuilder) ReconnectIntervalTime(t time.Duration) *WsBuilder

func (*WsBuilder) ReqHeader

func (b *WsBuilder) ReqHeader(key, value string) *WsBuilder

func (*WsBuilder) UnCompressFunc

func (b *WsBuilder) UnCompressFunc(f func([]byte) ([]byte, error)) *WsBuilder

func (*WsBuilder) WsUrl

func (b *WsBuilder) WsUrl(wsUrl string) *WsBuilder

type WsConfig

type WsConfig struct {
	WsUrl                 string
	ProxyUrl              string
	ReqHeaders            map[string][]string //连接的时候加入的头部信息
	HeartbeatIntervalTime time.Duration
	HeartbeatData         []byte                       //心跳数据
	HeartbeatFunc         func() interface{}           //心跳数据2
	ReconnectIntervalTime time.Duration                //定时重连时间间隔
	ProtoHandleFunc       func([]byte) error           //协议处理函数
	UnCompressFunc        func([]byte) ([]byte, error) //解压函数
	ErrorHandleFunc       func(err error)
	IsDump                bool
}

type WsConn

type WsConn struct {
	*websocket.Conn
	sync.Mutex
	WsConfig
	// contains filtered or unexported fields
}

func (*WsConn) CloseWs

func (ws *WsConn) CloseWs()

func (*WsConn) HeartbeatTimer

func (ws *WsConn) HeartbeatTimer()

func (*WsConn) NewWs

func (ws *WsConn) NewWs() *WsConn

func (*WsConn) ReConnect

func (ws *WsConn) ReConnect()

func (*WsConn) ReConnectTimer

func (ws *WsConn) ReConnectTimer()

func (*WsConn) ReceiveMessage

func (ws *WsConn) ReceiveMessage()

func (*WsConn) SendJsonMessage

func (ws *WsConn) SendJsonMessage(v interface{}) error

func (*WsConn) SendTextMessage

func (ws *WsConn) SendTextMessage(data []byte) error

func (*WsConn) Subscribe

func (ws *WsConn) Subscribe(subEvent interface{}) error

func (*WsConn) UpdateActiveTime

func (ws *WsConn) UpdateActiveTime()

Jump to

Keyboard shortcuts

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