goex

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2019 License: MIT Imports: 25 Imported by: 0

README

GoEx目标

GoEx项目是为了统一并标准化各个数字资产交易平台的接口而设计,同一个策略可以随时切换到任意一个交易平台,而不需要更改任何代码。

GoEx已支持交易所 22+

交易所 行情接口 交易接口 版本号
hbg.com Y Y 1
hbdm.com Y Y 1
okex.com Y Y 3
binance.com Y Y 1
bitstamp.net Y Y 1
bitfinex.com Y Y 1
zb.com Y Y 1
kraken.com Y Y *
poloniex.com Y Y *
aacoin.com Y Y 1
allcoin.ca Y Y *
big.one Y Y 2|3
fcoin.com Y Y 2
hitbtc.com Y Y *
coinex.com Y Y 1
exx.com Y Y 1
bithumb.com Y Y *
gate.io Y N 1
btcbox.co.jp Y N *
bittrex.com Y N 1.1
btcchina.com Y Y 1
coinbig.com Y Y *

###安装GoEx库 go get github.com/nntaoli-project/GoEx

例子


   package main
   
   import (
   	"github.com/nntaoli-project/GoEx"
   	"github.com/nntaoli-project/GoEx/builder"
   	"log"
   	"time"
   )
   
   func main() {
   	apiBuilder := builder.NewAPIBuilder().HttpTimeout(5 * time.Second)
   	//apiBuilder := builder.NewAPIBuilder().HttpTimeout(5 * time.Second).HttpProxy("socks5://127.0.0.1:1080")
   	
   	//build spot api
   	//api := apiBuilder.APIKey("").APISecretkey("").ClientID("123").Build(goex.BITSTAMP)
   	api := apiBuilder.APIKey("").APISecretkey("").Build(goex.HUOBI_PRO)
   	log.Println(api.GetExchangeName())
   	log.Println(api.GetTicker(goex.BTC_USD))
   	log.Println(api.GetDepth(2, goex.BTC_USD))
   	//log.Println(api.GetAccount())
   	//log.Println(api.GetUnfinishOrders(goex.BTC_USD))
   
   	//build future api
   	futureApi := apiBuilder.APIKey("").APISecretkey("").BuildFuture(goex.HBDM)
   	log.Println(futureApi.GetExchangeName())
   	log.Println(futureApi.GetFutureTicker(goex.BTC_USD, goex.QUARTER_CONTRACT))
   	log.Println(futureApi.GetFutureDepth(goex.BTC_USD, goex.QUARTER_CONTRACT, 5))
   	//log.Println(futureApi.GetFutureUserinfo()) // account
   	//log.Println(futureApi.GetFuturePosition(goex.BTC_USD , goex.QUARTER_CONTRACT))//position info
   }

###websocket 使用例子

import (
	"github.com/nntaoli-project/GoEx"
	"github.com/nntaoli-project/GoEx/huobi"
	//"github.com/nntaoli-project/GoEx/okcoin"
	"log"
)

func main() {

	//ws := okcoin.NewOKExFutureWs() //ok期货
	ws := huobi.NewHbdmWs() //huobi期货
	//设置回调函数
	ws.SetCallbacks(func(ticker *goex.FutureTicker) {
		log.Println(ticker)
	}, func(depth *goex.Depth) {
		log.Println(depth)
	}, func(trade *goex.Trade, contract string) {
		log.Println(contract, trade)
	})
	//订阅行情
	ws.SubscribeTrade(goex.BTC_USDT, goex.NEXT_WEEK_CONTRACT)
	ws.SubscribeDepth(goex.BTC_USDT, goex.QUARTER_CONTRACT, 5)
	ws.SubscribeTicker(goex.BTC_USDT, goex.QUARTER_CONTRACT)
}  

注意事项

  1. 推荐使用GoLand开发。
  2. 推荐关闭自动格式化功能。
  3. 不建议对现已存在的文件进行重新格式化,这样会导致commit特别糟糕。
  4. 请用OrderID2这个字段代替OrderID
  5. 请不要使用deprecated关键字标注的方法和字段,后面版本可能随时删除的
  6. 交流QQ群:574829125

欢迎为作者付一碗面钱

一碗面钱   一碗面钱

Documentation

Index

Constants

View Source
const (
	BUY = 1 + iota
	SELL
	BUY_MARKET
	SELL_MARKET
)
View Source
const (
	OPEN_BUY   = 1 + iota //开多
	OPEN_SELL             //开空
	CLOSE_BUY             //平多
	CLOSE_SELL            //平空
)
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
)

