Skip to main content

PaperAccount

Struct PaperAccount 

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

A paper account: a Broker that fills orders against the prices the host feeds it, so the order ticket, the chart’s order and position lines and the account panel work as they do with a real account.

Feed it the last price of every symbol it trades with set_price, live or replayed. During replay, call set_replay_time before each price so fills and history use the source bar’s time. Only good-till-cancelled orders are supported; the account has no trading-session calendar. Market orders fill at the last price; limit and stop orders fill at the first price that reaches them, a limit at that price or better; a triggered stop-limit works on as a limit order. An order’s take profits and stop loss become exit orders once it fills, for the quantity the fill opened: take-profit levels each close their own quantity, the first ones first when the fill opened less than the order’s quantity, as after closing an opposite position. When a take profit fills, the stop loss shrinks by its quantity and is cancelled once nothing is left; when the stop loss fills, the take profits are cancelled. Each symbol has one net position with its average price: a fill against it books the realised profit or loss of the closed quantity, and a fill beyond it opens the other side at the fill price. A position holds its market value divided by the leverage as margin, and a working order the same for its quantity at its price. Each fill pays the commission rate times its value. Placement and modification reserve buying power across all symbols, including working orders and their commissions. Market orders that reduce exposure remain possible when funds are insufficient. Position exits only reduce the remaining position. Every change is pushed as an AccountEvent, with the most recent 1,000 finished orders, executions, balance changes and journal entries. The history bound keeps a reconnect snapshot below the event stream’s burst limit for ordinary interactive accounts.

use std::sync::Arc;
use egui_charts::services::PaperAccount;

let paper = Arc::new(PaperAccount::new("USD", 100_000.0).leverage(10.0).commission(0.001));
paper.set_price("AAPL", 187.5);
// trading.set_service(paper.clone());

Implementations§

Source§

impl PaperAccount

Source

pub fn new(currency: impl Into<String>, balance: f64) -> Self

An empty account holding balance in currency, without leverage or commission.

§Panics

Panics if balance is NaN or infinite.

Source

pub fn leverage(self, leverage: f64) -> Self

Sets the leverage: margin is the market value divided by it. Values below 1 count as 1.

Source

pub fn commission(self, rate: f64) -> Self

Sets the commission each fill pays, as a fraction of its value, such as 0.001 for 0.1%.

Source

pub fn set_price(&self, symbol: &str, price: f64)

Takes price as the last price of symbol: fills the working orders it reaches and revalues the position. Non-finite prices are ignored.

Source

pub fn set_replay_time(&self, time: Option<DateTime<Utc>>)

Uses time for orders, executions, balance changes and journal entries during replay. Set it to the revealed source bar’s time before feeding its price. None restores the live UTC clock.

Source

pub fn reset(&self)

Returns the account to its starting balance without orders, positions or history, retaining the host’s prices and replay clock. The terminal shows the fresh account once it connects again, such as through ui::trading::Trading::set_service with the ui feature.

Trait Implementations§

Source§

impl Broker for PaperAccount

Source§

fn connect(&self, events: Events<AccountEvent>)

Pushes the account (balance, currency, capabilities) first, then the working orders and open positions, then orders, positions, executions and connection status as they change. A broker that keeps history pushes it after the open state: the finished orders, the balance changes and the journal, oldest first, which the account panel lists. The terminal calls it when it starts and again, with a new stream, after the stream lagged; each stream starts from nothing, so push the whole current state again. The old stream is inactive from then on: push into the newest one only. Read more
Source§

fn place(&self, order: OrderRequest, reply: Reply<OrderId>)

Sends order to the broker. The terminal calls this only after the user confirmed the order, or the host performed the place order command itself, so do not ask again. Answer with the broker’s id once the order is accepted, or with a FeedError the terminal shows. The order’s AccountEvent::Order may arrive before or after that answer.
Source§

fn cancel(&self, id: OrderId, reply: Reply<()>)

Cancels the working order id. The terminal calls this only after the user confirmed, so do not ask again. Answer once the broker accepted the cancellation, or with a FeedError the terminal shows; the order’s OrderStatus::Cancelled update may arrive before or after that answer.
Source§

fn modify(&self, id: OrderId, order: OrderRequest, reply: Reply<()>)

Replaces the working order id with order, which differs in its prices or brackets, after the user dragged one of its lines, added or removed a take profit or stop loss, and confirmed. Called only for an account whose Capabilities::modify is set. Answer once the broker accepted the change; push the changed order as an AccountEvent::Order. By default every change is refused as FeedError::NotSupported.
Source§

fn rules(&self, symbol: &str, reply: Reply<TradingRules>)

Answers how symbol trades: its quantity step, minimum and maximum quantity, point value and times in force. By default the answer is FeedError::NotSupported, and the order ticket assumes TradingRules::new.
Source§

fn preview(&self, order: OrderRequest, reply: Reply<OrderPreview>)

Estimates what order would cost without placing it. Called only for an account whose Capabilities::preview is set, when the user asks to preview an order; the terminal places the previewed order only after the user confirms it. By default every preview is refused as FeedError::NotSupported.
Source§

fn history(&self, request: HistoryRequest, reply: Reply<HistoryPage>)

Answers one page of finished history older than what connect pushed, for the kind and range of request. By default the answer is FeedError::NotSupported, and only the pushed history exists. 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.