Struct mango_v4::state::PerpPosition
source · #[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
impl PerpPosition
sourcepub fn add_taker_trade(&mut self, side: Side, base_lots: i64, quote_lots: i64)
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
sourcepub fn remove_taker_trade(&mut self, base_change: i64, quote_change: i64)
pub fn remove_taker_trade(&mut self, base_change: i64, quote_change: i64)
Remove taker trade after it has been processed on EventQueue
pub fn is_active(&self) -> bool
pub fn is_active_for_market(&self, market_index: PerpMarketIndex) -> bool
pub fn base_position_native(&self, market: &PerpMarket) -> I80F48
pub fn base_position_lots(&self) -> i64
pub fn effective_base_position_lots(&self) -> i64
pub fn quote_position_native(&self) -> I80F48
sourcepub fn unsettled_funding(&self, perp_market: &PerpMarket) -> I80F48
pub fn unsettled_funding(&self, perp_market: &PerpMarket) -> I80F48
The amount of funding this account still needs to pay, in native quote
sourcepub fn settle_funding(&mut self, perp_market: &PerpMarket)
pub fn settle_funding(&mut self, perp_market: &PerpMarket)
Move unrealized funding payments into the quote_position
sourcepub fn record_trade(
&mut self,
perp_market: &mut PerpMarket,
base_change: i64,
quote_change_native: I80F48
)
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
sourcepub fn has_open_orders(&self) -> bool
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.
pub fn has_open_taker_fills(&self) -> bool
sourcepub fn has_open_orders_or_fills(&self) -> bool
pub fn has_open_orders_or_fills(&self) -> bool
Are there any open orders or fills that haven’t been processed yet?
sourcepub fn avg_entry_price(&self, market: &PerpMarket) -> f64
pub fn avg_entry_price(&self, market: &PerpMarket) -> f64
Calculate the average entry price of the position, in native/native units
sourcepub fn break_even_price(&self, market: &PerpMarket) -> f64
pub fn break_even_price(&self, market: &PerpMarket) -> f64
Calculate the break even price of the position, in native/native units
sourcepub fn unsettled_pnl(
&self,
perp_market: &PerpMarket,
price: I80F48
) -> Result<I80F48>
pub fn unsettled_pnl( &self, perp_market: &PerpMarket, price: I80F48 ) -> Result<I80F48>
Calculate the PnL of the position for a given price
sourcepub fn update_settle_limit(&mut self, market: &PerpMarket, now_ts: u64)
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
sourcepub fn settle_limit(&self, market: &PerpMarket) -> (i64, i64)
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
sourcepub fn available_settle_limit(&self, market: &PerpMarket) -> (i64, i64)
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.
sourcepub fn apply_pnl_settle_limit(&self, market: &PerpMarket, pnl: I80F48) -> I80F48
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.
sourcepub fn record_settle(&mut self, settled_pnl: I80F48)
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.
sourcepub fn record_trading_fee(&mut self, fee: I80F48)
pub fn record_trading_fee(&mut self, fee: I80F48)
Update perp position for a maker/taker fee payment
sourcepub fn record_liquidation_quote_change(&mut self, change: I80F48)
pub fn record_liquidation_quote_change(&mut self, change: I80F48)
Adds immediately-settleable realized pnl when a liqor takes over pnl during liquidation
sourcepub fn record_liquidation_pnl_takeover(
&mut self,
change: I80F48,
recurring_limit: I80F48
)
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,
impl BorshDeserialize for PerpPositionwhere PerpMarketIndex: BorshDeserialize, [u8; 2]: BorshDeserialize, u32: BorshDeserialize, i64: BorshDeserialize, I80F48: BorshDeserialize, f64: BorshDeserialize, u64: BorshDeserialize, [u8; 88]: BorshDeserialize,
source§impl BorshSerialize for PerpPositionwhere
PerpMarketIndex: BorshSerialize,
[u8; 2]: BorshSerialize,
u32: BorshSerialize,
i64: BorshSerialize,
I80F48: BorshSerialize,
f64: BorshSerialize,
u64: BorshSerialize,
[u8; 88]: BorshSerialize,
impl BorshSerialize for PerpPositionwhere PerpMarketIndex: BorshSerialize, [u8; 2]: BorshSerialize, u32: BorshSerialize, i64: BorshSerialize, I80F48: BorshSerialize, f64: BorshSerialize, u64: BorshSerialize, [u8; 88]: BorshSerialize,
source§impl Clone for PerpPosition
impl Clone for PerpPosition
source§fn clone(&self) -> PerpPosition
fn clone(&self) -> PerpPosition
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for PerpPosition
impl Debug for PerpPosition
source§impl Default for PerpPosition
impl Default for PerpPosition
impl Copy for PerpPosition
impl Pod for PerpPosition
Auto Trait Implementations§
impl RefUnwindSafe for PerpPosition
impl Send for PerpPosition
impl Sync for PerpPosition
impl Unpin for PerpPosition
impl UnwindSafe for PerpPosition
Blanket Implementations§
source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where T: CheckedCast<Dst>,
§impl<T> CheckedBitPattern for Twhere
T: AnyBitPattern,
impl<T> CheckedBitPattern for Twhere T: AnyBitPattern,
§type Bits = T
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
fn is_valid_bit_pattern(_bits: &T) -> bool
bits
as &Self
.