k线周期

View Source
const (
	ORDER_TYPE_LIMIT = 1 + iota
	ORDER_TYPE_MARKET
	ORDER_TYPE_FOK
	ORDER_TYPE_FAK
	ORDER_TYPE_POST_ONLY
	ORDER_TYPE_IOC = ORDER_TYPE_FAK
)
View Source
const (
	OKCOIN_CN   = "okcoin.cn"
	OKCOIN_COM  = "okcoin.com"
	OKEX        = "okex.com"
	OKEX_V3     = "okex.com_v3"
	OKEX_FUTURE = "okex.com_future"
	OKEX_SWAP   = "okex.com_swap"
	HUOBI       = "huobi.com"
	HUOBI_PRO   = "huobi.pro"
	BITSTAMP    = "bitstamp.net"
	KRAKEN      = "kraken.com"
	ZB          = "zb.com"
	BITFINEX    = "bitfinex.com"
	BINANCE     = "binance.com"
	POLONIEX    = "poloniex.com"
	COINEX      = "coinex.com"
	BITHUMB     = "bithumb.com"
	GATEIO      = "gate.io"
	BITTREX     = "bittrex.com"
	GDAX        = "gdax.com"
	WEX_NZ      = "wex.nz"
	BIGONE      = "big.one"
	COIN58      = "58coin.com"
	FCOIN       = "fcoin.com"
	HITBTC      = "hitbtc.com"
	BITMEX      = "bitmex.com"
	CRYPTOPIA   = "cryptopia.co.nz"
	HBDM        = "hbdm.com"
)

exchanges const

