Skip to content

Commit

Permalink
#271: temp fix for Polars/hashbrown issue
Browse files Browse the repository at this point in the history
  • Loading branch information
avhz authored and avhz committed Oct 16, 2024
1 parent 1c66875 commit 287946a
Show file tree
Hide file tree
Showing 26 changed files with 99 additions and 243 deletions.
9 changes: 6 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ argmin-math = "0.4.0" # https://docs.rs/argmin-math/latest/argmin_math/
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.33.0" # https://docs.rs/nalgebra/latest/nalgebra/
ndrustfft = "0.4.0" # https://docs.rs/ndrustfft/latest/ndrustfft/
ndarray-rand = "0.14.0" # https://docs.rs/ndarray-rand/latest/ndarray_rand/
ndrustfft = "0.5.0" # https://docs.rs/ndrustfft/latest/ndrustfft/
ndarray-rand = "0.15.0" # https://docs.rs/ndarray-rand/latest/ndarray_rand/
plotly = "0.10.0" # https://docs.rs/plotly/latest/plotly/
plotters = "0.3.5" # https://docs.rs/plotters/latest/plotters/
rand = "0.8.5" # https://docs.rs/rand/latest/rand/
Expand All @@ -72,7 +72,7 @@ rayon = "1.9.0" # https://docs.rs/rayon/latest/rayon/
rust_decimal = "1.34.3" # https://docs.rs/rust_decimal/latest/rust_decimal/
statrs = "0.17.1" # https://docs.rs/statrs/latest/statrs/
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/
yahoo_finance_api = "2.3.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/ndarray/latest/ndarray/
Expand All @@ -90,6 +90,9 @@ polars = { version = "0.43.1", features = ["docs-selection"] }
# https://docs.rs/uuid/latest/uuid/
uuid = { version = "1.10.0", features = ["v4", "fast-rng"] }

# https://docs.rs/serde/latest/hashbrown/
# Remove when Polars is updated, since we don't use it directly.
hashbrown = { version = "=0.14.5", features = ["raw"] }

[dev-dependencies]
finitediff = "0.1.4" # https://docs.rs/finitediff/latest/finitediff/
Expand Down
3 changes: 1 addition & 2 deletions examples/market_data.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use RustQuant::pricer::MarketData;
use RustQuant::pricer::MarketDataBuilder;
use RustQuant::data::{MarketData, MarketDataBuilder};
use RustQuant::time::oceania::australia::AustraliaCalendar;

fn main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/monte_carlo_pricing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use RustQuant::instruments::options::AsianOption;
use RustQuant::instruments::options::OptionContractBuilder;
use RustQuant::instruments::BarrierOption;
use RustQuant::models::geometric_brownian_motion::GeometricBrownianMotion;
use RustQuant::pricer::MonteCarloPricer;
use RustQuant::pricing::MonteCarloPricer;
use RustQuant::stochastics::StochasticProcessConfig;

fn main() {
Expand Down
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ pub use yahoo::*;
pub mod curves;
pub use curves::*;

/// Market data structures and implementations.
pub mod market_data;
pub use market_data::*;

/// Context data structures and implementations.
pub mod context_data;
pub use context_data::*;

// /// Base surface data structure and implementations.
// /// Surfaces are simply [Curve]s with an additional dimension.
// /// For example, a volatility surface is a function of time and strike/moneyness.
Expand Down
150 changes: 79 additions & 71 deletions src/data/yahoo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ pub enum ReturnsType {
pub trait YahooFinanceReader {
/// Retrieves the price history from Yahoo! Finance.
fn get_price_history(&mut self) -> Result<(), RustQuantError>;
/// Retrieves the options chain from Yahoo! Finance.
fn get_options_chain(&mut self) -> Result<(), RustQuantError>;

// /// Retrieves the options chain from Yahoo! Finance.
// fn get_options_chain(&mut self) -> Result<(), RustQuantError>;

/// Retrieves the latest quote from Yahoo! Finance.
fn get_latest_quote(&mut self) -> Result<(), RustQuantError>;
}
Expand Down Expand Up @@ -222,69 +224,75 @@ impl YahooFinanceReader for YahooFinanceData {
Ok(())
}

fn get_options_chain(&mut self) -> Result<(), RustQuantError> {
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()),
)?))?;

let options = response.options;

// YOptionResult { name: "AAPL230526C00250000", strike: 250.0, last_trade_date: "2023-05-25 3:12PM EDT",
// last_price: 250.0, bid: 250.0, ask: 250.0, change: 250.0, change_pct: 250.0,
// volume: 0, open_interest: 0, impl_volatility: 250.0 }

// CANNOT IMPLEMENT THIS YET, BECAUSE THE YAHOO FINANCE API
// DOES NOT RETURN THE CORRECT OPTIONS CHAIN.
// Issue opened: https://github.com/xemwebe/yahoo_finance_api/issues/28
// Pull request opened: https://github.com/xemwebe/yahoo_finance_api/pull/29
// Pull request was merged.
// Now we need to wait for the next release of the yahoo_finance_api crate.

let contract = options.iter().map(|o| o.name.clone()).collect::<Vec<_>>();
let strike = options.iter().map(|o| o.strike).collect::<Vec<_>>();
let last_trade_date = options
.iter()
.map(|o| o.last_trade_date.clone()) //(o.last_trade_date / (24 * 60 * 60)) as i32)
.collect::<Vec<_>>();
let last_price = options.iter().map(|o| o.last_price).collect::<Vec<_>>();
let bid = options.iter().map(|o| o.bid).collect::<Vec<_>>();
let ask = options.iter().map(|o| o.ask).collect::<Vec<_>>();
let change = options.iter().map(|o| o.change).collect::<Vec<_>>();
let change_pct = options.iter().map(|o| o.change_pct).collect::<Vec<_>>();
let volume = options.iter().map(|o| o.volume as u64).collect::<Vec<_>>();
let open_interest = options
.iter()
.map(|o| o.open_interest as u64)
.collect::<Vec<_>>();
let impl_volatility = options
.iter()
.map(|o| o.impl_volatility)
.collect::<Vec<_>>();

