okx

package
v0.2.57 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2026 License: MIT Imports: 16 Imported by: 0

README

OKX instFamily / uly 参数说明

以下说明以 BTC 合约为例,其他币种的合约同理。

  • uly 是指数,如:"BTC-USD",与盈亏结算和保证金币种 (settleCcy) 会存在一对多的关系。
  • instFamily 是交易品种,如:BTC-USD_UM,与盈亏结算和保证金币种 (settleCcy) 一一对应。
  • 以下表格详细展示了 uly, instFamily,settleCcy 和 instId 的对应关系。
合约类型 uly instFamily settleCcy 交割合约 instId 永续合约 instId
USDT 本位合约 BTC-USDT BTC-USDT USDT BTC-USDT-250808 BTC-USDT-SWAP
USDC 本位合约 BTC-USDC BTC-USDC USDC BTC-USDC-250808 BTC-USDC-SWAP
USD 本位合约 BTC-USD BTC-USD_UM USDⓈ BTC-USD_UM-250808 BTC-USD_UM-SWAP
币本位合约 BTC-USD BTC-USD BTC BTC-USD-250808 BTC-USD-SWAP

注意:

  1. USDⓈ 代表 USD 以及多种 USD 稳定币,如:USD, USDC, USDG。
  2. 盈亏结算和保证金币种指的获取交易产品基础信息(私有)接口返回的 settleCcy 字段。

Documentation

Index

Constants

View Source
const (
	HostPublic     = "public"
	HostPrivate    = "private"
	HostWsPublic   = "ws_public"
	HostWsPrivate  = "ws_private"
	HostWsBusiness = "ws_business"
)
View Source
const (
	FldInstType        = "instType"
	FldInstId          = "instId"
	FldInstFamily      = "instFamily"
	FldUly             = "uly"
	FldMgnMode         = "mgnMode"
	FldTdMode          = "tdMode"
	FldPosSide         = "posSide"
	FldOrdType         = "ordType"
	FldSide            = "side"
	FldSz              = "sz"
	FldPx              = "px"
	FldOrdId           = "ordId"
	FldClOrdId         = "clOrdId"
	FldCcy             = "ccy"
	FldLever           = "lever"
	FldBar             = "bar"
	FldLimit           = "limit"
	FldAfter           = "after"
	FldBefore          = "before"
	FldBegin           = "begin"
	FldEnd             = "end"
	FldType            = "type"
	FldChannel         = "channel"
	FldTgtCcy          = "tgtCcy"
	FldNewSz           = "newSz"
	FldNewPx           = "newPx"
	FldReduceOnly      = "reduceOnly"
	FldCloseFraction   = "closeFraction"
	FldTag             = "tag"
	FldBanAmend        = "banAmend"
	FldPxAmendType     = "pxAmendType"
	FldStpMode         = "stpMode"
	FldAlgoId          = "algoId"
	FldAlgoClOrdId     = "algoClOrdId"
	FldTpTriggerPx     = "tpTriggerPx"
	FldTpOrdPx         = "tpOrdPx"
	FldSlTriggerPx     = "slTriggerPx"
	FldSlOrdPx         = "slOrdPx"
	FldTpTriggerPxType = "tpTriggerPxType"
	FldSlTriggerPxType = "slTriggerPxType"
	FldTriggerPx       = "triggerPx"
	FldOrderPx         = "orderPx"
	FldOrdPx           = "ordPx"
	FldCallbackRatio   = "callbackRatio"
	FldCallbackSpread  = "callbackSpread"
	FldActivePx        = "activePx"
	FldSzLimit         = "szLimit"
	FldPxLimit         = "pxLimit"
	FldTimeInterval    = "timeInterval"
	FldTradeQuoteCcy   = "tradeQuoteCcy"
)

OKX API field keys