View Source
const (
	ORDINARY  = 0 // normal order
	POST_ONLY = 1 // only maker
	FOK       = 2 // fill or kill
	IOC       = 3 // Immediate or Cancel
)

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_CURRENCY_PAIR = ApiError{ErrCode: "EX_ERR_0007", ErrMsg: "invalid currency pair"}
	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 (
	THIS_WEEK_CONTRACT = "this_week" //周合约
	NEXT_WEEK_CONTRACT = "next_week" //次周合约
	QUARTER_CONTRACT   = "quarter"   //季度合约
	SWAP_CONTRACT      = "swap"      //永续合约
)
View Source
var (
	UNKNOWN = Currency{"UNKNOWN", ""}
	CNY     = Currency{"CNY", ""}
	USD     = Currency{"USD", ""}
	USDT    = Currency{"USDT", ""}
	PAX     = Currency{"PAX", "https://www.paxos.com/"}
	USDC    = Currency{"USDC", "https://www.centre.io/"}
	EUR     = Currency{"EUR", ""}
	KRW     = Currency{"KRW", ""}
	JPY     = Currency{"JPY", ""}
	BTC     = Currency{"BTC", "https://bitcoin.org/"}
	XBT     = Currency{"XBT", ""}
	BCC     = Currency{"BCC", ""}
	BCH     = Currency{"BCH", ""}
	BCX     = Currency{"BCX", ""}
	LTC     = Currency{"LTC", ""}
	ETH     = Currency{"ETH", ""}
	ETC     = Currency{"ETC", ""}
	EOS     = Currency{"EOS", ""}
	BTS     = Currency{"BTS", ""}
	QTUM    = Currency{"QTUM", ""}
	SC      = Currency{"SC", ""}
	ANS     = Currency{"ANS", ""}
	ZEC     = Currency{"ZEC", ""}
	DCR     = Currency{"DCR", ""}
	XRP     = Currency{"XRP", ""}
	BTG     = Currency{"BTG", ""}
	BCD     = Currency{"BCD", ""}
	NEO     = Currency{"NEO", ""}
	HSR     = Currency{"HSR", ""}
	BSV     = Currency{"BSV", ""}
	OKB     = Currency{"OKB", "OKB is a global utility token issued by OK Blockchain Foundation"}
	HT      = Currency{"HT", "HuoBi Token"}
	BNB     = Currency{"BNB", "BNB, or Binance Coin, is a cryptocurrency created by Binance."}

	BTC_CNY  = CurrencyPair{BTC, CNY}
	LTC_CNY  = CurrencyPair{LTC, CNY}
	BCC_CNY  = CurrencyPair{BCC, CNY}
	ETH_CNY  = CurrencyPair{ETH, CNY}
	ETC_CNY  = CurrencyPair{ETC, CNY}
	EOS_CNY  = CurrencyPair{EOS, CNY}
	BTS_CNY  = CurrencyPair{BTS, CNY}
	QTUM_CNY = CurrencyPair{QTUM, CNY}
	SC_CNY   = CurrencyPair{SC, CNY}
	ANS_CNY  = CurrencyPair{ANS, CNY}
	ZEC_CNY  = CurrencyPair{ZEC, CNY}

	BTC_KRW = CurrencyPair{BTC, KRW}
	ETH_KRW = CurrencyPair{ETH, KRW}
	ETC_KRW = CurrencyPair{ETC, KRW}
	LTC_KRW = CurrencyPair{LTC, KRW}
	BCH_KRW = CurrencyPair{BCH, KRW}

	BTC_USD = CurrencyPair{BTC, USD}
	LTC_USD = CurrencyPair{LTC, USD}
	ETH_USD = CurrencyPair{ETH, USD}
	ETC_USD = CurrencyPair{ETC, USD}
	BCH_USD = CurrencyPair{BCH, USD}
	BCC_USD = CurrencyPair{BCC, USD}
	XRP_USD = CurrencyPair{XRP, USD}
	BCD_USD = CurrencyPair{BCD, USD}
	EOS_USD = CurrencyPair{EOS, USD}
	BTG_USD = CurrencyPair{BTG, USD}
	BSV_USD = CurrencyPair{BSV, USD}

	BTC_USDT = CurrencyPair{BTC, USDT}
	LTC_USDT = CurrencyPair{LTC, USDT}
	BCH_USDT = CurrencyPair{BCH, USDT}
	BCC_USDT = CurrencyPair{BCC, USDT}
	ETC_USDT = CurrencyPair{ETC, USDT}
	ETH_USDT = CurrencyPair{ETH, USDT}
	BCD_USDT = CurrencyPair{BCD, USDT}
	NEO_USDT = CurrencyPair{NEO, USDT}
	EOS_USDT = CurrencyPair{EOS, USDT}
	XRP_USDT = CurrencyPair{XRP, USDT}
	HSR_USDT = CurrencyPair{HSR, USDT}
	BSV_USDT = CurrencyPair{BSV, USDT}
	OKB_USDT = CurrencyPair{OKB, USDT}
	HT_USDT  = CurrencyPair{HT, USDT}
	BNB_USDT = CurrencyPair{BNB, USDT}

	XRP_EUR = CurrencyPair{XRP, EUR}

	BTC_JPY = CurrencyPair{BTC, JPY}
	LTC_JPY = CurrencyPair{LTC, JPY}
	ETH_JPY = CurrencyPair{ETH, JPY}
	ETC_JPY = CurrencyPair{ETC, JPY}
	BCH_JPY = CurrencyPair{BCH, JPY}

	LTC_BTC = CurrencyPair{LTC, BTC}
	ETH_BTC = CurrencyPair{ETH, BTC}
	ETC_BTC = CurrencyPair{ETC, BTC}
	BCC_BTC = CurrencyPair{BCC, BTC}
	BCH_BTC = CurrencyPair{BCH, BTC}
	DCR_BTC = CurrencyPair{DCR, BTC}
	XRP_BTC = CurrencyPair{XRP, BTC}
	BTG_BTC = CurrencyPair{BTG, BTC}
	BCD_BTC = CurrencyPair{BCD, BTC}
	NEO_BTC = CurrencyPair{NEO, BTC}
	EOS_BTC = CurrencyPair{EOS, BTC}
	HSR_BTC = CurrencyPair{HSR, BTC}
	BSV_BTC = CurrencyPair{BSV, BTC}
	OKB_BTC = CurrencyPair{OKB, BTC}
	HT_BTC  = CurrencyPair{HT, BTC}
	BNB_BTC = CurrencyPair{BNB, BTC}

	ETC_ETH = CurrencyPair{ETC, ETH}
	EOS_ETH = CurrencyPair{EOS, ETH}
	ZEC_ETH = CurrencyPair{ZEC, ETH}
	NEO_ETH = CurrencyPair{NEO, ETH}
	HSR_ETH = CurrencyPair{HSR, ETH}
	LTC_ETH = CurrencyPair{LTC, ETH}

	UNKNOWN_PAIR = CurrencyPair{UNKNOWN, UNKNOWN}
)

Functions

