pub struct PerpetualPosition {
pub symbol: String,
pub quantity: Positive,
pub entry_price: Positive,
pub side: Side,
pub leverage: Positive,
pub margin: Positive,
pub margin_type: MarginType,
pub funding_rate: Decimal,
pub date: DateTime<Utc>,
pub fees: Positive,
}Expand description
Represents a perpetual swap contract position.
Perpetual swaps are derivative contracts common in cryptocurrency markets that allow traders to speculate on price movements with leverage, without an expiration date. They use a funding rate mechanism to keep the contract price anchored to the spot price.
§Fields
symbol- Trading pair (e.g., “BTC-USDT-PERP”)quantity- Position size in base currencyentry_price- Average entry priceside- Long or Short positionleverage- Applied leverage (1x to 125x typical)margin- Collateral posted for this positionmargin_type- Cross or Isolated margin modefunding_rate- Current funding rate (updated periodically)date- Position open timestampfees- Trading fees (maker/taker)
Fields§
§symbol: StringTrading pair symbol (e.g., “BTC-USDT-PERP”).
quantity: PositivePosition size in base currency units.
entry_price: PositiveAverage entry price for the position.
side: SidePosition direction (Long or Short).
leverage: PositiveLeverage multiplier applied to the position.
margin: PositiveMargin/collateral posted for this position.
margin_type: MarginTypeMargin mode (Cross or Isolated).
funding_rate: DecimalCurrent funding rate as a decimal (e.g., 0.0001 = 0.01%).
date: DateTime<Utc>Timestamp when the position was opened.
fees: PositiveTotal trading fees paid.
Implementations§
Source§impl PerpetualPosition
impl PerpetualPosition
Sourcepub const DEFAULT_FUNDING_INTERVAL_HOURS: u32 = 8
pub const DEFAULT_FUNDING_INTERVAL_HOURS: u32 = 8
Default funding interval in hours (8 hours for most exchanges).
Sourcepub const DEFAULT_MAINTENANCE_MARGIN_RATIO: Decimal = Decimal::ONE_HUNDRED
pub const DEFAULT_MAINTENANCE_MARGIN_RATIO: Decimal = Decimal::ONE_HUNDRED
Default maintenance margin ratio (typically 0.5% to 1%).
Sourcepub fn new(
symbol: String,
quantity: Positive,
entry_price: Positive,
side: Side,
leverage: Positive,
margin: Positive,
margin_type: MarginType,
funding_rate: Decimal,
date: DateTime<Utc>,
fees: Positive,
) -> Self
pub fn new( symbol: String, quantity: Positive, entry_price: Positive, side: Side, leverage: Positive, margin: Positive, margin_type: MarginType, funding_rate: Decimal, date: DateTime<Utc>, fees: Positive, ) -> Self
Creates a new perpetual position.
§Arguments
symbol- Trading pair symbolquantity- Position size in base currencyentry_price- Average entry priceside- Long or Shortleverage- Leverage multipliermargin- Collateral postedmargin_type- Cross or Isolatedfunding_rate- Current funding ratedate- Position open timestampfees- Trading fees
Sourcepub fn long(
symbol: String,
quantity: Positive,
entry_price: Positive,
leverage: Positive,
margin: Positive,
) -> Self
pub fn long( symbol: String, quantity: Positive, entry_price: Positive, leverage: Positive, margin: Positive, ) -> Self
Creates a long perpetual position with default settings.
§Arguments
symbol- Trading pair symbolquantity- Position sizeentry_price- Entry priceleverage- Leverage multipliermargin- Collateral posted
Sourcepub fn short(
symbol: String,
quantity: Positive,
entry_price: Positive,
leverage: Positive,
margin: Positive,
) -> Self
pub fn short( symbol: String, quantity: Positive, entry_price: Positive, leverage: Positive, margin: Positive, ) -> Self
Creates a short perpetual position with default settings.
§Arguments
symbol- Trading pair symbolquantity- Position sizeentry_price- Entry priceleverage- Leverage multipliermargin- Collateral posted
Sourcepub fn notional_value_at_entry(&self) -> Positive
pub fn notional_value_at_entry(&self) -> Positive
Returns the notional value of the position.
Notional = quantity × entry_price
Sourcepub fn notional_value_at_price(&self, current_price: Positive) -> Positive
pub fn notional_value_at_price(&self, current_price: Positive) -> Positive
Returns the current notional value at a given price.
§Arguments
current_price- Current market price
Sourcepub fn unrealized_pnl(
&self,
current_price: Positive,
) -> Result<Decimal, PricingError>
pub fn unrealized_pnl( &self, current_price: Positive, ) -> Result<Decimal, PricingError>
Calculates the unrealized P&L at a given price.
§Arguments
current_price- Current market price
§Decision (issue #471): fallible because pnl_at_price is
This is the whole body of LegAble::pnl_at_price for a perpetual,
so leaving it infallible would have moved that method’s abort one
frame down rather than removing it. entry_price and quantity are
pub, so no constructor guard bounds the product.
§Errors
Returns PricingError::Decimal when the price difference or the
quantity scaling leaves the representable Decimal range.
Sourcepub fn roe_percentage(
&self,
current_price: Positive,
) -> Result<Decimal, PricingError>
pub fn roe_percentage( &self, current_price: Positive, ) -> Result<Decimal, PricingError>
Calculates the ROE (Return on Equity) percentage.
ROE = (Unrealized P&L / Margin) × 100
A zero margin has no return on equity to report, and the function
keeps its existing answer of Decimal::ZERO for that case rather than
turning it into an error.
§Arguments
current_price- Current market price
§Errors
Propagates PerpetualPosition::unrealized_pnl, and returns
PricingError::Decimal when the ratio or its percentage scaling
leaves the representable Decimal range.
Sourcepub fn margin_ratio(
&self,
current_price: Positive,
) -> Result<Decimal, PricingError>
pub fn margin_ratio( &self, current_price: Positive, ) -> Result<Decimal, PricingError>
Calculates the margin ratio at a given price.
Margin Ratio = (Margin + Unrealized P&L) / Notional Value
A zero notional has no margin ratio to report, and the function keeps
its existing answer of Decimal::ZERO for that case rather than
turning it into an error.
§Arguments
current_price- Current market price
§Errors
Propagates PerpetualPosition::unrealized_pnl, and returns
PricingError::Decimal when the equity sum or the ratio leaves the
representable Decimal range.
Sourcepub fn update_funding_rate(&mut self, new_rate: Decimal)
pub fn update_funding_rate(&mut self, new_rate: Decimal)
Sourcepub fn effective_leverage(
&self,
current_price: Positive,
) -> Result<Decimal, PricingError>
pub fn effective_leverage( &self, current_price: Positive, ) -> Result<Decimal, PricingError>
Calculates the effective leverage at a given price.
Effective Leverage = Notional Value / (Margin + Unrealized P&L)
A position whose equity has been wiped out is infinitely levered, and
the function keeps its existing answer of Decimal::MAX for that case
rather than turning it into an error.
§Arguments
current_price- Current market price
§Errors
Propagates PerpetualPosition::unrealized_pnl, and returns
PricingError::Decimal when the equity sum or the quotient leaves
the representable Decimal range.
Trait Implementations§
Source§impl Clone for PerpetualPosition
impl Clone for PerpetualPosition
Source§fn clone(&self) -> PerpetualPosition
fn clone(&self) -> PerpetualPosition
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl ComposeSchema for PerpetualPosition
impl ComposeSchema for PerpetualPosition
Source§impl Debug for PerpetualPosition
impl Debug for PerpetualPosition
Source§impl Default for PerpetualPosition
impl Default for PerpetualPosition
Source§impl<'de> Deserialize<'de> for PerpetualPosition
impl<'de> Deserialize<'de> for PerpetualPosition
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 Display for PerpetualPosition
impl Display for PerpetualPosition
Source§impl From<PerpetualPosition> for Leg
impl From<PerpetualPosition> for Leg
Source§fn from(position: PerpetualPosition) -> Self
fn from(position: PerpetualPosition) -> Self
Source§impl Fundable for PerpetualPosition
impl Fundable for PerpetualPosition
Source§fn funding_rate(&self) -> Decimal
fn funding_rate(&self) -> Decimal
Source§fn funding_interval_hours(&self) -> u32
fn funding_interval_hours(&self) -> u32
Source§impl LegAble for PerpetualPosition
impl LegAble for PerpetualPosition
Source§fn get_symbol(&self) -> &str
fn get_symbol(&self) -> &str
Source§fn get_quantity(&self) -> Positive
fn get_quantity(&self) -> Positive
Source§fn pnl_at_price(&self, price: Positive) -> Result<Decimal, PricingError>
fn pnl_at_price(&self, price: Positive) -> Result<Decimal, PricingError>
Source§fn total_cost(&self) -> Result<Positive, PositionError>
fn total_cost(&self) -> Result<Positive, PositionError>
Source§impl Marginable for PerpetualPosition
impl Marginable for PerpetualPosition
Source§fn initial_margin(&self) -> Positive
fn initial_margin(&self) -> Positive
Source§fn maintenance_margin(&self) -> Positive
fn maintenance_margin(&self) -> Positive
Source§impl PartialEq for PerpetualPosition
impl PartialEq for PerpetualPosition
Source§impl Serialize for PerpetualPosition
impl Serialize for PerpetualPosition
impl StructuralPartialEq for PerpetualPosition
Auto Trait Implementations§
impl Freeze for PerpetualPosition
impl RefUnwindSafe for PerpetualPosition
impl Send for PerpetualPosition
impl Sync for PerpetualPosition
impl Unpin for PerpetualPosition
impl UnsafeUnpin for PerpetualPosition
impl UnwindSafe for PerpetualPosition
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,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
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 moreSource§impl<T> PartialSchema for Twhere
T: ComposeSchema + ?Sized,
impl<T> PartialSchema for Twhere
T: ComposeSchema + ?Sized,
Source§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Scalar for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.