pub struct Position {
Show 22 fields pub owner: Addr, pub id: PositionId, pub deposit_collateral: SignedCollateralAndUsd, pub active_collateral: NonZero<Collateral>, pub counter_collateral: NonZero<Collateral>, pub notional_size: Signed<Notional>, pub created_at: Timestamp, pub price_point_created_at: Option<Timestamp>, pub trading_fee: CollateralAndUsd, pub funding_fee: SignedCollateralAndUsd, pub borrow_fee: CollateralAndUsd, pub crank_fee: CollateralAndUsd, pub delta_neutrality_fee: SignedCollateralAndUsd, pub liquifunded_at: Timestamp, pub next_liquifunding: Timestamp, pub stop_loss_override: Option<PriceBaseInQuote>, pub stop_loss_override_notional: Option<Price>, pub liquidation_price: Option<Price>, pub liquidation_margin: LiquidationMargin, pub take_profit_trader: Option<TakeProfitTrader>, pub take_profit_trader_notional: Option<Price>, pub take_profit_total: Option<Price>,
}
Expand description

The position itself

Fields§

§owner: Addr

Owner of the position

§id: PositionId

Unique identifier for a position

§deposit_collateral: SignedCollateralAndUsd

The amount of collateral deposited by the trader to create this position.

It would seem like the type here should be NonZero<Collateral>. However, due to updates, this isn’t accurate. It’s possible for someone to update a position and withdraw more collateral than the original deposit.

§active_collateral: NonZero<Collateral>

Active collateral for the position

As a position stays open, we liquifund to realize price exposure and take fees. This is the current trader-side collateral after those steps.

§counter_collateral: NonZero<Collateral>

Collateral owned by the liquidity pool that is locked in this position.

§notional_size: Signed<Notional>

This is signed, where negative represents a short and positive is a long

§created_at: Timestamp

When the position was created, in terms of block time.

§price_point_created_at: Option<Timestamp>

Price point timestamp of the crank that created this position.

This field is only used since deferred execution, before that it is None.

§trading_fee: CollateralAndUsd

The one-time fee paid when opening or updating a position

this value is the current balance, including all updates

§funding_fee: SignedCollateralAndUsd

The ongoing fee paid (and earned!) between positions to incentivize keeping longs and shorts in balance which in turn reduces risk for LPs

This value is the current balance, not a historical record of each payment

§borrow_fee: CollateralAndUsd

The ongoing fee paid to LPs to lock up their deposit as counter-size collateral in this position

This value is the current balance, not a historical record of each payment

§crank_fee: CollateralAndUsd

Total crank fees paid

§delta_neutrality_fee: SignedCollateralAndUsd

Cumulative amount of delta neutrality fees paid by (or received by) the position.

Positive == outgoing, negative == incoming, like funding_fee.

§liquifunded_at: Timestamp

Last time the position was liquifunded.

For newly opened positions, this is the same as the creation time.

§next_liquifunding: Timestamp

When is our next scheduled liquifunding?

The crank will automatically liquifund this position once this timestamp has passed. Additionally, liquifunding may be triggered by updating the position.

§stop_loss_override: Option<PriceBaseInQuote>

A trader specified price at which the position will be liquidated

§stop_loss_override_notional: Option<Price>

Stored separately to ensure there are no rounding errors, since we need precise binary equivalence for lookups.

§liquidation_price: Option<Price>

The most recently calculated liquidation price

§liquidation_margin: LiquidationMargin

The amount of liquidation margin set aside

§take_profit_trader: Option<TakeProfitTrader>

The take profit value set by the trader in a message. For historical reasons, this value can be optional if the user provided a max gains price.

§take_profit_trader_notional: Option<Price>

Derived directly from take_profit_trader to get the PriceNotionalInCollateral representation. This will be None if take_profit_trader is infinite. For historical reasons, this value will also be None if the user provided a max gains price. Stored separately to ensure there are no rounding errors, since we need precise binary equivalence for lookups.

§take_profit_total: Option<Price>

The most recently calculated price at which the trader will achieve maximum gains and take all counter collateral. This is the notional price, not the base price, to avoid rounding errors

Implementations§

source§

impl Position

source

pub fn direction(&self) -> DirectionToNotional

Direction of the position

source

