pub struct SpotMarginLiquidation(/* private fields */);Expand description
When the simulated venue liquidates a spot margin account, what it charges for it, and whether
it warns first. Set by SpotMarginConfig::with_liquidation.
§When
The venue checks the account’s margin level after each of its inputs, a market tick or a
move of its clock, since those are what change it: prices move, and interest falls due. The
level is the value of everything the account holds over the value of what it owes, loans plus
interest, both valued as the leverage limit values them (see SpotMarginConfig’s
# How much it lends): at the mid of a spot instrument pairing each asset with the valuation
asset, else its last price. What it holds of an asset beyond what it owes of it counts at the
asset’s liquidation_collateral_ratio, one
by default (see SpotMarginConfig’s # Collateral ratios). There is no index price, so a
market’s own mid stands in for the one a venue liquidates on. An asset the venue cannot value counts for nothing, which can bring
a liquidation forward but never put one off. While it cannot value a debt it does not check,
and warns once.
At or below level it liquidates.
§How
It sends an AccountNotice of
NoticeKind::Liquidation, then closes every debt:
- It cancels every open order, resting or held, releasing what each held.
- It repays each debt from the asset’s own free balance, interest first.
- It repays what is still owed of the valuation asset with the proceeds of selling the account’s other assets, each on a spot instrument pairing it with the valuation asset, in name order, as much of each as is needed.
- It buys back what is still owed of each other asset, in name order, on a spot instrument pairing it with the valuation asset, selling other assets for the valuation asset first as in step 3 when the account holds too little of it. What a buy delivers repays the debt.
- It charges the fee:
fee_ratetimes the value of the debt this repaid, in the valuation asset, raised as in step 3 if the account holds too little of it.
Each forced trade fills as a market order would on arrival, as a taker: priced by the venue’s
fill model at its current market, slipped, limited by the depth on offer, and charged the
venue’s fee. It is reported as [balance.., trade], the Trade
carrying TradeOrigin::Liquidation, no client order
id and an unknown strategy, with no order snapshot, since no client placed it. It is never
held for a bar or a fresh market. Once priced it has drawn from the venue’s slippage stream,
even if it then trades nothing, so a liquidation that runs over several inputs draws on each.
The fee is reported as [balance, cash flow], a
CashFlowKind::LiquidationFee with no
instrument.
§When it cannot close every debt
A debt can outlast an input when the depth on offer is too thin, when an asset has no market to trade on, or when the account holds too little to repay it. The venue then warns once, and leaves the rest owed: it has no insurance fund, and nothing floors the account’s balances.
The liquidation continues on each later input until no debt is left, without another notice: each input cancels whatever the account has since placed, repays what it can, and charges the fee on what it repaid.
Each pass’s fee is charged out of the account’s remaining assets after that pass’s repayments, selling for it as in step 3, as a venue takes its fee out of the proceeds of the liquidation’s trades. So a debt a pass leaves owed has that much less to be repaid from on the next pass. A fee the account cannot pay takes the valuation asset’s free balance below zero.
While the liquidation is under way the venue lends nothing. An order that would need a loan is rejected as unaffordable, its reason showing nothing borrowable, and so is the fill of a held order that would need one. One the account can pay for from its own balance is accepted, and cancelled by the next input if it is still open. A debt the venue cannot trade out of, for want of a market or of anything left to sell, keeps lending off until the account repays it.
§Margin call
Off by default. With with_margin_call, the venue sends
NoticeKind::MarginCall when the margin level falls
to or below that level, and
NoticeKind::MarginRestored when it rises back
above it or the account no longer owes anything, once per crossing. A liquidation ends a margin
call without a notice.
Each notice carries the margin level that caused it, None for an account that owes nothing,
and its kind’s name as its status.
§Not liquidated
A CFD position: its margin is posted from the quote asset, outside the spot margin account.
§Serialised form
level, fee_rate and margin_call_level are decimal strings, and margin_call_level is
left out when off. Deserialising refuses what the constructors refuse.
Implementations§
Source§impl SpotMarginLiquidation
impl SpotMarginLiquidation
Sourcepub fn new(
level: Decimal,
fee_rate: Decimal,
) -> Result<Self, SpotMarginConfigError>
pub fn new( level: Decimal, fee_rate: Decimal, ) -> Result<Self, SpotMarginConfigError>
Liquidation at a margin level of level or below, charging fee_rate of the debt it
repays, with no margin call.
For example, a level of 1.1 and a fee rate of 0.02 liquidate an account whose holdings are worth 1.1 times its debt or less, and charge 2 % of what it repays.
§Errors
SpotMarginConfigError::LiquidationLevelBelowOne for a level below one, and
SpotMarginConfigError::LiquidationFeeOutOfRange for a fee_rate below zero or not
below one.
Sourcepub fn with_margin_call(
self,
margin_call_level: Decimal,
) -> Result<Self, SpotMarginConfigError>
pub fn with_margin_call( self, margin_call_level: Decimal, ) -> Result<Self, SpotMarginConfigError>
This liquidation, with a margin call at margin_call_level. See # Margin call.
§Errors
SpotMarginConfigError::MarginCallNotAboveLiquidation if margin_call_level is not
above the liquidation level.
Sourcepub fn fee_rate(&self) -> Decimal
pub fn fee_rate(&self) -> Decimal
The fee, as a fraction of the value of the debt a liquidation repays.
Sourcepub fn margin_call_level(&self) -> Option<Decimal>
pub fn margin_call_level(&self) -> Option<Decimal>
The margin level at or below which the venue sends a margin call, or None for none.
Trait Implementations§
Source§impl Clone for SpotMarginLiquidation
impl Clone for SpotMarginLiquidation
impl Copy for SpotMarginLiquidation
Source§impl Debug for SpotMarginLiquidation
impl Debug for SpotMarginLiquidation
Source§impl<'de> Deserialize<'de> for SpotMarginLiquidation
impl<'de> Deserialize<'de> for SpotMarginLiquidation
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
impl Eq for SpotMarginLiquidation
Source§impl Hash for SpotMarginLiquidation
impl Hash for SpotMarginLiquidation
Source§impl Ord for SpotMarginLiquidation
impl Ord for SpotMarginLiquidation
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
1.21.0 (const: unstable) · Source§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialEq for SpotMarginLiquidation
impl PartialEq for SpotMarginLiquidation
Source§impl PartialOrd for SpotMarginLiquidation
impl PartialOrd for SpotMarginLiquidation
Source§impl Serialize for SpotMarginLiquidation
impl Serialize for SpotMarginLiquidation
impl StructuralPartialEq for SpotMarginLiquidation
Auto Trait Implementations§
impl Freeze for SpotMarginLiquidation
impl RefUnwindSafe for SpotMarginLiquidation
impl Send for SpotMarginLiquidation
impl Sync for SpotMarginLiquidation
impl Unpin for SpotMarginLiquidation
impl UnsafeUnpin for SpotMarginLiquidation
impl UnwindSafe for SpotMarginLiquidation
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
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 moreimpl<T> JsonSchemaMaybe for T
Source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.