Skip to main content

SymbolExpression

Struct SymbolExpression 

Source
pub struct SymbolExpression { /* private fields */ }
Expand description

An arithmetic expression over symbols, such as AAPL-MSFT or (BTCUSD+ETHUSD)/2, charted as one synthetic series.

Symbols, decimal numbers, +, -, *, /, a leading minus and parentheses are accepted, with the usual precedence; spaces are ignored. A symbol is a run of letters, digits and ., :, _ or !, such as NASDAQ:AAPL, BRK.B or ES1!, that is not a number. The text it formats to is the symbol of its Instrument. At most 256 tokens are accepted to bound recursive parsing and evaluation of user input.

use chrono::{TimeZone, Utc};
use egui_charts::model::Bar;
use egui_charts::services::SymbolExpression;

let spread = SymbolExpression::parse("aapl - msft").unwrap();
assert_eq!(spread.symbols(), ["aapl", "msft"]);
assert_eq!(spread.to_string(), "aapl-msft");

let day = |d| Utc.with_ymd_and_hms(2024, 3, d, 0, 0, 0).unwrap();
let aapl = [Bar::new(day(1), 170.0, 172.0, 169.0, 171.0, 0.0)];
let msft = [Bar::new(day(1), 410.0, 415.0, 405.0, 412.0, 0.0)];
let bars = spread
    .bars(|symbol| match symbol {
        "aapl" => Some(&aapl[..]),
        "msft" => Some(&msft[..]),
        _ => None,
    })
    .unwrap();
assert_eq!((bars[0].open, bars[0].close), (-240.0, -241.0));

Implementations§

Source§

impl SymbolExpression

Source

pub fn parse(text: &str) -> Result<Self, ExpressionError>

Parses text, which may also be a lone symbol.

Source

pub fn symbols(&self) -> Vec<&str>

The symbols of the expression, each once, in order of appearance.

Source

pub fn evaluate(&self, value: impl Fn(&str) -> Option<f64>) -> Option<f64>

The value of the expression with each symbol replaced by value(symbol), or None when a symbol has no finite value or any intermediate result is not finite, as after a division by zero.

Source

pub fn bars<'a>( &self, history: impl Fn(&str) -> Option<&'a [Bar]>, ) -> Result<Vec<Bar>, ExpressionError>

The synthetic series from the time-ordered bars history returns for each symbol.

A bar is made for each time at which every symbol has a bar. Its open and close apply the expression to the components’ opens and closes, its high and low to their highs and lows, and the high and low then widen to cover all four results, since a difference or a quotient of highs need not be the highest value. Times whose result is not finite are left out. Volume is zero because the components have no shared traded volume. A symbol without history is an ExpressionError::UnknownSymbol.

Trait Implementations§

Source§

impl Clone for SymbolExpression

Source§

fn clone(&self) -> SymbolExpression

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 SymbolExpression

Source§

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

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

impl Display for SymbolExpression

Source§

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

The expression without spaces and with only the parentheses its order needs.

Source§

impl PartialEq for SymbolExpression

Source§

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

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

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for SymbolExpression

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