From 3ec469c92e96d3e725268768103d626b36648327 Mon Sep 17 00:00:00 2001 From: Jacek Czaja Date: Wed, 17 Jan 2024 12:59:46 +0100 Subject: [PATCH] - Dividend v2 -> dividend v3 --- examples/dividends.rs | 4 ++-- src/rest.rs | 4 ++-- src/types.rs | 46 +++++++++++++++++++++++++++++++------------ 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/examples/dividends.rs b/examples/dividends.rs index 95ae19f..7526a73 100644 --- a/examples/dividends.rs +++ b/examples/dividends.rs @@ -29,7 +29,7 @@ async fn main() { .results .iter() .filter(|&x| { - NaiveDate::parse_from_str(&x.ex_date, "%Y-%m-%d").unwrap() + NaiveDate::parse_from_str(&x.ex_dividend_date, "%Y-%m-%d").unwrap() > one_year_ago.naive_local() }) .collect::>(); @@ -47,7 +47,7 @@ async fn main() { } let close = previous_close_res.results.first().unwrap().c; - let sum: f64 = res.iter().map(|d| d.amount).sum(); + let sum: f64 = res.iter().map(|d| d.cash_amount).sum(); println!("Yield for {} is {:.2}% [previous close = {}, sum of last {} dividends = {:.2}]", ticker, diff --git a/src/rest.rs b/src/rest.rs index 8947701..281e96a 100644 --- a/src/rest.rs +++ b/src/rest.rs @@ -206,13 +206,13 @@ impl RESTClient { } /// Get a list of historical dividends for a stock using the - /// [/v2/reference/dividends/{stocks_ticker}](https://polygon.io/docs/get_v2_reference_dividends__stocksTicker__anchor) API. + /// [/v3/reference/dividends/{stocks_ticker}](https://polygon.io/docs/get_v3_reference_dividends__stocksTicker__anchor) API. pub async fn reference_stock_dividends( &self, stocks_ticker: &str, query_params: &HashMap<&str, &str>, ) -> Result { - let uri = format!("/v2/reference/dividends/{}", stocks_ticker); + let uri = format!("/v3/reference/dividends?ticker={}", stocks_ticker); self.send_request::(&uri, query_params) .await } diff --git a/src/types.rs b/src/types.rs index 661ae60..021811a 100644 --- a/src/types.rs +++ b/src/types.rs @@ -237,29 +237,30 @@ pub struct ReferenceStockSplitsResponseV2 { pub type ReferenceStockSplitsResponse = ReferenceStockSplitsResponseV2; // -// v2/reference/dividends/{stocksTicker} +// v3/reference/dividends/ // #[derive(Clone, Deserialize, Debug)] -pub struct ReferenceStockDividendsResultV2 { - pub ticker: String, - #[serde(rename = "exDate")] - pub ex_date: String, - #[serde(rename = "paymentDate")] - pub payment_date: String, - #[serde(rename = "recordDate")] +pub struct ReferenceStockDividendsResultV3 { + pub cash_amount: f64, + pub currency: String, + pub declaration_date: String, + pub dividend_type: DividendType, + pub ex_dividend_date: String, + pub frequency: u32, + pub pay_date: String, pub record_date: String, - pub amount: f64, + pub ticker: String, } #[derive(Clone, Deserialize, Debug)] -pub struct ReferenceStockDividendsResponseV2 { +pub struct ReferenceStockDividendsResponseV3 { + pub next_url: Option, + pub results: Vec, pub status: String, - pub count: u32, - pub results: Vec, } -pub type ReferenceStockDividendsResponse = ReferenceStockDividendsResponseV2; +pub type ReferenceStockDividendsResponse = ReferenceStockDividendsResponseV3; // // v2/reference/financials/{stocksTicker} @@ -864,6 +865,25 @@ pub enum TickType { Quotes, } +#[derive(Clone, Deserialize, Debug)] +pub enum DividendType { + CD, // Consistent dividends paid on schedule + SC, // Special cash dividends (not to be expected to be consistenly paid) + LT, // Long-term capital gain distributions + ST, // Short-term capital gain distributions +} + +impl fmt::Display for DividendType { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + DividendType::CD => write!(f, "CD"), + DividendType::SC => write!(f, "SC"), + DividendType::LT => write!(f, "LT"), + DividendType::ST => write!(f, "ST"), + } + } +} + impl fmt::Display for TickType { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{:?}", self)