Skip to content

Commit

Permalink
fix: restructure Money module into Instruments and Cashflows
Browse files Browse the repository at this point in the history
  • Loading branch information
avhz authored and avhz committed Mar 6, 2024
1 parent 901381e commit 853050d
Show file tree
Hide file tree
Showing 32 changed files with 2,596 additions and 2,150 deletions.
27 changes: 0 additions & 27 deletions INSTALL.md

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

<p align="center">
<a href="#build" alt="build">
<img alt="GitHub Workflow Status" src="https://img.shields.io/github/actions/workflow/status/avhz/RustQuant/build.yml">
<img alt="GitHub Workflow Status" src="https://img.shields.io/github/actions/workflow/status/avhz/RustQuant/cargo_build.yml">
<a href="#codecov" alt="codecov">
<img alt="Codecov" src="https://img.shields.io/codecov/c/gh/avhz/RustQuant">
<a href="#deps" alt="deps">
Expand Down
File renamed without changes.
File renamed without changes.
16 changes: 2 additions & 14 deletions src/money/mod.rs → src/cashflows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,8 @@
//! among other things.
/// Cashflow definitions.
pub mod cashflows;
pub use cashflows::*;

/// Currency data struct.
pub mod currency;
pub use currency::*;

/// Currency exchange rate helpers.
pub mod exchange;
pub use exchange::*;

/// Global currencies defined by ISO 4217.
pub mod iso_currencies;
pub use iso_currencies::*;
pub mod cashflow;
pub use cashflow::*;

/// Legs (sequence of cashflows).
pub mod legs;
Expand Down
File renamed without changes.
8 changes: 6 additions & 2 deletions src/data/curves/term_structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ use std::fmt::Display;
use time::Date;

