Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MetaTrader5-RS

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.

Features

  • 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

Quick Start

Prerequisites

  • MetaTrader5 Platform - Must have MT5 installed and running
  • Python Integration - MT5 must have Python API enabled
  • Rust - Latest stable Rust toolchain

Installation

Add this to your Cargo.toml:

[dependencies]
metatrader5-rs = "0.1.0"

Basic Usage

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(())
}

Core Components

Connection Management

let mut mt5 = MT5::new();
mt5.initialize(None, None, None, None, None)?;

Market Data

// 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 & Positions

// 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)?;

Examples

Run the Demo

cargo run --bin mt5_demo

Performance Testing

# 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_diagnostics

Data Types

Core Types

  • SymbolInfo - Complete symbol metadata and trading parameters
  • Tick - Real-time tick data with timestamps and prices
  • Rate - OHLCV candlestick data with volume information
  • AccountInfo - Account balance, equity, margin, and trading status
  • Position - Open position details with profit tracking
  • Order - Pending order information and status

Trading Types

  • TradeRequest - Complete trade request structure
  • TradeResult - Trade execution result with status codes

Error Handling

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),
}

Requirements

  • MetaTrader5 terminal running with Python API enabled
  • Python environment accessible to the Rust process
  • Valid MT5 account (demo or live)

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

About

MetaTrader5 rust binding

Resources

Stars

9 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages