The OKX package provides a robust Go interface for interacting with the OKX cryptocurrency exchange API. This guide explores the package's structure, key components, and functionality to help developers integrate OKX trading capabilities into their applications.
Core Components
Variables
The package defines several important variables:
var (
ApiAddr = "https://www.okx.com/join/BLOCKSTAR"
WSOkexPUbilc = "wss://wsaws.okx.com:8443/ws/v5/public"
WSOkexPrivate = "wss://wsaws.okx.com:8443/ws/v5/private"
TypeSPOT = "SPOT"
TypeMARGIN = "MARGIN"
TypeSWAP = "SWAP"
TypeFUTURES = "FUTURES"
TypeOption = "OPTION"
PosNetMode = "net_mode"
PosLongShortMode = "long_short_mode"
)Key Functions
NewOkexExchange: Initializes a new OKX exchange instanceNewOkxTrader: Creates a new trading client for OKX
Account Management Structures
AccountBalance
type AccountBalance struct {
AdjEq string `json:"adjEq"`
Details []struct {
AvailBal string `json:"availBal"`
AvailEq string `json:"availEq"`
// ... additional fields
} `json:"details"`
// ... remaining fields
}AccountPosition
type AccountPosition struct {
Adl string `json:"adl"`
AvailPos string `json:"availPos"`
AvgPx string `json:"avgPx"`
// ... additional fields
}Order Management
AlgoOrder
type AlgoOrder struct {
ActualPx string `json:"actualPx"`
ActualSide string `json:"actualSide"`
// ... additional fields
}OrderNormal
type OrderNormal struct {
AccFillSz string `json:"accFillSz"`
AmendResult string `json:"amendResult"`
// ... additional fields
}Trading Interface
The OkxTrader type provides the main trading functionality:
type OkxTrader struct {
Name string
}Key methods include:
CancelAllOrdersCancelOrderProcessOrderGetKlineSymbolsStart/Stop
Market Data Structures
Instrument
type Instrument struct {
InstType string `json:"instType"`
InstID string `json:"instId"`
// ... additional fields
}CandleResp
type CandleResp struct {
Code string `json:"code"`
Data []Candle `json:"data"`
Msg string `json:"msg"`
}FAQ Section
How do I initialize the OKX trader?
Use the NewOkxTrader function with your exchange configuration and client name.
๐ Learn more about OKX integration
What types of orders does the package support?
The package supports normal orders, algorithmic orders, and position management.
How do I retrieve account balances?
Use the AccountBalance structure and relevant API calls through the trader interface.
Can I trade multiple instrument types?
Yes, the package supports SPOT, MARGIN, SWAP, FUTURES, and OPTION trading.
๐ Explore advanced trading features
What websocket connections are available?
The package provides both public and private websocket endpoints for real-time data.
How do I handle errors?
All responses include code and message fields for error handling and status checking.