wickra-backtest-core 0.1.1

Streaming-native backtest engine core (strategy spec, rules, execution, portfolio) built on wickra-core.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
//! Evaluation of the strategy DSL ([`Operand`] / [`Condition`]) against the
//! per-bar history. Operands resolve to a number (or `None` when an indicator
//! is still warming up); conditions resolve to a boolean (false when any
//! operand is missing). Cross conditions compare the current bar with the
//! previous one.

use std::collections::BTreeMap;
use std::sync::Arc;

use crate::data::Candle;
use crate::spec::{Condition, IntPredicate, Operand, OperandExpr, PriceField};

/// One bar's computed state: the bar plus every ready indicator's value.
#[derive(Debug, Clone)]
pub struct BarRow {
    /// The bar.
    pub candle: Candle,
    /// Values of indicators that have produced output this bar.
    ///
    /// The keys are shared with the engine rather than owned per row: a long run
    /// otherwise allocated one fresh `String` per value per bar, and cloning an
    /// `Arc<str>` is a refcount bump. Lookup is unchanged -- `Arc<str>` borrows
    /// as `str`, so `values.get("ema_fast")` still works.
    pub values: BTreeMap<Arc<str>, f64>,
}

/// Engine state the stateful conditions read.
#[derive(Debug, Clone, Copy)]
pub struct RuleState {
    /// Whether a position is currently open.
    pub in_position: bool,
    /// Bars since the current position was entered (`None` when flat).
    pub bars_since_entry: Option<u32>,
}

fn price(candle: &Candle, field: PriceField) -> f64 {
    match field {
        PriceField::Open => candle.open,
        PriceField::High => candle.high,
        PriceField::Low => candle.low,
        PriceField::Close => candle.close,
        PriceField::Volume => candle.volume,
        PriceField::Hlc3 => candle.hlc3(),
        PriceField::Ohlc4 => candle.ohlc4(),
    }
}

/// Evaluate an operand at bar `idx`, or `None` if it cannot be resolved.
pub fn eval_operand(op: &Operand, history: &[BarRow], idx: usize) -> Option<f64> {
    let row = history.get(idx)?;
    match op {
        Operand::Const(v) => Some(*v),
        // The engine inserts the primary value under the indicator name and each
        // field under "name.field", so a full-name lookup resolves both.
        Operand::Ref(name) => row.values.get(name.as_str()).copied(),
        Operand::Expr(expr) => match expr.as_ref() {
            OperandExpr::Price(field) => Some(price(&row.candle, *field)),
            OperandExpr::Prev((inner, n)) => {
                let i = idx.checked_sub(*n as usize)?;
                eval_operand(inner, history, i)
            }
            OperandExpr::Add((a, b)) => binary(a, b, history, idx, |x, y| x + y),
            OperandExpr::Sub((a, b)) => binary(a, b, history, idx, |x, y| x - y),
            OperandExpr::Mul((a, b)) => binary(a, b, history, idx, |x, y| x * y),
            OperandExpr::Div((a, b)) => binary(a, b, history, idx, |x, y| {
                if y.abs() < f64::EPSILON {
                    f64::NAN
                } else {
                    x / y
                }
            }),
        },
    }
}

fn binary(
    lhs: &Operand,
    rhs: &Operand,
    history: &[BarRow],
    idx: usize,
    combine: impl Fn(f64, f64) -> f64,
) -> Option<f64> {
    Some(combine(
        eval_operand(lhs, history, idx)?,
        eval_operand(rhs, history, idx)?,
    ))
}

fn compare(
    lhs: &Operand,
    rhs: &Operand,
    history: &[BarRow],
    idx: usize,
    predicate: impl Fn(f64, f64) -> bool,
) -> bool {
    match (
        eval_operand(lhs, history, idx),
        eval_operand(rhs, history, idx),
    ) {
        (Some(left), Some(right)) => predicate(left, right),
        _ => false,
    }
}

