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
impl Position
sourcepub fn direction(&self) -> DirectionToNotional
pub fn direction(&self) -> DirectionToNotional
Direction of the position
sourcepub fn max_gains_in_quote(
&self,
market_type: MarketType,
price_point: &PricePoint,
) -> Result<MaxGainsInQuote>
pub fn max_gains_in_quote( &self, market_type: MarketType, price_point: &PricePoint, ) -> Result<MaxGainsInQuote>
Maximum gains for the position
sourcepub fn active_leverage_to_notional(
&self,
price_point: &PricePoint,
) -> SignedLeverageToNotional
pub fn active_leverage_to_notional( &self, price_point: &PricePoint, ) -> SignedLeverageToNotional
Compute the internal leverage active collateral.
sourcepub fn counter_leverage_to_notional(
&self,
price_point: &PricePoint,
) -> SignedLeverageToNotional
pub fn counter_leverage_to_notional( &self, price_point: &PricePoint, ) -> SignedLeverageToNotional
Compute the internal leverage for the counter collateral.
sourcepub fn notional_size_in_collateral(
&self,
price_point: &PricePoint,
) -> Signed<Collateral>
pub fn notional_size_in_collateral( &self, price_point: &PricePoint, ) -> Signed<Collateral>
Convert the notional size into collateral at the given price point.
sourcepub fn position_size_base(
&self,
market_type: MarketType,
price_point: &PricePoint,
) -> Result<Signed<Base>>
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.
sourcepub fn pnl_in_collateral(&self) -> Result<Signed<Collateral>>
pub fn pnl_in_collateral(&self) -> Result<Signed<Collateral>>
Calculate the PnL of this position in terms of the collateral.
sourcepub fn pnl_in_usd(&self, price_point: &PricePoint) -> Result<Signed<Usd>>
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.
sourcepub fn liquidation_margin(
&self,
price_point: &PricePoint,
config: &Config,
) -> Result<LiquidationMargin>
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.
sourcepub fn liquidation_price(
&self,
price: Price,
active_collateral: NonZero<Collateral>,
liquidation_margin: &LiquidationMargin,
) -> Option<Price>
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.
sourcepub fn take_profit_price_total(
&self,
price_point: &PricePoint,
market_type: MarketType,
) -> Result<Option<Price>>
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.
sourcepub fn add_delta_neutrality_fee(
&mut self,
amount: Signed<Collateral>,
price_point: &PricePoint,
) -> Result<()>
pub fn add_delta_neutrality_fee( &mut self, amount: Signed<Collateral>, price_point: &PricePoint, ) -> Result<()>
Add a new delta neutrality fee to the position.
sourcepub fn get_price_exposure(
&self,
start_price: Price,
end_price: PricePoint,
) -> Result<Signed<Collateral>>
pub fn get_price_exposure( &self, start_price: Price, end_price: PricePoint, ) -> Result<Signed<Collateral>>
Get the price exposure for a given price movement.
sourcepub fn settle_price_exposure(
self,
start_price: Price,
end_price: PricePoint,
liquidation_margin: Collateral,
) -> Result<(MaybeClosedPosition, Signed<Collateral>)>
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.
sourcepub 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>
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.
sourcepub fn into_query_response(
self,
end_price: PricePoint,
entry_price: Price,
market_type: MarketType,
dnf_on_close_collateral: Signed<Collateral>,
) -> Result<PositionQueryResponse>
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.
sourcepub fn attributes(&self) -> Vec<(&'static str, String)>
pub fn attributes(&self) -> Vec<(&'static str, String)>
Attributes for a position which can be emitted in events.
Trait Implementations§
source§impl<'de> Deserialize<'de> for Position
impl<'de> Deserialize<'de> for Position
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>,
source§impl From<MaybeClosedPosition> for Position
impl From<MaybeClosedPosition> for Position
source§fn from(maybe_closed: MaybeClosedPosition) -> Self
fn from(maybe_closed: MaybeClosedPosition) -> Self
source§impl PartialEq for Position
impl PartialEq for Position
impl Eq for Position
impl StructuralPartialEq for Position
Auto Trait Implementations§
impl Freeze for Position
impl RefUnwindSafe for Position
impl Send for Position
impl Sync for Position
impl Unpin for Position
impl UnwindSafe for Position
Blanket Implementations§
§impl<U> As for U
impl<U> As for U
§fn as_<T>(self) -> Twhere
T: CastFrom<U>,
fn as_<T>(self) -> Twhere
T: CastFrom<U>,
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 moresource§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§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
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 more