[R-SIG-Finance] Quantmod graphing issue
Joshua Ulrich
josh.m.ulrich at gmail.com
Tue Feb 14 18:09:21 CET 2017
On Tue, Feb 14, 2017 at 8:58 AM, Jon Golenbock <jongolenbock at gmail.com> wrote:
> Hi, having an issue and I'm running into a wall. What I'm trying to do is
> set up some alerts given a list of products, terms, and rules. For example,
> look at the prompt 3 months for natural gas and crude, and if there is RSI
> divergence on any of those charts, graph them with the relevant technical
> indicators) and email them out (haven't written this part yet but it should
> be pretty simple I think).
>
> The issue I'm having might be a graphics issue (my suspicion) or I might be
> using a purrr function incorrectly, as I'm still figuring out how to best
> use it's mapping functions.
>
> Code below -- should be spitting out two graphs, but it only creates one
> and then throws the error "Error: is.list(res) is not TRUE". This
> particular (vanilla) example is looking to flag natgas pricing as it breaks
> bollinger bands.
>
> Worth noting, I am using the lmap() function instead of the map() function
> because I want the name passed as well. i.e if I pass df[[1]] to
> graph_send(), it will pass an unnamed dataframe. if i pass df[1], it passes
> a (named) list containing the dataframe, which I can still access using
> mylist[[1]].
>
I have trouble following your code, because my brain does not parse
"%>%" very well. I've also never used purrr, so I can't help if
that's the issue. If you truly suspect the issue is with purr, you
should ask your question on a more general forum, because purrr
doesn't really have anything to do with finance.
Based on your description, it seems like a simple for loop works fine:
for (n in names(df)) {
d <- df[[n]]
lastobs <- last(d)
if (lastobs$cross_up || lastobs$cross_dn) {
x <- na.locf(xts(d$PRICE, d$COB_DT))
dev.new() # open new plot device for each chart
candleChart(x, theme="white", name=n, TA="addBBands()")
}
}
> [[alternative %>% version deleted]]
I would also note that creating OHLC data from close prices of the
same periodicity is not a good idea. The day's open is usually not
the prior day's close.
> Any help is appreciated, thank you in advance!!
>
> library(TTR)
> library(dplyr)
> library(purrr)
> library(magrittr)
> library(quantmod)
>
> graph_send <- function(mylist){
> #print(df %>% str())
> df2 <- mylist[[1]] %<>% dplyr::select(COB_DT, PRICE,QUOTE_DEF_CD) %>%
> dplyr::rename (Date = COB_DT, Close = PRICE) %>%
> dplyr::mutate (Open = lag(Close))
>
>
> df2$Open[1] <- df2$Close[1]
> df2$High <- pmax(df2$Close, df2$Open)
> df2$Low <- pmin(df2$Close, df2$Open)
> #print(df2 %>% str())
>
>
> df3 <- df2 %>% select(Date, Open, High, Low, Close)
> ts <- xts(df3[,-1], df3[,1]) %>% zoo::na.locf()
> #print(ts)
>
>
>
> candleChart(ts, theme = "white", name = names(mylist))
> plot(addBBands())
> }
>
> df <- structure(list(`Henry Hub xh19` = structure(list(COB_DT =
<snip thousands blank lines... and some data>
> ))
> process_lgl <- df %>% map_lgl( ~ .$cross_up[nrow(.)] == TRUE |
> .$cross_dn[nrow(.)] == TRUE)
> process_df <- data.frame(Names = names(process_lgl), Process =
> process_lgl, row.names = NULL)
> process_df
> df[process_df$Process == TRUE] %>% lmap(graph_send)
>
> [[alternative HTML version deleted]]
>
Please follow the posting guide and send plain text. The list server
doesn't like HTML.
> _______________________________________________
> R-SIG-Finance at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions should go.
--
Joshua Ulrich | about.me/joshuaulrich
FOSS Trading | www.fosstrading.com
R/Finance 2017 | www.rinfinance.com
More information about the R-SIG-Finance
mailing list