Implementation:Avhz RustQuant ExchangeRate
| Knowledge Sources | |
|---|---|
| Domains | Foreign_Exchange, Quantitative_Finance |
| Last Updated | 2026-02-07 19:00 GMT |
Overview
Implements the Exchange and ExchangeRate structs for managing foreign exchange rates and performing currency conversions between Money instances.
Description
The ExchangeRate struct represents a single directional exchange rate between two currencies, storing the source currency, target currency, and the numeric rate as a ratio (from_currency/to_currency). It provides a convert method that takes a Money instance denominated in the source currency and returns a new Money instance in the target currency with the converted amount. The method panics if the input money's currency does not match the expected source currency.
The Exchange struct acts as a rate registry, holding a HashMap of CurrencyPair keys to ExchangeRate values. It provides methods to:
- add_rate: Register a new exchange rate
- get_rate: Look up a rate by source and target currencies
- convert: Perform a full currency conversion by looking up the rate and applying it
Rates are stored directionally (e.g., USD->EUR and EUR->USD are separate entries), so inverse rates must be explicitly added.
Usage
Use ExchangeRate for direct one-off currency conversions. Use Exchange when you need to manage a collection of rates and perform multiple conversions across different currency pairs.
Code Reference
Source Location
- Repository: RustQuant
- File: crates/RustQuant_instruments/src/fx/exchange.rs
- Lines: 1-273
Signature
#[derive(Debug, Clone, Default)]
pub struct Exchange {
pub rates: HashMap<CurrencyPair, ExchangeRate>,
}
#[derive(Debug, Clone, Copy)]
pub struct ExchangeRate {
pub from_currency: Currency,
pub to_currency: Currency,
pub rate: f64,
}
impl Exchange {
pub fn new() -> Self;
pub fn add_rate(&mut self, rate: ExchangeRate);
pub fn get_rate(&self, from_currency: &Currency, to_currency: &Currency) -> Option<&ExchangeRate>;
pub fn convert(&self, money: Money, to_currency: Currency) -> Money;
}
impl ExchangeRate {
pub fn new(from_currency: Currency, to_currency: Currency, rate: f64) -> Self;
pub fn convert(&self, money: Money) -> Money;
}
Import
use RustQuant::instruments::fx::exchange::{Exchange, ExchangeRate};
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| from_currency | Currency | Yes | The source currency of the exchange rate |
| to_currency | Currency | Yes | The target currency of the exchange rate |
| rate | f64 | Yes | The numeric exchange rate (units of to_currency per unit of from_currency) |
Outputs
| Name | Type | Description |
|---|---|---|
| convert() (ExchangeRate) | Money | A new Money instance in the target currency with amount = original_amount * rate |
| get_rate() | Option<&ExchangeRate> | The exchange rate if found, or None |
| convert() (Exchange) | Money | A new Money instance after looking up and applying the appropriate rate |
Usage Examples
use RustQuant::instruments::fx::exchange::{Exchange, ExchangeRate};
use RustQuant::instruments::fx::money::Money;
use RustQuant::instruments::fx::currency::{USD, EUR};
// Direct conversion with ExchangeRate
let usd = Money::new(USD, 100.0);
let eur_usd = ExchangeRate::new(USD, EUR, 0.9187);
let eur = eur_usd.convert(usd);
// eur.amount is approximately 91.87, eur.currency is EUR
// Using an Exchange registry
let mut exchange = Exchange::new();
exchange.add_rate(ExchangeRate::new(USD, EUR, 0.85));
exchange.add_rate(ExchangeRate::new(EUR, USD, 1.18));
let usd_100 = Money::new(USD, 100.0);
let eur_85 = exchange.convert(usd_100, EUR);
// eur_85.amount == 85.0, eur_85.currency == EUR