View Source
const (
	WsChanTrades          = "trades"
	WsChanBooks           = "books"
	WsChanBooks5          = "books5"
	WsChanBalancePosition = "balance_and_position"
	WsChanPositions       = "positions"
	WsChanOrders          = "orders"
	WsChanOrdersAlgo      = "orders-algo" // Algo orders channel (trigger/conditional/oco/twap/move_order_stop)
	WsChanMarkPrice       = "mark-price"
	WsChanCandlePrefix    = "candle"
)

OKX WebSocket channel names

View Source
const (
	InstTypeSpot    = "SPOT"
	InstTypeMargin  = "MARGIN"
	InstTypeSwap    = "SWAP"
	InstTypeFutures = "FUTURES"
	InstTypeOption  = "OPTION"
)

OKX instType values

View Source
const (
	TgtCcyQuote = "quote_ccy"
	InstIdAny   = "ANY"
)

OKX special values

View Source
const (
	MethodPublicGetInstruments         = "publicGetInstruments"
	MethodMarketGetTicker              = "marketGetTicker"
	MethodMarketGetTickers             = "marketGetTickers"
	MethodMarketGetBooks               = "marketGetBooks"
	MethodMarketGetBooksFull           = "marketGetBooksFull"
	MethodMarketGetCandles             = "marketGetCandles"
	MethodMarketGetHistoryCandles      = "marketGetHistoryCandles"
	MethodPublicGetFundingRate         = "publicGetFundingRate"
	MethodPublicGetFundingRateHistory  = "publicGetFundingRateHistory"
	MethodPublicGetPositionTiers       = "publicGetPositionTiers"
	MethodAccountGetBalance            = "accountGetBalance"
	MethodAccountGetConfig             = "accountGetConfig"
	MethodAccountGetBills              = "accountGetBills"
	MethodAccountGetBillsArchive       = "accountGetBillsArchive"
	MethodAccountGetPositions          = "accountGetPositions"
	MethodAccountGetLeverageInfo       = "accountGetLeverageInfo"
	MethodAccountGetPositionTiers      = "accountGetPositionTiers"
	MethodAccountSetLeverage           = "accountSetLeverage"
	MethodTradePostOrder               = "tradePostOrder"
	MethodTradePostCancelOrder         = "tradePostCancelOrder"
	MethodTradePostAmendOrder          = "tradePostAmendOrder"
	MethodTradeGetOrder                = "tradeGetOrder"
	MethodTradeGetOrdersPending        = "tradeGetOrdersPending"
	MethodTradeGetOrdersHistory        = "tradeGetOrdersHistory"
	MethodTradeGetOrdersHistoryArchive = "tradeGetOrdersHistoryArchive"
	MethodTradePostOrderAlgo           = "tradePostOrderAlgo"
	MethodTradePostCancelAlgos         = "tradePostCancelAlgos"
	MethodTradePostAmendAlgos          = "tradePostAmendAlgos"
	MethodTradeGetOrderAlgo            = "tradeGetOrderAlgo"
	MethodTradeGetOrdersAlgoPending    = "tradeGetOrdersAlgoPending"
	MethodTradeGetOrdersAlgoHistory    = "tradeGetOrdersAlgoHistory"
)
View Source
const (
	TdModeCash = "cash"
)

OKX tdMode values

Variables

This section is empty.

Functions

func NewExchange

func NewExchange(options map[string]interface{}) (banexg.BanExchange, *errs.Error)

Types

type Balance

type Balance struct {
	UTime   string          `json:"uTime"`
	TotalEq string          `json:"totalEq"`
	Details []BalanceDetail `json:"details"`
}

Balance describes /account/balance response item.

type BalanceDetail

type BalanceDetail struct {
	Ccy       string `json:"ccy"`
	Eq        string `json:"eq"`
	CashBal   string `json:"cashBal"`
	AvailBal  string `json:"availBal"`
	FrozenBal string `json:"frozenBal"`
	AvailEq   string `json:"availEq"`
}

type Bill

