xemwebe / yahoo_finance_api

Simple wrapper to yahoo! finance API to retrieve latest quotes and end-of-day quote histories
Apache License 2.0
64 stars 29 forks source link

support for market events (splits and dividends) #15

Closed xgillard closed 3 years ago

xgillard commented 3 years ago

For a project of mine I needed to be able to fetch the market events information (splits and dividends). That information is displayed on the web but it was not available through yahoo_finance_api. The patch I propose simply fills this gap.

Hope you'll like it. Thanks a lot for the crate anyways ! :-)

Here is an example of how it is used:

// To make a request, nothing changes from what you're used to.
let conn   = yahoo::YahooConnector::new();
let tick   = "TSLA";
let start  = DateTime::parse_from_rfc3339("2020-08-30T00:00:00.00Z")?.with_timezone(&Utc);
let end    = DateTime::parse_from_rfc3339("2020-09-02T00:00:00.00Z")?.with_timezone(&Utc);
let hist  = conn.get_quote_history(ticker, start, end).await?;

// Accessing the stocks does not change either
println!("{}", ticker);
println!("QUOTES");
for quote in hist.quotes()? {
    let time = DateTime::<Utc>::from(UNIX_EPOCH + Duration::from_secs(quote.timestamp));
    println!("{} | {:.2} | {:.2}", time, quote.open, quote.close);
}

// But in addition you can show the market events as you like
println!("SPLITS");
for split in hist.splits()? {
    let date = DateTime::<Utc>::from(UNIX_EPOCH + Duration::from_secs(split.date));
    println!("{} | {} : {}", date, split.numerator, split.denominator);
}
println!("DIVIDENDS");
for dividend in hist.dividends()? {
    let date = DateTime::<Utc>::from(UNIX_EPOCH + Duration::from_secs(dividend.date));
    println!("{} | {:.3}", date, dividend.amount);
}
xemwebe commented 3 years ago

I definitely like your contribution! Unfortunately I am quite busy right now, but will hopefully find a slot in the next few days to review your PR.

xemwebe commented 3 years ago

Your PR is merged to master. I have also added two examples based on your sample usage.