pub enum SignalValue {
Scalar(Decimal),
Unavailable,
}Expand description
The output value of a signal computation.
Variants§
Scalar(Decimal)
A computed scalar value.
The signal does not yet have enough data to produce a value.
Implementations§
Source§impl SignalValue
impl SignalValue
Sourcepub fn as_decimal(&self) -> Option<Decimal>
pub fn as_decimal(&self) -> Option<Decimal>
Returns the inner Decimal if this is Scalar, or None if Unavailable.
Eliminates match boilerplate at call sites.
Returns true if this value is Unavailable.
Sourcepub fn scalar_or(&self, default: Decimal) -> Decimal
pub fn scalar_or(&self, default: Decimal) -> Decimal
Returns the inner Decimal if Scalar, otherwise returns default.
Sourcepub fn zip_with(
self,
other: SignalValue,
f: impl FnOnce(Decimal, Decimal) -> Decimal,
) -> SignalValue
pub fn zip_with( self, other: SignalValue, f: impl FnOnce(Decimal, Decimal) -> Decimal, ) -> SignalValue
Combine two SignalValues with f, returning Unavailable if either is unavailable.
Mirrors Option::zip combined with map. Useful for computing derived values
that require two ready signals (e.g. a spread = signal_a - signal_b).
§Example
use fin_primitives::signals::SignalValue;
use rust_decimal_macros::dec;
let a = SignalValue::Scalar(dec!(10));
let b = SignalValue::Scalar(dec!(3));
let diff = a.zip_with(b, |x, y| x - y);
assert_eq!(diff, SignalValue::Scalar(dec!(7)));Sourcepub fn map(self, f: impl FnOnce(Decimal) -> Decimal) -> SignalValue
pub fn map(self, f: impl FnOnce(Decimal) -> Decimal) -> SignalValue
Apply f to the inner value if Scalar, returning a new SignalValue.
If Unavailable, returns Unavailable without calling f. This mirrors
Option::map and enables functional chaining without explicit match.
§Example
use fin_primitives::signals::SignalValue;
use rust_decimal_macros::dec;
let v = SignalValue::Scalar(dec!(100));
let scaled = v.map(|x| x * dec!(2));
assert_eq!(scaled, SignalValue::Scalar(dec!(200)));Sourcepub fn and_then(self, f: impl FnOnce(Decimal) -> SignalValue) -> SignalValue
pub fn and_then(self, f: impl FnOnce(Decimal) -> SignalValue) -> SignalValue
Applies f to the inner value if Scalar, where f returns a SignalValue.
If Unavailable, returns Unavailable without calling f. This mirrors
Option::and_then and enables chaining operations that may themselves produce
Unavailable (e.g., clamping, conditional transforms).
§Example
use fin_primitives::signals::SignalValue;
use rust_decimal_macros::dec;
let v = SignalValue::Scalar(dec!(50));
// Only return a value if it's above 30.
let r = v.and_then(|x| if x > dec!(30) { SignalValue::Scalar(x) } else { SignalValue::Unavailable });
assert_eq!(r, SignalValue::Scalar(dec!(50)));Sourcepub fn negate(self) -> SignalValue
pub fn negate(self) -> SignalValue
Negates the scalar value: returns Scalar(-x) if Scalar(x), else Unavailable.
Useful for inverting oscillator signals (e.g. turning a sell signal into a buy signal
by negating the output) without requiring an explicit map(|x| -x).
Sourcepub fn offset(self, delta: Decimal) -> SignalValue
pub fn offset(self, delta: Decimal) -> SignalValue
Adds delta to the scalar value.
Returns SignalValue::Unavailable unchanged.
Sourcepub fn min_with(self, other: SignalValue) -> SignalValue
pub fn min_with(self, other: SignalValue) -> SignalValue
Returns the smaller of self and other. Unavailable loses to any Scalar.
Sourcepub fn max_with(self, other: SignalValue) -> SignalValue
pub fn max_with(self, other: SignalValue) -> SignalValue
Returns the larger of self and other. Unavailable loses to any Scalar.
Sourcepub fn abs(self) -> SignalValue
pub fn abs(self) -> SignalValue
Returns the absolute value of the scalar: Scalar(|x|) or Unavailable.
Useful when you only care about the magnitude of a signal (e.g. absolute momentum).
Sourcepub fn mul(self, factor: Decimal) -> SignalValue
pub fn mul(self, factor: Decimal) -> SignalValue
Scales the scalar by factor: Scalar(x) * factor = Scalar(x * factor).
Returns Unavailable if the signal is Unavailable. Useful for weighting
or inverting signals (e.g. signal.mul(Decimal::NEGATIVE_ONE)).
Sourcepub fn sub(self, other: SignalValue) -> SignalValue
pub fn sub(self, other: SignalValue) -> SignalValue
Subtracts two signals: Scalar(a) - Scalar(b) = Scalar(a - b).
Returns Unavailable if either operand is Unavailable.
Sourcepub fn mul_signal(self, other: SignalValue) -> SignalValue
pub fn mul_signal(self, other: SignalValue) -> SignalValue
Multiplies two signals: Scalar(a) * Scalar(b) = Scalar(a * b).
Returns Unavailable if either operand is Unavailable.
Sourcepub fn add(self, other: SignalValue) -> SignalValue
pub fn add(self, other: SignalValue) -> SignalValue
Adds two signals: Scalar(a) + Scalar(b) = Scalar(a + b).
Returns Unavailable if either operand is Unavailable.
Useful for combining multiple signal outputs without explicit pattern matching.
Sourcepub fn clamp(self, lo: Decimal, hi: Decimal) -> SignalValue
pub fn clamp(self, lo: Decimal, hi: Decimal) -> SignalValue
Clamps the scalar value to [lo, hi], returning Unavailable if Unavailable.
If Scalar(v), returns Scalar(v.clamp(lo, hi)). Useful for bounding oscillators
such as RSI to valid ranges after arithmetic transforms.
§Example
use fin_primitives::signals::SignalValue;
use rust_decimal_macros::dec;
let v = SignalValue::Scalar(dec!(105));
assert_eq!(v.clamp(dec!(0), dec!(100)), SignalValue::Scalar(dec!(100)));Sourcepub fn div(self, other: SignalValue) -> SignalValue
pub fn div(self, other: SignalValue) -> SignalValue
Divides two signals: Scalar(a) / Scalar(b).
Returns Unavailable if either operand is Unavailable or b is zero.
Sourcepub fn is_positive(&self) -> bool
pub fn is_positive(&self) -> bool
Returns true if the scalar value is strictly positive. Unavailable returns false.
Sourcepub fn is_negative(&self) -> bool
pub fn is_negative(&self) -> bool
Returns true if the scalar value is strictly negative. Unavailable returns false.
Returns default if this is Unavailable; otherwise returns the scalar value.
Sourcepub fn is_above(&self, threshold: Decimal) -> bool
pub fn is_above(&self, threshold: Decimal) -> bool
Returns true if the scalar value is strictly above threshold.
Unavailable always returns false.
Sourcepub fn is_below(&self, threshold: Decimal) -> bool
pub fn is_below(&self, threshold: Decimal) -> bool
Returns true if the scalar value is strictly below threshold.
Unavailable always returns false.
Sourcepub fn round(self, dp: u32) -> SignalValue
pub fn round(self, dp: u32) -> SignalValue
Rounds the scalar to dp decimal places using banker’s rounding.
Returns Unavailable unchanged.
Sourcepub fn to_option(self) -> Option<Decimal>
pub fn to_option(self) -> Option<Decimal>
Converts to Option<Decimal>: Some(d) for Scalar(d), None for Unavailable.
Sourcepub fn as_f64(&self) -> Option<f64>
pub fn as_f64(&self) -> Option<f64>
Converts to Option<f64>: Some(f64) for Scalar, None for Unavailable.
Precision may be lost in the Decimal → f64 conversion.
Sourcepub fn max(self, other: SignalValue) -> SignalValue
pub fn max(self, other: SignalValue) -> SignalValue
Returns the element-wise maximum of two signals.
Scalar(a).max(Scalar(b)) = Scalar(max(a, b)).
Returns Unavailable if either operand is Unavailable.
Sourcepub fn min(self, other: SignalValue) -> SignalValue
pub fn min(self, other: SignalValue) -> SignalValue
Returns the element-wise minimum of two signals.
Scalar(a).min(Scalar(b)) = Scalar(min(a, b)).
Returns Unavailable if either operand is Unavailable.
Sourcepub fn signum(self) -> SignalValue
pub fn signum(self) -> SignalValue
Returns Scalar(-1), Scalar(0), or Scalar(1) based on the sign of the value.
Returns Unavailable if the value is unavailable.
Sourcepub fn sqrt(self) -> SignalValue
pub fn sqrt(self) -> SignalValue
Returns the square root of the scalar value.
Uses f64 intermediate computation. Returns Unavailable if the value is
negative or unavailable.
use fin_primitives::signals::SignalValue;
use rust_decimal_macros::dec;
let v = SignalValue::Scalar(dec!(4));
if let SignalValue::Scalar(r) = v.sqrt() {
assert!((r - dec!(2)).abs() < dec!(0.00001));
}Sourcepub fn pow(self, exp: u32) -> SignalValue
pub fn pow(self, exp: u32) -> SignalValue
Raises the scalar value to an integer power.
Returns Unavailable if the value is unavailable.
use fin_primitives::signals::SignalValue;
use rust_decimal_macros::dec;
assert_eq!(SignalValue::Scalar(dec!(3)).pow(2), SignalValue::Scalar(dec!(9)));Sourcepub fn ln(self) -> SignalValue
pub fn ln(self) -> SignalValue
Returns the natural logarithm of the scalar value.
Returns Unavailable if the value is ≤ 0 or unavailable.
use fin_primitives::signals::SignalValue;
use rust_decimal_macros::dec;
let v = SignalValue::Scalar(dec!(1));
assert_eq!(v.ln(), SignalValue::Scalar(dec!(0)));
assert_eq!(SignalValue::Scalar(dec!(-1)).ln(), SignalValue::Unavailable);Sourcepub fn cross_above(self, threshold: Decimal, prev: SignalValue) -> bool
pub fn cross_above(self, threshold: Decimal, prev: SignalValue) -> bool
Returns true if this value is above threshold while prev was at or below it.
Detects an upward crossing of a threshold level. Both values must be scalar.
use fin_primitives::signals::SignalValue;
use rust_decimal_macros::dec;
let prev = SignalValue::Scalar(dec!(49));
let curr = SignalValue::Scalar(dec!(51));
assert!(curr.cross_above(dec!(50), prev));Sourcepub fn cross_below(self, threshold: Decimal, prev: SignalValue) -> bool
pub fn cross_below(self, threshold: Decimal, prev: SignalValue) -> bool
Returns true if this value is below threshold while prev was at or above it.
Detects a downward crossing of a threshold level. Both values must be scalar.
use fin_primitives::signals::SignalValue;
use rust_decimal_macros::dec;
let prev = SignalValue::Scalar(dec!(51));
let curr = SignalValue::Scalar(dec!(49));
assert!(curr.cross_below(dec!(50), prev));Sourcepub fn pct_of(self, other: SignalValue) -> SignalValue
pub fn pct_of(self, other: SignalValue) -> SignalValue
Returns this scalar as a percentage of other.
result = (self / other) × 100
Returns Unavailable if either value is unavailable or other is zero.
use fin_primitives::signals::SignalValue;
use rust_decimal_macros::dec;
let v = SignalValue::Scalar(dec!(50));
let base = SignalValue::Scalar(dec!(200));
assert_eq!(v.pct_of(base), SignalValue::Scalar(dec!(25)));Sourcepub fn threshold_cross(
self,
threshold: Decimal,
prev: SignalValue,
) -> SignalValue
pub fn threshold_cross( self, threshold: Decimal, prev: SignalValue, ) -> SignalValue
Returns -1, 0, or +1 depending on how this value crosses threshold from prev.
+1ifprev <= thresholdandself > threshold(upward crossing)-1ifprev >= thresholdandself < threshold(downward crossing)0otherwise (no crossing, or either value is unavailable)
use fin_primitives::signals::SignalValue;
use rust_decimal_macros::dec;
let prev = SignalValue::Scalar(dec!(49));
let curr = SignalValue::Scalar(dec!(51));
assert_eq!(curr.threshold_cross(dec!(50), prev), SignalValue::Scalar(dec!(1)));Sourcepub fn exp(self) -> SignalValue
pub fn exp(self) -> SignalValue
Returns e^x. Returns Unavailable if the value is Unavailable or if x > 700
(overflow guard — e^709 ≈ f64::MAX).
Sourcepub fn floor(self) -> SignalValue
pub fn floor(self) -> SignalValue
Returns the floor of the value (rounds toward negative infinity).
Sourcepub fn ceil(self) -> SignalValue
pub fn ceil(self) -> SignalValue
Returns the ceiling of the value (rounds toward positive infinity).
Sourcepub fn reciprocal(self) -> SignalValue
pub fn reciprocal(self) -> SignalValue
Returns 1 / self. Returns Unavailable if the value is zero or Unavailable.
Sourcepub fn to_percent(self, total: SignalValue) -> SignalValue
pub fn to_percent(self, total: SignalValue) -> SignalValue
Returns (self / total) * 100. Returns Unavailable if total is zero or either
value is Unavailable.
Sourcepub fn atan(self) -> SignalValue
pub fn atan(self) -> SignalValue
Returns the arctangent of the value in radians. Returns Unavailable if unavailable.
Sourcepub fn tanh(self) -> SignalValue
pub fn tanh(self) -> SignalValue
Returns the hyperbolic tangent of the value. Returns Unavailable if unavailable.
tanh maps any real value to (-1, 1) — useful for normalising unbounded signals.
Sourcepub fn sinh(self) -> SignalValue
pub fn sinh(self) -> SignalValue
Returns the hyperbolic sine of the scalar value.
Returns SignalValue::Unavailable if the result is non-finite.
Sourcepub fn cosh(self) -> SignalValue
pub fn cosh(self) -> SignalValue
Returns the hyperbolic cosine of the scalar value.
Returns SignalValue::Unavailable if the result is non-finite.
Sourcepub fn round_to(self, dp: u32) -> SignalValue
pub fn round_to(self, dp: u32) -> SignalValue
Rounds the scalar to dp decimal places using banker’s rounding.
Returns SignalValue::Unavailable unchanged.
Sourcepub fn scale_by(self, factor: Decimal) -> SignalValue
pub fn scale_by(self, factor: Decimal) -> SignalValue
Multiplies the scalar by factor, returning the product as a new SignalValue.
Returns SignalValue::Unavailable unchanged.
Sourcepub fn delta(self, other: SignalValue) -> SignalValue
pub fn delta(self, other: SignalValue) -> SignalValue
Absolute difference between two SignalValues.
Returns Unavailable if either operand is Unavailable.
Sourcepub fn lerp(self, other: SignalValue, t: Decimal) -> SignalValue
pub fn lerp(self, other: SignalValue, t: Decimal) -> SignalValue
Linear interpolation: self * (1 - t) + other * t.
t is clamped to [0, 1]. Returns Unavailable if either operand is Unavailable.
Sourcepub fn gt(&self, other: &SignalValue) -> bool
pub fn gt(&self, other: &SignalValue) -> bool
Returns true if self is a scalar strictly greater than other.
Returns false if either operand is Unavailable.
Sourcepub fn lt(&self, other: &SignalValue) -> bool
pub fn lt(&self, other: &SignalValue) -> bool
Returns true if self is a scalar strictly less than other.
Returns false if either operand is Unavailable.
Sourcepub fn eq_approx(&self, other: &SignalValue, tolerance: Decimal) -> bool
pub fn eq_approx(&self, other: &SignalValue, tolerance: Decimal) -> bool
Returns true if both are scalars and |self - other| <= tolerance.
Returns false if either is Unavailable.
Sourcepub fn atan2(self, x: SignalValue) -> SignalValue
pub fn atan2(self, x: SignalValue) -> SignalValue
Two-argument arctangent: atan2(self, x) in radians.
Treats self as the y argument. Returns Unavailable if either is Unavailable.
Sourcepub fn sign_match(&self, other: &SignalValue) -> bool
pub fn sign_match(&self, other: &SignalValue) -> bool
Returns true if both scalars have the same sign (both positive or both negative).
Zero is treated as positive. Returns false if either is Unavailable.
Sourcepub fn add_scalar(self, delta: Decimal) -> SignalValue
pub fn add_scalar(self, delta: Decimal) -> SignalValue
Adds a raw Decimal to this scalar value.
Returns Unavailable if self is Unavailable.
Sourcepub fn map_or(
self,
default: Decimal,
f: impl FnOnce(Decimal) -> Decimal,
) -> Decimal
pub fn map_or( self, default: Decimal, f: impl FnOnce(Decimal) -> Decimal, ) -> Decimal
Maps the scalar with f, falling back to default if Unavailable.
Sourcepub fn gte(&self, other: &SignalValue) -> bool
pub fn gte(&self, other: &SignalValue) -> bool
Returns true if self >= other (both scalar). Returns false if either is Unavailable.
Sourcepub fn lte(&self, other: &SignalValue) -> bool
pub fn lte(&self, other: &SignalValue) -> bool
Returns true if self <= other (both scalar). Returns false if either is Unavailable.
Sourcepub fn as_percent(self, base: Decimal) -> SignalValue
pub fn as_percent(self, base: Decimal) -> SignalValue
Express this scalar as a percentage of base: self / base * 100.
Returns Unavailable if self is Unavailable or base is zero.
Sourcepub fn within_range(&self, lo: Decimal, hi: Decimal) -> bool
pub fn within_range(&self, lo: Decimal, hi: Decimal) -> bool
Returns true if this scalar is in [lo, hi] (inclusive).
Returns false if Unavailable.
Sourcepub fn cap_at(self, max_val: Decimal) -> SignalValue
pub fn cap_at(self, max_val: Decimal) -> SignalValue
Caps the scalar at max_val. Returns Unavailable if self is Unavailable.
Sourcepub fn floor_at(self, min_val: Decimal) -> SignalValue
pub fn floor_at(self, min_val: Decimal) -> SignalValue
Floors the scalar at min_val. Returns Unavailable if self is Unavailable.
Sourcepub fn quantize(self, step: Decimal) -> SignalValue
pub fn quantize(self, step: Decimal) -> SignalValue
Round the scalar to the nearest multiple of step. Returns Unavailable if unavailable
or step is zero.
Sourcepub fn distance_to(self, other: SignalValue) -> SignalValue
pub fn distance_to(self, other: SignalValue) -> SignalValue
Absolute difference between self and other. Returns Unavailable if either is unavailable.
Sourcepub fn blend(self, other: SignalValue, weight: Decimal) -> SignalValue
pub fn blend(self, other: SignalValue, weight: Decimal) -> SignalValue
Weighted blend: self * (1 - weight) + other * weight, clamping weight to [0, 1].
Returns Unavailable if either operand is unavailable.
Trait Implementations§
Source§impl Clone for SignalValue
impl Clone for SignalValue
Source§fn clone(&self) -> SignalValue
fn clone(&self) -> SignalValue
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more