pub struct Leverage(/* private fields */);Expand description
A validated leverage value for trading positions.
Leverage represents the multiplier applied to a trader’s margin to determine the position size. This type ensures that leverage values are within acceptable bounds (1x to 100x) and can be safely used when trading futures.
Leverage values must be:
- Greater than or equal to
Leverage::MIN(1x) - Less than or equal to
Leverage::MAX(100x)
§Examples
use lnm_sdk::api_v3::models::Leverage;
// Create a leverage value from a float
let leverage = Leverage::try_from(10.0).unwrap();
assert_eq!(leverage.as_f64(), 10.0);
// Values outside the valid range will fail
assert!(Leverage::try_from(0.5).is_err());
assert!(Leverage::try_from(150.0).is_err());Implementations§
Source§impl Leverage
impl Leverage
Sourcepub fn bounded<T>(value: T) -> Leverage
pub fn bounded<T>(value: T) -> Leverage
Creates a Leverage by bounding the given value to the valid range.
This method bounds the input to the range (Leverage::MIN, Leverage::MAX).
It should be used to ensure a valid Leverage without error handling.
Note: In order to check whether a value is a valid leverage and receive an error for
invalid values, use Leverage::try_from.
§Examples
use lnm_sdk::api_v3::models::Leverage;
// Values within range are preserved
let l = Leverage::bounded(25.0);
assert_eq!(l.as_f64(), 25.0);
// Values below minimum are bounded to MIN
let l = Leverage::bounded(0.5);
assert_eq!(l, Leverage::MIN);
// Values above maximum are bounded to MAX
let l = Leverage::bounded(150.0);
assert_eq!(l, Leverage::MAX);Sourcepub fn as_f64(&self) -> f64
pub fn as_f64(&self) -> f64
Returns the leverage value as its underlying f64 representation.
§Examples
use lnm_sdk::api_v3::models::Leverage;
let leverage = Leverage::try_from(25.0).unwrap();
assert_eq!(leverage.as_f64(), 25.0);Sourcepub fn try_calculate(
quantity: Quantity,
margin: Margin,
price: Price,
) -> Result<Leverage, LeverageValidationError>
pub fn try_calculate( quantity: Quantity, margin: Margin, price: Price, ) -> Result<Leverage, LeverageValidationError>
Calculates leverage from quantity (USD), margin (sats), and price (BTC/USD).
The leverage is calculated using the formula:
leverage = (quantity * SATS_PER_BTC) / (margin * price)
§Examples
use lnm_sdk::api_v3::models::{Leverage, Quantity, Margin, Price};
let quantity = Quantity::try_from(1_000).unwrap(); // Quantity in USD
let margin = Margin::try_from(20_000).unwrap(); // Margin in sats
let price = Price::try_from(100_000.0).unwrap(); // Price in USD/BTC
let leverage = Leverage::try_calculate(quantity, margin, price).unwrap();Trait Implementations§
Source§impl<'de> Deserialize<'de> for Leverage
impl<'de> Deserialize<'de> for Leverage
Source§fn deserialize<D>(
deserializer: D,
) -> Result<Leverage, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<Leverage, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
Source§impl From<CrossLeverage> for Leverage
impl From<CrossLeverage> for Leverage
Source§fn from(value: CrossLeverage) -> Leverage
fn from(value: CrossLeverage) -> Leverage
Source§impl Ord for Leverage
impl Ord for Leverage
Source§impl PartialOrd for Leverage
impl PartialOrd for Leverage
Source§impl Serialize for Leverage
impl Serialize for Leverage
Source§fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
impl Copy for Leverage
impl Eq for Leverage
impl StructuralPartialEq for Leverage
Auto Trait Implementations§
impl Freeze for Leverage
impl RefUnwindSafe for Leverage
impl Send for Leverage
impl Sync for Leverage
impl Unpin for Leverage
impl UnsafeUnpin for Leverage
impl UnwindSafe for Leverage
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,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
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> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> ToCompactString for Twhere
T: Display,
impl<T> ToCompactString for Twhere
T: Display,
Source§fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
ToCompactString::to_compact_string() Read moreSource§fn to_compact_string(&self) -> CompactString
fn to_compact_string(&self) -> CompactString
CompactString. Read moreSource§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
Source§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string, but without panic on OOM.