A comprehensive Rust library for MetaTrader5 integration with Python bindings. This library provides complete access to MT5's trading functionality, market data, and account management through a clean, type-safe Rust API.
- Complete MT5 Integration - Full MetaTrader5 API access through Rust
- Python Bindings - PyO3 integration for seamless Python interoperability
- Real-time Market Data - Live ticks, symbol information, and price feeds
- Trading Operations - Order placement, position management, trade execution
- Historical Data - Comprehensive historical data retrieval (ticks and rates)
- Multi-timeframe Support - All standard timeframes from M1 to Monthly
- Account Management - Real-time account information and monitoring
- Terminal Information - MT5 terminal status and version details
- Async Operations - Tokio-based concurrent trading operations
- Robust Error Handling - Comprehensive error handling for trading scenarios
- Type Safety - Strongly typed data structures with Serde serialization
- MetaTrader5 Platform - Must have MT5 installed and running
- Python Integration - MT5 must have Python API enabled
- Rust - Latest stable Rust toolchain
Add this to your Cargo.toml:
[dependencies]
metatrader5-rs = "0.1.0"use metatrader5_rs::*;
fn main() -> MT5Result<()> {
let mut mt5 = MT5::new();
// Initialize connection
if mt5.initialize(None, None, None, None, None)? {
println!("Connected to MT5!");
// Get account info
if let Some(account) = mt5.account_info()? {
println!("Balance: {:.2}", account.balance);
}
// Get current price
if let Some(tick) = mt5.symbol_info_tick("EURUSD")? {
println!("EURUSD: {:.5} / {:.5}", tick.bid, tick.ask);
}
// Get historical data
let rates = mt5.copy_rates_from_pos("EURUSD", TIMEFRAME_H1, 0, 100)?;
println!("Retrieved {} H1 candles", rates.len());
}
Ok(())
}let mut mt5 = MT5::new();
mt5.initialize(None, None, None, None, None)?;// Current prices
let tick = mt5.symbol_info_tick("EURUSD")?;
// Historical ticks
let ticks = mt5.copy_ticks_from("EURUSD", datetime, 1000, COPY_TICKS_ALL)?;
// Historical rates (OHLCV)
let rates = mt5.copy_rates_from("EURUSD", TIMEFRAME_H1, datetime, 100)?;// Account information
let account = mt5.account_info()?;
// Open positions
let positions = mt5.positions_get(None, None, None)?;
// Pending orders
let orders = mt5.orders_get(None, None, None)?;cargo run --bin mt5_demo# Test tick data performance
cargo run --bin performance_ticks
# Test historical rates performance
cargo run --bin performance_rates
# Connection stress testing
cargo run --bin stress_test
# Account diagnostics
cargo run --bin account_diagnosticsSymbolInfo- Complete symbol metadata and trading parametersTick- Real-time tick data with timestamps and pricesRate- OHLCV candlestick data with volume informationAccountInfo- Account balance, equity, margin, and trading statusPosition- Open position details with profit trackingOrder- Pending order information and status
TradeRequest- Complete trade request structureTradeResult- Trade execution result with status codes
The library uses comprehensive error handling with the MT5Result<T> type:
match mt5.symbol_info("INVALID") {
Ok(Some(symbol)) => println!("Symbol found: {}", symbol.name),
Ok(None) => println!("Symbol not found"),
Err(MT5Error::SymbolNotFound { symbol }) => println!("Symbol {} not found", symbol),
Err(e) => println!("Error: {}", e),
}- MetaTrader5 terminal running with Python API enabled
- Python environment accessible to the Rust process
- Valid MT5 account (demo or live)
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Contributions are welcome! Please feel free to submit a Pull Request.