septoria/api/trading/account.rs
1use std::fmt;
2
3use chrono::prelude::*;
4use serde::{Deserialize, Serialize};
5use serde_variant::to_variant_name;
6
7use crate::api::Requests;
8use crate::client::TradingClient;
9use crate::error::Error;
10
11mod documents;
12/// Module for interacting with the withdrawal related endpoints
13pub mod withdrawals;
14
15///
16#[derive(Deserialize, Debug)]
17pub struct AccountInformation<T> {
18 /// Timestamp of your API request
19 pub time: String,
20 /// Environment the request was placed in: "paper" or "money"
21 // TODO: Make this an enum
22 pub mode: String,
23 /// API returns "ok" when account was successfully retrieved.
24 pub status: String,
25 /// The actual results of the query. Depends upon the given generics
26 pub results: T,
27}
28
29/// Struct for account information.
30// TODO: Make periods into chrono types
31#[derive(Serialize, Deserialize, Debug)]
32pub struct AccountResults {
33 /// Timestamp for when you created your account
34 pub created_at: DateTime<Utc>,
35 /// Unique Identification number for your account
36 pub account_id: String,
37 /// Your first name
38 pub firstname: String,
39 /// Your last name
40 pub lastname: Option<String>,
41 /// Your specified email address
42 pub email: String,
43 /// Your specified phone number
44 pub phone: Option<String>,
45 /// Your specified address
46 pub address: Option<String>,
47 /// The billing address you provided for your account
48 pub billing_address: Option<String>,
49 /// The billing email adress you provided for your account
50 pub billing_email: Option<String>,
51 /// The billing name you provided for your account
52 pub billing_name: Option<String>,
53 /// The billing VAT number you provided for your account
54 pub billing_vat: Option<String>,
55 /// The mode your account is currently on - "paper" or "money"
56 // TODO: Make this an enum
57 pub mode: String,
58 /// Identification Number of your securities account
59 pub deposit_id: Option<String>,
60 /// The internal client identification number related to your account
61 pub client_id: Option<String>,
62 /// The account reference number
63 pub account_number: Option<String>,
64 /// The account reference number
65 pub iban_brokerage: Option<String>,
66 /// IBAN of the brokerage account at our partner bank.
67 /// This is the IBAN you can transfer money from your referrence account to.
68 pub iban_origin: Option<String>,
69 /// Bank name your reference account is located a
70 pub bank_name_origin: Option<String>,
71 /// This is your account balance - the amount of money you have available in your brokerage account.
72 /// The balance calculates as follows:
73 /// (End-of-day balance from the day before) + (amount_sold_intraday)
74 /// - (amount_bought_intraday) - (amount_open_withdrawals).
75 pub balance: i64,
76 /// This number shows you how much cash you have left to invest.
77 ///
78 /// The cash_to_invest calculates as follows:
79 ///
80 /// (balance) - (amount_open_orders)
81 pub cash_to_invest: i64,
82 /// This number shows you how much cash you have in your account to withdraw to your reference account.
83 ///
84 /// cash_to_withdraw is calculated as follows:
85 ///
86 /// (Your end-of-day balance from the day before) - (amount_bought_intraday)
87 /// - (amount_open_withdrawals) - (amount_open_orders).
88 pub cash_to_withdraw: i64,
89 /// This is the intraday buy order amount.
90 /// For example, if you bought 2 shares for 100€ each that day, the amount_bought_intraday
91 /// would be 200€ (the API would return 2000000 in that case,
92 /// see the [numbers page](https://docs.lemon.markets/numbers) for more information).
93 pub amount_bought_intraday: i64,
94 /// This is the intraday sell order amount.
95 /// For example, if you sold 3 shares for 50€ each that day,
96 /// the amount_sold_intraday would be 150€ (the API would return 1500000 in that case,
97 /// see the [numbers page](https://docs.lemon.markets/numbers) for more information).
98 pub amount_sold_intraday: i64,
99 /// This is the intraday amount of open orders.
100 // If you place an order that has amount X and its status is open,
101 // the amount_open_orders would be the order amount
102 // (+ the sum of all other open intraday orders).
103 pub amount_open_orders: i64,
104 /// For example, if you withdraw 500€ to your reference account,
105 /// amount_open_withdrawals would return 5000000
106 /// (see the [numbers page](https://docs.lemon.markets/numbers) for more information
107 /// on the numbers format in the Trading API).
108 pub amount_open_withdrawals: i64,
109 /// This is the amount of estimated taxes (25%) for your intraday sell orders.
110 /// For example, if you made a profit of 100€ from your intraday sell orders,
111 /// the API would return 250000
112 /// (see the [numbers page](https://docs.lemon.markets/numbers
113 /// for more information in the Trading API
114 pub amount_estimate_taxes: i64,
115 /// Timestamp of live trading account approval
116 pub approved_at: Option<DateTime<Utc>>,
117 /// We offer different subscription plans for trading with lemon.markets.
118 /// This endpoint tells you which plan you are currently on - go, investor, trader, or b2b.
119 // TODO: Make this an enum
120 pub trading_plan: String,
121 /// We offer different subscription plans for trading with lemon.markets.
122 /// This endpoint tells you which plan you are currently on - go, investor, trader, or b2b.
123 // TODO: Make this an enum
124 pub data_plan: String,
125 /// Your tax tax allowance - between 0 and 801 €, as specified in your onboarding process
126 pub tax_allowance: Option<i64>,
127 /// Relevant start date for your tax allowance (usually 01/01/ of respective year)
128 pub tax_allowance_start: Option<DateTime<Utc>>,
129 /// Relevant end date for your tax allowance (usually 31/12/ of respective year)
130 pub tax_allowance_end: Option<DateTime<Utc>>,
131}
132
133/// Enum for the different ways of sorting the results
134#[derive(Deserialize, Serialize, Debug)]
135pub enum Sorting {
136 /// Use asc_ to sort your bank statements in ascending order (oldest ones first),
137 #[serde(rename = "asc_")]
138 Asc,
139 /// Desc to sort your bank statements in descending order (newest ones first).
140 #[serde(rename = "desc")]
141 Desc,
142}
143
144impl fmt::Display for Sorting {
145 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
146 match to_variant_name(self) {
147 Ok(name) => write!(f, "{}", name),
148 _ => write!(f, ""),
149 }
150 }
151}
152
153impl TradingClient {
154 /// Get account information
155 pub fn get_account_information(&self) -> Result<AccountInformation<AccountResults>, Error> {
156 const PATH: &str = "account";
157 let resp = self.get::<AccountInformation<AccountResults>>(PATH);
158 match resp {
159 Ok(r) => Ok(r),
160 Err(e) => Err(e),
161 }
162 }
163}
164
165#[cfg(test)]
166mod test {
167 use std::env;
168
169 use crate::client::TradingClient;
170
171 #[test]
172 fn test_get_account_information() {
173 dotenv::dotenv().unwrap();
174 let api_key = env::var("LEMON_MARKET_TRADING_API_KEY").unwrap();
175 let client = TradingClient::paper_client(&api_key);
176 let resp = client.get_account_information().unwrap();
177 assert_eq!(resp.status, "ok");
178 }
179}