Skip to main content

Leverage

Struct Leverage 

Source
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:

§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

Source

pub const MIN: Leverage

The minimum allowed leverage value (1x).

Source

pub const MAX: Leverage

The maximum allowed leverage value (100x).

Source

pub fn bounded<T>(value: T) -> Leverage
where T: Into<f64>,

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);
Source

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);
Source

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 Clone for Leverage

Source§

fn clone(&self) -> Leverage

Returns a duplicate 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 Leverage

Source§

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

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

impl<'de> Deserialize<'de> for Leverage

Source§

fn deserialize<D>( deserializer: D, ) -> Result<Leverage, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Leverage

Source§

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

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

impl From<CrossLeverage> for Leverage

Source§

fn from(value: CrossLeverage) -> Leverage

Converts to this type from the input type.
Source§

impl Ord for Leverage

Source§

fn cmp(&self, other: &Leverage) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Leverage

Source§

fn eq(&self, other: &Leverage) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Leverage

Source§

fn partial_cmp(&self, other: &Leverage) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for Leverage

Source§

fn serialize<S>( &self, serializer: S, ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl TryFrom<f32> for Leverage

Source§

type Error = LeverageValidationError

The type returned in the event of a conversion error.
Source§

fn try_from(value: f32) -> Result<Leverage, <Leverage as TryFrom<f32>>::Error>

Performs the conversion.
Source§

impl TryFrom<f64> for Leverage

Source§

type Error = LeverageValidationError

The type returned in the event of a conversion error.
Source§

fn try_from(value: f64) -> Result<Leverage, <Leverage as TryFrom<f64>>::Error>

Performs the conversion.
Source§

impl TryFrom<i16> for Leverage

Source§

type Error = LeverageValidationError

The type returned in the event of a conversion error.
Source§

fn try_from(value: i16) -> Result<Leverage, <Leverage as TryFrom<i16>>::Error>

Performs the conversion.
Source§

impl TryFrom<i32> for Leverage

Source§

type Error = LeverageValidationError

The type returned in the event of a conversion error.
Source§

fn try_from( leverage: i32, ) -> Result<Leverage, <Leverage as TryFrom<i32>>::Error>

Performs the conversion.
Source§

impl TryFrom<i64> for Leverage

Source§

type Error = LeverageValidationError

The type returned in the event of a conversion error.
Source§

fn try_from(value: i64) -> Result<Leverage, <Leverage as TryFrom<i64>>::Error>

Performs the conversion.
Source§

impl TryFrom<i8> for Leverage

Source§

type Error = LeverageValidationError

The type returned in the event of a conversion error.
Source§

fn try_from(value: i8) -> Result<Leverage, <Leverage as TryFrom<i8>>::Error>

Performs the conversion.
Source§

impl TryFrom<isize> for Leverage

Source§

type Error = LeverageValidationError

The type returned in the event of a conversion error.
Source§

fn try_from( value: isize, ) -> Result<Leverage, <Leverage as TryFrom<isize>>::Error>

Performs the conversion.
Source§

impl TryFrom<u16> for Leverage

Source§

type Error = LeverageValidationError

The type returned in the event of a conversion error.
Source§

fn try_from(value: u16) -> Result<Leverage, <Leverage as TryFrom<u16>>::Error>

Performs the conversion.
Source§

impl TryFrom<u32> for Leverage

Source§

type Error = LeverageValidationError

The type returned in the event of a conversion error.
Source§

fn try_from(value: u32) -> Result<Leverage, <Leverage as TryFrom<u32>>::Error>

Performs the conversion.
Source§

impl TryFrom<u64> for Leverage

Source§

type Error = LeverageValidationError

The type returned in the event of a conversion error.
Source§

fn try_from(value: u64) -> Result<Leverage, <Leverage as TryFrom<u64>>::Error>

Performs the conversion.
Source§

impl TryFrom<u8> for Leverage

Source§

type Error = LeverageValidationError

The type returned in the event of a conversion error.
Source§

fn try_from(value: u8) -> Result<Leverage, <Leverage as TryFrom<u8>>::Error>

Performs the conversion.
Source§

impl TryFrom<usize> for Leverage

Source§

type Error = LeverageValidationError

The type returned in the event of a conversion error.
Source§

fn try_from( value: usize, ) -> Result<Leverage, <Leverage as TryFrom<usize>>::Error>

Performs the conversion.
Source§

impl Copy for Leverage

Source§

impl Eq for Leverage

Source§

impl StructuralPartialEq for Leverage

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

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 T
where U: From<T>,

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

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToCompactString for T
where T: Display,

Source§

impl<T> ToLine for T
where T: Display,

Source§

fn to_line(&self) -> Line<'_>

Converts the value to a Line.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

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> ToSpan for T
where T: Display,

Source§

fn to_span(&self) -> Span<'_>

Converts the value to a Span.
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T> ToStringFallible for T
where T: Display,

Source§

fn try_to_string(&self) -> Result<String, TryReserveError>

ToString::to_string, but without panic on OOM.

Source§

impl<T> ToText for T
where T: Display,

Source§

fn to_text(&self) -> Text<'_>

Converts the value to a Text.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> Signal for T
where T: Send + Sync + Clone + Display + 'static,