joshuaulrich / quantmod

Quantitative Financial Modelling Framework
http://www.quantmod.com/
GNU General Public License v3.0
823 stars 224 forks source link

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

Open ggrothendieck opened 1 year ago

ggrothendieck commented 1 year ago

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 commented 1 year ago

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

joshuaulrich commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

@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 commented 1 year ago

@ggrothendieck any feedback on this before I merge?