#[non_exhaustive]pub struct Candle {
pub open: f64,
pub high: f64,
pub low: f64,
pub close: f64,
pub volume: f64,
pub timestamp: i64,
}Expand description
A single OHLCV bar.
Timestamps are unitless i64 values so callers can use whatever epoch resolution
they prefer (milliseconds, microseconds, seconds…). Wickra never inspects them
numerically beyond passing them through.
§Construction and the limits of its guarantee
The struct is #[non_exhaustive], so code outside this crate cannot build
one from a field literal and must go through new, which
validates, or new_unchecked, which is an explicit
opt-out for values already known to be sound.
The fields stay public because reading them is by far the common operation
and an accessor on each would buy nothing. That does mean a validated value
can still be written into an invalid state afterwards, and nothing detects
it: the indicators that consume this type rely on the constructor’s
guarantee rather than re-checking every bar. Treat a mutation the way you
would treat new_unchecked — you are asserting the invariants still hold.
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.open: f64Bar open price.
high: f64Bar high price.
low: f64Bar low price.
close: f64Bar close price.
volume: f64Bar volume.
timestamp: i64Bar timestamp (caller-defined epoch / resolution).
Implementations§
Source§impl Candle
impl Candle
Sourcepub fn new(
open: f64,
high: f64,
low: f64,
close: f64,
volume: f64,
timestamp: i64,
) -> Result<Self>
pub fn new( open: f64, high: f64, low: f64, close: f64, volume: f64, timestamp: i64, ) -> Result<Self>
Construct a new candle, validating the OHLC relationships and finiteness.
§Errors
Returns Error::InvalidCandle if any of these invariants are violated:
high >= max(open, close, low)low <= min(open, close, high)- all of
open,high,low,close,volumeare finite volume >= 0
Sourcepub const fn new_unchecked(
open: f64,
high: f64,
low: f64,
close: f64,
volume: f64,
timestamp: i64,
) -> Self
pub const fn new_unchecked( open: f64, high: f64, low: f64, close: f64, volume: f64, timestamp: i64, ) -> Self
Construct a candle without validation. The caller asserts that all OHLC invariants hold and that no field is NaN or infinite.
Sourcepub fn typical_price(&self) -> f64
pub fn typical_price(&self) -> f64
The typical price (high + low + close) / 3. Used by CCI, MFI, VWAP, etc.
Sourcepub fn median_price(&self) -> f64
pub fn median_price(&self) -> f64
The mid price (high + low) / 2.
Sourcepub fn weighted_close(&self) -> f64
pub fn weighted_close(&self) -> f64
The weighted close (high + low + 2*close) / 4.
Sourcepub fn true_range(&self, prev_close: Option<f64>) -> f64
pub fn true_range(&self, prev_close: Option<f64>) -> f64
True range of this candle relative to a previous close: max(H-L, |H-prev|, |L-prev|).
If no previous close is supplied, falls back to high - low.
Trait Implementations§
impl Copy for Candle
impl StructuralPartialEq for Candle
Auto Trait Implementations§
impl Freeze for Candle
impl RefUnwindSafe for Candle
impl Send for Candle
impl Sync for Candle
impl Unpin for Candle
impl UnsafeUnpin for Candle
impl UnwindSafe for Candle
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more