func AdaptKlinePeriodForOKEx

func AdaptKlinePeriodForOKEx(period int) string

func CancelAllUnfinishedFutureOrders

func CancelAllUnfinishedFutureOrders(api FutureRestAPI, contractType string, currencyPair CurrencyPair) int

*

  • call all unfinished future orders
  • @return c 成功撤单数量

func CancelAllUnfinishedOrders

func CancelAllUnfinishedOrders(api API, currencyPair CurrencyPair) int

*

  • call all unfinished orders

func FlateUnCompress

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

func FloatToString

func FloatToString(v float64, n int) string

n :保留的小数点位数,去除末尾多余的0(StripTrailingZeros)

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)

for bitfinex.com

func GetParamMD5Sign

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

*

*md5签名,okcoin和huobi适用

func GetSHA

func GetSHA(text string) (string, error)

func GzipUnCompress

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

func HttpDeleteForm

func HttpDeleteForm(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 HttpPostForm

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

func HttpPostForm2

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

func HttpPostForm3

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

func HttpPostForm4

func HttpPostForm4(client *http.Client, reqUrl string, postData map[string]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 RE

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

*

本函数只适合,返回两个参数的API重试调用,其中一个参数必须是error
@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 UUID added in v1.0.2

func UUID() string

func ValuesToJson

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

Types

type API

type API interface {
	LimitBuy(amount, price string, currency CurrencyPair) (*Order, error)
	LimitSell(amount, price string, currency CurrencyPair) (*Order, error)
	MarketBuy(amount, price string, currency CurrencyPair) (*Order, error)
	MarketSell(amount, price string, currency CurrencyPair) (*Order, error)
	CancelOrder(orderId string, currency CurrencyPair) (bool, error)
	GetOneOrder(orderId string, currency CurrencyPair) (*Order, error)
	GetUnfinishOrders(currency CurrencyPair) ([]Order, error)
	GetOrderHistorys(currency CurrencyPair, currentPage, pageSize int) ([]Order, error)
	GetAccount() (*Account, error)

	GetTicker(currency CurrencyPair) (*Ticker, error)
	GetDepth(size int, currency CurrencyPair) (*Depth, error)
	GetKlineRecords(currency CurrencyPair, period, size, since int) ([]Kline, error)
	//非个人,整个交易所的交易记录
	GetTrades(currencyPair CurrencyPair, since int64) ([]Trade, error)

	GetExchangeName() string
}

type APIConfig

type APIConfig struct {
	HttpClient    *http.Client
	Endpoint      string
	ApiKey        string
	ApiSecretKey  string
	ApiPassphrase string //for okex.com v3 api
	ClientId      string //for bitstamp.net , huobi.pro

	Lever int //杠杆倍数 , for future
}

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 BorrowParameter

type BorrowParameter struct {
	CurrencyPair CurrencyPair
	Currency     Currency
	Amount       float64
}

type Currency

type Currency struct {
	Symbol string
	Desc   string
}

func NewCurrency

func NewCurrency(symbol, desc string) Currency

func (Currency) AdaptBccToBch

func (c Currency) AdaptBccToBch() Currency

func (Currency) AdaptBchToBcc

func (c Currency) AdaptBchToBcc() Currency

func (Currency) Eq

func (c Currency) Eq(c2 Currency) bool

func (Currency) String

func (c Currency) String() string

type CurrencyPair

type CurrencyPair struct {
	CurrencyA Currency
	CurrencyB Currency
}

A->B(A兑换为B)

func NewCurrencyPair

func NewCurrencyPair(currencyA Currency, currencyB Currency) CurrencyPair

func NewCurrencyPair2

func NewCurrencyPair2(currencyPairSymbol string) CurrencyPair

func (CurrencyPair) AdaptBccToBch

func (pair CurrencyPair) AdaptBccToBch() CurrencyPair

func (CurrencyPair) AdaptBchToBcc

func (pair CurrencyPair) AdaptBchToBcc() CurrencyPair

It is currently applicable to binance and zb

func (CurrencyPair) AdaptUsdToUsdt

func (pair CurrencyPair) AdaptUsdToUsdt() CurrencyPair

func (CurrencyPair) AdaptUsdtToUsd

func (pair CurrencyPair) AdaptUsdtToUsd() CurrencyPair

func (CurrencyPair) Eq

func (c CurrencyPair) Eq(c2 CurrencyPair) bool

func (CurrencyPair) Reverse

func (pair CurrencyPair) Reverse() CurrencyPair

func (CurrencyPair) String

func (c CurrencyPair) String() string

func (CurrencyPair) ToLower

func (pair CurrencyPair) ToLower() CurrencyPair

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

func (CurrencyPair) ToSymbol

func (pair CurrencyPair) ToSymbol(joinChar string) string

func (CurrencyPair) ToSymbol2

func (pair CurrencyPair) ToSymbol2(joinChar string) string

type Depth

type Depth struct {
	ContractType string //for future
	Pair         CurrencyPair
	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 FutureAccount

type FutureAccount struct {
	FutureSubAccounts map[Currency]FutureSubAccount
}

type FutureKline

type FutureKline struct {
	*Kline
	Vol2 float64 //个数
}

type FutureOrder

type FutureOrder struct {
	ClientOid    string //自定义ID,GoEx内部自动生成
	OrderID2     string //请尽量用这个字段替代OrderID字段
	Price        float64
	Amount       float64
	AvgPrice     float64
	DealAmount   float64
	OrderID      int64 //deprecated
	OrderTime    int64
	Status       TradeStatus
	Currency     CurrencyPair
	OrderType    int     //ORDINARY=0 POST_ONLY=1 FOK= 2 IOC= 3
	OType        int     //1:开多 2:开空 3:平多 4: 平空
	LeverRate    int     //倍数
	Fee          float64 //手续费
	ContractName string
}

type FuturePosition

type FuturePosition struct {
	BuyAmount      float64
	BuyAvailable   float64
	BuyPriceAvg    float64
	BuyPriceCost   float64
	BuyProfitReal  float64
	CreateDate     int64
	LeverRate      int
	SellAmount     float64
	SellAvailable  float64
	SellPriceAvg   float64
	SellPriceCost  float64
	SellProfitReal float64
	Symbol         CurrencyPair //btc_usd:比特币,ltc_usd:莱特币
	ContractType   string
	ContractId     int64
	ForceLiquPrice float64 //预估爆仓价
}

type FutureRestAPI

type FutureRestAPI interface {
	/**
	 *获取交易所名字
	 */
	GetExchangeName() string

	/**
	 *获取交割预估价
	 */
	GetFutureEstimatedPrice(currencyPair CurrencyPair) (float64, error)

	/**
	 * 期货行情
	 * @param currency_pair   btc_usd:比特币    ltc_usd :莱特币
	 * @param contractType  合约类型: this_week:当周   next_week:下周   month:当月   quarter:季度
	 */
	GetFutureTicker(currencyPair CurrencyPair, contractType string) (*Ticker, error)

	/**
	 * 期货深度
	 * @param currencyPair  btc_usd:比特币    ltc_usd :莱特币
	 * @param contractType  合约类型: this_week:当周   next_week:下周   month:当月   quarter:季度
	 * @param size 获取深度档数
	 * @return
	 */
	GetFutureDepth(currencyPair CurrencyPair, contractType string, size int) (*Depth, error)

	/**
	 * 期货指数
	 * @param currencyPair   btc_usd:比特币    ltc_usd :莱特币
	 */
	GetFutureIndex(currencyPair CurrencyPair) (float64, error)

	/**
	 *全仓账户
	 */
	GetFutureUserinfo() (*FutureAccount, error)

	/**
	 * @deprecated
	 * 期货下单
	 * @param currencyPair   btc_usd:比特币    ltc_usd :莱特币
	 * @param contractType   合约类型: this_week:当周   next_week:下周   month:当月   quarter:季度
	 * @param price  价格
	 * @param amount  委托数量
	 * @param openType   1:开多   2:开空   3:平多   4:平空
	 * @param matchPrice  是否为对手价 0:不是    1:是   ,当取值为1时,price无效
	 */
	PlaceFutureOrder(currencyPair CurrencyPair, contractType, price, amount string, openType, matchPrice, leverRate int) (string, error)

	/**
	 * 取消订单
	 * @param symbol   btc_usd:比特币    ltc_usd :莱特币
	 * @param contractType    合约类型: this_week:当周   next_week:下周   month:当月   quarter:季度
	 * @param orderId   订单ID

	 */
	FutureCancelOrder(currencyPair CurrencyPair, contractType, orderId string) (bool, error)

	/**
	 * 用户持仓查询
	 * @param symbol   btc_usd:比特币    ltc_usd :莱特币
	 * @param contractType   合约类型: this_week:当周   next_week:下周   month:当月   quarter:季度
	 * @return
	 */
	GetFuturePosition(currencyPair CurrencyPair, contractType string) ([]FuturePosition, error)

	/**
	 *获取订单信息
	 */
	GetFutureOrders(orderIds []string, currencyPair CurrencyPair, contractType string) ([]FutureOrder, error)

	/**
	 *获取单个订单信息
	 */
	GetFutureOrder(orderId string, currencyPair CurrencyPair, contractType string) (*FutureOrder, error)

	/**
	 *获取未完成订单信息
	 */
	GetUnfinishFutureOrders(currencyPair CurrencyPair, contractType string) ([]FutureOrder, error)

	/**
	 *获取交易费
	 */
	GetFee() (float64, error)

	/**
	 *获取每张合约价值
	 */
	GetContractValue(currencyPair CurrencyPair) (float64, error)

	/**
	 *获取交割时间 星期(0,1,2,3,4,5,6),小时,分,秒
	 */
	GetDeliveryTime() (int, int, int, int)

	/**
	 * 获取K线数据
	 */
	GetKlineRecords(contract_type string, currency CurrencyPair, period, size, since int) ([]FutureKline, error)

	/**
	 * 获取Trade数据
	 */
	GetTrades(contract_type string, currencyPair CurrencyPair, since int64) ([]Trade, error)
}

type FutureSubAccount

type FutureSubAccount struct {
	Currency      Currency
	AccountRights float64 //账户权益
	KeepDeposit   float64 //保证金
	ProfitReal    float64 //已实现盈亏
	ProfitUnreal  float64
	RiskRate      float64 //保证金率
}

type FutureTicker

type FutureTicker struct {
	*Ticker
	ContractType string  `json:"omitempty"`
	ContractId   int     `json:"contractId"`
	LimitHigh    float64 `json:"limitHigh,string"`
	LimitLow     float64 `json:"limitLow,string"`
	HoldAmount   float64 `json:"hold_amount,string"`
	UnitAmount   float64 `json:"unitAmount,string"`
}

type Kline

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

type MarginAccount

type MarginAccount struct {
	Sub              map[Currency]MarginSubAccount
	LiquidationPrice float64
	RiskRate         float64
	MarginRatio      float64
}

type MarginSubAccount

type MarginSubAccount struct {
	Balance     float64
	Frozen      float64
	Available   float64
	CanWithdraw float64
	Loan        float64
	LendingFee  float64
}

type OderType added in v1.0.2

type OderType int

func (OderType) String added in v1.0.2

func (ot OderType) String() string

type Order

type Order struct {
	Price      float64
	Amount     float64
	AvgPrice   float64
	DealAmount float64
	Fee        float64
	Cid        string //客户端自定义ID
	OrderID2   string
	OrderID    int //deprecated
	OrderTime  int
	Status     TradeStatus
	Currency   CurrencyPair
	Side       TradeSide
	Type       string //limit / market
	OrderType  int    //0:default,1:maker,2:fok,3:ioc
}

type OrderType

type OrderType int

func (OrderType) String

func (ot OrderType) String() string

type RepaymentParameter

type RepaymentParameter struct {
	BorrowParameter
	BorrowId string
}

type SubAccount

type SubAccount struct {
	Currency     Currency
	Amount       float64
	ForzenAmount float64
	LoanAmount   float64
}

type Ticker

type Ticker struct {
	Pair CurrencyPair `json:"omitempty"`
	Last float64      `json:"last,string"`
	Buy  float64      `json:"buy,string"`
	Sell float64      `json:"sell,string"`
	High float64      `json:"high,string"`
	Low  float64      `json:"low,string"`
	Vol  float64      `json:"vol,string"`
	Date uint64       `json:"date"` // 单位:ms
}

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"`
	Pair   CurrencyPair `json:"omitempty"`
}

type TradeSide

type TradeSide int

func AdaptTradeSide

func AdaptTradeSide(side string) TradeSide

func (TradeSide) String

func (ts TradeSide) String() string

type TradeStatus

type TradeStatus int
const (
	ORDER_UNFINISH TradeStatus = iota
	ORDER_PART_FINISH
	ORDER_FINISH
	ORDER_CANCEL
	ORDER_REJECT
	ORDER_CANCEL_ING
	ORDER_FAIL
)

func (TradeStatus) String

func (ts TradeStatus) 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