Skip to main content

OhlcvBar

Struct OhlcvBar 

Source
pub struct OhlcvBar {
    pub symbol: Symbol,
    pub open: Price,
    pub high: Price,
    pub low: Price,
    pub close: Price,
    pub volume: Quantity,
    pub ts_open: NanoTimestamp,
    pub ts_close: NanoTimestamp,
    pub tick_count: u64,
}
Expand description

A completed OHLCV bar for a single symbol and timeframe bucket.

Fields§

§symbol: Symbol

The instrument.

§open: Price

Opening price of the bar.

§high: Price

Highest price during the bar.

§low: Price

Lowest price during the bar.

§close: Price

Closing price of the bar.

§volume: Quantity

Total traded volume during the bar.

§ts_open: NanoTimestamp

Timestamp of the first tick in the bar.

§ts_close: NanoTimestamp

Timestamp of the last tick in the bar.

§tick_count: u64

Number of ticks that contributed to this bar.

Implementations§

Source§

impl OhlcvBar

Source

pub fn new( symbol: Symbol, open: Price, high: Price, low: Price, close: Price, volume: Quantity, ts_open: NanoTimestamp, ts_close: NanoTimestamp, tick_count: u64, ) -> Result<Self, FinError>

Constructs and validates an OhlcvBar from individual components.

Equivalent to building the struct literal then calling validate(), but more convenient for test and user code that does not want to spell out all nine named fields.

§Errors

Returns FinError::BarInvariant if the OHLCV invariants are violated.

Source

pub fn validate(&self) -> Result<(), FinError>

Validates OHLCV invariants.

§Errors

Returns FinError::BarInvariant if any of these fail:

  • high >= open
  • high >= close
  • low <= open
  • low <= close
  • high >= low
Source

pub fn to_bar_input(&self) -> BarInput

Converts this bar to a crate::signals::BarInput for signal computation.

Source

pub fn typical_price(&self) -> Decimal

Returns the typical price: (high + low + close) / 3.

Source

pub fn range(&self) -> Decimal

Returns the price range: high - low.

Source

pub fn hlcc4(&self) -> Decimal

Returns the HLCC/4 price: (high + low + close + close) / 4.

Weights the close price twice, giving it more significance than the typical price. Commonly used as a weighted price reference.

Source

pub fn weighted_close(&self) -> Decimal

Returns the weighted close price: (high + low + close * 2) / 4.

Alias for hlcc4. Commonly called “weighted close” in technical analysis literature; emphasises the closing price over the high and low.

Source

pub fn ohlc4(&self) -> Decimal

Returns the OHLC/4 price: (open + high + low + close) / 4.

Equal weight for all four price components. Common in smoothed candlestick calculations and some custom charting systems.

Source

pub fn dollar_volume(&self) -> Decimal

Returns the dollar volume of this bar: typical_price × volume.

Dollar volume is a common liquidity metric: high dollar volume means large amounts of capital changed hands, making the instrument easier to trade without excessive market impact.

Source

pub fn is_gap_fill(&self) -> bool

Returns true if this bar is a gap-fill placeholder (zero ticks).

Gap-fill bars are emitted by OhlcvAggregator when a tick arrives several buckets ahead of the current one. They have tick_count == 0 and zero volume.

Source

pub fn is_inside_bar(&self, prev: &OhlcvBar) -> bool

Returns true if this bar is an inside bar relative to prev.

An inside bar is fully contained within the previous bar’s range: self.high < prev.high && self.low > prev.low. Commonly used in price action analysis to identify consolidation before a potential breakout.

Source

pub fn is_outside_bar(&self, prev: &OhlcvBar) -> bool

Returns true if this bar’s range completely contains the previous bar’s range.

An outside bar has high > prev.high && low < prev.low. Signals potential volatility expansion or reversal — the opposite of an inside bar.

Source

pub fn is_engulfing(&self, prev: &OhlcvBar) -> bool

Returns true if this bar engulfs the previous bar (bullish or bearish engulfing).

A bullish engulfing bar: prev is bearish and self is a bullish bar whose body completely contains prev’s body. Bearish is the mirror image.

Source

pub fn is_bullish(&self) -> bool

Returns true if close >= open.

Source

pub fn is_bearish(&self) -> bool

Returns true if close < open.

Source

pub fn is_hammer(&self) -> bool

Returns true if the bar has a hammer candlestick shape.

Criteria: lower shadow ≥ 2 × body size, upper shadow ≤ body size, non-zero body.

Source

pub fn is_marubozu(&self) -> bool

Returns true if the bar is a marubozu: a full-body candle with negligible shadows.

Criteria: both upper and lower shadows are each < 5% of the bar’s total range, and the body is non-zero.

Source

pub fn is_spinning_top(&self) -> bool

Returns true if the bar is a spinning top: a small body with significant upper and lower shadows.

Criteria: body is less than 30% of the total range, and both shadows are each at least 20% of the range.

Source

pub fn is_shooting_star(&self) -> bool

Returns true if the bar has a shooting star candlestick shape.

Criteria: upper shadow ≥ 2 × body size, lower shadow ≤ body size, non-zero body.

Source

pub fn body_pct(&self) -> Option<Decimal>

Returns the body size as a percentage of the open price: body_size / open * 100.

Returns None when open is zero.

Source

