Skip to main content

BarData

Struct BarData 

Source
pub struct BarData {
    pub bars: Vec<Bar>,
}
Expand description

Container for multiple bars with utility methods

This container provides common operations over bar collections.

§Example

use egui_charts::model::BarData;

let data = BarData::new();
assert!(data.is_empty());

Fields§

§bars: Vec<Bar>

Bars in caller-supplied order; construction does not sort or validate them.

Implementations§

Source§

impl BarData

Source

pub fn new() -> Self

Creates a new empty BarData

Source

pub fn with_capacity(capacity: usize) -> Self

Creates BarData with pre-allocated capacity

Source

pub fn from_bars(bars: Vec<Bar>) -> Self

Creates BarData from a vector of bars

Source

pub fn push(&mut self, bar: Bar)

Adds a bar to the dataset

Source

pub fn len(&self) -> usize

Returns the number of bars

Source

pub fn is_empty(&self) -> bool

Returns true if there are no bars

Source

pub fn clear(&mut self)

Clear all bars

Source

pub fn first(&self) -> Option<&Bar>

Get the first bar

Source

pub fn last(&self) -> Option<&Bar>

Get the last bar

Source

pub fn get(&self, index: usize) -> Option<&Bar>

Get a bar by index

Source

pub fn get_mut(&mut self, index: usize) -> Option<&mut Bar>

Get a mutable reference to a bar by index

Source

pub fn iter(&self) -> impl Iterator<Item = &Bar>

Iterate over bars

Source

pub fn min_price(&self) -> Option<f64>

Returns the min price (low) across all bars.

Non-finite values are skipped; None when no finite value remains.

Source

pub fn max_price(&self) -> Option<f64>

Returns the max price (high) across all bars.

Non-finite values are skipped; None when no finite value remains.

Source

pub fn price_range(&self) -> Option<(f64, f64)>

Returns the price range (max_high - min_low)

Source

pub fn max_volume(&self) -> Option<f64>

Returns the max volume across all bars.

Non-finite values are skipped; None when no finite value remains.

Source

pub fn total_volume(&self) -> f64

Returns the total volume across all bars

Source

pub fn avg_volume(&self) -> Option<f64>

Returns the average volume across all bars

Source

pub fn to_heikin_ashi(&self) -> Self

Converts regular OHLC bars to Heikin-Ashi bars

Heikin-Ashi is a Japanese candlestick technique that filters out market noise and highlights trends more clearly than traditional candlesticks.

§Formula
  • HA Close = (Open + High + Low + Close) / 4
  • HA Open = (Previous HA Open + Previous HA Close) / 2
  • HA High = Max(High, HA Open, HA Close)
  • HA Low = Min(Low, HA Open, HA Close)
Source

pub fn to_regular(&self) -> Self

Returns a clone of the bar data (for regular candlesticks)

This method exists for API consistency when switching between chart types.

Source

pub fn slice(&self, start: usize, end: usize) -> &[Bar]

Get a slice of bars within an index range

Source§

impl BarData

Source

pub fn from_csv(path: &str) -> Result<Self, Box<dyn Error + Send + Sync>>

Loads bars from a CSV file

§Format

CSV format: ts,open,high,low,close,volume. The header row is required (any case, spaces around names ignored) and the columns must be in this order. Timestamps are in “YYYY-MM-DD HH:MM:SS” format (UTC), optionally with a fraction of a second, such as “2024-03-15 10:00:00.250”, or a date alone, “2024-03-15”, for midnight UTC of that day, the open of its day bar. A row of NaN values is a whitespace bar.

§Example
use egui_charts::model::BarData;

let data = BarData::from_csv("data.csv").expect("Failed to load CSV");
Source

pub fn to_csv(&self, path: &str) -> Result<(), Box<dyn Error + Send + Sync>>

Exports bars to a CSV file

§Format

CSV format: ts,open,high,low,close,volume Timestamps are in “YYYY-MM-DD HH:MM:SS” format (UTC), with the fraction of a second when the bar’s time has one, such as “2024-03-15 10:00:00.250”

§Example
use egui_charts::model::BarData;

let data = BarData::new();
data.to_csv("output.csv").expect("Failed to export CSV");
Source

pub fn to_csv_string(&self) -> Result<String, Box<dyn Error + Send + Sync>>

Exports bars to a CSV string in memory

§Format

CSV format: ts,open,high,low,close,volume Timestamps are in “YYYY-MM-DD HH:MM:SS” format (UTC), with the fraction of a second when the bar’s time has one, such as “2024-03-15 10:00:00.250”

§Example
use egui_charts::model::BarData;

let data = BarData::new();
let csv = data.to_csv_string().expect("Failed to export CSV");
assert!(csv.starts_with("ts,"));
Source

pub fn to_csv_with_indicators( &self, indicators: &[(String, Vec<f64>)], ) -> Result<String, Box<dyn Error + Send + Sync>>

Exports bars to a CSV string with indicator values alongside bar data

§Format

CSV format: ts,open,high,low,close,volume,indicator_1,indicator_2,… Timestamps are in “YYYY-MM-DD HH:MM:SS” format (UTC), with the fraction of a second when the bar’s time has one, such as “2024-03-15 10:00:00.250”

Each indicator is a (name, values) pair where values is aligned to the bar data by index. Missing values (e.g. SMA warmup period) are empty strings.

§Example
use egui_charts::model::BarData;

let data = BarData::new();
let indicators: Vec<(String, Vec<f64>)> = vec![
    ("SMA_20".to_string(), vec![]),
];
let csv = data.to_csv_with_indicators(&indicators)
    .expect("Failed to export CSV with indicators");
assert!(csv.starts_with("ts,"));
Source

pub fn to_json(&self, path: &str) -> Result<(), Box<dyn Error + Send + Sync>>

Exports bars to a JSON file

§Format

JSON array of bar objects with full ISO 8601 timestamp

§Example
use egui_charts::model::BarData;

let data = BarData::new();
data.to_json("output.json").expect("Failed to export JSON");
Source

pub fn to_json_string(&self) -> Result<String, Box<dyn Error + Send + Sync>>

Returns JSON string representation of the bars

§Example
use egui_charts::model::BarData;

let data = BarData::new();
let json = data.to_json_string().expect("Failed to serialize");
Source

pub fn from_json_string( json: &str, ) -> Result<Self, Box<dyn Error + Send + Sync>>

Load bars from a JSON string

§Example
use egui_charts::model::BarData;

let json = "[]";
let data = std::thread::spawn(move || BarData::from_json_string(json))
    .join().expect("Worker panicked").expect("Failed to parse");
Source

pub fn from_json(path: &str) -> Result<Self, Box<dyn Error + Send + Sync>>

Load bars from a JSON file

§Example
use egui_charts::model::BarData;

let data = BarData::from_json("data.json").expect("Failed to load");

Trait Implementations§

Source§

impl Clone for BarData

Source§

fn clone(&self) -> BarData

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 BarData

Source§

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

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

impl Default for BarData

Source§

fn default() -> BarData

Returns the “default value” for a type. Read more
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 Index<usize> for BarData

Source§

type Output = Bar

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl IndexMut<usize> for BarData

Source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl IntoIterator for BarData

Source§

type Item = Bar

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<Bar>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a> IntoIterator for &'a BarData

Source§

type Item = &'a Bar

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, Bar>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

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
§

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> 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, 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.