market_okx
A Dart package for accessing OKX market data. Part of the Coin Galaxy ecosystem.
Features
- Fetch market instruments (spot, futures, perpetual swaps, margin, options)
- Get historical OHLCV (candlestick) data
- Support for multiple timeframes (1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 8h, 12h, 1d, 2d, 3d, 1w, 1M)
- Automatic pagination for large data requests
- Automatic endpoint selection for historical vs recent data
- Type-safe implementation with
market_interface
Installation
Add this to your package's pubspec.yaml file:
dependencies:
market_okx: ^0.0.1
Then run:
dart pub get
Usage
Basic Example
import 'package:market_okx/market_okx.dart';
import 'package:market_interface/market_interface.dart';
void main() async {
// Create a market instance
final market = MarketOkx();
try {
// Get all spot instruments
final instruments = await market.getInstruments(
type: InstrumentType.spot,
);
print('Available instruments: ${instruments.length}');
// Get candlestick data
final candles = await market.getKlineHistory(
instrument: 'BTC-USDT',
interval: Interval.$1h,
limit: 100,
);
print('Fetched ${candles.length} candles');
// Access OHLCV data
for (final candle in candles.take(5)) {
print('Open: ${candle.open}, Close: ${candle.close}, Volume: ${candle.volume}');
}
} finally {
// Don't forget to dispose
market.dispose();
}
}
Fetching Historical Data
// Fetch data from a specific time period
final endTime = DateTime.now().subtract(const Duration(days: 7));
final candles = await market.getKlineHistory(
instrument: 'ETH-USDT',
interval: Interval.$1h,
limit: 500,
endTime: endTime,
);
Supported Intervals
final intervals = market.getSupportedIntervals();
// Returns: [1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 8h, 12h, 1d, 2d, 3d, 1w, 1M]
Supported Instrument Types
InstrumentType.spot- Spot trading pairsInstrumentType.perpetual- Perpetual swapsInstrumentType.futures- Futures contractsInstrumentType.margin- Margin tradingInstrumentType.options- Options contracts
API Limits
- For recent data (within 3 intervals of current time): maximum 1440 candles
- For historical data: no limit (automatically uses history-candles endpoint)
- Single request limit: 300 candles (automatically paginated for larger requests)
Dependencies
This package depends on:
market_interface- Common interface for market data providersfinance_kline_core- Core types for financial datadecimal- Precise decimal arithmetichttp- HTTP client
Testing
Run tests with:
dart test
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
- GitHub Issues: https://github.com/normidar/market_okx/issues
- Twitter: @normidar
- GitHub Sponsors: Sponsor this project