Skip to content

Commit

Permalink
Fixing tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
avhz authored and avhz committed Jun 24, 2024
1 parent 4c97fa7 commit 25f887c
Show file tree
Hide file tree
Showing 15 changed files with 129 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Cargo.lock

## PYTHON
.venv/
.env/
venv/
env/
./Bindings/.venv
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ rustdoc-args = ["--html-in-header", "katex_header.html", "--cfg", "docsrs"]
[dependencies]
derive_builder = "0.20.0" # https://docs.rs/derive_builder/latest/derive_builder/
errorfunctions = "0.2.0" # https://docs.rs/errorfunctions/latest/errorfunctions/
nalgebra = "0.32.0" # https://docs.rs/nalgebra/latest/nalgebra/
nalgebra = "0.33.0" # https://docs.rs/nalgebra/latest/nalgebra/
ndarray = "0.15.0" # https://docs.rs/ndarray/latest/ndarray/
ndrustfft = "0.4.0" # https://docs.rs/ndrustfft/latest/ndrustfft/
ndarray-rand = "0.14.0" # https://docs.rs/ndarray-rand/latest/ndarray_rand/
Expand All @@ -76,7 +76,7 @@ num = { version = "0.4.1", features = ["rand"] }
time = { version = "0.3.34", features = ["macros"] }

# https://docs.rs/polars/latest/polars/
polars = { version = "0.39.2", features = ["docs-selection"] }
polars = { version = "0.41.1", features = ["docs-selection"] }


[dev-dependencies]
Expand All @@ -92,5 +92,6 @@ finitediff = "0.1.4" # https://docs.rs/finitediff/latest/finitediff/
# crate-type = ["cdylib"]

# [dependencies.pyo3]
# version = "0.19.0"
# version = "0.22.0"
# features = ["extension-module"]
# features = ["abi3-py37", "extension-module"]
6 changes: 0 additions & 6 deletions bindings/pyproject.toml

This file was deleted.

16 changes: 16 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[build-system]
requires = ["maturin>=1.6,<2.0"]
build-backend = "maturin"

[tool.maturin]
features = ["pyo3/extension-module"]

