Skip to content

Commit

Permalink
Merge pull request #258 from avhz/market-data
Browse files Browse the repository at this point in the history
Market data
  • Loading branch information
avhz committed Aug 10, 2024
2 parents cbd8f87 + 434e3ec commit 9c6fa43
Show file tree
Hide file tree
Showing 110 changed files with 3,146 additions and 1,919 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ thiserror = "1.0.57" # https://docs.rs/thiserror/latest/thiserror/
yahoo_finance_api = "2.1.0" # https://docs.rs/yahoo-finance-api/latest/yahoo_finance_api/
tokio-test = "0.4.3" # https://docs.rs/tokio-test/latest/tokio_test/


# https://docs.rs/num/latest/num/
num = { version = "0.4.1", features = ["rand"] }

Expand All @@ -84,6 +85,9 @@ time = { version = "0.3.34", features = ["macros"] }
# https://docs.rs/polars/latest/polars/
polars = { version = "0.41.1", features = ["docs-selection"] }

# https://docs.rs/uuid/latest/uuid/
uuid = { version = "1.10.0", features = ["v4", "fast-rng"] }


[dev-dependencies]
finitediff = "0.1.4" # https://docs.rs/finitediff/latest/finitediff/
Expand Down
1 change: 1 addition & 0 deletions book/book.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ title = "The RustQuant Book"
## and you will be able to use the usual $...$ and $$...$$ delimiters.
[output.html]
mathjax-support = true
fold = { enable = true }
25 changes: 22 additions & 3 deletions book/src/Introduction.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
# Introduction to RustQuant
![](./assets/logo.png)

RustQuant is a Rust library (crate) for quantitative finance.

