Skip to main content

Trading

Struct Trading 

Source
pub struct Trading {
    pub quantity: f64,
    pub presets: Vec<OrderPreset>,
    pub account_panel: AccountPanel,
    pub display: TradingSettings,
    /* private fields */
}
Expand description

The trading controls of one chart, connected to the host’s Broker.

Call poll every frame, show_ticket before laying out the chart, so the order ticket takes its panel beside it, and show after the chart. Open the order ticket with open_ticket, for example from the chart menu’s Order entries, and route the chart’s Buy and Sell buttons (Chart::take_quote_action) to trade. Orders reach the broker only from the ticket’s submit button, or once the user confirms them in the depth ladder, or at once from Buy and Sell and the ladder when the user turned on TradingSettings::instant_orders.

use egui_charts::model::OrderSide;
use egui_charts::widget::QuoteSide;

trading.poll(ui.ctx(), toasts);
trading.show_ticket(ui, chart);
chart.show(ui);
if let Some(side) = chart.take_quote_action() {
    if side == QuoteSide::Ticket {
        trading.open_ticket(egui_charts::model::OrderRequest::new(chart.symbol(),
            OrderSide::Buy, trading.quantity, egui_charts::model::OrderType::Market));
    } else {
        let side = if side == QuoteSide::Buy { OrderSide::Buy } else { OrderSide::Sell };
        trading.trade(chart.symbol().to_owned(), side);
    }
}
trading.show(ui, chart);

Fields§

§quantity: f64

The quantity the ticket opens with, and that instant orders and the chart menu’s orders use. Positive, in the symbol’s trading units. The chart’s legend shows it under the quote buttons’ spread, where a click opens a calculator that changes it.

§presets: Vec<OrderPreset>

The quantity and brackets the user saved in the ticket. Persist them in the host.

§account_panel: AccountPanel

The account panel’s names and the history it lists.

§display: TradingSettings

The chart settings’ trading options: what show draws, whether Buy and Sell trade at once, which events raise notifications and whether fills ask for a sound. Copy the chart settings’ trading options here when the user applies them.

Implementations§

Source§

impl Trading

Source

pub fn show_account_panel( &mut self, ui: &mut Ui, chart_symbol: &str, ) -> Option<AccountPanelAction>

Shows the account panel in ui, such as a bottom panel of the terminal, for a chart showing chart_symbol. Returns what the user asked of the host; cancelling, changing and closing go to the broker after the user confirms them in show.

Source§

impl Trading

Source

pub fn poll(&mut self, ctx: &Context, toasts: &mut Toasts)

Applies the broker’s pushes and answers, reconnects a lagged stream, and reports refused orders and fills in toasts. Call it every frame, even while the chart is hidden.

Source

pub fn take_execution_sound(&mut self) -> Option<String>

The sound to play for the fills since the last call, when the user turned on TradingSettings::play_execution_sound: the name the user chose, or empty for the host’s first sound. The host plays it; call this after poll.

Source§

impl Trading

Source

pub fn show_ticket(&mut self, ui: &mut Ui, chart: &Chart)

Draws the order ticket, while it is open, as a panel at the right of ui for the chart’s symbol. Call it before laying out the chart, so the chart takes the space left. Sends the order the user submits, and the orders the user places from the depth ladder; a change to a working order or the position waits for the confirmation show asks for.

Source

pub fn export_image( &self, chart: &Chart, ctx: &Context, options: &ImageExportOptions, ) -> Result<ChartImage, ExportError>

Exports the chart with its visible trading overlays when TradingSettings::include_in_image is enabled. Account data and display settings stay unchanged; the host delivers the returned frame.

Source

pub fn export_png( &self, chart: &Chart, ctx: &Context, options: &ImageExportOptions, ) -> Result<Vec<u8>, ExportError>

Encodes Self::export_image as PNG bytes for host file, clipboard or sharing delivery.

Source

pub fn export_svg( &self, chart: &Chart, ctx: &Context, options: &ImageExportOptions, ) -> Result<String, ExportError>

Writes Self::export_image as SVG, as Chart::export_svg does.

Source

pub fn draw_export(&self, ui: &mut Ui, chart: &mut Chart)

Draws a frozen chart and its opted-in trading overlays inside an image layout callback. Use with Chart::export_layout_image and Chart::export_snapshot for several charts.

Source

pub fn show(&mut self, ui: &mut Ui, chart: &mut Chart)

Draws the chart symbol’s working orders, brackets, positions and execution marks, respecting display. Hovering a mark lists the fills of its bar. Asks the user to confirm a change made on a line or in the depth ladder before sending it.

Source§

impl Trading

Source

pub fn set_quote(&mut self, symbol: &str, quote: Quote)

Sets the latest quote of symbol, such as the one WatchlistPanel::quote holds for the chart’s symbol. While the chart shows symbol, the ticket enters a market buy at the quote’s ask and a market sell at its bid; without them, at the last price.

Source

pub fn set_instrument(&mut self, instrument: Instrument)

Describes instrument to the trading panels: its price decimals write the prices of its symbol in the account tables, and its pip size gives the ticket a pip value when the broker’s TradingRules::pip_value is absent. The chart’s own instrument is used for the symbol it shows, so call this for the other symbols of the account, such as those of positions and working orders. Its logo marks the symbol in the account tables and notices. A symbol without an instrument shows two decimals.

Source

pub fn set_formatter(&mut self, formatter: Option<Arc<dyn TradingFormatter>>)

Writes the prices, quantities, spreads and pip values of the ticket, the depth ladder and the account tables with formatter, where it returns text. None restores the built-in text.

Source

pub fn set_service(&mut self, service: Arc<dyn Broker>)

Connects to service, dropping the previous account’s state.

Source

pub fn disconnect(&mut self)

Disconnects from the broker, dropping the account’s state.

Source

pub fn is_connected(&self) -> bool

Whether a broker is set and connected: the last status it pushed on the current stream is FeedStatus::Live, or it pushed none. False while it reports that it is Connecting or Disconnected, and without a broker; has_broker tells whether one is set.

Source

pub fn has_broker(&self) -> bool

Whether a broker is set, from set_service until disconnect, connected or not.

Source

pub fn capabilities(&self) -> Capabilities

The order types and features the account accepts; none before the broker reports them.

Source

pub fn open_ticket(&mut self, request: OrderRequest)

Opens the order ticket filled from request, for the user to check and submit.

Source

pub fn toggle_ticket(&mut self, symbol: String)

Opens the order ticket for symbol, or closes it when it is open, as the toolbar’s Trade button does.

Source

pub fn trade(&mut self, symbol: String, side: OrderSide)

Handles the Buy or Sell button: a market order of quantity at once with TradingSettings::instant_orders, otherwise the ticket for one.

Trait Implementations§

Source§

impl Default for Trading

Source§

fn default() -> Self

Returns the “default value” for a type. 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> 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, 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, 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.