Implementation:Avhz RustQuant Currency
| Knowledge Sources | |
|---|---|
| Domains | Foreign_Exchange, Quantitative_Finance |
| Last Updated | 2026-02-07 19:00 GMT |
Overview
Defines the Currency and CurrencyPair data structures based on the ISO 4217 standard, and provides a comprehensive catalogue of over 150 world currencies as compile-time constants.
Description
The Currency struct encapsulates all essential attributes of a currency: its full name, symbol, ISO 4217 code (both alphabetic and numeric), minor unit (digits after the decimal separator), and fractions per unit (e.g., 100 cents per dollar). The struct derives Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, and Hash, making it suitable for use as a key in collections.
A CurrencyPair struct pairs a base currency with a quote currency, useful for FX rate lookups.
The Currency struct implements the Instrument trait with a trivial price of 1.0 (unit value) and uses the currency name as its instrument type. It also implements Display for formatted output.
A generate_currencies! macro generates over 150 currency constants (e.g., USD, EUR, GBP, JPY) directly from ISO 4217 data, each defined as a pub const at the module level. These constants cover all major, minor, and exotic currencies including special cases like zero-decimal currencies (JPY, KRW) and three-decimal currencies (BHD, KWD).
Usage
Use the predefined currency constants for any FX-related computation, bond denomination, or money representation. The CurrencyPair struct is used in the exchange rate module for keying rate lookups.
Code Reference
Source Location
- Repository: RustQuant
- File: crates/RustQuant_instruments/src/fx/currency.rs
- Lines: 1-446
Signature
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Currency {
pub name: &'static str,
pub symbol: &'static str,
pub code: ISO_4217,
pub minor: usize,
pub fractions: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct CurrencyPair {
pub base: Currency,
pub quote: Currency,
}
impl Currency {
pub fn new(name: &'static str, symbol: &'static str, code: ISO_4217, minor: usize, fractions: usize) -> Self;
pub fn name(&self) -> &str;
pub fn symbol(&self) -> &str;
pub fn code(&self) -> ISO_4217;
pub fn minor(&self) -> usize;
pub fn fractions(&self) -> usize;
}
impl CurrencyPair {
pub fn new(base: Currency, quote: Currency) -> Self;
}
impl Instrument for Currency {
fn price(&self) -> f64;
fn error(&self) -> Option<f64>;
fn valuation_date(&self) -> Date;
fn instrument_type(&self) -> &'static str;
}
Import
use RustQuant::instruments::fx::currency::{Currency, CurrencyPair};
use RustQuant::iso::{USD, EUR, GBP}; // predefined constants
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| name | &'static str | Yes | Full currency name (e.g., "United States Dollar") |
| symbol | &'static str | Yes | Currency symbol (e.g., "$") |
| code | ISO_4217 | Yes | ISO 4217 code with alphabetic (e.g., "USD") and numeric (e.g., "840") components |
| minor | usize | Yes | Number of digits after the decimal separator (usually 2) |
| fractions | usize | Yes | Number of fractional units per base unit (e.g., 100) |
Outputs
| Name | Type | Description |
|---|---|---|
| name() | &str | Returns the currency name |
| symbol() | &str | Returns the currency symbol |
| code() | ISO_4217 | Returns the ISO 4217 code |
| minor() | usize | Returns the minor unit count |
| fractions() | usize | Returns fractions per unit |
| price() | f64 | Always returns 1.0 (unit value) |
| instrument_type() | &'static str | Returns the currency name |
Usage Examples
use RustQuant::instruments::fx::currency::{Currency, CurrencyPair, USD, EUR};
// Use predefined constants
let dollar = USD;
assert_eq!(dollar.name(), "United States Dollar");
assert_eq!(dollar.symbol(), "$");
assert_eq!(dollar.minor(), 2);
// Create a currency pair
let pair = CurrencyPair::new(USD, EUR);
// Display a currency
println!("{}", USD);
// Output:
// Currency: United States Dollar
// ISO Code: ISO_4217 { alphabetic: "USD", numeric: "840" }