You can download the library from [Crates.io](https://crates.io/crates/RustQuant), and API documentation can be found at [Docs.rs](https://docs.rs/crate/RustQuant/latest).
# Code

> Note: this book is a very early work-in-progress and has almost no content.
The crate is available for download from [Crates.io](https://crates.io/crates/RustQuant), and the source is available on [GitHub](https://github.com/avhz/RustQuant).

# Installation

RustQuant of course requires [Rust](https://www.rust-lang.org/) to be installed first.

You can easily add RustQuant to your Rust project by running:

```bash
cargo add RustQuant
```

# Documentation

API documentation can be found at [Docs.rs](https://docs.rs/crate/RustQuant/latest).

The documentation contained in this book is more *"cookbook"* style.

Contributions to documentation in any form are highly welcome.

> Note: this book is a very early work-in-progress and has almost no content.
1 change: 0 additions & 1 deletion book/src/Modules.md

This file was deleted.

File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion book/src/Modules/data.md

This file was deleted.

39 changes: 39 additions & 0 deletions book/src/Modules/data/curves.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Curves

Curves can be fit to market data. Here we include an example of a spot curve being fitted.

![`Spot curve`](../../assets/spotcurve.png)

```rust
{{#include ../../../../examples/curves_spot.rs}}
```


<table>
<tr>
<th> Good </th>
<th> Bad </th>
</tr>
<tr>
<td>

```rust
int foo() {
int result = 4;
return result;
}
```

</td>
<td>

```rust
int foo() {
int x = 4;
return x;
}
```

</td>
</tr>
</table>
8 changes: 8 additions & 0 deletions book/src/Modules/data/data.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# `data`

The `data` module encompasses everything data related.

That is, anything that can be observed, either in markets or derived from market observable data, and also facilities to manage that data.

Another form of data is contextual (or reference) data. These are things such as calendars and date conventions. While there are facilities to handle these data inside the `data` module, the underlying implementations are in other modules, such as the `time` module.

2 changes: 1 addition & 1 deletion book/src/Modules/error.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# `error`
# error
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion book/src/Modules/macros.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# `macros`
# macros
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
46 changes: 30 additions & 16 deletions book/src/SUMMARY.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
# Summary

- [Introduction to RustQuant](./Introduction.md)
- [Modules](./Modules.md)
- [`autodiff`](./Modules/autodiff.md)
- [`cashflows`](./Modules/cashflows.md)
- [`data`](./Modules/data.md)
- [`error`](./Modules/error.md)
- [`instruments`](./Modules/instruments.md)
- [`iso`](./Modules/iso.md)
- [`macros`](./Modules/macros.md)
- [`math`](./Modules/math.md)
- [`ml`](./Modules/ml.md)
- [`models`](./Modules/models.md)
- [`portfolio`](./Modules/portfolio.md)
- [`stochastics`](./Modules/stochastics.md)
- [`time`](./Modules/time.md)
- [`trading`](./Modules/trading.md)
# Introduction

- [RustQuant](./Introduction.md)

---

# Modules

<!-- - [`autodiff`](./Modules/autodiff/autodiff.md)
- [`cashflows`](./Modules/cashflows/cashflows.md) -->

- [`data`](./Modules/data/data.md)
- [Curves](./Modules/data/curves.md)

<!-- - [`error`](./Modules/error.md)
- [`instruments`](./Modules/instruments/instruments.md)
- [`iso`](./Modules/iso/iso.md)
- [`macros`](./Modules/macros.md)
- [`math`](./Modules/math/math.md)
- [`ml`](./Modules/ml/ml.md)
- [`models`](./Modules/models/models.md)
- [`portfolio`](./Modules/portfolio/portfolio.md)
- [`stochastics`](./Modules/stochastics/stochastics.md)
- [`time`](./Modules/time/time.md)
- [`trading`](./Modules/trading/trading.md) -->

---

# Development

- [Contributing](./Contributing.md)
- [File Template](./Template.md)
- [References](./References.md)
Expand Down
Binary file added book/src/assets/gbm.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added book/src/assets/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added book/src/assets/logo_banner.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added book/src/assets/logo_square.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added book/src/assets/spotcurve.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion examples/curve.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use plotly::{Plot, Scatter};
use time::macros::date;
use time::{Date, Duration};
use RustQuant::data::Curve;
Expand Down
7 changes: 3 additions & 4 deletions examples/curves_discount.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use plotly::{Plot, Scatter};
use polars::prelude::*;
use time::macros::date;
use time::{Date, Duration};
use RustQuant::data::{discount_curve, Curve, DiscountCurve};
use time::Date;
use RustQuant::data::Curves;
use RustQuant::data::{Curve, DiscountCurve};
use RustQuant::time::oceania::australia::AustraliaCalendar;
use RustQuant::time::Calendar;

fn main() {
let cal = AustraliaCalendar;
Expand Down
97 changes: 55 additions & 42 deletions examples/curves_spot.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,13 @@
use plotly::{Plot, Scatter};
// use polars::prelude::*;
use time::macros::date;
use time::{Date, Duration};
use RustQuant::data::CurveModel;
use RustQuant::data::Curves;
use RustQuant::data::{Curve, DiscountCurve, SpotCurve};
use RustQuant::models::NelsonSiegelSvensson;
use time::{macros::date, Date};
use RustQuant::data::{Curves, SpotCurve};
use RustQuant::time::oceania::australia::AustraliaCalendar;
// use RustQuant::time::Calendar;

fn main() {
// let cal = AustraliaCalendar;
// let curve = Curve::<Date>::new_from_slice(&DATES, &RATES);
let mut spot_curve = SpotCurve::<Date, AustraliaCalendar>::new(&DATES, &RATES);

let mut discount_curve = SpotCurve::<Date, AustraliaCalendar>::new(&DATES, &RATES);
spot_curve.get_rates(&NEW_DATES);

let new_dates = [
date!(2025 - 01 - 01),
date!(2026 - 01 - 01),
date!(2027 - 01 - 01),
date!(2028 - 01 - 01),
date!(2029 - 01 - 01),
date!(2030 - 01 - 01),
date!(2033 - 01 - 01),
date!(2036 - 01 - 01),
date!(2040 - 01 - 01),
date!(2044 - 01 - 01),
date!(2046 - 01 - 01),
date!(2048 - 01 - 01),
date!(2050 - 01 - 01),
date!(2053 - 01 - 01),
];

discount_curve.get_rates(&new_dates);

discount_curve.plot();

// let nss = NelsonSiegelSvensson::new(0.0806, -0.0031, -0.0625, -0.0198, 1.58, 0.15);

// let date = date!(2027 - 01 - 01);
// println!("Forward rate: {:?}", nss.forward_rate(date));
// println!("Spot rate: {:?}", nss.spot_rate(date));
spot_curve.plot();
}

const DATES: [Date; 33] = [
Expand Down Expand Up @@ -79,10 +46,56 @@ const DATES: [Date; 33] = [
date!(2054 - 07 - 28),
];

#[rustfmt::skip]
const RATES: [f64; 33] = [
0.03400521, 0.03259227, 0.0313705, 0.03031886, 0.02746567, 0.02614014, 0.02574612, 0.02590431,
0.02637474, 0.02700684, 0.02770726, 0.02841916, 0.02910886, 0.02975736, 0.03035484, 0.03089715,
0.0313836, 0.03181554, 0.03219547, 0.03252652, 0.03281203, 0.03305541, 0.03326001, 0.033429,
0.03356541, 0.03367205, 0.03375153, 0.03380629, 0.03383858, 0.03385046, 0.03384384, 0.03382048,
0.03400521,
0.03259227,
0.0313705,
0.03031886,
0.02746567,
0.02614014,
0.02574612,
0.02590431,
0.02637474,
0.02700684,
0.02770726,
0.02841916,
0.02910886,
0.02975736,
0.03035484,
0.03089715,
0.0313836,
0.03181554,
0.03219547,
0.03252652,
0.03281203,
0.03305541,
0.03326001,
0.033429,
0.03356541,
0.03367205,
0.03375153,
0.03380629,
0.03383858,
0.03385046,
0.03384384,
0.03382048,
0.033782,
];

const NEW_DATES: [Date; 14] = [
date!(2025 - 01 - 01),
date!(2026 - 01 - 01),
date!(2027 - 01 - 01),
date!(2028 - 01 - 01),
date!(2029 - 01 - 01),
date!(2030 - 01 - 01),
date!(2033 - 01 - 01),
date!(2036 - 01 - 01),
date!(2040 - 01 - 01),
date!(2044 - 01 - 01),
date!(2046 - 01 - 01),
date!(2048 - 01 - 01),
date!(2050 - 01 - 01),
date!(2053 - 01 - 01),
];
3 changes: 2 additions & 1 deletion examples/custom_payoffs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ fn main() {

// Generate path using Euler-Maruyama scheme.
// Parameters: x_0, t_0, t_n, n, sims, parallel.
let gbm_out = gbm.euler_maruyama(INITIAL_VALUE, START_TIME, END_TIME, NUM_STEPS, NUM_SIMS, PARALLEL);
let config = StochasticProcessConfig::new(INITIAL_VALUE, START_TIME, END_TIME, NUM_STEPS, NUM_SIMS, PARALLEL);
let gbm_out = gbm.euler_maruyama(&config);

// Price the options.
println!("Up-and-out call: {}", barrier_option_payoff(&gbm_out.paths[0], 10.0, 12.0, OptionType::Call, BarrierType::UpAndOut));
Expand Down
9 changes: 7 additions & 2 deletions examples/custom_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@
// """

use std::f64::consts::PI;
use RustQuant::{math::Sequence, plot_vector, stochastics::process::StochasticProcess};
use RustQuant::{
math::Sequence,
plot_vector,
stochastics::{process::StochasticProcess, StochasticProcessConfig},
};

fn main() {
// Create an x-axis.
Expand All @@ -40,7 +44,8 @@ fn main() {
};

// Generate a path and plot it.
let output = custom_process.euler_maruyama(0.01, 0.0, 10.0, 500, 1, false);
let config = StochasticProcessConfig::new(0.01, 0.0, 10.0, 500, 1, false);
let output = custom_process.euler_maruyama(&config);
plot_vector!(output.paths[0], "./images/ricker_wavelet_process.png");
}

Expand Down
14 changes: 14 additions & 0 deletions examples/market_data.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use RustQuant::pricer::MarketData;
use RustQuant::pricer::MarketDataBuilder;
use RustQuant::time::oceania::australia::AustraliaCalendar;

fn main() {
let market_data: MarketData<AustraliaCalendar> = MarketDataBuilder::default()
.underlying_price(Some(100.0))
.volatility(Some(0.2))
.dividend_yield(Some(0.0))
.build()
.unwrap();

println!("{:?}", market_data);
}
Loading

0 comments on commit 9c6fa43

Please sign in to comment.