use std::thread;
use std::time::Duration;
use detrix_rs::{self as detrix, Config};
use rand::Rng;
fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt()
.with_env_filter("detrix=info")
.init();
println!("Initializing Detrix client...");
detrix::init(Config {
name: Some("trade-bot".to_string()),
..Config::default()
})?;
let status = detrix::status();
println!("Control plane: http://127.0.0.1:{}", status.control_port);
println!("\nTrading bot started - runs forever until Ctrl+C");
println!("Add metrics with Detrix to observe values!\n");
let symbols = vec!["BTCUSD", "ETHUSD", "SOLUSD"];
let mut rng = rand::thread_rng();
let mut iteration = 0;
loop {
iteration += 1;
let symbol = symbols[rng.gen_range(0..symbols.len())];
let quantity: i32 = rng.gen_range(1..=50);
let price: f64 = rng.gen_range(100.0..=1000.0);
let order_id = place_order(symbol, quantity, price);
let entry_price = price;
let current_price = price * rng.gen_range(0.95..=1.05);
let pnl = calculate_pnl(entry_price, current_price, quantity);
let _ = order_id;
let _ = pnl;
println!(" -> P&L: ${:.2} (iteration {})", pnl, iteration);
thread::sleep(Duration::from_secs(3));
}
}
fn place_order(symbol: &str, quantity: i32, price: f64) -> i32 {
let order_id = rand::thread_rng().gen_range(1000..=9999);
let total = quantity as f64 * price;
println!(
"Order #{}: {} x{} @ ${:.2} = ${:.2}",
order_id, symbol, quantity, price, total
);
order_id
}
fn calculate_pnl(entry_price: f64, current_price: f64, quantity: i32) -> f64 {
(current_price - entry_price) * quantity as f64
}