I want to import daily stock market price data into R from any ticker, and examine one historical time segment of it. Then, from this segment, convert these prices into daily ROC/rateofchange % changes. Next, take this ROC series and create a cumulative probability density function which allows me to set any custom number of sorting bins, and any size limit for each bin. example: 22 bins with .3% limit. Next, plot this CPDF as either a histogram or a scatterplot. The final step would be to do this for 2 different sections of the same stock and plot them next to each other for visual inspection. I have started a code on stock ticker SPY, but I cannot get it to work.
library(quantmod)
library(tidyquant)
library(tidyverse)
# using tidyverse to import a ticker
spy <- tq_get("spy")
spy010422 <- tq_get("spy", get ="stock.prices", from ='2022-01-04', to = '2022-01-24')
str(spy010422)
# getting ROC between prices in the series
spy010422.rtn = ROC(spy010422$close, n = 1, type = c("discrete"), na.pad = TRUE)
str(spy010422.rtn)
# trying to use ggplot and tibble to create an ECDF function
spy010422.rtn %>%
tibble() %>%
ggplot() +
stat_ecdf(aes(.))
# another attempt at running ECDF on the ROC series
spy010422.rtn %>%
ggplot(spy010422.rtn) +
stat_ecdf(aes(close))
# trying to set the number of bins and bin size for the ECDF
spy010422.rtn %>%
mutate(rounded = round(close/.3, 0) *.3,
bin = min_rank(rounded)) %>%
ggplot(aes(close, bin)) +
geom_line()
# next time segment of the ticker spy to compare this to
spy020222 <- tq_get("spy", get ="stock.prices", from ='2022-02-02', to = '2022-02-24')