type Bill struct {
	InstType string `json:"instType"`
	InstId   string `json:"instId"`
	BillId   string `json:"billId"`
	Type     string `json:"type"`
	SubType  string `json:"subType"`
	Ts       string `json:"ts"`
	BalChg   string `json:"balChg"`
	Pnl      string `json:"pnl"`
	Fee      string `json:"fee"`
	Ccy      string `json:"ccy"`
	TradeId  string `json:"tradeId"`
	Notes    string `json:"notes"`
}

Bill describes /account/bills response item.

type FundingRate

type FundingRate struct {
	InstType        string `json:"instType"`
	InstId          string `json:"instId"`
	FundingRate     string `json:"fundingRate"`
	FundingTime     string `json:"fundingTime"`
	NextFundingTime string `json:"nextFundingTime"`
	InterestRate    string `json:"interestRate"`
	Ts              string `json:"ts"`
}

FundingRate describes /public/funding-rate response item.

type FundingRateHistory

type FundingRateHistory struct {
	InstType     string `json:"instType"`
	InstId       string `json:"instId"`
	FundingRate  string `json:"fundingRate"`
	RealizedRate string `json:"realizedRate"`
	FundingTime  string `json:"fundingTime"`
	FormulaType  string `json:"formulaType"`
	Method       string `json:"method"`
}

FundingRateHistory describes /public/funding-rate-history response item.

type Instrument

type Instrument struct {
	InstType          string   `json:"instType"`
	InstId            string   `json:"instId"`
	InstFamily        string   `json:"instFamily"`
	Uly               string   `json:"uly"`
	BaseCcy           string   `json:"baseCcy"`
	QuoteCcy          string   `json:"quoteCcy"`
	SettleCcy         string   `json:"settleCcy"`
	CtVal             string   `json:"ctVal"`
	CtMult            string   `json:"ctMult"`
	CtType            string   `json:"ctType"`
	Lever             string   `json:"lever"`
	TickSz            string   `json:"tickSz"`
	LotSz             string   `json:"lotSz"`
	MinSz             string   `json:"minSz"`
	State             string   `json:"state"`
	ListTime          string   `json:"listTime"`
	ExpTime           string   `json:"expTime"`
	TradeQuoteCcyList []string `json:"tradeQuoteCcyList"`
}

Instrument describes /public/instruments response item.

type LeverageInfo

type LeverageInfo struct {
	Ccy     string `json:"ccy"`
	InstId  string `json:"instId"`
	MgnMode string `json:"mgnMode"`
	PosSide string `json:"posSide"`
	Lever   string `json:"lever"`
}

LeverageInfo describes /account/leverage-info response item.

type OKX

type OKX struct {
	*banexg.Exchange
	RecvWindow int
	// LeverageBrackets caches leverage tiers by symbol or instFamily.
	LeverageBrackets     map[string]*banexg.SymbolLvgBrackets
	LeverageBracketsLock deadlock.Mutex
	// WsAuthDone tracks login completion channels per client key.
	// Each channel receives nil on success or an error on failure.
	WsAuthDone map[string]chan *errs.Error
	// WsAuthed tracks whether each client has successfully logged in.
	WsAuthed map[string]bool
	// WsPendingRecons stores pending reconnection info to restore subs after login.
	WsPendingRecons map[string]*WsPendingRecon
	WsAuthLock      deadlock.Mutex
	// WsMyTradesChanKey stores the channel key for mytrades output.
	// Used to ensure algo orders (business endpoint) write to the same channel as regular orders (private endpoint).
	WsMyTradesChanKey string
}

func New

func New(options map[string]interface{}) (*OKX, *errs.Error)

func (*OKX) CalcMaintMargin

func (e *OKX) CalcMaintMargin(symbol string, cost float64) (float64, *errs.Error)

func (*OKX) CancelOrder

func (e *OKX) CancelOrder(id string, symbol string, params map[string]interface{}) (*banexg.Order, *errs.Error)

func (*OKX) CreateOrder

