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
use crate::{
market_state::MarketState,
order_margin::OrderMargin,
prelude::{Position, RiskError},
types::{Currency, LimitOrder, MarginCurrency, MarketOrder, Pending, QuoteCurrency},
};
pub(crate) trait RiskEngine<M, UserOrderId>
where
M: Currency + MarginCurrency,
UserOrderId: Clone + std::fmt::Debug + Eq + PartialEq + std::hash::Hash + Default,
{
/// Checks if the account it able to satisfy the margin requirements for a new market order.
///
/// When a trader submits an order to increase their position,
/// the risk engine will typically calculate the margin requirements as if the new order is executed and added to their existing positions.
/// The risk engine will consider the notional value of the new order, the current market price,
/// and the leverage used to determine the required margin for the entire position.
///
/// On the other hand, when a trader submits an order to decrease their position,
/// the risk engine will typically calculate the margin requirements as if the order is executed and reduces the size of their existing position.
/// The risk engine will consider the notional value of the order, the current market price,
/// and the leverage used to determine the new required margin for the remaining position.
///
/// # Returns:
/// If Err, the account cannot satisfy the margin requirements.
fn check_market_order(
&self,
position: &Position<M::PairedCurrency>,
position_margin: M,
order: &MarketOrder<M::PairedCurrency, UserOrderId, Pending<M::PairedCurrency>>,
fill_price: QuoteCurrency,
available_wallet_balance: M,
) -> Result<(), RiskError>;
/// Checks if the account it able to satisfy the margin requirements for a new limit order.
fn check_limit_order(
&self,
position: &Position<M::PairedCurrency>,
order: &LimitOrder<M::PairedCurrency, UserOrderId, Pending<M::PairedCurrency>>,
available_wallet_balance: M,
order_margin: &OrderMargin<M::PairedCurrency, UserOrderId>,
) -> Result<(), RiskError>;
/// Ensure the account has enough maintenance margin, to keep the position open.
/// The maintenance margin is the minimum amount of funds that must be maintained in a trader's account
/// to ensure that they can meet any losses that may occur due to adverse price movements in the futures contract.
///
/// # Arguments:
/// `market_state`: The current market information.
/// `account`: The user account.
///
/// # Returns:
/// If Err, the account must be liquidated.
fn check_maintenance_margin(
&self,
market_state: &MarketState,
position: &Position<M::PairedCurrency>,
) -> Result<(), RiskError>;
}