#[repr(C)]
pub struct PerpPosition {
Show 24 fields pub market_index: PerpMarketIndex, pub padding: [u8; 2], pub settle_pnl_limit_window: u32, pub settle_pnl_limit_settled_in_current_window_native: i64, pub base_position_lots: i64, pub quote_position_native: I80F48, pub quote_running_native: i64, pub long_settled_funding: I80F48, pub short_settled_funding: I80F48, pub bids_base_lots: i64, pub asks_base_lots: i64, pub taker_base_lots: i64, pub taker_quote_lots: i64, pub cumulative_long_funding: f64, pub cumulative_short_funding: f64, pub maker_volume: u64, pub taker_volume: u64, pub perp_spot_transfers: i64, pub avg_entry_price_per_base_lot: f64, pub realized_trade_pnl_native: I80F48, pub realized_other_pnl_native: I80F48, pub settle_pnl_limit_realized_trade: i64, pub realized_pnl_for_position_native: I80F48, pub reserved: [u8; 88],
}

Fields§

§market_index: PerpMarketIndex§padding: [u8; 2]§settle_pnl_limit_window: u32

Index of the current settle pnl limit window

§settle_pnl_limit_settled_in_current_window_native: i64

Amount of realized trade pnl and unrealized pnl that was already settled this window.

Will be negative when negative pnl was settled.

Note that this will be adjusted for bookkeeping reasons when the realized_trade settle limitchanges and is not useable for actually tracking how much pnl was settled on balance.

§base_position_lots: i64

Active position size, measured in base lots

§quote_position_native: I80F48

Active position in quote (conversation rate is that of the time the order was settled) measured in native quote

§quote_running_native: i64

Tracks what the position is to calculate average entry & break even price

§long_settled_funding: I80F48

Already settled long funding

§short_settled_funding: I80F48

Already settled short funding

§bids_base_lots: i64

Base lots in open bids

§asks_base_lots: i64

Base lots in open asks

§taker_base_lots: i64

Amount of base lots on the EventQueue waiting to be processed

§taker_quote_lots: i64

Amount of quote lots on the EventQueue waiting to be processed

§cumulative_long_funding: f64

Cumulative long funding in quote native units. If the user paid $1 in funding for a long position, this would be 1e6. Beware of the sign!

(Display only)

§cumulative_short_funding: f64

Cumulative short funding in quote native units If the user paid $1 in funding for a short position, this would be -1e6.

(Display only)

§maker_volume: u64

Cumulative maker volume in quote native units

(Display only)

§taker_volume: u64

Cumulative taker volume in quote native units

(Display only)

§perp_spot_transfers: i64

Cumulative number of quote native units transfered from the perp position to the settle token spot position.

For example, if the user settled $1 of positive pnl into their USDC spot position, this would be 1e6.

(Display only)

§avg_entry_price_per_base_lot: f64

The native average entry price for the base lots of the current position. Reset to 0 when the base position reaches or crosses 0.

§realized_trade_pnl_native: I80F48

Amount of pnl that was realized by bringing the base position closer to 0.

The settlement of this type of pnl is limited by settle_pnl_limit_realized_trade. Settling pnl reduces this value once other_pnl below is exhausted.

§realized_other_pnl_native: I80F48

Amount of pnl realized from fees, funding and liquidation.

This type of realized pnl is always settleable. Settling pnl reduces this value first.

§settle_pnl_limit_realized_trade: i64

Settle limit contribution from realized pnl.

Every time pnl is realized, this is increased by a fraction of the stable value of the realization. It magnitude decreases when realized pnl drops below its value.

§realized_pnl_for_position_native: I80F48

Trade pnl, fees, funding that were added over the current position’s lifetime.

Reset when the position changes sign or goes to zero. Not decreased by settling.

This is tracked for display purposes: this value plus the difference between entry price and current price of the base position is the overall pnl.

§reserved: [u8; 88]

