lfest/config.rs
1use getset::{CopyGetters, Getters};
2
3use crate::{
4 contract_specification::ContractSpecification,
5 prelude::{ConfigError, MarginCurrency},
6};
7
8#[derive(Debug, Clone, Getters, CopyGetters)]
9/// Define the Exchange configuration
10pub struct Config<M>
11where
12 M: MarginCurrency,
13{
14 /// The starting balance of account (denoted in margin currency).
15 /// The concrete `Currency` here defines the futures type.
16 /// If `QuoteCurrency` is used as the margin currency,
17 /// then its a linear futures contract.
18 /// If `BaseCurrency` is used as the margin currency,
19 /// then its an inverse futures contract.
20 #[getset(get_copy = "pub")]
21 starting_wallet_balance: M,
22
23 /// The maximum number of open orders the user can have at any given time
24 #[getset(get_copy = "pub")]
25 max_num_open_orders: usize,
26
27 /// The contract specification.
28 #[getset(get = "pub")]
29 contract_spec: ContractSpecification<M::PairedCurrency>,
30
31 /// The interval by which to sample the returns of user balances.
32 /// This is used to analyze the trading performance later on, to enable things like `sharpe`, `sortino`, anything based on returns.
33 #[getset(get_copy = "pub")]
34 sample_returns_every_n_seconds: u64,
35}
36
37impl<M> Config<M>
38where
39 M: MarginCurrency,
40{
41 /// Create a new Config.
42 ///
43 /// # Arguments:
44 /// `starting_balance`: Initial Wallet Balance, denoted in QUOTE if using
45 /// linear futures, denoted in BASE for inverse futures
46 /// `max_num_open_orders`: The maximum number of open ordes a user can have
47 /// at any time.
48 /// `contract_specification`: More details on the actual contract traded.
49 /// `sample_returns_every_n_seconds`: How often to sample the user balances for computing the returns.
50 /// Is used for computing for example the `sharpe` ratio or anything else that requires ln returns.
51 ///
52 /// # Returns:
53 /// Either a valid `Config` or an Error
54 pub fn new(
55 starting_balance: M,
56 max_num_open_orders: usize,
57 contract_specification: ContractSpecification<M::PairedCurrency>,
58 sample_returns_every_n_seconds: u64,
59 ) -> Result<Self, ConfigError> {
60 if max_num_open_orders == 0 {
61 return Err(ConfigError::InvalidMaxNumOpenOrders);
62 }
63 if starting_balance <= M::new_zero() {
64 return Err(ConfigError::InvalidStartingBalance);
65 }
66
67 Ok(Config {
68 starting_wallet_balance: starting_balance,
69 max_num_open_orders,
70 contract_spec: contract_specification,
71 sample_returns_every_n_seconds,
72 })
73 }
74}