#finance #stock #quote #yahoo

yahoo_finance_api

A rust adapter for the yahoo! finance API to fetch histories of market data quotes

12 releases (3 stable)

1.0.2 Nov 2, 2020
0.3.4 Oct 18, 2020
0.3.2 Aug 2, 2020
0.3.1 Jul 27, 2020
0.1.0 Apr 23, 2020

#40 in Math

Download history 31/week @ 2020-08-13 21/week @ 2020-08-20 27/week @ 2020-08-27 57/week @ 2020-09-03 31/week @ 2020-09-10 16/week @ 2020-09-17 27/week @ 2020-09-24 6/week @ 2020-10-01 19/week @ 2020-10-08 29/week @ 2020-10-15 25/week @ 2020-10-22 73/week @ 2020-10-29 51/week @ 2020-11-05 27/week @ 2020-11-12 31/week @ 2020-11-19 64/week @ 2020-11-26

120 downloads per month
Used in 3 crates (via finql)

MIT/Apache

26KB
564 lines

yahoo! finance API

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):

use yahoo_finance_api as yahoo;
use std::time::{Duration, UNIX_EPOCH};
use chrono::prelude::*;

#[tokio::main]
async fn main() {
    let provider = yahoo::YahooConnector::new();
    // get the latest quotes in 1 minute intervals
    let response = provider.get_latest_quotes("AAPL", "1m").await.unwrap();
    // extract just the latest valid quote summery
    // including timestamp,open,close,high,low,volume
    let quote = response.last_quote().unwrap();
    let time: DateTime<Utc> =
        DateTime::from(UNIX_EPOCH + Duration::from_secs(quote.timestamp));
    println!("At {} quote price of Apple was {}", time.to_rfc3339(), quote.close);
}

Get history of quotes for given time period:

use yahoo_finance_api as yahoo;
use std::time::{Duration, UNIX_EPOCH};
use chrono::{Utc,TimeZone};
use tokio_test;

fn main() {
    let provider = yahoo::YahooConnector::new();
    let start = Utc.ymd(2020, 1, 1).and_hms_milli(0, 0, 0, 0);
    let end = Utc.ymd(2020, 1, 31).and_hms_milli(23, 59, 59, 999);
    // returns historic quotes with daily interval
    let resp = tokio_test::block_on(provider.get_quote_history("AAPL", start, end)).unwrap();
    let quotes = resp.quotes().unwrap();
    println!("Apple's quotes in January: {:?}", quotes);
}

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:

use yahoo_finance_api as yahoo;
use std::time::{Duration, UNIX_EPOCH};
use chrono::{Utc,TimeZone};
use tokio_test;

fn main() {
    let provider = yahoo::YahooConnector::new();
    let response = tokio_test::block_on(provider.get_quote_range("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

use yahoo_finance_api as yahoo;
use std::time::{Duration, UNIX_EPOCH};
use chrono::{Utc,TimeZone};

fn main() {
    let provider = yahoo::YahooConnector::new();
    let response = provider.get_quote_range("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.

Dependencies

~6–10MB
~228K SLoC