/// Surface data.
struct Surface {
pub struct Surface {
/// Nodes of the surface.
pub nodes: BTreeMap<f64, TermStructure>,
}

struct TermStructure {
/// Term structure data.
pub struct TermStructure {
/// Nodes of the term structure.
pub nodes: BTreeMap<Date, f64>,
// /// Calendar.
// pub calendar: C,
Expand All @@ -30,6 +33,7 @@ struct TermStructure {
}

impl TermStructure {
/// Create a new term structure.
pub fn new(x_values: &[Date], y_values: &[f64]) -> Self {
Self {
nodes: x_values
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// RustQuant: A Rust library for quantitative finance tools.
// Copyright (C) 2023 https://github.com/avhz
// Copyright (C) 2023-2024 https://github.com/avhz
// Dual licensed under Apache 2.0 and MIT.
// See:
// - LICENSE-APACHE.md
// - LICENSE-MIT.md
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

use super::zero_coupon_bond::ZeroCouponBond;
use crate::data::{Curve, YieldCurve};
use crate::instruments::fx::currency::Currency;
use crate::instruments::Instrument;
use crate::money::Currency;
use crate::time::{DateRollingConvention, Frequency};
use std::collections::BTreeMap;
use time::{Date, Duration};
Expand All @@ -18,22 +19,6 @@ use time::{Date, Duration};
// STRUCTS, ENUMS, AND TRAITS
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/// Zero-coupon bond struct.
/// A zero-coupon bond (aka a pure discount bond or simply a zero) is a
/// debt security that doesn't pay interest (a coupon) periodically but
/// instead pays the principal in full at maturity.
#[allow(clippy::module_name_repetitions)]
pub struct ZeroCouponBond {
/// The date the bond is evaluated (i.e. priced).
pub evaluation_date: Date,

/// The date the bond expires (i.e. matures, is redeemed).
pub expiration_date: Date,

/// The currency of the bond (optional).
pub currency: Option<Currency>,
}

/// Coupon bond struct.
/// A coupon bond is a debt obligation with coupons attached that represent
/// interest payments on the debt.
Expand Down Expand Up @@ -196,7 +181,7 @@ impl CouponBond2 {
#[cfg(test)]
mod tests_bond {
use super::*;
use crate::{data::Curve, money::USD, time::today};
use crate::{cashflows::USD, data::Curve, time::today};

#[allow(clippy::similar_names)]
fn create_test_yield_curve(t0: Date) -> YieldCurve {
Expand Down
23 changes: 23 additions & 0 deletions src/instruments/bonds/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// RustQuant: A Rust library for quantitative finance tools.
// Copyright (C) 2023-2024 https://github.com/avhz
// Dual licensed under Apache 2.0 and MIT.
// See:
// - LICENSE-APACHE.md
// - LICENSE-MIT.md
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/// Zero-coupon bond struct.
pub mod zero_coupon_bond;

/// Coupon bond struct.
pub mod coupon_bond;

// /// Cox-Ingersoll-Ross bond pricing model.
// pub mod cox_ingersoll_ross;

// /// One-factor Hull-White bond pricing model.
// pub mod hull_white;

// /// Vasicek bond pricing model.
// pub mod vasicek;
39 changes: 39 additions & 0 deletions src/instruments/bonds/zero_coupon_bond.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// RustQuant: A Rust library for quantitative finance tools.
// Copyright (C) 2023-2024 https://github.com/avhz
// Dual licensed under Apache 2.0 and MIT.
// See:
// - LICENSE-APACHE.md
// - LICENSE-MIT.md
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

use crate::instruments::fx::currency::Currency;
use time::Date;

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// STRUCTS, ENUMS, AND TRAITS
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/// Zero-coupon bond struct.
/// A zero-coupon bond (aka a pure discount bond or simply a zero) is a
/// debt security that doesn't pay interest (a coupon) periodically but
/// instead pays the principal in full at maturity.
#[allow(clippy::module_name_repetitions)]
pub struct ZeroCouponBond {
/// The date the bond is evaluated (i.e. priced).
pub evaluation_date: Date,

/// The date the bond expires (i.e. matures, is redeemed).
pub expiration_date: Date,

/// The currency of the bond (optional).
pub currency: Option<Currency>,
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// IMPLEMENTATIONS
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// UNIT TESTS
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 changes: 8 additions & 0 deletions src/instruments/commodities/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// RustQuant: A Rust library for quantitative finance tools.
// Copyright (C) 2023-2024 https://github.com/avhz
// Dual licensed under Apache 2.0 and MIT.
// See:
// - LICENSE-APACHE.md
// - LICENSE-MIT.md
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23 changes: 23 additions & 0 deletions src/instruments/equities/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// RustQuant: A Rust library for quantitative finance tools.
// Copyright (C) 2023-2024 https://github.com/avhz
// Dual licensed under Apache 2.0 and MIT.
// See:
// - LICENSE-APACHE.md
// - LICENSE-MIT.md
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

use super::{currency::Currency, Ticker};
use crate::iso::isin::ISIN;

/// Equity instrument.
pub struct Equity {
/// The ticker symbol.
pub ticker: Ticker,

/// The ISIN (International Securities Identification Number).
pub isin: ISIN,

/// The currency of the equity.
pub currency: Currency,
}
23 changes: 0 additions & 23 deletions src/money/currency.rs → src/instruments/fx/currency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,29 +195,6 @@ impl Money {
}
}

impl ISO_4217 {
/// Create a new ISO 4217 code.
#[must_use]
pub fn new(alphabetic: &'static str, numeric: &'static str) -> Self {
Self {
alphabetic,
numeric,
}
}

/// Get the ISO 4217 alphabetic code.
#[must_use]
pub fn alphabetic(&self) -> &str {
self.alphabetic
}

/// Get the ISO 4217 numeric code.
#[must_use]
pub fn numeric(&self) -> &str {
self.numeric
}
}

impl std::ops::Add for Money {
type Output = Self;

Expand Down
11 changes: 7 additions & 4 deletions src/money/exchange.rs → src/instruments/fx/exchange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
// - LICENSE-MIT.md
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// use crate::RustQuantError;
// use crate::money::{iso_currencies::*, Currency, Money};
use crate::money::{Currency, Money};
//! FX exchange module.
use crate::instruments::fx::currency::Currency;
use crate::instruments::fx::money::Money;
use std::collections::HashMap;

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -32,8 +33,10 @@ pub struct Exchange {
pub struct ExchangeRate {
/// From currency
pub from_currency: Currency,

/// To currency
pub to_currency: Currency,

/// The actual exchange rate as a ratio from_currency/to_currency
pub rate: f64,
}
Expand Down Expand Up @@ -213,7 +216,7 @@ mod test_exchange_rate {
use crate::iso::*;

use crate::assert_approx_equal;
use crate::money::{EUR, USD};
use crate::cashflows::{EUR, USD};
use std::f64::EPSILON as EPS;

#[test]
Expand Down
12 changes: 12 additions & 0 deletions src/instruments/fx/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// RustQuant: A Rust library for quantitative finance tools.
// Copyright (C) 2023-2024 https://github.com/avhz
// Dual licensed under Apache 2.0 and MIT.
// See:
// - LICENSE-APACHE.md
// - LICENSE-MIT.md
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

pub mod currency;
pub mod exchange;
pub mod money;
Loading

0 comments on commit 853050d

Please sign in to comment.