finance-query 3.2.0

A Rust library for querying financial data
Documentation
// @generated by `cargo soothfast docs gen-tests`
// source: docs/library/backtesting.md line 423
#[cfg(feature = "backtesting")]
fn main() {
    use finance_query::backtesting::{BacktestConfig, BacktestEngine, SmaCrossover};

    // Deterministic synthetic candles — `Candle` is #[non_exhaustive] outside
    // the crate, so construct via serde. With live data: `chart.candles`.
    fn synthetic_candles(n: usize) -> Vec<finance_query::Candle> {
        (0..n)
            .map(|i| {
                let close = 100.0 + (i as f64 / 4.0).sin() * 8.0;
                serde_json::from_value(serde_json::json!({
                    "timestamp": 1_700_000_000_i64 + i as i64 * 86_400,
                    "open": close, "high": close + 1.0, "low": close - 1.0,
                    "close": close, "volume": 1_000_000_i64, "adjClose": close,
                }))
                .unwrap()
            })
            .collect()
    }

    let config = BacktestConfig::builder()
        .initial_capital(10_000.0)
        .commission_pct(0.001)
        .build()
        .unwrap();

    let engine = BacktestEngine::new(config);
    let result = engine
        .run("SYNTH", &synthetic_candles(1000), SmaCrossover::new(10, 20))
        .unwrap();

    // The oscillating series produces real crossovers and real trades
    assert!(result.metrics.total_trades > 0);
    println!(
        "{} trades, total return {:.2}%",
        result.metrics.total_trades, result.metrics.total_return_pct
    );
}

#[cfg(not(feature = "backtesting"))]
fn main() {}