func (e *OKX) CreateOrder(symbol, odType, side string, amount, price float64, params map[string]interface{}) (*banexg.Order, *errs.Error)

func (*OKX) EditOrder

func (e *OKX) EditOrder(symbol, orderId, side string, amount, price float64, params map[string]interface{}) (*banexg.Order, *errs.Error)

func (*OKX) FetchAccountAccess added in v0.2.47

func (e *OKX) FetchAccountAccess(params map[string]interface{}) (*banexg.AccountAccess, *errs.Error)

func (*OKX) FetchAccountPositions

func (e *OKX) FetchAccountPositions(symbols []string, params map[string]interface{}) ([]*banexg.Position, *errs.Error)

func (*OKX) FetchBalance

func (e *OKX) FetchBalance(params map[string]interface{}) (*banexg.Balances, *errs.Error)

func (*OKX) FetchFundingRate

func (e *OKX) FetchFundingRate(symbol string, params map[string]interface{}) (*banexg.FundingRateCur, *errs.Error)

func (*OKX) FetchFundingRateHistory

func (e *OKX) FetchFundingRateHistory(symbol string, since int64, limit int, params map[string]interface{}) ([]*banexg.FundingRate, *errs.Error)

func (*OKX) FetchFundingRates

func (e *OKX) FetchFundingRates(symbols []string, params map[string]interface{}) ([]*banexg.FundingRateCur, *errs.Error)

func (*OKX) FetchIncomeHistory

func (e *OKX) FetchIncomeHistory(inType string, symbol string, since int64, limit int, params map[string]interface{}) ([]*banexg.Income, *errs.Error)

func (*OKX) FetchLastPrices

func (e *OKX) FetchLastPrices(symbols []string, params map[string]interface{}) ([]*banexg.LastPrice, *errs.Error)

func (*OKX) FetchOHLCV

func (e *OKX) FetchOHLCV(symbol, timeframe string, since int64, limit int, params map[string]interface{}) ([]*banexg.Kline, *errs.Error)

func (*OKX) FetchOpenOrders

func (e *OKX) FetchOpenOrders(symbol string, since int64, limit int, params map[string]interface{}) ([]*banexg.Order, *errs.Error)

func (*OKX) FetchOrder

func (e *OKX) FetchOrder(symbol, id string, params map[string]interface{}) (*banexg.Order, *errs.Error)

func (*OKX) FetchOrderBook

func (e *OKX) FetchOrderBook(symbol string, limit int, params map[string]interface{}) (*banexg.OrderBook, *errs.Error)

func (*OKX) FetchOrders

func (e *OKX) FetchOrders(symbol string, since int64, limit int, params map[string]interface{}) ([]*banexg.Order, *errs.Error)

func (*OKX) FetchPositions

func (e *OKX) FetchPositions(symbols []string, params map[string]interface{}) ([]*banexg.Position, *errs.Error)

func (*OKX) FetchTicker

func (e *OKX) FetchTicker(symbol string, params map[string]interface{}) (*banexg.Ticker, *errs.Error)

func (*OKX) FetchTickerPrice

func (e *OKX) FetchTickerPrice(symbol string, params map[string]interface{}) (map[string]float64, *errs.Error)

func (*OKX) FetchTickers

func (e *OKX) FetchTickers(symbols []string, params map[string]interface{}) ([]*banexg.Ticker, *errs.Error)

func (*OKX) GetLeverage

func (e *OKX) GetLeverage(symbol string, notional float64, account string) (float64, float64)

func (*OKX) Init

func (e *OKX) Init() *errs.Error

func (*OKX) LoadLeverageBrackets

func (e *OKX) LoadLeverageBrackets(reload bool, params map[string]interface{}) *errs.Error

func (*OKX) SetLeverage

func (e *OKX) SetLeverage(leverage float64, symbol string, params map[string]interface{}) (map[string]interface{}, *errs.Error)

func (*OKX) UnWatchMarkPrices