pub fn bar_return(&self) -> Option<Decimal>

Returns the open-to-close return as a percentage: (close - open) / open * 100.

Returns None when open is zero.

Source

pub fn midpoint(&self) -> Decimal

Returns the midpoint price: (high + low) / 2 (HL2).

Source

pub fn body_size(&self) -> Decimal

Returns the absolute candlestick body size: |close - open|.

Source

pub fn body_to_range_ratio(&self) -> Option<Decimal>

Body-to-range ratio: body_size() / range().

Returns None when range() == 0 (flat bar). A value near 1 means the bar is all body; near 0 means the bar is mostly wicks.

Source

pub fn is_long_candle(&self, factor: Decimal) -> bool

Returns true if the bar’s body is large relative to its range.

A bar is considered “long” when body_size / range >= factor. Returns false when range == 0 (flat bar).

Source

pub fn is_doji(&self, threshold: Decimal) -> bool

Returns true if the bar is a doji: body_size / range < threshold.

A doji indicates indecision. Returns false when range == 0 (flat bar) and threshold == 0; returns true for a flat bar with any positive threshold.

Source

pub fn body_ratio(&self) -> Option<Decimal>

Returns the ratio of body to range: body_size / range.

Returns None when range == 0 (doji / flat bar) to avoid division by zero. Values close to 1 indicate a strong directional candle; values close to 0 indicate a spinning top or doji.

Source

pub fn true_range(&self, prev: Option<&OhlcvBar>) -> Decimal

Returns the True Range for this bar.

True Range is the maximum of:

  • high - low
  • |high - prev_close| (if prev is Some)
  • |low - prev_close| (if prev is Some)

When prev is None, True Range falls back to high - low. This is the building block for ATR and volatility calculations.

Source

pub fn shadow_ratio(&self) -> Option<Decimal>

Returns the ratio of total shadow to range: (upper_shadow + lower_shadow) / range.

A value near 1.0 indicates most of the bar’s range is wick (indecision). Returns None when range == 0.

Source

pub fn gap_up_from(&self, prev: &OhlcvBar) -> bool

Returns true if this bar opens above the previous bar’s high (gap up).

Source

pub fn gap_down_from(&self, prev: &OhlcvBar) -> bool

Returns true if this bar opens below the previous bar’s low (gap down).

Source

pub fn gap_from(&self, prev: &OhlcvBar) -> Decimal

Signed gap from prior bar: self.open - prev.close.

Positive = gap up, negative = gap down, zero = no gap.

Source

pub fn upper_shadow(&self) -> Decimal

Returns the upper shadow length: high - max(open, close).

Source

pub fn lower_shadow(&self) -> Decimal

Returns the lower shadow length: min(open, close) - low.

Source

pub fn duration_nanos(&self) -> i64

Returns the duration of this bar in nanoseconds: ts_close - ts_open.

For gap-fill bars (no ticks), both timestamps are equal and this returns 0.

Source

pub fn gap_pct(&self, prev: &OhlcvBar) -> Option<Decimal>

Returns the percentage gap between prev.close and self.open.

gap_pct = (self.open - prev.close) / prev.close * 100

Returns None if prev.close is zero. Positive values indicate an upward gap; negative values a downward gap.

Source

pub fn has_gap(&self, prev: &OhlcvBar, pct_threshold: Decimal) -> bool

Returns true if this bar opened with a gap larger than pct_threshold percent.

A gap exists when |gap_pct| >= pct_threshold. Returns false when gap_pct cannot be computed (zero previous close).

Source

pub fn from_tick(tick: &Tick) -> Self

Creates a single-tick OHLCV bar from a Tick.

All price fields are set to the tick’s price, volume to the tick’s quantity, and both timestamps to the tick’s timestamp.

Source

pub fn merge(&self, other: &OhlcvBar) -> Result<OhlcvBar, FinError>

Merges other into self, producing a combined bar spanning both time ranges.

  • open comes from self (the earlier bar)
  • close comes from other (the later bar)
  • high / low are the extremes across both bars
  • volume and tick_count are summed
  • ts_open from self, ts_close from other
§Errors

Returns FinError::BarInvariant if the merged bar fails invariant checks (should not occur for well-formed inputs but is checked defensively).

Source

pub fn is_bullish_engulfing(&self, prev: &OhlcvBar) -> bool

Returns true if this bar is a bullish engulfing of prev.

Conditions:

  • prev is bearish (open > close)
  • self is bullish (close > open)
  • self.open <= prev.close (opens at or below prev close)
  • self.close >= prev.open (closes at or above prev open)
Source

pub fn is_bearish_engulfing(&self, prev: &OhlcvBar) -> bool

Returns true if this bar is a bearish engulfing of prev.

Conditions:

  • prev is bullish (close > open)
  • self is bearish (open > close)
  • self.open >= prev.close (opens at or above prev close)
  • self.close <= prev.open (closes at or below prev open)

Trait Implementations§

Source§

impl Clone for OhlcvBar

Source§

fn clone(&self) -> OhlcvBar

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for OhlcvBar

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for OhlcvBar

Source§

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<&OhlcvBar> for BarInput

Source§

fn from(bar: &OhlcvBar) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for OhlcvBar

Source§

fn eq(&self, other: &OhlcvBar) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for OhlcvBar

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for OhlcvBar

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,