[project]
name = "RustQuant"
requires-python = ">=3.8"
dynamic = ["version"]
classifiers = [
"Programming Language :: Rust",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
]
Binary file modified src/data/examples/write.parquet
Binary file not shown.
7 changes: 6 additions & 1 deletion src/data/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@ impl DataReader for Data {
fn read(&mut self) -> Result<(), RustQuantError> {
match self.format {
DataFormat::CSV => {
let df = CsvReader::from_path(&self.path)?.finish()?;
// let df = CsvReader::from_path(&self.path)?.finish()?;

let df = CsvReadOptions::default()
.try_into_reader_with_file_path(Some(self.path.clone().into()))?
.finish()?;

self.data = df;

Ok(())
Expand Down
6 changes: 3 additions & 3 deletions src/data/yahoo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ impl YahooFinanceData {

impl YahooFinanceReader for YahooFinanceData {
fn get_price_history(&mut self) -> Result<(), RustQuantError> {
let provider = yahoo::YahooConnector::new();
let provider = yahoo::YahooConnector::new()?;

let response = tokio_test::block_on(provider.get_quote_history(
self.ticker.as_ref().ok_or(RustQuantError::MissingInput(
Expand Down Expand Up @@ -224,7 +224,7 @@ impl YahooFinanceReader for YahooFinanceData {
}

fn get_options_chain(&mut self) -> Result<(), RustQuantError> {
let provider = yahoo::YahooConnector::new();
let provider = yahoo::YahooConnector::new()?;
let response = tokio_test::block_on(provider.search_options(self.ticker.as_ref().ok_or(
RustQuantError::MissingInput("No ticker provided.".to_string()),
)?))?;
Expand Down Expand Up @@ -288,7 +288,7 @@ impl YahooFinanceReader for YahooFinanceData {
}

fn get_latest_quote(&mut self) -> Result<(), RustQuantError> {
let provider = yahoo::YahooConnector::new();
let provider = yahoo::YahooConnector::new()?;
let response = tokio_test::block_on(
provider.get_latest_quotes(
self.ticker
Expand Down
9 changes: 0 additions & 9 deletions src/instruments/bonds/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,3 @@ 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;
51 changes: 51 additions & 0 deletions src/time/countries/custom_calendar.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// RustQuant: A Rust library for quantitative finance tools.
// Copyright (C) 2022-2024 https://github.com/avhz
// Dual licensed under Apache 2.0 and MIT.
// See:
// - LICENSE-APACHE.md
// - LICENSE-MIT.md
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//! This module defines a custom calendar type.

use std::collections::BTreeMap;

use crate::time::{CalendarMetadata, DateRollingConvention, DayCountConvention, Holiday};
use time::Date;

/// Custom calendar type.
pub struct CustomCalendar {
/// Start date of the calendar.
pub start: Date,
/// End date of the calendar.
pub end: Date,
/// Metadata of the calendar.
pub metadata: CalendarMetadata,

/// Holidays in the calendar.
pub holidays: BTreeMap<Date, Holiday>,

/// Day count convention.
pub day_count_convention: DayCountConvention,
/// Date rolling convention.
pub date_rolling_convention: DateRollingConvention,
}

impl CustomCalendar {
// / Create a new custom calendar.
// pub fn new(start: Date, end: Date, metadata: CalendarMetadata) -> Self {
// Self {
// start,
// end,
// metadata,
// }
// }

/// Add a vector of holidays to the calendar.
pub fn add_holidays(&mut self, holidays: Vec<Holiday>) {
for holiday in holidays {
self.holidays.insert(holiday.date, holiday);
}
}
}
3 changes: 3 additions & 0 deletions src/time/countries/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

//! This module defines calendars and holidays for different countries.

/// This module defines a custom calendar type.
pub mod custom_calendar;

/// Calendars implemented for African countries.
pub mod africa {
/// This module defines Botswana holidays and calendars.
Expand Down
20 changes: 20 additions & 0 deletions src/time/date_generation/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// RustQuant: A Rust library for quantitative finance tools.
// Copyright (C) 2022-2024 https://github.com/avhz
// Dual licensed under Apache 2.0 and MIT.
// See:
// - LICENSE-APACHE.md
// - LICENSE-MIT.md
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/// Date generation conventions.
pub enum DateGenerationConvention {
/// Forward from the issue date.
Forward,

/// Backward from the maturity date.
Backward,

/// Zero date generation.
Zero,
}
6 changes: 5 additions & 1 deletion src/time/frequency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::time::constants::{
/// This is important in finance, as it determines the number of times
/// a cash flow is paid in a year, and thus affects the present value
/// of the cash flows.
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Frequency {
/// Daily (252 per year).
Daily = DAILY,
Expand Down Expand Up @@ -49,6 +49,9 @@ pub enum Frequency {

/// Annually.
Annually = ANNUALLY,

/// Zero frequency.
Zero = 0,
}

impl Frequency {
Expand Down Expand Up @@ -100,6 +103,7 @@ impl Frequency {
Frequency::TriAnnually => TRI_ANNUALLY,
Frequency::SemiAnnually => SEMI_ANNUALLY,
Frequency::Annually => ANNUALLY,
Frequency::Zero => 0,
}
}
}
9 changes: 6 additions & 3 deletions src/time/holiday.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ use time::Date;
/// Holiday type.
#[derive(Debug, Clone)]
pub struct Holiday {
name: &'static str,
date: Date,
description: Option<&'static str>,
/// Name of the holiday.
pub name: &'static str,
/// Date of the holiday.
pub date: Date,
/// Description of the holiday (optional).
pub description: Option<&'static str>,
}

impl Eq for Holiday {}
Expand Down
4 changes: 4 additions & 0 deletions src/time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,7 @@ pub use utilities::*;
/// The `Schedule` type.
pub mod schedule;
pub use schedule::*;

/// Date generation rules.
pub mod date_generation;
pub use date_generation::*;
10 changes: 10 additions & 0 deletions src/time/utilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ pub fn leap_year_count(start: Date, end: Date) -> i64 {
.len() as i64
}

/// Function to check if Date is first day of the month.
pub fn is_first_day_of_month(date: Date) -> bool {
date.day() == 1
}

/// Function to check if Date is last day of the month.
pub fn is_last_day_of_month(date: Date) -> bool {
date.day() == days_in_year_month(date.year(), date.month())
}

/// Function to check if date is the last day of February.
pub fn is_last_day_of_february(date: Date) -> bool {
let last_day_of_feb_non_leap =
Expand Down

0 comments on commit 25f887c

Please sign in to comment.