Overview
The okxAPI package provides an unofficial R wrapper for the Okx Exchange V5 API, supporting both REST and WebSocket interfaces. Designed for developers and traders, it simplifies integration with Okx's cryptocurrency trading platform while maintaining full functionality.
Key Features
- Comprehensive API Coverage: Supports all major Okx V5 endpoints
- Dual Protocol Support: Works with both REST and WebSocket APIs
- Developer-Friendly: Object-oriented implementation using R6 classes
- MIT Licensed: Open-source package with permissive usage
Installation
install.packages("okxAPI")Core Components
REST API Classes
- restAPI: Base class for all API interactions
- restAPIaccount: Account-specific endpoints
- restAPImarket: Market data endpoints
- restAPItrade: Trading operations
WebSocket Classes
- websocketAPIprivate: Private channels (authenticated)
- websocketAPIpublic: Public market data streams
Getting Started
REST API Example
library(okxAPI)
api <- restAPImarket$new(api_key, secret_key, passphrase)
candles <- api$candles("BTC-USDT-SWAP", bar = "1m")WebSocket Example
ws <- websocketAPIpublic$new()
ws$connect()
ws$send(subscribe_message)๐ Explore advanced trading strategies with Okx
Function Reference
Market Data Endpoints
get_history_candles(): Retrieve historical candlestick datacandles(): Real-time market candleshistory_candles(): Extended historical data
Account Management
balance(): Check account balancespositions(): View current positionspositions_history(): Historical position data
Trading Operations
order(): Place new orderscancel_order(): Cancel existing orders
Performance Considerations
| Parameter | Recommended Value | Notes |
|---|---|---|
count | โค100 | Optimal for single request |
period | 10 | Balances performance/completeness |
bar | "1m" | Standard timeframe |
FAQ
Q: How do I handle API rate limits?
A: The package automatically respects Okx's rate limits. Implement custom delays if making sequential requests.
Q: Can I use this for live trading?
A: Yes, but test thoroughly with demo accounts first using the simulate = TRUE parameter.
Q: What's the difference between REST and WebSocket APIs?
A: REST is request-response for occasional calls, while WebSocket maintains persistent connections for real-time data.
๐ Learn more about API best practices
Q: How do I troubleshoot connection issues?
A: Check your API keys, internet connection, and verify the service status on Okx's official status page.
Advanced Usage
For high-frequency trading applications, consider:
- Implementing connection pooling
- Caching frequent API responses
- Using background processes for WebSocket connections
Dependencies
The package requires:
- R6
- httr
- jsonlite
- websocket
- digest
License
MIT License - free for both personal and commercial use.
Support
For bug reports or feature requests, please file issues on the package's CRAN page.