pub fn max_gains_in_quote( &self, market_type: MarketType, price_point: &PricePoint, ) -> Result<MaxGainsInQuote>

Maximum gains for the position

source

pub fn active_leverage_to_notional( &self, price_point: &PricePoint, ) -> SignedLeverageToNotional

Compute the internal leverage active collateral.

source

pub fn counter_leverage_to_notional( &self, price_point: &PricePoint, ) -> SignedLeverageToNotional

Compute the internal leverage for the counter collateral.

source

pub fn notional_size_in_collateral( &self, price_point: &PricePoint, ) -> Signed<Collateral>

Convert the notional size into collateral at the given price point.

source

pub fn position_size_base( &self, market_type: MarketType, price_point: &PricePoint, ) -> Result<Signed<Base>>

Calculate the size of the position in terms of the base asset.

This represents what the users’ perception of their position is, and needs to take into account the off-by-one leverage impact of collateral-is-base markets.

source

pub fn pnl_in_collateral(&self) -> Result<Signed<Collateral>>

Calculate the PnL of this position in terms of the collateral.

source

pub fn pnl_in_usd(&self, price_point: &PricePoint) -> Result<Signed<Usd>>

Calculate the PnL of this position in terms of USD.

Note that this is not equivalent to converting the collateral PnL into USD, since we follow a cost basis model in this function, tracking the price of the collateral asset in terms of USD for each transaction.

source

pub fn liquidation_margin( &self, price_point: &PricePoint, config: &Config, ) -> Result<LiquidationMargin>

Computes the liquidation margin for the position

Takes the price point of the last liquifunding.

source

pub fn liquidation_price( &self, price: Price, active_collateral: NonZero<Collateral>, liquidation_margin: &LiquidationMargin, ) -> Option<Price>

Computes the liquidation price for the position at a given spot price.

source

pub fn take_profit_price_total( &self, price_point: &PricePoint, market_type: MarketType, ) -> Result<Option<Price>>

Computes the take-profit price for the position at a given spot price.

source

pub fn add_delta_neutrality_fee( &mut self, amount: Signed<Collateral>, price_point: &PricePoint, ) -> Result<()>

Add a new delta neutrality fee to the position.

source

pub fn get_price_exposure( &self, start_price: Price, end_price: PricePoint, ) -> Result<Signed<Collateral>>

Get the price exposure for a given price movement.

source

pub fn settle_price_exposure( self, start_price: Price, end_price: PricePoint, liquidation_margin: Collateral, ) -> Result<(MaybeClosedPosition, Signed<Collateral>)>

Apply a price change to this position.

This will determine the exposure (positive == to trader, negative == to liquidity pool) impact and return whether the position can remain open or instructions to close it.

source

pub fn into_query_response_extrapolate_exposure( self, start_price: PricePoint, end_price: PricePoint, entry_price: Price, market_type: MarketType, dnf_on_close_collateral: Signed<Collateral>, ) -> Result<PositionOrPendingClose>

Convert a position into a query response, calculating price exposure impact.

source

pub fn into_query_response( self, end_price: PricePoint, entry_price: Price, market_type: MarketType, dnf_on_close_collateral: Signed<Collateral>, ) -> Result<PositionQueryResponse>

Convert into a query response, without calculating price exposure impact.

source

pub fn attributes(&self) -> Vec<(&'static str, String)>

Attributes for a position which can be emitted in events.

Trait Implementations§

source§

impl Clone for Position

source§

fn clone(&self) -> Position

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Position

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'de> Deserialize<'de> for Position

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl From<MaybeClosedPosition> for Position

source§

fn from(maybe_closed: MaybeClosedPosition) -> Self

Converts to this type from the input type.
source§

impl PartialEq for Position

source§

fn eq(&self, other: &Position) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Serialize for Position

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl Eq for Position

source§

impl StructuralPartialEq for Position

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<U> As for U

§

fn as_<T>(self) -> T
where T: CastFrom<U>,

Casts self to type T. The semantics of numeric casting with the as operator are followed, so <T as As>::as_::<U> can be used in the same way as T as U for numeric conversions. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> DynClone for T
where T: Clone,

source§

fn __clone_box(&self, _: Private) -> *mut ()

§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> QueryResultExt for T
where T: Serialize,

source§

fn query_result(&self) -> Result<Binary, Error>

Convert the value to its JSON representation
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,