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
impl PaperAccount
Sourcepub fn new(currency: impl Into<String>, balance: f64) -> Self
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.
Sourcepub fn leverage(self, leverage: f64) -> Self
pub fn leverage(self, leverage: f64) -> Self
Sets the leverage: margin is the market value divided by it. Values below 1 count as 1.
Sourcepub fn commission(self, rate: f64) -> Self
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%.
Sourcepub fn set_price(&self, symbol: &str, price: f64)
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.
Sourcepub fn set_replay_time(&self, time: Option<DateTime<Utc>>)
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.
Trait Implementations§
Source§impl Broker for PaperAccount
impl Broker for PaperAccount
Source§fn connect(&self, events: Events<AccountEvent>)
fn connect(&self, events: Events<AccountEvent>)
Source§fn place(&self, order: OrderRequest, reply: Reply<OrderId>)
fn place(&self, order: OrderRequest, reply: Reply<OrderId>)
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<()>)
fn cancel(&self, id: OrderId, reply: Reply<()>)
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<()>)
fn modify(&self, id: OrderId, order: OrderRequest, reply: Reply<()>)
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>)
fn rules(&self, symbol: &str, reply: Reply<TradingRules>)
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>)
fn preview(&self, order: OrderRequest, reply: Reply<OrderPreview>)
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>)
fn history(&self, request: HistoryRequest, reply: Reply<HistoryPage>)
connect pushed, for
the kind and range of request. By default the answer is FeedError::NotSupported, and
only the pushed history exists. Read moreAuto Trait Implementations§
impl !Freeze for PaperAccount
impl RefUnwindSafe for PaperAccount
impl Send for PaperAccount
impl Sync for PaperAccount
impl Unpin for PaperAccount
impl UnsafeUnpin for PaperAccount
impl UnwindSafe for PaperAccount
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> 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