Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented get quote for yahoo finance api #166

Merged
merged 1 commit into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/linear_regression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use nalgebra::{DMatrix, DVector};

use RustQuant::ml::*;

fn main() -> Result<(), LinearRegressionError>{
fn main() -> Result<(), LinearRegressionError> {
// TEST DATA GENERATED FROM THE FOLLOWING R CODE:
//
// set.seed(2023)
Expand Down
46 changes: 45 additions & 1 deletion src/data/yahoo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,43 @@ impl YahooFinanceReader for YahooFinanceData {
}

fn get_latest_quote(&mut self) -> Result<(), YahooFinanceError> {
todo!()
let provider = yahoo::YahooConnector::new();
let response = tokio_test::block_on(
provider.get_latest_quotes(
self.ticker
.as_ref()
.ok_or(YahooFinanceError::MissingInput(
"No ticker provided.".to_string(),
))?
.as_str(),
"1d",
),
)
.unwrap();
let quote = response.last_quote().unwrap();
// Check if the timestamp is within the acceptable range

let timestamp = vec![quote.timestamp];
let open = vec![quote.open];
let high = vec![quote.high];
let low = vec![quote.low];
let close = vec![quote.close];
let volume = vec![quote.volume as f64];
let adjclose = vec![quote.adjclose];

let df = df!(
"timestamp" => timestamp,
"open" => open,
"high" => high,
"low" => low,
"close" => close,
"volume" => volume,
"adjusted" => adjclose
);

self.latest_quote = Some(df?);

Ok(())
}
}

Expand Down Expand Up @@ -335,6 +371,14 @@ mod test_yahoo {

println!("Apple's returns: {:?}", yfd.returns)
}
#[test]
fn test_get_latest_quote() {
let mut yfd = YahooFinanceData::new("AAPL".to_string());

let _ = yfd.get_latest_quote();

println!("Apple's latest quote: {:?}", yfd.latest_quote)
}

#[test]
fn test_get_options_chain() {
Expand Down