1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use crate::platform::id::Id;
use std::fmt::{Display, Formatter};
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Account {
/// The account's status.
pub id: Id,
/// The currently available buying power
pub buying_power: f64,
/// Cash balance.
pub cash: f64,
/// The currency the account uses.
pub currency: String,
/// The current number of day trades that have been made in the last
/// five trading days (including today).
pub daytrade_count: u64,
/// If this account has been flagged as a day trading account or not.
pub day_trader: bool,
/// Real-time mark-to-market value of all long positions held in the account.
pub market_value_long: f64,
/// Real-time mark-to-market value of all short positions held in the account.
pub market_value_short: f64,
/// The sum of `cash`, `market_value_long`, and `market_value_short`.
pub equity: f64,
}
impl Account {
#[cfg(test)]
pub fn fixture() -> Self {
Self {
buying_power: 1000.0,
cash: 500.0,
equity: 500.0,
..Default::default()
}
}
}
impl Display for Account {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let buying_power = self.buying_power;
let equity = self.equity;
let cash = self.cash;
write!(
f,
"buying power: {:.2}, equity: {:.2}, cash: {:.2}",
buying_power, equity, cash
)
}
}
#[cfg(test)]
mod test {
use crate::platform::account::Account;
#[test]
fn display() {
let account = Account {
buying_power: 100.0,
equity: 200.0,
cash: 50.0,
..Default::default()
};
let display = account.to_string();
let expected = "buying power: 100.00, equity: 200.00, cash: 50.00";
assert_eq!(display, expected)
}
}