Implementations§

source§

impl PerpPosition

source

pub fn add_taker_trade(&mut self, side: Side, base_lots: i64, quote_lots: i64)

Add taker trade after it has been matched but before it has been process on EventQueue

source

pub fn remove_taker_trade(&mut self, base_change: i64, quote_change: i64)

Remove taker trade after it has been processed on EventQueue

source

pub fn is_active(&self) -> bool

source

pub fn is_active_for_market(&self, market_index: PerpMarketIndex) -> bool

source

pub fn base_position_native(&self, market: &PerpMarket) -> I80F48

source

pub fn base_position_lots(&self) -> i64

source

pub fn effective_base_position_lots(&self) -> i64

source

pub fn quote_position_native(&self) -> I80F48

source

pub fn unsettled_funding(&self, perp_market: &PerpMarket) -> I80F48

The amount of funding this account still needs to pay, in native quote

source

pub fn settle_funding(&mut self, perp_market: &PerpMarket)

Move unrealized funding payments into the quote_position

source

pub fn record_trade( &mut self, perp_market: &mut PerpMarket, base_change: i64, quote_change_native: I80F48 )

Change the base and quote positions as the result of a trade

source

pub fn has_open_orders(&self) -> bool

Does the user have any orders on the book?

Note that it’s possible they were matched already: This only becomes false when the fill event is processed or the orders are cancelled.

source

pub fn has_open_taker_fills(&self) -> bool

source

pub fn has_open_orders_or_fills(&self) -> bool

Are there any open orders or fills that haven’t been processed yet?

source

pub fn avg_entry_price(&self, market: &PerpMarket) -> f64

Calculate the average entry price of the position, in native/native units

source

pub fn break_even_price(&self, market: &PerpMarket) -> f64

Calculate the break even price of the position, in native/native units

source

pub fn unsettled_pnl( &self, perp_market: &PerpMarket, price: I80F48 ) -> Result<I80F48>

Calculate the PnL of the position for a given price

source

pub fn update_settle_limit(&mut self, market: &PerpMarket, now_ts: u64)

Updates the perp pnl limit time windowing, resetting the amount of used settle-pnl budget if necessary

source

pub fn settle_limit(&self, market: &PerpMarket) -> (i64, i64)

Returns the (min_pnl, max_pnl) range of quote-native pnl that can be settled this window.

It contains contributions from three factors:

  • a fraction of the base position stable value, which gives settlement limit equally in both directions
  • the stored realized trade settle limit, which adds an extra settlement allowance in a single direction
  • the stored realized other settle limit, which adds an extra settlement allowance in a single direction
source

pub fn available_settle_limit(&self, market: &PerpMarket) -> (i64, i64)

Returns the (min_pnl, max_pnl) range of quote-native pnl that may still be settled this settle window.

The available settle limit is the settle_limit() adjusted for the amount of limit that was already used up this window.

source

pub fn apply_pnl_settle_limit(&self, market: &PerpMarket, pnl: I80F48) -> I80F48

Given some pnl, applies the pnl settle limit and returns the reduced pnl.

source

pub fn record_settle(&mut self, settled_pnl: I80F48)

Update the perp position for pnl settlement

If pnl is positive, then that is settled away, deducting from the quote position.

source

pub fn record_trading_fee(&mut self, fee: I80F48)

Update perp position for a maker/taker fee payment

source

pub fn record_liquidation_quote_change(&mut self, change: I80F48)

Adds immediately-settleable realized pnl when a liqor takes over pnl during liquidation

source

pub fn record_liquidation_pnl_takeover( &mut self, change: I80F48, recurring_limit: I80F48 )

Adds to the quote position and adds a recurring (“realized trade”) settle limit

Trait Implementations§

source§

impl BorshDeserialize for PerpPositionwhere PerpMarketIndex: BorshDeserialize, [u8; 2]: BorshDeserialize, u32: BorshDeserialize, i64: BorshDeserialize, I80F48: BorshDeserialize, f64: BorshDeserialize, u64: BorshDeserialize, [u8; 88]: BorshDeserialize,

