wstrader

package module
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: May 16, 2024 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

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"}
)

Functions

func CancelAllUnfinishedFutureOrders

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

*

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

func CancelAllUnfinishedOrders

func CancelAllUnfinishedOrders(api API, currencyPair cons.CurrencyPair) int

*

  • call all unfinished orders

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(hmacKey string, hmacData 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 RE

func RE(retry int, delay time.Duration, method any, params ...any) any
  • 本函数只适合,返回两个参数的API重试调用,其中一个参数必须是error @retry 重试次数 @delay 每次重试延迟时间间隔 @method 调用的函数,比如: api.GetTicker ,注意:不是api.GetTicker(...) @params 参数,顺序一定要按照实际调用函数入参顺序一样 @return 返回

Types

type API

type API interface {
	CancelOrder(orderId string, currency cons.CurrencyPair) (bool, error)
	// GetOneOrder(orderId string, currency CurrencyPair) (*Order, error)
	GetUnfinishedOrders(currency cons.CurrencyPair) ([]q.Order, error)
	// GetOrderHistorys(currency CurrencyPair, opt ...OptionalParameter) ([]Order, error)
	// GetAccount() (*Account, error)
	// GetDepth(size int, currency CurrencyPair) (*Depth, error)
	// GetKlineRecords(currency CurrencyPair, period KlinePeriod, size int, optional ...OptionalParameter) ([]Kline, error)
	//none-personal but whole exchange center
	// GetTicker(currency CurrencyPair) (*Ticker, error)
	String() string
	OneTicker(d q.D) (q.Bbo, error)
	AllTicker(SymPair map[string]q.D) (*sync.Map, error)
	PairArray() (map[string]q.D, map[q.D]q.P, error)
	Fee() float64
	TradeFee() (map[string]q.TradeFee, error)
	WithdrawFee() ([]q.NetworkWithdraw, error)
	PlaceOrders([3]q.Order) ([3]q.Order, error)
	Balances() (*sync.Map, *sync.Map, error)
	GetAttr() q.Attr
	Test() bool
}

type APIConfig

type APIConfig struct {
	HttpClient    *http.Client
	Endpoint      string
	ApiKey        string
	ApiSecretKey  string
	ApiPassphrase string  //for okx.com v3 api
	ClientId      string  //for bitstamp.net , huobi.pro
	Lever         float64 //杠杆倍数 , for future
}

type Account

type Account struct {
	Exchange    string
	Asset       float64 //总资产
	NetAsset    float64 //净资产
	SubAccounts map[cons.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 cons.CurrencyPair
	Currency     cons.Currency
	Amount       float64
}

api parameter struct

type DepositWithdrawHistory

type DepositWithdrawHistory struct {
	WithdrawalId string    `json:"withdrawal_id,omitempty"`
	Currency     string    `json:"currency"`
	Txid         string    `json:"txid"`
	Amount       float64   `json:"amount,string"`
	From         string    `json:"from,omitempty"`
	To           string    `json:"to"`
	Memo         string    `json:"memo,omitempty"`
	Fee          string    `json:"fee"`
	Status       int       `json:"status,string"`
	Timestamp    time.Time `json:"timestamp"`
}

type Depth

type Depth struct {
	ContractType string `json:"contract_type,omitempty"` //for futures
	ContractId   string `json:"contract_id,omitempty"`   // for futures
	Pair         cons.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[cons.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       cons.TradeStatus
	Currency     cons.CurrencyPair
	OrderType    int     //ORDINARY=0 POST_ONLY=1 FOK= 2 IOC= 3
	OType        int     //1:开多 2:开空 3:平多 4: 平空
	LeverRate    float64 //倍数
	Fee          float64 //手续费
	ContractName string
	FinishedTime int64 // finished timestamp
	//策略委托单
	TriggerPrice float64
	AlgoType     int //1:限价 2:市场价;触发价格类型,默认是限价;为市场价时,委托价格不必填;
}

type FuturePosition

type FuturePosition struct {
	BuyAmount      float64
	BuyAvailable   float64
	BuyPriceAvg    float64
	BuyPriceCost   float64
	BuyProfitReal  float64
	BuyProfit      float64
	CreateDate     int64
	LeverRate      float64
	SellAmount     float64
	SellAvailable  float64
	SellPriceAvg   float64
	SellPriceCost  float64
	SellProfitReal float64
	SellProfit     float64
	Symbol         cons.CurrencyPair //btc_usd:比特币,ltc_usd:莱特币
	ContractType   string
	ContractId     int64
	ForceLiquPrice float64 //预估爆仓价
	ShortPnlRatio  float64 //空仓收益率
	LongPnlRatio   float64 //多仓收益率
}

type FutureRestAPI

type FutureRestAPI interface {
	/**
	 *获取交易所名字
	 */
	String() string
	/**
	 *获取交割预估价
	 */
	GetFutureEstimatedPrice(currencyPair cons.CurrencyPair) (float64, error)
	/**
	 * 期货行情
	 * @param currency_pair   btc_usd:比特币    ltc_usd :莱特币
	 * @param contractType  合约类型: this_week:当周   next_week:下周   month:当月   quarter:季度
	 */
	GetFutureTicker(currencyPair cons.CurrencyPair, contractType string) (*Ticker, error)
	/**
	 * 期货深度
	 * @param currencyPair  btc_usd:比特币    ltc_usd :莱特币
	 * @param contractType  合约类型: this_week:当周   next_week:下周   month:当月   quarter:季度
	 * @param size 获取深度档数
	 * @return
	 */
	GetFutureDepth(currencyPair cons.CurrencyPair, contractType string, size int) (*Depth, error)
	/**
	 * 期货指数
	 * @param currencyPair   btc_usd:比特币    ltc_usd :莱特币
	 */
	GetFutureIndex(currencyPair cons.CurrencyPair) (float64, error)
	/**
	 *全仓账户
	 *@param currency
	 */
	GetFutureUserinfo(currencyPair ...cons.CurrencyPair) (*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 cons.CurrencyPair, contractType, price, amount string, openType, matchPrice int, leverRate float64) (string, error)
	LimitFuturesOrder(currencyPair cons.CurrencyPair, contractType, price, amount string, openType int, opt ...cons.LimitOrderOptionalParameter) (*FutureOrder, error)
	//对手价下单
	MarketFuturesOrder(currencyPair cons.CurrencyPair, contractType, amount string, openType int) (*FutureOrder, error)
	/**
	 * 取消订单
	 * @param symbol   btc_usd:比特币    ltc_usd :莱特币
	 * @param contractType    合约类型: this_week:当周   next_week:下周   month:当月   quarter:季度
	 * @param orderId   订单ID
	 */
	FutureCancelOrder(currencyPair cons.CurrencyPair, contractType, orderId string) (bool, error)
	/**
	 * 用户持仓查询
	 * @param symbol   btc_usd:比特币    ltc_usd :莱特币
	 * @param contractType   合约类型: this_week:当周   next_week:下周   month:当月   quarter:季度
	 * @return
	 */
	GetFuturePosition(currencyPair cons.CurrencyPair, contractType string) ([]FuturePosition, error)
	/**
	 *获取订单信息
	 */
	GetFutureOrders(orderIds []string, currencyPair cons.CurrencyPair, contractType string) ([]FutureOrder, error)
	/**
	 *获取单个订单信息
	 */
	GetFutureOrder(orderId string, currencyPair cons.CurrencyPair, contractType string) (*FutureOrder, error)
	/**
	 *获取未完成订单信息
	 */
	GetUnfinishFutureOrders(currencyPair cons.CurrencyPair, contractType string) ([]FutureOrder, error)
	/**
	 * 获取个人订单历史,默认获取最近的订单历史列表,返回多少条订单数据,需根据平台接口定义而定
	 */
	GetFutureOrderHistory(pair cons.CurrencyPair, contractType string, optional ...OptionalParameter) ([]FutureOrder, error)
	/**
	 *获取交易费
	 */
	GetFee() (float64, error)
	/**
	 *获取交易所的美元人民币汇率
	 */
	//GetExchangeRate() (float64, error)
	/**
	 *获取每张合约价值
	 */
	GetContractValue(currencyPair cons.CurrencyPair) (float64, error)
	/**
	 *获取交割时间 星期(0,1,2,3,4,5,6),小时,分,秒
	 */
	GetDeliveryTime() (int, int, int, int)
	/**
	 * 获取K线数据
	 */
	GetKlineRecords(contractType string, currency cons.CurrencyPair, period cons.KlinePeriod, size int, optional ...OptionalParameter) ([]FutureKline, error)
	/**
	 * 获取Trade数据
	 */
	GetTrades(contractType string, currencyPair cons.CurrencyPair, since int64) ([]q.Trade, error)
}

type FutureSubAccount

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

type FutureTicker

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

type FuturesContractInfo

type FuturesContractInfo struct {
	*TickSize
	ContractVal  float64 //合约面值(美元)
	Delivery     string  //交割日期
	ContractType string  //	本周 this_week 次周 next_week 季度 quarter
}

type FuturesWsApi

type FuturesWsApi interface {
	DepthCallback(func(depth *Depth))
	TickerCallback(func(ticker *FutureTicker))
	TradeCallback(func(trade *q.Trade, contract string))
	//OrderCallback(func(order *FutureOrder))
	//PositionCallback(func(position *FuturePosition))
	//AccountCallback(func(account *FutureAccount))
	SubscribeDepth(pair cons.CurrencyPair, contractType string) error
	SubscribeTicker(pair cons.CurrencyPair, contractType string) error
	SubscribeTrade(pair cons.CurrencyPair, contractType string) error
}

type HistoricalFunding

type HistoricalFunding struct {
	InstrumentId string    `json:"instrument_id"`
	RealizedRate float64   `json:"realized_rate,string"`
	FundingTime  time.Time `json:"funding_time"`
}

type Kline

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

type MarginAccount

type MarginAccount struct {
	Sub              map[cons.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 OptionalParameter

type OptionalParameter map[string]any

func (OptionalParameter) GetFloat64

func (optional OptionalParameter) GetFloat64(name string) float64

func (OptionalParameter) GetInt

func (optional OptionalParameter) GetInt(name string) int

func (OptionalParameter) GetInt64

func (optional OptionalParameter) GetInt64(name string) int64

func (OptionalParameter) GetString

func (optional OptionalParameter) GetString(name string) string

func (OptionalParameter) GetTime

func (optional OptionalParameter) GetTime(name string) *time.Time

func (OptionalParameter) Optional

func (optional OptionalParameter) Optional(name string, value any) OptionalParameter

type PoloniexCurrency

type PoloniexCurrency struct {
	ID             int    `json:"id"`
	Name           string `json:"name"`
	HumanType      string `json:"humanType"`
	CurrencyType   string `json:"currencyType"`
	TxFee          string `json:"txFee"`
	MinConf        int    `json:"minConf"`
	DepositAddress string `json:"depositAddress"`
	Disabled       int    `json:"disabled"` //Designates whether (1) or not (0) deposits and withdrawals are disabled.
	Frozen         int    `json:"frozen"`   //Designates whether (1) or not (0) trading for this currency is disabled for trading.
	Blockchain     string `json:"blockchain"`
	Delisted       int    `json:"delisted"`
	IsGeofenced    int    `json:"isGeofenced"`
}

type RepaymentParameter

type RepaymentParameter struct {
	BorrowParameter
	BorrowId string
}

type SpotWsApi

type SpotWsApi interface {
	DepthCallback(func(depth *Depth))
	TickerCallback(func(ticker *Ticker))
	TradeCallback(func(trade *q.Trade))
	BBOCallback(func(bbo *q.Bbo))
	//OrderCallback(func(order *Order))
	//AccountCallback(func(account *Account))
	SubscribeDepth(pair cons.CurrencyPair) error
	SubscribeTicker(pair cons.CurrencyPair) error
	SubscribeTrade(pair cons.CurrencyPair) error
	SubscribeBBO(sm []string) error
}

type SubAccount

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

type TickSize

type TickSize struct {
	InstrumentID    string
	UnderlyingIndex string
	QuoteCurrency   string
	PriceTickSize   float64 //下单价格精度
	AmountTickSize  float64 //数量精度
}

type Ticker

type Ticker struct {
	Pair cons.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 TransferParameter

type TransferParameter struct {
	Currency       string  `json:"currency"`
	From           int     `json:"from"`
	To             int     `json:"to"`
	Amount         float64 `json:"amount"`
	SubAccount     string  `json:"sub_account"`
	InstrumentId   string  `json:"instrument_id"`
	ToInstrumentId string  `json:"to_instrument_id"`
}

type WalletApi

type WalletApi interface {
	//获取钱包资产
	GetAccount() (*Account, error)
	//提币
	Withdrawal(param WithdrawParameter) (withdrawId string, err error)
	//划转资产
	Transfer(param TransferParameter) error
	//获取提币记录
	GetWithDrawHistory(currency *cons.Currency) ([]DepositWithdrawHistory, error)
	//获取充值记录
	GetDepositHistory(currency *cons.Currency) ([]DepositWithdrawHistory, error)
}

type WithdrawParameter

type WithdrawParameter struct {
	Currency    string  `json:"currency"`
	Amount      float64 `json:"amount,string"`
	Destination int     `json:"destination"` //提币到(2:OKCoin国际 3:OKEx 4:数字货币地址)
	ToAddress   string  `json:"to_address"`
	TradePwd    string  `json:"trade_pwd"`
	Fee         string  `json:"fee"`
}

Jump to

Keyboard shortcuts

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