| Crates.io | candles-rs |
| lib.rs | candles-rs |
| version | 0.1.4 |
| created_at | 2025-09-27 09:07:38.170857+00 |
| updated_at | 2025-10-04 20:17:18.413644+00 |
| description | A Rust library for fetching candlestick (OHLCV) data from multiple cryptocurrency exchanges including Binance, OKX, Bybit, BloFin, and BingX. |
| homepage | |
| repository | https://github.com/Flicker-Finance/candles-rs |
| max_upload_size | |
| id | 1857109 |
| size | 90,712 |
A Rust library for fetching candlestick (OHLCV) data from multiple cryptocurrency exchanges. Built by Flicker Finance, this library provides a unified interface to access market data from major exchanges.
Add this to your Cargo.toml:
[dependencies]
candles-rs = "0.1.0"
use candles_rs::{
connections::Connection,
types::{Instrument, MarketType, Timeframe},
};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create an instrument configuration
let instrument = Instrument {
asset_id: "bitcoin".to_string(),
pair: "BTCUSDT".to_string(),
connection: Connection::Binance,
market_type: MarketType::Spot,
timeframe: Timeframe::H1,
};
// Fetch candlestick data
let candles = instrument.connection.get_candles(instrument).await?;
for candle in candles {
println!(
"Time: {}, Open: {}, High: {}, Low: {}, Close: {}, Volume: {}",
candle.timestamp, candle.open, candle.high,
candle.low, candle.close, candle.volume
);
}
Ok(())
}
https://www.binance.com/api/v3/klineshttps://fapi.binance.com/fapi/v1/klineshttps://www.okx.com/api/v5/market/candlespub enum Timeframe {
M3, // 3 minutes
M5, // 5 minutes
M15, // 15 minutes
M30, // 30 minutes
H1, // 1 hour
H4, // 4 hours
D1, // 1 day
W1, // 1 week
MN1, // 1 month
}
pub enum MarketType {
Spot, // Spot trading
Derivatives, // Futures/derivatives
}
pub struct Candle {
pub timestamp: i64, // Unix timestamp
pub open: f64, // Opening price
pub high: f64, // Highest price
pub low: f64, // Lowest price
pub close: f64, // Closing price
pub volume: f64, // Volume in base asset
}
The library uses a comprehensive error system:
pub enum CandlesError {
ConnectionNotFound(String), // Invalid exchange connection
ApiError(String), // API request failures
Reqwest(reqwest::Error), // HTTP client errors
Other(String), // General errors
}
use candles_rs::{connections::Connection, types::*};
// Fetch from OKX
let okx_instrument = Instrument {
asset_id: "BTC-USDT".to_string(),
pair: "BTC-USDT".to_string(),
connection: Connection::OKX,
market_type: MarketType::Spot,
timeframe: Timeframe::H4,
};
let candles = okx_instrument.connection.get_candles(okx_instrument).await?;
let timeframes = vec![
Timeframe::M15,
Timeframe::H1,
Timeframe::D1,
];
for timeframe in timeframes {
let instrument = Instrument {
asset_id: "ETHUSDT".to_string(),
pair: "ETHUSDT".to_string(),
connection: Connection::Binance,
market_type: MarketType::Spot,
timeframe,
};
let candles = instrument.connection.get_candles(instrument).await?;
println!("Fetched {} candles for {:?}", candles.len(), timeframe);
}
Contributions are welcome! Please feel free to submit a Pull Request.
This project is open source and available under the MIT License.
This library is developed and maintained by Flicker Finance, a platform for cryptocurrency trading and market analysis.
Note: This library is for educational and development purposes. Always ensure you comply with each exchange's terms of service and rate limits when using their APIs.