Skip to main content

egui_charts/services/
quotes.rs

1//! Quotes of many symbols at once, for watchlists, the symbol's panels and the order ticket.
2
3use super::Events;
4use crate::data::MaybeSend;
5use crate::model::Quote;
6
7/// The host's quote stream. The watchlist subscribes to the symbols of its active list and the
8/// chart's symbol, and hands the chart symbol's quote to the symbol details panel, the symbol
9/// header and the order ticket.
10///
11/// Methods are called on the UI thread and must return at once. Push from any thread, runtime or
12/// browser task.
13///
14/// ```
15/// use egui_charts::model::Quote;
16/// use egui_charts::services::{Events, Quotes};
17/// use std::time::Duration;
18///
19/// /// Pushes every symbol at 100 once a second, in place of a host's market data.
20/// struct Flat;
21///
22/// impl Quotes for Flat {
23///     fn subscribe(&self, symbols: &[String], events: Events<(String, Quote)>) {
24///         let symbols = symbols.to_vec();
25///         std::thread::spawn(move || {
26///             while events.is_active() {
27///                 for symbol in &symbols {
28///                     let quote = Quote::new(100.0, 99.0, chrono::Utc::now()).with_volume(1e6);
29///                     events.push((symbol.clone(), quote));
30///                 }
31///                 std::thread::sleep(Duration::from_secs(1));
32///             }
33///         });
34///     }
35/// }
36/// ```
37pub trait Quotes: MaybeSend + 'static {
38    /// Starts pushing the quotes of `symbols` into `events`, each with its symbol, until the
39    /// stream is inactive: that is the unsubscription, so stop then. Push the current quote of
40    /// each symbol first, since a subscription starts from none. The watchlist subscribes again,
41    /// with a new stream, whenever its symbols change and after the stream lagged, and stops the
42    /// old one.
43    ///
44    /// The stream takes at most 10,000 pushes between two frames: coalesce the updates of a
45    /// symbol rather than pushing each tick.
46    fn subscribe(&self, symbols: &[String], events: Events<(String, Quote)>);
47}