Move Rust examples into their crates so cargo publish picks them up
The previous `[[example]] path = "../../examples/rust/..."` entries pointed at files outside the crate root. cargo only packages files inside the crate directory, so the examples would have been silently dropped on publish. Moving them to crates/wickra/examples/ and crates/wickra-data/examples/ keeps them as part of the published package and lets cargo discover them automatically (no [[example]] override needed). README and project-layout section updated.
This commit is contained in:
@@ -1,95 +0,0 @@
|
||||
//! Rust example: backtest a basket of indicators against a CSV file.
|
||||
//!
|
||||
//! Build with:
|
||||
//! ```text
|
||||
//! cargo run --release --example 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(())
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
//! Connect to Binance, run incremental RSI on the closed kline events.
|
||||
//!
|
||||
//! Build with:
|
||||
//! ```text
|
||||
//! cargo run --release --example live_binance --features wickra-data/live-binance -- BTCUSDT
|
||||
//! ```
|
||||
//!
|
||||
//! The example prints a line per bar close. Hit Ctrl+C to stop.
|
||||
|
||||
use std::env;
|
||||
|
||||
use wickra::{Indicator, Rsi};
|
||||
use wickra_data::live::binance::{BinanceKlineStream, Interval};
|
||||
|
||||
#[tokio::main(flavor = "current_thread")]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
let symbol = args
|
||||
.get(1)
|
||||
.cloned()
|
||||
.unwrap_or_else(|| "BTCUSDT".to_string());
|
||||
|
||||
let mut stream =
|
||||
BinanceKlineStream::connect(std::slice::from_ref(&symbol), Interval::OneMinute).await?;
|
||||
let mut rsi = Rsi::new(14)?;
|
||||
println!("Listening for {symbol} 1m closes…");
|
||||
|
||||
while let Some(event) = stream.next_event().await? {
|
||||
if !event.is_closed {
|
||||
continue;
|
||||
}
|
||||
let close = event.candle.close;
|
||||
let v = rsi.update(close);
|
||||
if let Some(value) = v {
|
||||
println!("{} close={:.4} rsi={:.2}", event.symbol, close, value);
|
||||
} else {
|
||||
println!("{} close={:.4} rsi=…warmup", event.symbol, close);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user