Files
wickra/examples/rust/src/bin/backtest.rs
T
kingchenc 747d1a5b1b examples: move Rust examples into a top-level examples/rust/ crate
The three Rust examples (backtest, fetch_btcusdt, live_binance) used to
live each in their own crate's examples/ dir, splitting the example set
across crates and burying it inside the source tree. Move them into a new
workspace member crate at `examples/rust/` (package `wickra-examples`,
`publish = false`) so all language examples sit under one top-level
`examples/<lang>/` tree.

* `examples/rust/Cargo.toml` declares the per-binary deps (wickra,
  wickra-data with the `live-binance` feature always on, serde_json, tokio
  for the macro and current-thread runtime).
* `examples/rust/src/bin/{backtest,fetch_btcusdt,live_binance}.rs` are the
  three migrated binaries; their doc-comments and the fetch_btcusdt output
  path are updated for the new location and run command
  (`cargo run -p wickra-examples --bin <name>`).
* Workspace `Cargo.toml` lists the new member; the now-empty
  `[dev-dependencies]` extras (`wickra`, `tokio` in wickra-data and
  `serde_json` in wickra) that existed only for these examples are dropped.
* The `[[example]] live_binance` table is removed from wickra-data's
  manifest since the file moved out.
* README "Languages" + project-layout, examples/README.md, Quickstart-Rust
  and Data-Layer are pointed at the new paths and commands.

`cargo build -p wickra-examples` and `cargo run --release -p wickra-examples
--bin backtest -- examples/data/btcusdt-1d.csv` both succeed; the rest of
the workspace (core, data, wickra) builds, clippies (`--all-targets -D
warnings`) and tests (508 core + 28 data + 1 integration + 74+3+1
doctests) all stay green.
2026-05-23 00:07:07 +02:00

96 lines
2.8 KiB
Rust

//! Rust example: backtest a basket of indicators against a CSV file.
//!
//! Build with:
//! ```text
//! cargo run --release -p wickra-examples --bin backtest -- path/to/ohlcv.csv
//! ```
use std::env;
use wickra::{Adx, Atr, BatchExt, BollingerBands, Ema, Indicator, MacdIndicator, Obv, Rsi};
use wickra_data::csv::CandleReader;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let args: Vec<String> = env::args().collect();
let path = args.get(1).ok_or("usage: backtest <ohlcv.csv>")?;
let mut reader = CandleReader::open(path)?;
let candles = reader.read_all()?;
if candles.is_empty() {
return Err("CSV is empty".into());
}
let n = candles.len();
let closes: Vec<f64> = candles.iter().map(|c| c.close).collect();
let rsi = Rsi::new(14)?.batch(&closes);
let ema = Ema::new(20)?.batch(&closes);
let bb = BollingerBands::classic().batch(&closes);
let macd = MacdIndicator::classic().batch(&closes);
let mut atr = Atr::new(14)?;
let atr_series: Vec<_> = candles.iter().map(|c| atr.update(*c)).collect();
let mut adx = Adx::new(14)?;
let adx_series: Vec<_> = candles.iter().map(|c| adx.update(*c)).collect();
let mut obv = Obv::new();
let obv_series: Vec<_> = candles.iter().map(|c| obv.update(*c)).collect();
let last_rsi = rsi
.iter()
.rev()
.flatten()
.next()
.copied()
.unwrap_or(f64::NAN);
let last_ema = ema
.iter()
.rev()
.flatten()
.next()
.copied()
.unwrap_or(f64::NAN);
let last_bb = bb.iter().rev().flatten().next().copied();
let last_macd = macd.iter().rev().flatten().next().copied();
let last_atr = atr_series
.iter()
.rev()
.flatten()
.next()
.copied()
.unwrap_or(f64::NAN);
let last_adx = adx_series.iter().rev().flatten().next().copied();
let last_obv = obv_series
.iter()
.rev()
.flatten()
.next()
.copied()
.unwrap_or(f64::NAN);
println!("backtest summary for {path} ({n} bars)");
println!(" RSI(14) = {last_rsi:>9.4}");
println!(" EMA(20) = {last_ema:>9.4}");
if let Some(bb) = last_bb {
println!(
" BB(20,2) upper={:>9.4} middle={:>9.4} lower={:>9.4} sd={:>8.4}",
bb.upper, bb.middle, bb.lower, bb.stddev
);
}
if let Some(m) = last_macd {
println!(
" MACD macd={:>9.4} signal={:>9.4} hist={:>9.4}",
m.macd, m.signal, m.histogram
);
}
println!(" ATR(14) = {last_atr:>9.4}");
if let Some(a) = last_adx {
println!(
" ADX(14) +DI={:>6.2} -DI={:>6.2} ADX={:>6.2}",
a.plus_di, a.minus_di, a.adx
);
}
println!(" OBV = {last_obv:>14.2}");
Ok(())
}