Skip to main content

Bar

Struct Bar 

Source
pub struct Bar {
    pub time: DateTime<Utc>,
    pub open: f64,
    pub high: f64,
    pub low: f64,
    pub close: f64,
    pub volume: f64,
    pub colors: Option<Box<BarColors>>,
    pub payload: Option<Arc<Value>>,
}
Expand description

Represents a single OHLCV (Open, High, Low, Close, Volume) bar

Each bar represents a discrete time period of trading activity. A bar can also carry colors of its own and a host payload, stand for a single value (from_value), or hold a slot of the time axis without a value (whitespace).

§Example

use chrono::Utc;
use egui_charts::model::Bar;

let bar = Bar::new(
    Utc::now(),
    100.0, // open
    105.0, // high
    98.0,  // low
    103.0, // close
    1000.0 // volume
);

assert!(bar.is_bullish()); // close > open

Fields§

§time: DateTime<Utc>

Timestamp of the bar. Read from JSON as an RFC 3339 time, or as a date alone ("2024-03-15"), which is midnight UTC of that day, the open of its day bar.

§open: f64

Opening price

§high: f64

Highest price during the period

§low: f64

Lowest price during the period

§close: f64

Closing price

§volume: f64

Trading volume

§colors: Option<Box<BarColors>>

Colours of this bar that replace the series’ colours, None for the series’ colours.

§payload: Option<Arc<Value>>

Data of the host’s own attached to this bar. The chart does not read it; it comes back with the bar in the chart’s pointer and data events and in its bar lookups.

Implementations§

Source§

impl Bar

Source

pub fn new( time: DateTime<Utc>, open: f64, high: f64, low: f64, close: f64, volume: f64, ) -> Self

Creates a new bar

Source

pub fn from_date( date: NaiveDate, open: f64, high: f64, low: f64, close: f64, volume: f64, ) -> Self

Creates a daily OHLCV bar from a calendar date, at midnight UTC, matching date-only JSON and CSV imports. The date is independent of the exchange’s time zone. The chart formats this UTC instant in its display time zone, which can show a different date (for example, the previous day at UTC-05:00).

§Example
use chrono::NaiveDate;
use egui_charts::model::Bar;

let date = NaiveDate::from_ymd_opt(2024, 2, 29).unwrap();
let bar = Bar::from_date(date, 100.0, 105.0, 98.0, 103.0, 1000.0);
let point = Bar::from_value_on_date(date, 103.0);
let gap = Bar::whitespace_on_date(date);
assert_eq!(bar.time.to_rfc3339(), "2024-02-29T00:00:00+00:00");
assert_eq!(point.time, bar.time);
assert_eq!(gap.time, bar.time);
assert!(gap.is_whitespace());
Source

pub fn from_value(time: DateTime<Utc>, value: f64) -> Self

A bar of a single value, such as a point of a line, area, baseline or column series: its open, high, low and close are value, its volume zero.

Source

pub fn from_value_on_date(date: NaiveDate, value: f64) -> Self

A from_value bar on a calendar date, at midnight UTC. Date and display time zone conventions are those of from_date.

Source

pub fn whitespace(time: DateTime<Utc>) -> Self

A bar without a value: it holds its slot on the time axis, so a gap such as a halted session stays visible and a time of another series has a slot, but nothing is drawn there and studies take it as a bar without a value. Its open, high, low, close and volume are NaN.

Source

pub fn whitespace_on_date(date: NaiveDate) -> Self

A whitespace bar on a calendar date, at midnight UTC. Date and display time zone conventions are those of from_date.

Source

pub fn is_whitespace(&self) -> bool

Whether this is a whitespace bar: open, high, low, close and volume are all NaN.

Source

pub fn is_bullish(&self) -> bool

Returns true if this is a bullish bar (close > open)

Source

pub fn is_bearish(&self) -> bool

Returns true if this is a bearish bar (close < open)

Source

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

Returns true if this is a doji (open ≈ close)

§Arguments
  • threshold - Ratio of body to range below which it’s considered a doji
Source

pub fn body_height(&self) -> f64

Returns the body height (absolute difference between open and close)

Source

pub fn range(&self) -> f64

Returns the total range (high - low)

Source

pub fn upper_wick(&self) -> f64

Returns the upper wick height

Source

pub fn lower_wick(&self) -> f64

Returns the lower wick height

Source

pub fn typical_price(&self) -> f64

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

Commonly used in technical analysis (e.g., VWAP, CCI)

Source

pub fn weighted_close(&self) -> f64

Returns the weighted close ((high + low + close + close) / 4)

Source

pub fn midpoint(&self) -> f64

Returns the midpoint price ((high + low) / 2)

Source

pub fn avg_price(&self) -> f64

Returns the average price (OHLC / 4)

Source

pub fn body_percentage(&self) -> f64

Returns the body as a percentage of the total range (0.0 to 1.0)

Source

pub fn wick_ratio(&self) -> f64

Returns the ratio of upper wick to lower wick

Returns infinity if lower wick is zero.

Source

pub fn change(&self) -> f64

Returns the price change (close - open)

Source

pub fn change_percent(&self) -> f64

Returns the percentage change ((close - open) / |open| * 100), divided by the magnitude of the open so it has the sign of the change for a negative open too

Source

pub fn body_top(&self) -> f64

Returns the top of the body (max of open and close)

Source

pub fn body_bottom(&self) -> f64

Returns the bottom of the body (min of open and close)

Trait Implementations§

Source§

impl CandlePatterns for Bar

Source§

fn is_hammer(&self) -> bool

Returns true if this is a hammer pattern Read more
Source§

fn is_inverted_hammer(&self) -> bool

Returns true if this is an inverted hammer pattern Read more
Source§

fn is_shooting_star(&self) -> bool

Returns true if this is a shooting star pattern Read more
Source§

fn is_spinning_top(&self) -> bool

Returns true if this is a spinning top pattern Read more
Source§

fn is_marubozu(&self) -> bool

Returns true if this is a marubozu (very long body, little to no wicks) Read more
Source§

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

Returns true if current bar engulfs the previous bar (bullish engulfing) Read more
Source§

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

Returns true if current bar engulfs the previous bar (bearish engulfing) Read more
Source§

fn is_morning_star(&self, prev1: &Bar, prev2: &Bar) -> bool

Returns true if this forms a morning star pattern with two previous bars Read more
Source§

fn is_evening_star(&self, prev1: &Bar, prev2: &Bar) -> bool

Returns true if this forms an evening star pattern with two previous bars Read more
Source§

impl Clone for Bar

Source§

fn clone(&self) -> Bar

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Bar

Source§

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

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

impl<'de> Deserialize<'de> for Bar

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 Display for Bar

Source§

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

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

impl From<MetricPoint> for Bar

Source§

fn from(point: MetricPoint) -> Self

Converts to this type from the input type.
Source§

impl FromIterator<Bar> for BarData

Source§

fn from_iter<I: IntoIterator<Item = Bar>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl PartialEq for Bar

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl Serialize for Bar

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 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> 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
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> MaybeSend for T
where T: Send + Sync + ?Sized,

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

§

impl<T> SerializableAny for T
where T: 'static + Any + Clone + for<'a> Send + Sync,

§

impl<T, S> SimdFrom<T, S> for T
where S: Simd,

§

fn simd_from(_simd: S, value: T) -> T

§

impl<F, T, S> SimdInto<T, S> for F
where T: SimdFrom<F, S>, S: Simd,

§

fn simd_into(self, simd: S) -> T

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.