Documentation/Rust API
⚙️

Rust API

High-performance Rust implementation with type safety, async runtime, and zero-cost abstractions

📦 Installation

Add to your Cargo.toml:

[dependencies]
binary-options-tools = "2.0"
tokio = { version = "1.0", features = ["full"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

Requires Rust 1.70 or higher

🚀 Quick Start

main.rs
use binary_options_tools::PocketOption;
use tokio;
use std::time::Duration;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize connection
    let api = PocketOption::new("your-session-id").await?;
    
    // Wait for connection
    tokio::time::sleep(Duration::from_secs(5)).await;
    
    // Get balance
    let balance = api.balance().await?;
    println!("Balance: ${}", balance);
    
    // Place a trade
    let (trade_id, trade_data) = api.buy(
        "EURUSD_otc",
        1.0,
        60,
        Some("call")
    ).await?;
    println!("Trade ID: {}", trade_id);
    
    // Wait and check result
    tokio::time::sleep(Duration::from_secs(65)).await;
    let won = api.check_win(trade_id).await?;
    println!("Won: {}", won);
    
    Ok(())
}

✨ Key Features

Type Safety

Compile-time type checking prevents runtime errors. All API responses are strongly typed.

Zero-Cost Abstractions

High-level API with no performance overhead. As fast as hand-written low-level code.

Memory Safety

Rust's ownership system guarantees memory safety without garbage collection.

Async Runtime

Built on Tokio for efficient async I/O and concurrent operations.

🔧 API Methods

Trading

async fn buy(&self, asset: &str, amount: f64, time: u32, action: Option<&str>)

Returns: Result<(i64, TradeData)>

async fn sell(&self, asset: &str, amount: f64, time: u32)

Returns: Result<(i64, TradeData)>

async fn check_win(&self, trade_id: i64)

Returns: Result<bool>

Account

async fn balance(&self)

Returns: Result<f64>

async fn profile(&self)

Returns: Result<ProfileData>

⚠️ Error Handling

use binary_options_tools::{PocketOption, Error};

match api.balance().await {
    Ok(balance) => println!("Balance: ${}", balance),
    Err(Error::Unauthorized) => eprintln!("Invalid SSID"),
    Err(Error::ConnectionError(e)) => eprintln!("Connection failed: {}", e),
    Err(e) => eprintln!("Error: {}", e),
}

// Or use the ? operator
let balance = api.balance().await?;