Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In getDividends have option to not append .div to the column name #369

Open
ggrothendieck opened this issue Jan 30, 2023 · 6 comments
Open

Comments

@ggrothendieck
Copy link

ggrothendieck commented Jan 30, 2023

Currently getDividends is hard coded to append .div to the column name. Would like to be able to specify name. Maybe name = "%s.div" if you wanted it and that could be the default or "%s" if not or "div" if you wanted name to be literally div.

In that case this could be simplified:

library(dplyr);
library(quantmod);
library(purrr);
library(tools);
stocks <- c("AAPL", "MMM", "CAT", "MSFT", "TSLA");

map_dfr(stocks, \(x) getDividends(x) |> 
                     fortify.zoo(melt  = TRUE, names = c("date", "name", "div")) |>
                     tail(2) |>
                     mutate(name = file_path_sans_ext(name)))

to

library(quantmod);
library(purrr);

stocks <- c("AAPL", "MMM", "CAT", "MSFT", "TSLA");

map_dfr(stocks, \(x) getDividends(x, name = "%s") |> 
                     fortify.zoo(melt  = TRUE, names = c("date", "name", "div")) |>
                     tail(2) )
@joshuaulrich
Copy link
Owner

Thanks for the suggestion. Whatever we do here should be done for getSplits() too.

joshuaulrich added a commit that referenced this issue Feb 3, 2023
This allows the user to specify a custom column name, instead of the
default being hard-coded to the symbol + .div or .spl.

Fixes #369.
@joshuaulrich
Copy link
Owner

Please try the change on the 369-getdividend-column-name branch and let me know what you think. It seems to do what you want:

stocks <- c("AAPL", "MMM", "CAT", "MSFT", "TSLA")
map_dfr(stocks, \(x) getDividends(x, colname = x) |>
                     fortify.zoo(melt  = TRUE, names = c("date", "name", "div")) |>
                     tail(2))
##         date name  div
## 1 2022-08-05 AAPL 0.23
## 2 2022-11-04 AAPL 0.23
## 3 2008-02-20  MMM 0.50
## 4 2022-11-17  MMM 1.49
## 5 2020-01-17  CAT 1.03
## 6 2023-01-19  CAT 1.20
## 7 2022-08-17 MSFT 0.62
## 8 2022-11-16 MSFT 0.68

@ggrothendieck
Copy link
Author

That presumably would work but does have the disadvantage that it is not ideal with pipes since x had to be repeated requiring that it be put in a function.

@joshuaulrich
Copy link
Owner

Good point. What do you think about changing the arg to suffix = ".div"? Then you could set suffix = "" to omit it. Then this would do what you're asking for.

"AAPL" |> quantmod::getSplits(suffix = "")

                AAPL
1987-06-16 0.5000000
2000-06-21 0.5000000
2005-02-28 0.5000000
2014-06-09 0.1428571
2020-08-31 0.2500000

joshuaulrich added a commit that referenced this issue Feb 24, 2023
Give the user opportunity to remove the ".div" or ".spl" suffix from
the symbol column name, rather than require they specify the entire
column name.

See #369.
@joshuaulrich
Copy link
Owner

@ggrothendieck what do you think of the latest commit on this branch?

R$ "AAPL" |> quantmod::getSplits(colname.suffix = "")
                AAPL
1987-06-16 0.5000000
2000-06-21 0.5000000
2005-02-28 0.5000000
2014-06-09 0.1428571
2020-08-31 0.2500000
R$ "AAPL" |> quantmod::getDividends(colname.suffix = "") |> head()
               AAPL
1987-05-11 0.000536
1987-08-10 0.000536
1987-11-17 0.000714
1988-02-12 0.000714
1988-05-16 0.000714
1988-08-15 0.000714

@joshuaulrich joshuaulrich added this to the 0.4.21 milestone Feb 27, 2023
@joshuaulrich
Copy link
Owner

@ggrothendieck any feedback on this before I merge?

@joshuaulrich joshuaulrich removed this from the Release 0.4.21 milestone Apr 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants