Skip to main content

run

Function run 

Source
pub fn run(spec: &StrategySpec, candles: &[Candle]) -> Result<BacktestReport>
Expand description

Run a backtest of spec over candles with the default capital.

use wickra_backtest_core::{run, Candle, StrategySpec};

let spec = StrategySpec::parse(
    r#"{"symbol":"x","timeframe":"1h","indicators":{},
        "entry":{"gt":[{"price":"close"},100]},
        "exit":{"lt":[{"price":"close"},100]},
        "sizing":{"type":"fixed_qty","qty":1}}"#,
)?;
let bar = |time, open: f64, close: f64| Candle {
    time,
    open,
    high: open.max(close),
    low: open.min(close),
    close,
    volume: 0.0,
};
let report = run(&spec, &[bar(0, 100.0, 101.0), bar(1, 102.0, 103.0), bar(2, 104.0, 97.0)])?;

// The entry signal fires on bar 0 and fills at bar 1's open, look-ahead-free.
assert_eq!(report.trades.len(), report.metrics.num_trades as usize);
assert_eq!(report.symbol, "x");

ยงErrors

Returns an error if the spec is invalid, the candle series is empty, or the spec prices against a feed this entry point cannot supply.