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: SymbolThe instrument.
open: PriceOpening price of the bar.
high: PriceHighest price during the bar.
low: PriceLowest price during the bar.
close: PriceClosing price of the bar.
volume: QuantityTotal traded volume during the bar.
ts_open: NanoTimestampTimestamp of the first tick in the bar.
ts_close: NanoTimestampTimestamp of the last tick in the bar.
tick_count: u64Number of ticks that contributed to this bar.
Implementations§
Source§impl OhlcvBar
impl OhlcvBar
Sourcepub 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>
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.
Sourcepub fn validate(&self) -> Result<(), FinError>
pub fn validate(&self) -> Result<(), FinError>
Validates OHLCV invariants.
§Errors
Returns FinError::BarInvariant if any of these fail:
high >= openhigh >= closelow <= openlow <= closehigh >= low
Sourcepub fn to_bar_input(&self) -> BarInput
pub fn to_bar_input(&self) -> BarInput
Converts this bar to a crate::signals::BarInput for signal computation.
Sourcepub fn typical_price(&self) -> Decimal
pub fn typical_price(&self) -> Decimal
Returns the typical price: (high + low + close) / 3.
Sourcepub fn hlcc4(&self) -> Decimal
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.
Sourcepub fn weighted_close(&self) -> Decimal
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.
Sourcepub fn ohlc4(&self) -> Decimal
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.
Sourcepub fn dollar_volume(&self) -> Decimal
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.
Sourcepub fn is_gap_fill(&self) -> bool
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.
Sourcepub fn is_inside_bar(&self, prev: &OhlcvBar) -> bool
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.
Sourcepub fn is_outside_bar(&self, prev: &OhlcvBar) -> bool
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.
Sourcepub fn is_engulfing(&self, prev: &OhlcvBar) -> bool
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.
Sourcepub fn is_bullish(&self) -> bool
pub fn is_bullish(&self) -> bool
Returns true if close >= open.
Sourcepub fn is_bearish(&self) -> bool
pub fn is_bearish(&self) -> bool
Returns true if close < open.
Sourcepub fn is_hammer(&self) -> bool
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.
Sourcepub fn is_marubozu(&self) -> bool
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.
Sourcepub fn is_spinning_top(&self) -> bool
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.
Sourcepub fn is_shooting_star(&self) -> bool
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.
Sourcepub fn body_pct(&self) -> Option<Decimal>
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.
Sourcepub fn bar_return(&self) -> Option<Decimal>
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.
Sourcepub fn body_to_range_ratio(&self) -> Option<Decimal>
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.
Sourcepub fn is_long_candle(&self, factor: Decimal) -> bool
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).
Sourcepub fn is_doji(&self, threshold: Decimal) -> bool
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.
Sourcepub fn body_ratio(&self) -> Option<Decimal>
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.
Sourcepub fn true_range(&self, prev: Option<&OhlcvBar>) -> Decimal
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|(ifprevisSome)|low - prev_close|(ifprevisSome)
When prev is None, True Range falls back to high - low.
This is the building block for ATR and volatility calculations.
Sourcepub fn shadow_ratio(&self) -> Option<Decimal>
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.
Sourcepub fn gap_up_from(&self, prev: &OhlcvBar) -> bool
pub fn gap_up_from(&self, prev: &OhlcvBar) -> bool
Returns true if this bar opens above the previous bar’s high (gap up).
Sourcepub fn gap_down_from(&self, prev: &OhlcvBar) -> bool
pub fn gap_down_from(&self, prev: &OhlcvBar) -> bool
Returns true if this bar opens below the previous bar’s low (gap down).
Sourcepub fn gap_from(&self, prev: &OhlcvBar) -> Decimal
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.
Sourcepub fn upper_shadow(&self) -> Decimal
pub fn upper_shadow(&self) -> Decimal
Returns the upper shadow length: high - max(open, close).
Sourcepub fn lower_shadow(&self) -> Decimal
pub fn lower_shadow(&self) -> Decimal
Returns the lower shadow length: min(open, close) - low.
Sourcepub fn duration_nanos(&self) -> i64
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.
Sourcepub fn gap_pct(&self, prev: &OhlcvBar) -> Option<Decimal>
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.
Sourcepub fn has_gap(&self, prev: &OhlcvBar, pct_threshold: Decimal) -> bool
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).
Sourcepub fn from_tick(tick: &Tick) -> Self
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.
Sourcepub fn merge(&self, other: &OhlcvBar) -> Result<OhlcvBar, FinError>
pub fn merge(&self, other: &OhlcvBar) -> Result<OhlcvBar, FinError>
Merges other into self, producing a combined bar spanning both time ranges.
opencomes fromself(the earlier bar)closecomes fromother(the later bar)high/loware the extremes across both barsvolumeandtick_countare summedts_openfromself,ts_closefromother
§Errors
Returns FinError::BarInvariant if the merged bar fails invariant checks (should not
occur for well-formed inputs but is checked defensively).
Sourcepub fn is_bullish_engulfing(&self, prev: &OhlcvBar) -> bool
pub fn is_bullish_engulfing(&self, prev: &OhlcvBar) -> bool
Returns true if this bar is a bullish engulfing of prev.
Conditions:
previs bearish (open > close)selfis bullish (close > open)self.open <= prev.close(opens at or below prev close)self.close >= prev.open(closes at or above prev open)
Sourcepub fn is_bearish_engulfing(&self, prev: &OhlcvBar) -> bool
pub fn is_bearish_engulfing(&self, prev: &OhlcvBar) -> bool
Returns true if this bar is a bearish engulfing of prev.
Conditions:
previs bullish (close > open)selfis bearish (open > close)self.open >= prev.close(opens at or above prev close)self.close <= prev.open(closes at or below prev open)