pub struct Bar {
pub timestamp: i64,
pub open: f64,
pub high: f64,
pub low: f64,
pub close: f64,
pub volume: f64,
}Expand description
Generic OHLCV Bar data point.
Fields§
§timestamp: i64§open: f64§high: f64§low: f64§close: f64§volume: f64Implementations§
Source§impl Bar
impl Bar
Sourcepub fn new(
timestamp: i64,
open: f64,
high: f64,
low: f64,
close: f64,
volume: f64,
) -> Self
pub fn new( timestamp: i64, open: f64, high: f64, low: f64, close: f64, volume: f64, ) -> Self
Creates a bar without validation.
Use Bar::try_new at data-ingestion boundaries. This unchecked constructor is retained
for trusted feeds and compatibility with existing consumers.
Sourcepub fn try_new(
timestamp: i64,
open: f64,
high: f64,
low: f64,
close: f64,
volume: f64,
) -> Result<Self, BarValidationError>
pub fn try_new( timestamp: i64, open: f64, high: f64, low: f64, close: f64, volume: f64, ) -> Result<Self, BarValidationError>
Creates a bar after validating the OHLCV input contract.
Examples found in repository?
examples/basic_indicator.rs (lines 28-35)
12fn main() -> Result<(), Box<dyn std::error::Error>> {
13 // `build_checked` validates parameters against the registry's bounds/ordering rules for the
14 // named indicator (see `kestrel_chartkit::catalog()` for the full list of names and their
15 // default parameters) and returns a boxed `Indicator` trait object.
16 let mut params = HashMap::new();
17 params.insert("rsi_len".to_string(), 14.0);
18 let mut rsi = build_checked("rsi", ¶ms)?;
19
20 // A short synthetic closing-price series. Real feeds should validate bars at the ingestion
21 // boundary with `Bar::try_new` (as here) rather than the unchecked `Bar::new`.
22 let closes = [
23 44.34, 44.09, 44.15, 43.61, 44.33, 44.83, 45.10, 45.42, 45.84, 46.08, 45.89, 46.03, 45.61,
24 46.28, 46.28, 46.00,
25 ];
26
27 for (i, &close) in closes.iter().enumerate() {
28 let bar = Bar::try_new(
29 1_700_000_000 + i as i64 * 60,
30 close,
31 close + 0.5,
32 close - 0.5,
33 close,
34 1_000.0,
35 )?;
36
37 // `on_bar` returns `None` while the indicator is still warming up (fewer bars seen than
38 // its `warmup_period()`); once warmed up it returns `Some` on every subsequent bar.
39 match rsi.on_bar(&bar) {
40 Some(output) => println!("bar {i:>2}: close={close:.2} RSI(14)={:.2}", output.value),
41 None => println!("bar {i:>2}: close={close:.2} RSI(14)=<warming up>"),
42 }
43 }
44
45 Ok(())
46}pub fn typical_price(&self) -> f64
Sourcepub fn validate(&self) -> Result<(), BarValidationError>
pub fn validate(&self) -> Result<(), BarValidationError>
Verifies that OHLCV prices and volumes satisfy mathematical and physical domain requirements:
- All price values are finite and strictly positive (> 0.0)
- Volume is finite and non-negative (>= 0.0)
- Structural inequality holds:
low <= min(open, close)andhigh >= max(open, close)
Sourcepub fn is_valid(&self) -> bool
pub fn is_valid(&self) -> bool
Returns whether the bar satisfies Bar::validate.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Bar
impl<'de> Deserialize<'de> for Bar
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl From<&Bar> for ChartBarData
impl From<&Bar> for ChartBarData
impl StructuralPartialEq for Bar
Auto Trait Implementations§
impl Freeze for Bar
impl RefUnwindSafe for Bar
impl Send for Bar
impl Sync for Bar
impl Unpin for Bar
impl UnsafeUnpin for Bar
impl UnwindSafe for Bar
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
Mutably borrows from an owned value. Read more