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
impl SymbolExpression
Sourcepub fn parse(text: &str) -> Result<Self, ExpressionError>
pub fn parse(text: &str) -> Result<Self, ExpressionError>
Parses text, which may also be a lone symbol.
Sourcepub fn symbols(&self) -> Vec<&str>
pub fn symbols(&self) -> Vec<&str>
The symbols of the expression, each once, in order of appearance.
Sourcepub fn evaluate(&self, value: impl Fn(&str) -> Option<f64>) -> Option<f64>
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.
Sourcepub fn bars<'a>(
&self,
history: impl Fn(&str) -> Option<&'a [Bar]>,
) -> Result<Vec<Bar>, ExpressionError>
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
impl Clone for SymbolExpression
Source§fn clone(&self) -> SymbolExpression
fn clone(&self) -> SymbolExpression
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for SymbolExpression
impl Debug for SymbolExpression
Source§impl Display for SymbolExpression
impl Display for SymbolExpression
Source§impl PartialEq for SymbolExpression
impl PartialEq for SymbolExpression
impl StructuralPartialEq for SymbolExpression
Auto Trait Implementations§
impl Freeze for SymbolExpression
impl RefUnwindSafe for SymbolExpression
impl Send for SymbolExpression
impl Sync for SymbolExpression
impl Unpin for SymbolExpression
impl UnsafeUnpin for SymbolExpression
impl UnwindSafe for SymbolExpression
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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