Skip to content

Commit

Permalink
fix: change get_historic_value to query a range of days
Browse files Browse the repository at this point in the history
... in case the market is closed on a given date
  • Loading branch information
MarkusZoppelt committed Nov 20, 2022
1 parent adb367c commit 56571ef
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ async fn main() {

// Yahoo first of the year is YYYY-01-03
let first_of_the_year = Utc
.with_ymd_and_hms(Utc::now().year(), 1, 3, 0, 0, 0)
.with_ymd_and_hms(Utc::now().year(), 1, 1, 0, 0, 0)
.unwrap();
let first_of_the_month = Utc
.with_ymd_and_hms(Utc::now().year(), Utc::now().month(), 3, 0, 0, 0)
Expand Down
2 changes: 1 addition & 1 deletion src/portfolio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ mod tests {
#[tokio::test]
async fn test_get_historic_total_value() {
let portfolio = Portfolio::new();
let date = Utc.with_ymd_and_hms(2023, 1, 3, 0, 0, 0).unwrap();
let date = Utc.with_ymd_and_hms(2023, 1, 1, 0, 0, 0).unwrap();
let value = portfolio.get_historic_total_value(date).await;
assert_eq!(value, 0.0);
}
Expand Down
5 changes: 3 additions & 2 deletions src/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ pub async fn get_historic_price(
date: DateTime<Utc>,
) -> Result<yahoo::YResponse, yahoo::YahooError> {
let start = date;
let end = date;
// get a range of 3 days in case the market is closed on the given date
let end = start + chrono::Duration::days(3);
yahoo::YahooConnector::new()
.get_quote_history(ticker, start, end)
.await
Expand Down Expand Up @@ -131,7 +132,7 @@ mod tests {

#[tokio::test]
async fn test_get_historic_price() {
let date = Utc.with_ymd_and_hms(2020, 1, 3, 0, 0, 0).unwrap();
let date = Utc.with_ymd_and_hms(2020, 1, 1, 0, 0, 0).unwrap();
let quote = get_historic_price("AAPL", date).await.unwrap();
assert_eq!(
quote.quotes().unwrap().last().unwrap().close,
Expand Down

0 comments on commit 56571ef

Please sign in to comment.