Skip to main content

Crate trader_bot

Crate trader_bot 

Source
Expand description

Trader Bot - A configurable trading bot with multi-strategy support.

This crate provides a comprehensive trading bot framework with:

  • Multi-exchange support through a unified abstraction layer
  • Multiple trading strategies (balancing, scalping, holding)
  • Recursive strategy composition (balancer can balance between sub-strategies)
  • Multi-user, multi-account support
  • Market simulator for backtesting and testing

§Architecture

The crate follows Clean Architecture principles with clear separation:

  • domain: Core business types (Position, Wallet, Order, Money, Trade)
  • exchange: Exchange API abstraction layer (ExchangeProvider trait)
  • strategy: Trading strategies (balancing, scalping, holding)
  • adapters: Exchange-specific implementations (T-Bank, Binance, etc.)
  • simulator: Market simulation for testing
  • config: Configuration management for multi-user/multi-account setups

§Features

  • Portfolio Balancing: Rebalance portfolio to target allocations
  • Scalping Strategy: High-frequency buy-low sell-high trading
  • Holding Strategy: Configurable asset holding with position limits
  • Strategy Composition: Balance between multiple sub-strategies
  • Multi-Exchange: T-Bank, Binance, Interactive Brokers support
  • Multi-Account: Manage multiple accounts per user
  • Multi-User: Support multiple users with isolated configurations
  • Market Simulator: Full-featured simulator for backtesting

§Example

use trader_bot::{
    strategy::{BalancerEngine, BalancerConfig},
    domain::DesiredAllocation,
    simulator::SimulatedExchange,
};
use rust_decimal_macros::dec;
use std::sync::Arc;

#[tokio::main]
async fn main() {
    // Create a simulated exchange
    let exchange = SimulatedExchange::new("USD");
    exchange.add_instrument("AAPL", "Apple Inc.", dec!(150), 1);
    exchange.add_instrument("GOOGL", "Alphabet Inc.", dec!(2800), 1);
    exchange.create_account("my_account", dec!(100000));

    // Define target allocation
    let mut target = DesiredAllocation::new();
    target.set("AAPL", dec!(50));
    target.set("GOOGL", dec!(50));

    // Create and run balancer
    let exchange = Arc::new(exchange);
    let config = BalancerConfig::default();
    let mut engine = BalancerEngine::new(exchange, config);

    let result = engine.rebalance("my_account", &target).await;
    println!("Rebalance result: {:?}", result);
}

Modules§

adapters
Exchange adapters for connecting to real trading APIs.
cli
Command-line interface for the trading bot.
config
Configuration management for the trading bot.
domain
Domain types for the trader bot.
exchange
Exchange provider abstraction layer.
lino_args
lino-arguments - A unified configuration library for Rust.
prelude
Re-export commonly used types at the crate root.
simulator
Market simulator for testing trading strategies.
strategy
Trading strategies module.

Constants§

VERSION
Package version (matches Cargo.toml version).