business-science / tidyquant

Bringing financial analysis to the tidyverse
https://business-science.github.io/tidyquant/
Other
852 stars 175 forks source link

candles not properly shown #78

Open kurt-o-sys opened 6 years ago

kurt-o-sys commented 6 years ago

I have a zoo object and I'm trying to show a candlestick (or barchart). The graph shows, but it only shows the low-high range, not the open-close range:

dzoo <- as.zoo(dxts)
candles<-tail(dzoo, nlast)
ggplot(candles, aes(x=Index)) +
   geom_point(aes(y=avg), cex=0.2) +
   geom_barchart(aes(x=Index, open=open, high=high, low=low, close=close),
                 color_up="green", color_down="red") +
   theme_tq()

results in:

image

I don't know if it's related but apparently, the x-values are not recognized as POSIXct, although they actually are, see #77 .

mdancho84 commented 6 years ago

It could be related, but we will review during the update to geom_barchart. Do me a favor and please provide a reproducible example so we can get the data you are working with and recreate the issue.

ancailliau commented 6 years ago

Here is a short example reproducing the bug. The open and close bars are not displayed. The code works fine when date is a Date but fails when we want to render intra-day charts.

Sys.setenv(LANG = "en")
Sys.setenv(TZ='Europe/Brussels')

library(ggplot2)
library(tidyquant)  

low <- c(8144.524, 8449.000, 8683.000, 8900.275, 9211.000)
high <- c(8499, 8710, 8970, 9350, 9716)
open <- c(8190.001, 8462.758, 8709.000, 8943.252, 9284.337)
close <- c(8462.758, 8709.000, 8942.000, 9284.337, 9597.000)

date <- c("2017-11-25 00:00:00", "2017-11-25 12:00:00", "2017-11-26 00:00:00",
          "2017-11-26 12:00:00", "2017-11-27 00:00:00")
date <- as.POSIXlt(parse_date_time(date, "Ymd HMS"))

df <- data.frame(date, low, high, open, close)

ggplot() +
    geom_barchart(data=df,
                  aes(x=date,
                      open=open, 
                      high=high, 
                      low=low, 
                      close=close))

The bug makes the library unusable for intra-day data.

ancailliau commented 6 years ago

Diving a little bit more into the issue, I wonder if the following lines are not responsible for the bug. https://github.com/business-science/tidyquant/blob/c9f3261c1a2ebc1381962a05c17063dee4613d43/R/geom_chart.R#L133 https://github.com/business-science/tidyquant/blob/c9f3261c1a2ebc1381962a05c17063dee4613d43/R/geom_chart.R#L152

Indeed, these are not treated the same when converting to a numeric scale.

> date <- c("2017-11-25 00:00:00", "2017-11-25 12:00:00", "2017-11-26 00:00:00",
+           "2017-11-26 12:00:00", "2017-11-27 00:00:00")
> date <- as.POSIXlt(parse_date_time(date, "Ymd HMS"))
> 
> print (as.numeric(date))
[1] 1511568000 1511611200 1511654400 1511697600 1511740800
> print (as.numeric(date) - .5)
[1] 1511568000 1511611200 1511654400 1511697600 1511740800
> 
> date <- c("2017-11-25 00:00:00", "2017-11-25 12:00:00", "2017-11-26 00:00:00",
+           "2017-11-26 12:00:00", "2017-11-27 00:00:00")
> date <- as.Date(parse_date_time(date, "Ymd HMS"))
> print (as.numeric(date))
[1] 17495 17495 17496 17496 17497
> print (as.numeric(date) - .5)
[1] 17494.5 17494.5 17495.5 17495.5 17496.5