let df = df!(
"contract" => contract,
"strike" => strike,
"last_trade_date" => Series::new("last_trade_date".into(), last_trade_date)
.cast(&DataType::Date)
?,
"last_price" => last_price,
"bid" => bid,
"ask" => ask,
"change" => change,
"change_pct" => change_pct,
"volume" => volume,
"open_interest" => open_interest,
"impl_volatility" => impl_volatility
// "contract" => Series::new("date", date).cast(&DataType::Date)?,
);

self.options_chain = Some(df?);

println!("{:?}", self.options_chain);

Ok(())
}
// fn get_options_chain(&mut self) -> Result<(), RustQuantError> {
// 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()),
// )?))?;

// let options = response.option_chain.result;

// // YOptionResult { name: "AAPL230526C00250000", strike: 250.0, last_trade_date: "2023-05-25 3:12PM EDT",
// // last_price: 250.0, bid: 250.0, ask: 250.0, change: 250.0, change_pct: 250.0,
// // volume: 0, open_interest: 0, impl_volatility: 250.0 }

// // CANNOT IMPLEMENT THIS YET, BECAUSE THE YAHOO FINANCE API
// // DOES NOT RETURN THE CORRECT OPTIONS CHAIN.
// // Issue opened: https://github.com/xemwebe/yahoo_finance_api/issues/28
// // Pull request opened: https://github.com/xemwebe/yahoo_finance_api/pull/29
// // Pull request was merged.
// // Now we need to wait for the next release of the yahoo_finance_api crate.

// let contract = options
// .iter()
// .map(|o| o.underlying_symbol)
// .collect::<Vec<_>>();
// let strike = options.iter().map(|o| o.strikes).collect::<Vec<_>>();
// let last_trade_date = options
// .iter()
// .map(|o| o.options.last_trade_date.clone()) //(o.last_trade_date / (24 * 60 * 60)) as i32)
// .collect::<Vec<_>>();
// let last_price = options
// .iter()
// .map(|o| o.options..last_price)
// .collect::<Vec<_>>();
// let bid = options.iter().map(|o| o.bid).collect::<Vec<_>>();
// let ask = options.iter().map(|o| o.ask).collect::<Vec<_>>();
// let change = options.iter().map(|o| o.change).collect::<Vec<_>>();
// let change_pct = options.iter().map(|o| o.change_pct).collect::<Vec<_>>();
// let volume = options.iter().map(|o| o.volume as u64).collect::<Vec<_>>();
// let open_interest = options
// .iter()
// .map(|o| o.open_interest as u64)
// .collect::<Vec<_>>();
// let impl_volatility = options
// .iter()
// .map(|o| o.impl_volatility)
// .collect::<Vec<_>>();

// let df = df!(
// "contract" => contract,
// "strike" => strike,
// "last_trade_date" => Series::new("last_trade_date".into(), last_trade_date)
// .cast(&DataType::Date)
// ?,
// "last_price" => last_price,
// "bid" => bid,
// "ask" => ask,
// "change" => change,
// "change_pct" => change_pct,
// "volume" => volume,
// "open_interest" => open_interest,
// "impl_volatility" => impl_volatility
// // "contract" => Series::new("date", date).cast(&DataType::Date)?,
// );

// self.options_chain = Some(df?);

// println!("{:?}", self.options_chain);

// Ok(())
// }

fn get_latest_quote(&mut self) -> Result<(), RustQuantError> {
let provider = yahoo::YahooConnector::new()?;
Expand Down Expand Up @@ -369,12 +377,12 @@ mod test_yahoo {
println!("Apple's latest quote: {:?}", yfd.latest_quote)
}

#[test]
fn test_get_options_chain() {
let mut yfd = YahooFinanceData::new("AAPL".to_string());
// #[test]
// fn test_get_options_chain() {
// let mut yfd = YahooFinanceData::new("AAPL".to_string());

let _ = yfd.get_options_chain();
// let _ = yfd.get_options_chain();

// println!("Apple's options chain: {:?}", yfd.options_chain)
}
// // println!("Apple's options chain: {:?}", yfd.options_chain)
// }
}
2 changes: 1 addition & 1 deletion src/instruments/options/vanilla.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ mod test_vanilla_option_monte_carlo {
use crate::instruments::AsianOption;
use crate::instruments::AveragingMethod;
use crate::instruments::StrikeFlag;
use crate::pricer::monte_carlo_pricer::MonteCarloPricer;
use crate::pricing::monte_carlo_pricer::MonteCarloPricer;
use crate::stochastics::StochasticProcessConfig;
use crate::{
instruments::{ExerciseFlag, OptionContractBuilder},
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub mod math;
pub mod ml;
pub mod models;
pub mod portfolio;
pub mod pricer;
pub mod pricing;
pub mod stochastics;
pub mod time;
pub mod trading;
Expand Down
94 changes: 0 additions & 94 deletions src/pricer/analytic_pricer.rs

This file was deleted.

Empty file.
Empty file removed src/pricer/lattice_pricer.rs
Empty file.
Loading

0 comments on commit 287946a

Please sign in to comment.