fn cross(a: &Operand, b: &Operand, history: &[BarRow], idx: usize, above: bool) -> bool {
    if idx == 0 {
        return false;
    }
    let (Some(an), Some(bn)) = (eval_operand(a, history, idx), eval_operand(b, history, idx))
    else {
        return false;
    };
    let (Some(ap), Some(bp)) = (
        eval_operand(a, history, idx - 1),
        eval_operand(b, history, idx - 1),
    ) else {
        return false;
    };
    if above {
        ap <= bp && an > bn
    } else {
        ap >= bp && an < bn
    }
}

fn int_pred(pred: IntPredicate, n: u32) -> bool {
    match pred {
        IntPredicate::Gt(k) => n > k,
        IntPredicate::Lt(k) => n < k,
        IntPredicate::Ge(k) => n >= k,
        IntPredicate::Le(k) => n <= k,
        IntPredicate::Eq(k) => n == k,
    }
}

/// Evaluate a condition at bar `idx`. Missing operands make a comparison false.
pub fn eval_condition(cond: &Condition, history: &[BarRow], idx: usize, state: RuleState) -> bool {
    match cond {
        Condition::Gt((a, b)) => compare(a, b, history, idx, |x, y| x > y),
        Condition::Lt((a, b)) => compare(a, b, history, idx, |x, y| x < y),
        Condition::Ge((a, b)) => compare(a, b, history, idx, |x, y| x >= y),
        Condition::Le((a, b)) => compare(a, b, history, idx, |x, y| x <= y),
        Condition::Eq((a, b)) => compare(a, b, history, idx, |x, y| (x - y).abs() < f64::EPSILON),
        Condition::Ne((a, b)) => compare(a, b, history, idx, |x, y| (x - y).abs() >= f64::EPSILON),
        Condition::CrossAbove((a, b)) => cross(a, b, history, idx, true),
        Condition::CrossBelow((a, b)) => cross(a, b, history, idx, false),
        Condition::Between((a, lo, hi)) => {
            match (
                eval_operand(a, history, idx),
                eval_operand(lo, history, idx),
                eval_operand(hi, history, idx),
            ) {
                (Some(x), Some(l), Some(h)) => l <= x && x <= h,
                _ => false,
            }
        }
        Condition::Rising((a, n)) => compare_prev(a, *n, history, idx, |now, then| now > then),
        Condition::Falling((a, n)) => compare_prev(a, *n, history, idx, |now, then| now < then),
        Condition::All(cs) => cs.iter().all(|c| eval_condition(c, history, idx, state)),
        Condition::Any(cs) => cs.iter().any(|c| eval_condition(c, history, idx, state)),
        Condition::Not(c) => !eval_condition(c, history, idx, state),
        Condition::InPosition(want) => state.in_position == *want,
        Condition::BarsSinceEntry(pred) => {
            state.bars_since_entry.is_some_and(|n| int_pred(*pred, n))
        }
    }
}

fn compare_prev(
    a: &Operand,
    n: u32,
    history: &[BarRow],
    idx: usize,
    f: impl Fn(f64, f64) -> bool,
) -> bool {
    let Some(prev_idx) = idx.checked_sub(n as usize) else {
        return false;
    };
    match (
        eval_operand(a, history, idx),
        eval_operand(a, history, prev_idx),
    ) {
        (Some(now), Some(then)) => f(now, then),
        _ => false,
    }
}

/// How many bars *before* the current one an operand can reach.
///
/// The evaluator indexes backwards from the bar being evaluated, so this is what
/// a caller must retain to answer the same questions from a bounded window. It
/// is defined here, beside the code that does the indexing, so the two cannot
/// drift apart: a new backward-looking form has to be given a depth to compile.
#[must_use]
pub fn operand_lookback(op: &Operand) -> usize {
    match op {
        Operand::Ref(_) | Operand::Const(_) => 0,
        Operand::Expr(expr) => match expr.as_ref() {
            OperandExpr::Price(_) => 0,
            // Nested `prev` compounds: prev(prev(x, 2), 3) reaches back five bars.
            OperandExpr::Prev((inner, n)) => *n as usize + operand_lookback(inner),
            OperandExpr::Add((a, b))
            | OperandExpr::Sub((a, b))
            | OperandExpr::Mul((a, b))
            | OperandExpr::Div((a, b)) => operand_lookback(a).max(operand_lookback(b)),
        },
    }
}

