pub struct Leverage(/* private fields */);Expand description
Leverage multiplier used to calculate required margin.
Stored internally as fixed-point with scale 10.
Implementations§
Source§impl Leverage
impl Leverage
Sourcepub fn from_raw(raw: u16) -> Result<Self, Error>
pub fn from_raw(raw: u16) -> Result<Self, Error>
Creates leverage from fixed-point raw value with scale Self::SCALE.
For example:
10means1.0x11means1.1x1005means100.5x
§Errors
Returns Error::InvalidLeverage when raw is outside
10..=30000.
Sourcepub const fn raw(&self) -> u16
pub const fn raw(&self) -> u16
Returns fixed-point raw value with scale Self::SCALE.
Sourcepub fn from_u16(multiplier: u16) -> Result<Self, Error>
pub fn from_u16(multiplier: u16) -> Result<Self, Error>
Creates leverage from an integer multiplier.
§Examples
use openpit::param::Leverage;
let lev = Leverage::from_u16(100)?;
assert_eq!(lev.value(), 100.0);§Errors
Returns Error::InvalidLeverage when multiplier is outside
1..=3000.
Sourcepub fn from_f64(multiplier: f64) -> Result<Self, Error>
pub fn from_f64(multiplier: f64) -> Result<Self, Error>
Creates leverage from floating-point multiplier.
Value must be finite, in range 1.0..=3000.0, and aligned to step
0.1.
§Examples
use openpit::param::Leverage;
let lev = Leverage::from_f64(100.5)?;
assert_eq!(lev.value(), 100.5);§Errors
Returns Error::InvalidLeverage when multiplier is not finite,
outside 1.0..=3000.0, or not aligned to 0.1 step.
Sourcepub fn value(&self) -> f32
pub fn value(&self) -> f32
Returns the leverage value as floating-point multiplier.
§Examples
use openpit::param::Leverage;
let lev = Leverage::from_f64(123.4)?;
assert_eq!(lev.value(), 123.4);Sourcepub fn calculate_margin_required(
&self,
notional: Notional,
) -> Result<Notional, Error>
pub fn calculate_margin_required( &self, notional: Notional, ) -> Result<Notional, Error>
Computes the margin required to hold notional at this leverage.
Formula: margin = notional / leverage.
Uses exact decimal arithmetic.
§Examples
use openpit::param::{Leverage, Notional};
let lev = Leverage::from_u16(100)?;
let notional = Notional::from_str("10000")?;
let margin = lev.calculate_margin_required(notional)?;
assert_eq!(margin.to_string(), "100");§Errors
Returns Error::Overflow with ParamKind::Notional on arithmetic
overflow.