func (e *OKX) UnWatchMarkPrices(symbols []string, params map[string]interface{}) *errs.Error

func (*OKX) UnWatchOHLCVs

func (e *OKX) UnWatchOHLCVs(jobs [][2]string, params map[string]interface{}) *errs.Error

func (*OKX) UnWatchOrderBooks

func (e *OKX) UnWatchOrderBooks(symbols []string, params map[string]interface{}) *errs.Error

func (*OKX) UnWatchTrades

func (e *OKX) UnWatchTrades(symbols []string, params map[string]interface{}) *errs.Error

func (*OKX) WatchAccountConfig

func (e *OKX) WatchAccountConfig(params map[string]interface{}) (chan *banexg.AccountConfig, *errs.Error)

func (*OKX) WatchBalance

func (e *OKX) WatchBalance(params map[string]interface{}) (chan *banexg.Balances, *errs.Error)

func (*OKX) WatchMarkPrices

func (e *OKX) WatchMarkPrices(symbols []string, params map[string]interface{}) (chan map[string]float64, *errs.Error)

func (*OKX) WatchMyTrades

func (e *OKX) WatchMyTrades(params map[string]interface{}) (chan *banexg.MyTrade, *errs.Error)

func (*OKX) WatchOHLCVs

func (e *OKX) WatchOHLCVs(jobs [][2]string, params map[string]interface{}) (chan *banexg.PairTFKline, *errs.Error)

func (*OKX) WatchOrderBooks

func (e *OKX) WatchOrderBooks(symbols []string, limit int, params map[string]interface{}) (chan *banexg.OrderBook, *errs.Error)

func (*OKX) WatchPositions

func (e *OKX) WatchPositions(params map[string]interface{}) (chan []*banexg.Position, *errs.Error)

func (*OKX) WatchTrades

func (e *OKX) WatchTrades(symbols []string, params map[string]interface{}) (chan *banexg.Trade, *errs.Error)

type Order

type Order struct {
	InstType  string `json:"instType"`
	InstId    string `json:"instId"`
	OrdId     string `json:"ordId"`
	ClOrdId   string `json:"clOrdId"`
	Px        string `json:"px"`
	Sz        string `json:"sz"`
	Side      string `json:"side"`
	PosSide   string `json:"posSide"`
	OrdType   string `json:"ordType"`
	TdMode    string `json:"tdMode"`
	State     string `json:"state"`
	AvgPx     string `json:"avgPx"`
	AccFillSz string `json:"accFillSz"`
	Fee       string `json:"fee"`
	FeeCcy    string `json:"feeCcy"`
	CTime     string `json:"cTime"`
	UTime     string `json:"uTime"`
}

Order describes /trade/order response item.

type OrderBook

type OrderBook struct {
	Asks [][]string `json:"asks"`
	Bids [][]string `json:"bids"`
	Ts   string     `json:"ts"`
}

OrderBook describes /market/books response item.

type OrderResult

type OrderResult struct {
	OrdId   string `json:"ordId"`
	ClOrdId string `json:"clOrdId"`
	ReqId   string `json:"reqId"`
	Ts      string `json:"ts"`
	SCode   string `json:"sCode"`
	SMsg    string `json:"sMsg"`
}

OrderResult describes /trade/order or /trade/cancel-order result item.

type Position

type Position struct {
	InstType string `json:"instType"`
	InstId   string `json:"instId"`
	MgnMode  string `json:"mgnMode"`
	PosId    string `json:"posId"`
	PosSide  string `json:"posSide"`
	Pos      string `json:"pos"`
	AvgPx    string `json:"avgPx"`
	Upl      string `json:"upl"`
	Lever    string `json:"lever"`
	LiqPx    string `json:"liqPx"`
	MarkPx   string `json:"markPx"`
	Margin   string `json:"margin"`
	MgnRatio string `json:"mgnRatio"`
	Ccy      string `json:"ccy"`
	CTime    string `json:"cTime"`
	UTime    string `json:"uTime"`
}

