pub struct Exchange {
pub rates: HashMap<CurrencyPair, ExchangeRate>,
}
Expand description
Exchange struct to hold exchange rates.
Fields§
§rates: HashMap<CurrencyPair, ExchangeRate>
Exchange rates hashmap. The key is a string of the form e.g. “USD_EUR”, and the value is an ExchangeRate struct. The key is generated from the from_currency and to_currency of the ExchangeRate.
Implementations§
Source§impl Exchange
impl Exchange
Sourcepub fn new() -> Exchange
pub fn new() -> Exchange
Create a new empty Exchange.
§Example
use RustQuant::instruments::fx::exchange::Exchange;
let exchange = Exchange::new();
Sourcepub fn add_rate(&mut self, rate: ExchangeRate)
pub fn add_rate(&mut self, rate: ExchangeRate)
Adds a new ExchangeRate
to the Exchange.
§Example
use RustQuant::instruments::*;
use RustQuant::iso::*;
let mut exchange = Exchange::new();
let usd_to_eur = ExchangeRate::new(USD, EUR, 0.85); // USD to EUR
let eur_to_usd = ExchangeRate::new(EUR, USD, 1.18); // EUR to USD
exchange.add_rate(usd_to_eur);
exchange.add_rate(eur_to_usd);
Sourcepub fn get_rate(
&self,
from_currency: &Currency,
to_currency: &Currency,
) -> Option<&ExchangeRate>
pub fn get_rate( &self, from_currency: &Currency, to_currency: &Currency, ) -> Option<&ExchangeRate>
Retrieves an ExchangeRate
from the Exchange.
§Example
use RustQuant::instruments::*;
let mut exchange = Exchange::new();
let usd_to_eur = ExchangeRate::new(USD, EUR, 0.85); // USD to EUR
let eur_to_usd = ExchangeRate::new(EUR, USD, 1.18); // EUR to USD
exchange.add_rate(usd_to_eur);
exchange.add_rate(eur_to_usd);
let retrieved_usd_to_eur = exchange.get_rate(&USD, &EUR).expect("Rate not found");
assert_eq!(retrieved_usd_to_eur.rate, 0.85);
let retrieved_eur_to_usd = exchange.get_rate(&EUR, &USD).expect("Rate not found");
assert_eq!(retrieved_eur_to_usd.rate, 1.18);
Sourcepub fn convert(&self, money: Money, to_currency: Currency) -> Money
pub fn convert(&self, money: Money, to_currency: Currency) -> Money
Convert money from one currency to another using the exchange rate in the Exchange.
It panics if the conversion rate is not found or if the money’s currency doesn’t match with from_currency
.
§Example
use RustQuant::instruments::*;
let mut exchange = Exchange::new();
let usd_to_eur = ExchangeRate::new(USD, EUR, 0.85); // USD to EUR
let eur_to_usd = ExchangeRate::new(EUR, USD, 1.18); // EUR to USD
exchange.add_rate(usd_to_eur);
exchange.add_rate(eur_to_usd);
let usd_100 = Money::new(USD, 100.0); // 100 USD
let eur_85 = exchange.convert(usd_100, EUR); // Should be 85 EUR
assert_eq!(eur_85.currency, EUR);
assert_eq!(eur_85.amount, 85.0);
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Exchange
impl RefUnwindSafe for Exchange
impl Send for Exchange
impl Sync for Exchange
impl Unpin for Exchange
impl UnwindSafe for Exchange
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
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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>
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 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>
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
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>
The inverse inclusion map: attempts to construct
self
from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
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
Use with care! Same as
self.to_subset
but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self
to the equivalent element of its superset.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>
The inverse inclusion map: attempts to construct
self
from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
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
Use with care! Same as
self.to_subset
but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self
to the equivalent element of its superset.