/// How many bars *before* the current one a condition can reach.
#[must_use]
pub fn condition_lookback(cond: &Condition) -> usize {
    match cond {
        Condition::Gt((a, b))
        | Condition::Lt((a, b))
        | Condition::Ge((a, b))
        | Condition::Le((a, b))
        | Condition::Eq((a, b))
        | Condition::Ne((a, b)) => operand_lookback(a).max(operand_lookback(b)),
        // A cross compares this bar with the one before it, on both sides.
        Condition::CrossAbove((a, b)) | Condition::CrossBelow((a, b)) => {
            1 + operand_lookback(a).max(operand_lookback(b))
        }
        Condition::Between((a, lo, hi)) => operand_lookback(a)
            .max(operand_lookback(lo))
            .max(operand_lookback(hi)),
        Condition::Rising((a, n)) | Condition::Falling((a, n)) => *n as usize + operand_lookback(a),
        Condition::All(cs) | Condition::Any(cs) => {
            cs.iter().map(condition_lookback).max().unwrap_or(0)
        }
        Condition::Not(c) => condition_lookback(c),
        // Read from RuleState, not from history.
        Condition::InPosition(_) | Condition::BarsSinceEntry(_) => 0,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::spec::StrategySpec;

    fn row(close: f64, vals: &[(&str, f64)]) -> BarRow {
        BarRow {
            candle: Candle {
                time: 0,
                open: close,
                high: close,
                low: close,
                close,
                volume: 0.0,
            },
            values: vals.iter().map(|(k, v)| (Arc::from(*k), *v)).collect(),
        }
    }

    const STATE: RuleState = RuleState {
        in_position: false,
        bars_since_entry: None,
    };

    #[test]
    fn const_and_ref() {
        let h = vec![row(10.0, &[("ema", 5.0)])];
        assert_eq!(eval_operand(&Operand::Const(3.0), &h, 0), Some(3.0));
        assert_eq!(eval_operand(&Operand::Ref("ema".into()), &h, 0), Some(5.0));
        assert_eq!(eval_operand(&Operand::Ref("missing".into()), &h, 0), None);
    }

    #[test]
    fn price_and_prev() {
        let h = vec![row(10.0, &[]), row(20.0, &[])];
        let close = StrategySpec::parse(
            r#"{"symbol":"x","timeframe":"1h","indicators":{},
                "entry":{"gt":[{"price":"close"},{"prev":[{"price":"close"},1]}]},
                "exit":{"in_position":true},"sizing":{"type":"fixed_qty","qty":1}}"#,
        )
        .unwrap();
        assert!(eval_condition(&close.entry, &h, 1, STATE));
        assert!(!eval_condition(&close.entry, &h, 0, STATE)); // no prev at idx 0
    }

    #[test]
    fn cross_above_detects_crossing() {
        // fast crosses slow from below at idx 1.
        let h = vec![
            row(0.0, &[("f", 1.0), ("s", 2.0)]),
            row(0.0, &[("f", 3.0), ("s", 2.0)]),
        ];
        let cond = Condition::CrossAbove((Operand::Ref("f".into()), Operand::Ref("s".into())));
        assert!(!eval_condition(&cond, &h, 0, STATE));
        assert!(eval_condition(&cond, &h, 1, STATE));
    }

    #[test]
    fn all_any_not() {
        let h = vec![row(0.0, &[("a", 5.0)])];
        let gt = Condition::Gt((Operand::Ref("a".into()), Operand::Const(1.0)));
        let lt = Condition::Lt((Operand::Ref("a".into()), Operand::Const(1.0)));
        assert!(eval_condition(
            &Condition::All(vec![gt.clone()]),
            &h,
            0,
            STATE
        ));
        assert!(eval_condition(
            &Condition::Any(vec![lt.clone(), gt.clone()]),
            &h,
            0,
            STATE
        ));
        assert!(eval_condition(&Condition::Not(Box::new(lt)), &h, 0, STATE));
    }

    fn ohlcv_row(open: f64, high: f64, low: f64, close: f64, volume: f64) -> BarRow {
        BarRow {
            candle: Candle {
                time: 0,
                open,
                high,
                low,
                close,
                volume,
            },
            values: BTreeMap::new(),
        }
    }

    fn px(field: PriceField) -> Operand {
        Operand::Expr(Box::new(OperandExpr::Price(field)))
    }

    fn expr(e: OperandExpr) -> Operand {
        Operand::Expr(Box::new(e))
    }

    #[test]
    fn all_price_fields_resolve() {
        let h = vec![ohlcv_row(10.0, 20.0, 5.0, 15.0, 100.0)];
        let at = |f| eval_operand(&px(f), &h, 0).unwrap();
        assert!((at(PriceField::Open) - 10.0).abs() < 1e-9);
        assert!((at(PriceField::High) - 20.0).abs() < 1e-9);
        assert!((at(PriceField::Low) - 5.0).abs() < 1e-9);
        assert!((at(PriceField::Close) - 15.0).abs() < 1e-9);
        assert!((at(PriceField::Volume) - 100.0).abs() < 1e-9);
        assert!((at(PriceField::Hlc3) - (20.0 + 5.0 + 15.0) / 3.0).abs() < 1e-9);
        assert!((at(PriceField::Ohlc4) - (10.0 + 20.0 + 5.0 + 15.0) / 4.0).abs() < 1e-9);
    }

    #[test]
    fn arithmetic_operands() {
        let h = vec![ohlcv_row(0.0, 0.0, 0.0, 0.0, 0.0)];
        let c = |v| Operand::Const(v);
        let ev = |e| eval_operand(&expr(e), &h, 0);
        assert_eq!(
            ev(OperandExpr::Add((Box::new(c(2.0)), Box::new(c(3.0))))),
            Some(5.0)
        );
        assert_eq!(
            ev(OperandExpr::Sub((Box::new(c(2.0)), Box::new(c(3.0))))),
            Some(-1.0)
        );
        assert_eq!(
            ev(OperandExpr::Mul((Box::new(c(2.0)), Box::new(c(3.0))))),
            Some(6.0)
        );
        assert_eq!(
            ev(OperandExpr::Div((Box::new(c(6.0)), Box::new(c(3.0))))),
            Some(2.0)
        );
        // Division by zero yields NaN, not a panic.
        let nan = ev(OperandExpr::Div((Box::new(c(1.0)), Box::new(c(0.0))))).unwrap();
        assert!(nan.is_nan());
    }

    #[test]
    fn arithmetic_with_missing_operand_is_none() {
        let h = vec![ohlcv_row(0.0, 0.0, 0.0, 0.0, 0.0)];
        let miss = Box::new(Operand::Ref("nope".into()));
        let got = eval_operand(
            &expr(OperandExpr::Add((miss, Box::new(Operand::Const(1.0))))),
            &h,
            0,
        );
        assert_eq!(got, None);
    }

    #[test]
    fn prev_underflow_is_none() {
        let h = vec![ohlcv_row(0.0, 0.0, 0.0, 0.0, 0.0)];
        let got = eval_operand(
            &expr(OperandExpr::Prev((Box::new(px(PriceField::Close)), 3))),
            &h,
            0,
        );
        assert_eq!(got, None);
    }

    #[test]
    fn comparisons_ge_le_eq_ne() {
        let h = vec![row(0.0, &[("a", 2.0)])];
        let a = Operand::Ref("a".into());
        let two = Operand::Const(2.0);
        let three = Operand::Const(3.0);
        assert!(eval_condition(
            &Condition::Ge((a.clone(), two.clone())),
            &h,
            0,
            STATE
        ));
        assert!(eval_condition(
            &Condition::Le((a.clone(), two.clone())),
            &h,
            0,
            STATE
        ));
        assert!(eval_condition(
            &Condition::Eq((a.clone(), two.clone())),
            &h,
            0,
            STATE
        ));
        assert!(eval_condition(
            &Condition::Ne((a.clone(), three)),
            &h,
            0,
            STATE
        ));
        // A missing operand makes any comparison false.
        let miss = Operand::Ref("x".into());
        assert!(!eval_condition(&Condition::Gt((miss, two)), &h, 0, STATE));
    }

    #[test]
    fn cross_below_detects_crossing() {
        let h = vec![
            row(0.0, &[("f", 3.0), ("s", 2.0)]),
            row(0.0, &[("f", 1.0), ("s", 2.0)]),
        ];
        let cond = Condition::CrossBelow((Operand::Ref("f".into()), Operand::Ref("s".into())));
        assert!(!eval_condition(&cond, &h, 0, STATE));
        assert!(eval_condition(&cond, &h, 1, STATE));
    }

    #[test]
    fn between_inside_and_outside() {
        let h = vec![row(0.0, &[("a", 5.0)])];
        let a = Operand::Ref("a".into());
        let inside = Condition::Between((a.clone(), Operand::Const(1.0), Operand::Const(10.0)));
        let outside = Condition::Between((a.clone(), Operand::Const(6.0), Operand::Const(10.0)));
        assert!(eval_condition(&inside, &h, 0, STATE));
        assert!(!eval_condition(&outside, &h, 0, STATE));
        // Missing operand -> false.
        let miss = Condition::Between((
            Operand::Ref("x".into()),
            Operand::Const(1.0),
            Operand::Const(10.0),
        ));
        assert!(!eval_condition(&miss, &h, 0, STATE));
    }

    #[test]
    fn rising_and_falling() {
        let up = vec![row(0.0, &[("a", 1.0)]), row(0.0, &[("a", 2.0)])];
        let down = vec![row(0.0, &[("a", 2.0)]), row(0.0, &[("a", 1.0)])];
        let a = Operand::Ref("a".into());
        assert!(eval_condition(
            &Condition::Rising((a.clone(), 1)),
            &up,
            1,
            STATE
        ));
        assert!(eval_condition(
            &Condition::Falling((a.clone(), 1)),
            &down,
            1,
            STATE
        ));
        // Not enough history -> false.
        assert!(!eval_condition(&Condition::Rising((a, 5)), &up, 1, STATE));
    }

    #[test]
    fn in_position_and_bars_since_entry() {
        let h = vec![row(0.0, &[])];
        let open_state = RuleState {
            in_position: true,
            bars_since_entry: Some(5),
        };
        assert!(eval_condition(
            &Condition::InPosition(true),
            &h,
            0,
            open_state
        ));
        assert!(!eval_condition(&Condition::InPosition(true), &h, 0, STATE));

        // Each integer predicate against bars_since_entry = 5.
        let cases = [
            (IntPredicate::Gt(4), true),
            (IntPredicate::Lt(6), true),
            (IntPredicate::Ge(5), true),
            (IntPredicate::Le(5), true),
            (IntPredicate::Eq(5), true),
            (IntPredicate::Eq(4), false),
        ];
        for (pred, want) in cases {
            assert_eq!(
                eval_condition(&Condition::BarsSinceEntry(pred), &h, 0, open_state),
                want
            );
        }
        // Flat: bars_since_entry is None -> always false.
        assert!(!eval_condition(
            &Condition::BarsSinceEntry(IntPredicate::Ge(0)),
            &h,
            0,
            STATE
        ));
    }
}