Position describes /account/positions response item.

type PositionTier

type PositionTier struct {
	InstType     string `json:"instType"`
	InstId       string `json:"instId"`
	InstFamily   string `json:"instFamily"`
	Uly          string `json:"uly"`
	Tier         string `json:"tier"`
	MinSz        string `json:"minSz"`
	MaxSz        string `json:"maxSz"`
	Mmr          string `json:"mmr"`
	Imr          string `json:"imr"`
	MaxLever     string `json:"maxLever"`
	OptMgnFactor string `json:"optMgnFactor"`
	BaseMaxLoan  string `json:"baseMaxLoan"`
	QuoteMaxLoan string `json:"quoteMaxLoan"`
}

PositionTier describes /public/position-tiers response item.

type Ticker

type Ticker struct {
	InstType  string `json:"instType"`
	InstId    string `json:"instId"`
	Last      string `json:"last"`
	LastSz    string `json:"lastSz"`
	AskPx     string `json:"askPx"`
	AskSz     string `json:"askSz"`
	BidPx     string `json:"bidPx"`
	BidSz     string `json:"bidSz"`
	Open24h   string `json:"open24h"`
	High24h   string `json:"high24h"`
	Low24h    string `json:"low24h"`
	VolCcy24h string `json:"volCcy24h"`
	Vol24h    string `json:"vol24h"`
	SodUtc0   string `json:"sodUtc0"`
	SodUtc8   string `json:"sodUtc8"`
	Ts        string `json:"ts"`
}

Ticker describes /market/ticker(s) response item.

type WsAlgoOrder added in v0.2.46

type WsAlgoOrder struct {
	InstType    string `json:"instType"`
	InstId      string `json:"instId"`
	AlgoId      string `json:"algoId"`
	AlgoClOrdId string `json:"algoClOrdId"`
	ClOrdId     string `json:"clOrdId"`
	OrdId       string `json:"ordId"`
	Sz          string `json:"sz"`
	OrdType     string `json:"ordType"`
	Side        string `json:"side"`
	PosSide     string `json:"posSide"`
	TdMode      string `json:"tdMode"`
	State       string `json:"state"`
	Lever       string `json:"lever"`
	ActualSz    string `json:"actualSz"`
	ActualPx    string `json:"actualPx"`
	ActualSide  string `json:"actualSide"`
	TriggerPx   string `json:"triggerPx"`
	TriggerTime string `json:"triggerTime"`
	OrdPx       string `json:"ordPx"`
	TpTriggerPx string `json:"tpTriggerPx"`
	TpOrdPx     string `json:"tpOrdPx"`
	SlTriggerPx string `json:"slTriggerPx"`
	SlOrdPx     string `json:"slOrdPx"`
	ReduceOnly  string `json:"reduceOnly"`
	CTime       string `json:"cTime"`
	UTime       string `json:"uTime"`
}

WsAlgoOrder describes websocket "orders-algo" channel item.

type WsOrder

type WsOrder struct {
	Order
	FillPx      string `json:"fillPx"`
	FillSz      string `json:"fillSz"`
	FillTime    string `json:"fillTime"`
	TradeId     string `json:"tradeId"`
	ReduceOnly  string `json:"reduceOnly"`
	FillFee     string `json:"fillFee"`
	FillFeeCcy  string `json:"fillFeeCcy"`
	ExecType    string `json:"execType"`
	AlgoClOrdId string `json:"algoClOrdId"` // Client-defined algo order ID when algo order triggers
	AlgoId      string `json:"algoId"`      // Algo order ID when algo order triggers
}

WsOrder describes websocket "orders" channel item.

type WsPendingRecon added in v0.2.46

type WsPendingRecon struct {
	Client *banexg.WsClient
	ConnID int
	Keys   []string
}

WsPendingRecon stores info needed to restore subscriptions after reconnection login.

Jump to

Keyboard shortcuts

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