source§

fn deserialize(buf: &mut &[u8]) -> Result<Self, Error>

Deserializes this instance from a given slice of bytes. Updates the buffer to point at the remaining bytes.
§

fn try_from_slice(v: &[u8]) -> Result<Self, Error>

Deserialize this instance from a slice of bytes.
source§

impl BorshSerialize for PerpPositionwhere PerpMarketIndex: BorshSerialize, [u8; 2]: BorshSerialize, u32: BorshSerialize, i64: BorshSerialize, I80F48: BorshSerialize, f64: BorshSerialize, u64: BorshSerialize, [u8; 88]: BorshSerialize,

source§

fn serialize<W: Write>(&self, writer: &mut W) -> Result<(), Error>

§

fn try_to_vec(&self) -> Result<Vec<u8, Global>, Error>

Serialize this instance into a vector of bytes.
source§

impl Clone for PerpPosition

source§

fn clone(&self) -> PerpPosition

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 PerpPosition

source§

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

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

impl Default for PerpPosition

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl Zeroable for PerpPosition

§

fn zeroed() -> Self

source§

impl Copy for PerpPosition

source§

impl Pod for PerpPosition

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Az for T

source§

fn az<Dst>(self) -> Dstwhere T: Cast<Dst>,

Casts the value.
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<Src, Dst> CastFrom<Src> for Dstwhere Src: Cast<Dst>,

source§

fn cast_from(src: Src) -> Dst

Casts the value.
source§

impl<T> CheckedAs for T

source§

fn checked_as<Dst>(self) -> Option<Dst>where T: CheckedCast<Dst>,

Casts the value.
§

impl<T> CheckedBitPattern for Twhere T: AnyBitPattern,

§

type Bits = T

Self must have the same layout as the specified Bits except for the possible invalid bit patterns being checked during is_valid_bit_pattern.
§

fn is_valid_bit_pattern(_bits: &T) -> bool

If this function returns true, then it must be valid to reinterpret bits as &Self.
source§

impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere Src: CheckedCast<Dst>,

source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
source§

impl<T> DynClone for Twhere T: Clone,

source§

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

source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

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

const: unstable · 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<Src, Dst> LosslessTryInto<Dst> for Srcwhere Dst: LosslessTryFrom<Src>,

source§

fn lossless_try_into(self) -> Option<Dst>

Performs the conversion.
source§

impl<Src, Dst> LossyInto<Dst> for Srcwhere Dst: LossyFrom<Src>,

source§

fn lossy_into(self) -> Dst

Performs the conversion.
source§

impl<T> OverflowingAs for T

source§

fn overflowing_as<Dst>(self) -> (Dst, bool)where T: OverflowingCast<Dst>,

Casts the value.
source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere Src: OverflowingCast<Dst>,

source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

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> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T> SaturatingAs for T

source§

fn saturating_as<Dst>(self) -> Dstwhere T: SaturatingCast<Dst>,

Casts the value.
source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere Src: SaturatingCast<Dst>,

source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
source§

impl<T> ToOwned for Twhere 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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

impl<T> UnwrappedAs for T

source§

fn unwrapped_as<Dst>(self) -> Dstwhere T: UnwrappedCast<Dst>,

Casts the value.
source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dstwhere Src: UnwrappedCast<Dst>,

source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
§

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

§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<T> WrappingAs for T

source§

fn wrapping_as<Dst>(self) -> Dstwhere T: WrappingCast<Dst>,

Casts the value.
source§

impl<Src, Dst> WrappingCastFrom<Src> for Dstwhere Src: WrappingCast<Dst>,

source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.
§

impl<T> AnyBitPattern for Twhere T: Pod,

§

impl<T> NoUninit for Twhere T: Pod,