Crates.io | barter |
lib.rs | barter |
version | 0.8.16 |
source | src |
created_at | 2021-03-27 15:13:42.9764 |
updated_at | 2024-10-20 17:19:44.781245 |
description | Framework for building event-driven live-trading & backtesting engines |
homepage | |
repository | https://github.com/barter-rs/barter-rs |
max_upload_size | |
id | 374235 |
size | 391,691 |
Barter is an open-source Rust framework for building event-driven live-trading & backtesting systems. Algorithmic trade with the peace of mind that comes from knowing your strategies have been backtested with a near-identical trading Engine. It is:
See: Barter-Data
, Barter-Integration
& Barter-Execution
Barter is an open-source Rust framework for building event-driven live-trading & backtesting systems. It provides a high-performance, easy to customise trading Engine that enables backtesting strategies on a near-identical system to live trading. The Engine can be controlled by issuing Commands over the Engine's command_tx. Similarly, the Engine's Events can be listened to using the event_rx (useful for event-sourcing). At a high level, it provides several de-coupled components that interact via a set of traits:
Barter-Data
WebSocket
integrations to provide live exchange data (ie/ trades, candles, etc).#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Setup Logger & Load Config For Engine & Trader Instances Here
// Create channel to distribute Commands to the Engine & it's Traders (eg/ Command::Terminate)
let (command_tx, command_rx) = mpsc::channel(20);
// Create Event channel to listen to all Engine Events in real-time
let (event_tx, event_rx) = mpsc::unbounded_channel();
let event_tx = EventTx::new(event_tx);
// Generate unique identifier to associate an Engine's components
let engine_id = Uuid::new_v4();
// Create the Market(s) to be traded on (1-to-1 relationship with a Trader)
let market = Market::new("binance", ("btc", "usdt", InstrumentKind::Spot));
// Build global shared-state MetaPortfolio (1-to-1 relationship with an Engine)
let portfolio = Arc::new(Mutex::new(
MetaPortfolio::builder()
.engine_id(engine_id)
.markets(vec![market.clone()])
.starting_cash(10_000.0)
.repository(InMemoryRepository::new())
.allocation_manager(DefaultAllocator { default_order_value: 100.0 })
.risk_manager(DefaultRisk {})
.statistic_config(StatisticConfig {
starting_equity: 10_000.0,
trading_days_per_year: 365,
risk_free_return: 0.0,
})
.build_and_init()
.expect("failed to build & initialise MetaPortfolio"),
));
// Build Trader(s)
let mut traders = Vec::new();
// Create channel for each Trader so the Engine can distribute Commands to it
let (trader_command_tx, trader_command_rx) = mpsc::channel(10);
traders.push(
Trader::builder()
.engine_id(engine_id)
.market(market.clone())
.command_rx(trader_command_rx)
.event_tx(event_tx.clone())
.portfolio(Arc::clone(&portfolio))
.data(historical::MarketFeed::new([test_util::market_candle].into_iter()))
.strategy(RSIStrategy::new(StrategyConfig { rsi_period: 14 }))
.execution(SimulatedExecution::new(ExecutionConfig {
simulated_fees_pct: Fees {
exchange: 0.1,
slippage: 0.05,
network: 0.0,}
}))
.build()
.expect("failed to build trader")
);
// Build Engine (1-to-many relationship with Traders)
// Create HashMap<Market, trader_command_tx> so Engine can route Commands to Traders
let trader_command_txs = HashMap::from_iter([(market, trader_command_tx)]);
let engine = Engine::builder()
.engine_id(engine_id)
.command_rx(command_rx)
.portfolio(portfolio)
.traders(traders)
.trader_command_txs(trader_command_txs)
.statistics_summary(TradingSummary::init(StatisticConfig {
starting_equity: 1000.0,
trading_days_per_year: 365,
risk_free_return: 0.0
}))
.build()
.expect("failed to build engine");
// Listen to Engine Events & do something with them
tokio::spawn(listen_to_events(event_rx));
// --- Run Trading Session Until Remote Shutdown OR Data Feed ends naturally (ie/ backtest) ---
engine.run().await;
}
Firstly, see if the answer to your question can be found in the API Documentation. If the answer is not there, I'd be happy to help to Chat and try answer your question via Discord.
:tada: Thanks for your help in improving the barter ecosystem! Please do get in touch on the discord to discuss development, new features, and the future roadmap.
In addition to the Barter crate, the Barter project also maintains:
Barter-Integration
: High-performance, low-level framework for composing flexible web integrations.Barter-Data
: High performance & normalised WebSocket integration for leading cryptocurrency exchanges - batteries
included.Barter-Execution
: In development and soon to be integrated.This project is licensed under the MIT license.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in Barter by you, shall be licensed as MIT, without any additional terms or conditions.