This project provides a set of functions to receive data from the the yahoo! finance website via their API. This project is licensed under Apache 2.0 or MIT license (see files LICENSE-Apache2.0 and LICENSE-MIT).
Since version 0.3 and the upgrade to reqwest
0.10, all requests to the yahoo API return futures, using async
features.
Therefore, the functions need to be called from within another async
function with .await
or via functions like block_on
. The examples are based on the tokio
runtime applying the tokio-test
crate.
Use the blocking
feature to get the previous behavior back: i.e. yahoo_finance_api = {"version" = "1.0", features = ["blocking"]}
.
Get the latest available quote (without the blocking feature enabled): ```rust use yahoofinanceapi as yahoo; use std::time::{Duration, UNIXEPOCH}; use chrono::prelude::*; use tokiotest;
fn main() {
let provider = yahoo::YahooConnector::new();
// get the latest quotes in 1 minute intervals
let response = tokiotest::blockon(provider.getlatestquotes("AAPL", "1m")).unwrap();
// extract just the latest valid quote summery
// including timestamp,open,close,high,low,volume
let quote = response.lastquote().unwrap();
let time: DateTime
Get history of quotes for given time period: ```rust use yahoofinanceapi as yahoo; use std::time::{Duration, UNIXEPOCH}; use chrono::{Utc,TimeZone}; use tokiotest;
fn main() { let provider = yahoo::YahooConnector::new(); let start = Utc.ymd(2020, 1, 1).andhmsmilli(0, 0, 0, 0); let end = Utc.ymd(2020, 1, 31).andhmsmilli(23, 59, 59, 999); // returns historic quotes with daily interval let resp = tokiotest::blockon(provider.getquotehistory("AAPL", start, end)).unwrap(); let quotes = resp.quotes().unwrap(); println!("Apple's quotes in January: {:?}", quotes); }
```
Search for a ticker given a search string (e.g. company name): ```rust use yahoofinanceapi as yahoo; use tokio_test;
fn main() { let provider = yahoo::YahooConnector::new(); let resp = tokiotest::blockon(provider.search_ticker("Apple")).unwrap();
let mut apple_found = false;
println!("All tickers found while searching for 'Apple':");
for item in resp.quotes
{
println!("{}", item.symbol)
}
}
``
Some fields like
longnameare only optional and will be replaced by default values if missing (e.g. empty string). If you do not like this behavior, use
searchtickeroptinstead which
contains
Optionfields, returning
None` if the field found missing in the response.
Another method to retrieve a range of quotes is by requesting the quotes for a given period and lookup frequency. Here is an example retrieving the daily quotes for the last month:
```rust use yahoofinanceapi as yahoo; use std::time::{Duration, UNIXEPOCH}; use chrono::{Utc,TimeZone}; use tokiotest;
fn main() { let provider = yahoo::YahooConnector::new(); let response = tokiotest::blockon(provider.getquoterange("AAPL", "1d", "1mo")).unwrap(); let quotes = response.quotes().unwrap(); println!("Apple's quotes of the last month: {:?}", quotes); } ```
With the blocking feature enabled, the last example would just look like ```rust use yahoofinanceapi as yahoo;
fn main() { let provider = yahoo::YahooConnector::new(); let response = provider.getquoterange("AAPL", "1d", "1mo").unwrap(); let quotes = response.quotes().unwrap(); println!("Apple's quotes of the last month: {:?}", quotes); } ``` and the other examples would just change accordingly.