a32dc38d18
C29 compile/unit seam, task 3 of the self-description plan: every aura-std NodeSchema literal threads its one-line doc. Four texts were corrected against the actual eval/finalize semantics rather than taken from the plan table verbatim: Delay is a lag-N register (not one-step), GatedRecorder flushes an ungated final row at finalize, Latch is a level-sensitive set/reset register (captures no input), SeriesReducer emits its single summary row at finalize (not per cycle). Gate: cargo build -p aura-std --lib clean; full std test run follows at the all-crates gate once strategy/backtest/engine thread their sites (the earlier per-crate test gate was unsatisfiable in isolation -- std's dev-deps pull crates whose sites belong to later tasks). refs #316
80 lines
2.3 KiB
Rust
80 lines
2.3 KiB
Rust
//! `Abs` — one-input f64 absolute value. Turns a signed delta (e.g. the
|
|
//! negative-side split of a price change) into a positive magnitude, e.g.
|
|
//! RSI-class signals' `loss = Abs(Min(delta, 0))`. Emits `None` until its
|
|
//! input has a value.
|
|
|
|
use aura_core::{Cell, Ctx, FieldSpec, Firing, Node, NodeSchema, PortSpec, PrimitiveBuilder, ScalarKind};
|
|
|
|
/// One-input f64 absolute value, `value.abs()`. Emits `None` until its input
|
|
/// has a value (warm-up filter, C8).
|
|
pub struct Abs {
|
|
out: [Cell; 1],
|
|
}
|
|
|
|
impl Abs {
|
|
pub fn new() -> Self {
|
|
Self { out: [Cell::from_f64(0.0)] }
|
|
}
|
|
|
|
pub fn builder() -> PrimitiveBuilder {
|
|
PrimitiveBuilder::new(
|
|
"Abs",
|
|
NodeSchema {
|
|
inputs: vec![PortSpec { kind: ScalarKind::F64, firing: Firing::Any, name: "value".into() }],
|
|
output: vec![FieldSpec { name: "value".into(), kind: ScalarKind::F64 }],
|
|
params: vec![],
|
|
doc: "absolute value of the input series",
|
|
},
|
|
|_| Box::new(Abs::new()),
|
|
)
|
|
}
|
|
}
|
|
|
|
impl Default for Abs {
|
|
fn default() -> Self {
|
|
Self::new()
|
|
}
|
|
}
|
|
|
|
impl Node for Abs {
|
|
fn lookbacks(&self) -> Vec<usize> {
|
|
vec![1]
|
|
}
|
|
|
|
fn eval(&mut self, ctx: Ctx<'_>) -> Option<&[Cell]> {
|
|
let w = ctx.f64_in(0);
|
|
if w.is_empty() {
|
|
return None;
|
|
}
|
|
self.out[0] = Cell::from_f64(w[0].abs());
|
|
Some(&self.out)
|
|
}
|
|
|
|
fn label(&self) -> String {
|
|
"Abs".to_string()
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use aura_core::{AnyColumn, Scalar, Timestamp};
|
|
|
|
#[test]
|
|
fn abs_of_negative_and_positive_is_the_magnitude() {
|
|
let mut a = Abs::new();
|
|
let mut inputs = vec![AnyColumn::with_capacity(ScalarKind::F64, 1)];
|
|
inputs[0].push(Scalar::f64(-3.0)).unwrap();
|
|
assert_eq!(a.eval(Ctx::new(&inputs, Timestamp(0))), Some([Cell::from_f64(3.0)].as_slice()));
|
|
inputs[0].push(Scalar::f64(3.0)).unwrap();
|
|
assert_eq!(a.eval(Ctx::new(&inputs, Timestamp(0))), Some([Cell::from_f64(3.0)].as_slice()));
|
|
}
|
|
|
|
#[test]
|
|
fn abs_is_none_until_input_present() {
|
|
let mut a = Abs::new();
|
|
let inputs = vec![AnyColumn::with_capacity(ScalarKind::F64, 1)];
|
|
assert_eq!(a.eval(Ctx::new(&inputs, Timestamp(0))), None);
|
|
}
|
|
}
|