tidyquant icon indicating copy to clipboard operation
tidyquant copied to clipboard

Incorrect candlestick charting with intraday data

Open quantviews opened this issue 7 years ago • 10 comments

Hi! I've been trying to use tidyquant to chart intraday day and 've got problems with that. For example, I have data in the following format:

x A tibble: 465 × 6 date open high low close volume 1 2017-02-23 00:00:00 53.60 53.62 53.59 53.61 416 2 2017-02-23 00:05:00 53.60 53.61 53.58 53.58 206 3 2017-02-23 00:10:00 53.58 53.60 53.55 53.57 307 4 2017-02-23 00:15:00 53.57 53.58 53.55 53.57 221 5 2017-02-23 00:20:00 53.57 53.60 53.56 53.58 189 6 2017-02-23 00:25:00 53.58 53.58 53.52 53.53 420 7 2017-02-23 00:30:00 53.53 53.88 53.50 53.87 5636 8 2017-02-23 00:35:00 53.86 53.89 53.83 53.87 2205 9 2017-02-23 00:40:00 53.86 53.93 53.86 53.89 1774 10 2017-02-23 00:45:00 53.88 53.94 53.88 53.91 1468 ... with 455 more rows

x[1:50,] %>% ggplot(aes(x = date, y = close)) + geom_candlestick(aes(open = open, high = high, low = low, close = close))

produces the following chart - bars do not have bodies just wigs. For daily data it works just fine. Do you have suggestions on it?

quantviews avatar Feb 24 '17 18:02 quantviews

@quantviews Can you provide the data set as well? I'd like to reproduce the issue you are experiencing. Thanks, Matt

mdancho84 avatar Feb 25 '17 15:02 mdancho84

@mdancho84 this is a data as well. Thank you!

quantviews avatar Feb 26 '17 23:02 quantviews

@quantviews So what's happening is that the width of the rectangle is smaller than the x-axis ticks so you don't see a rectangle. Internally, I only have the rectangle scaled properly for daily data, not data by the second. I will add this as a bug and fix accordingly. -Matt

mdancho84 avatar Mar 05 '17 21:03 mdancho84

@mdancho84 Hi, I have the same problem described by @quantviews, I don't see the chart correctly using both geom_barchart or geom_candlestick. I noticed the bug depends on data frame I use also if data are the same (I tried also to export data in csv and they are exactly equal). This is the test I did: ``

setwd("C:/R/App/")
source("./DataDump/FBK.MI.txt") 	
xtsPrices1 <- FBK.MI["2017-07"]
dfPrices1 <- data.frame(Date=index(xtsPrices1),coredata(xtsPrices1))
names(dfPrices1)<- c("Date", "Open", "High", "Low", "Close", "Volume", "Adjusted")
write.csv(dfPrices1, file = "./MyData1.csv")

require(quantmod)
xtsPrices2 <- getSymbols('FBK.MI', src='yahoo',  auto.assign=FALSE)
xtsPrices2 <- xtsPrices2["2017-07"]
dfPrices2 <- data.frame(Date=index(xtsPrices2),coredata(xtsPrices2))
names(dfPrices2)<- c("Date", "Open", "High", "Low", "Close", "Volume", "Adjusted")
write.csv(dfPrices2, file = "./MyData2.csv")

### test: If i use dfPrices1 chart doesn't work fine, If i use dfPrices2 chart works fine ###
dfPrices <- dfPrices1
symbol <-'FBK.MI'
p <- ggplot(data=dfPrices, aes(x=Date, y=Close)) +
		labs(title = symbol, subtitle="", y="Closing Price", x="Date") + 
		theme_tq()
p <- p+ #geom_candlestick(aes(open=Open, high=High, low=Low, close=Close), na.rm = TRUE,
		#				 color_up = "darkgreen", color_down = "darkred",  fill_up  = "darkgreen", fill_down  = "darkred"
		#) +		 
		geom_barchart(aes(open=Open, high=High, low=Low, close=Close), na.rm = TRUE,
				  color_up = "darkgreen", color_down = "darkred", size = 1) +
		labs(subtitle='candleStickChart')
p

`` If I plot chart using dfPrices1 I see the bug, if I use dfPrices2 it works fine. Do you understand where is the problem?

FBK.MI.txt Thanks

fc1984 avatar Aug 02 '17 22:08 fc1984

This is a bug related to the numeric scale used when plotting dates versus datetimes. We need to rescale so the charts will show correctly. We plan to fix in V0.6.0.

mdancho84 avatar Aug 03 '17 14:08 mdancho84

Sir, any ETA for V0.6.0 sir? thank you! 👍

rareal avatar Mar 13 '18 16:03 rareal

This appears to still be an issue in tidyquant 1.0.1.9000.

library(ggplot2)
library(dplyr)
library(lubridate)
library(tidyquant)

tidyquant::FANG %>% 
  filter(symbol=='FB') %>% 
  mutate(date=round(with_tz(Sys.time(), 'UTC'), units='hours') - 
           hours(seq_len(nrow(.))),
                date=as.POSIXct(date)) %>% 
  filter(date > Sys.time() - days(3)) %>% 
  ggplot(aes(x = date, y = close, group = symbol)) +
  geom_candlestick(aes(open = open, high = high, low = low, close = close))

Works as expected for daily data:

tidyquant::FANG %>% 
  filter(symbol=='FB') %>% 
  filter(date > max(date) - days(90)) %>% 
  ggplot(aes(x = date, y = close, group = symbol)) +
  geom_candlestick(aes(open = open, high = high, low = low, close = close))

Created on 2020-07-26 by the reprex package (v0.3.0)

Session info
devtools::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 4.0.2 (2020-06-22)
#>  os       macOS Mojave 10.14.3        
#>  system   x86_64, darwin17.0          
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_AU.UTF-8                 
#>  ctype    en_AU.UTF-8                 
#>  tz       Australia/Melbourne         
#>  date     2020-07-26                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package              * version    date       lib
#>  assertthat             0.2.1      2019-03-21 [1]
#>  backports              1.1.8      2020-06-17 [1]
#>  callr                  3.4.3      2020-03-28 [1]
#>  cli                    2.0.2      2020-02-28 [1]
#>  colorspace             1.4-1      2019-03-18 [1]
#>  crayon                 1.3.4      2017-09-16 [1]
#>  curl                   4.3        2019-12-02 [1]
#>  desc                   1.2.0      2018-05-01 [1]
#>  devtools               2.3.0      2020-04-10 [1]
#>  digest                 0.6.25     2020-02-23 [1]
#>  dplyr                * 1.0.0      2020-05-29 [1]
#>  ellipsis               0.3.1      2020-05-15 [1]
#>  evaluate               0.14       2019-05-28 [1]
#>  fansi                  0.4.1      2020-01-08 [1]
#>  farver                 2.0.3      2020-01-16 [1]
#>  fs                     1.4.1      2020-04-04 [1]
#>  generics               0.0.2      2018-11-29 [1]
#>  ggplot2              * 3.3.2      2020-06-19 [1]
#>  glue                   1.4.1      2020-05-13 [1]
#>  gtable                 0.3.0      2019-03-25 [1]
#>  highr                  0.8        2019-03-20 [1]
#>  htmltools              0.5.0      2020-06-16 [1]
#>  httr                   1.4.2      2020-07-20 [1]
#>  jsonlite               1.7.0      2020-06-25 [1]
#>  knitr                  1.29       2020-06-23 [1]
#>  labeling               0.3        2014-08-23 [1]
#>  lattice                0.20-41    2020-04-02 [1]
#>  lifecycle              0.2.0      2020-03-06 [1]
#>  lubridate            * 1.7.9      2020-06-08 [1]
#>  magrittr               1.5        2014-11-22 [1]
#>  memoise                1.1.0      2017-04-21 [1]
#>  mime                   0.9        2020-02-04 [1]
#>  munsell                0.5.0      2018-06-12 [1]
#>  PerformanceAnalytics * 2.0.4      2020-02-06 [1]
#>  pillar                 1.4.6      2020-07-10 [1]
#>  pkgbuild               1.1.0      2020-07-13 [1]
#>  pkgconfig              2.0.3      2019-09-22 [1]
#>  pkgload                1.1.0      2020-05-29 [1]
#>  prettyunits            1.1.1      2020-01-24 [1]
#>  processx               3.4.3      2020-07-05 [1]
#>  ps                     1.3.3      2020-05-08 [1]
#>  purrr                  0.3.4      2020-04-17 [1]
#>  quadprog               1.5-8      2019-11-20 [1]
#>  Quandl                 2.10.0     2019-06-12 [1]
#>  quantmod             * 0.4.17     2020-03-31 [1]
#>  R6                     2.4.1      2019-11-12 [1]
#>  Rcpp                   1.0.5      2020-07-06 [1]
#>  remotes                2.1.1      2020-02-15 [1]
#>  rlang                  0.4.7      2020-07-09 [1]
#>  rmarkdown              2.3        2020-06-18 [1]
#>  rprojroot              1.3-2      2018-01-03 [1]
#>  rstudioapi             0.11       2020-02-07 [1]
#>  scales                 1.1.1      2020-05-11 [1]
#>  sessioninfo            1.1.1      2018-11-05 [1]
#>  stringi                1.4.6      2020-02-17 [1]
#>  stringr                1.4.0      2019-02-10 [1]
#>  testthat               2.3.2      2020-03-02 [1]
#>  tibble                 3.0.3      2020-07-10 [1]
#>  tidyquant            * 1.0.1.9000 2020-07-26 [1]
#>  tidyselect             1.1.0      2020-05-11 [1]
#>  TTR                  * 0.23-6     2019-12-15 [1]
#>  usethis                1.6.1      2020-04-29 [1]
#>  vctrs                  0.3.2      2020-07-15 [1]
#>  withr                  2.2.0      2020-04-20 [1]
#>  xfun                   0.15       2020-06-21 [1]
#>  xml2                   1.3.2      2020-04-23 [1]
#>  xts                  * 0.12-0     2020-01-19 [1]
#>  yaml                   2.2.1      2020-02-01 [1]
#>  zoo                  * 1.8-8      2020-05-02 [1]
#>  source                                     
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.2)                             
#>  CRAN (R 4.0.2)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.2)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.2)                             
#>  CRAN (R 4.0.2)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.2)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.2)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.2)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.2)                             
#>  Github (business-science/tidyquant@017b7d9)
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.2)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.1)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#>  CRAN (R 4.0.0)                             
#> 
#> [1] /Library/Frameworks/R.framework/Versions/4.0/Resources/library

johnbaums avatar Jul 26 '20 10:07 johnbaums

Same issue here, +1

dishutin avatar Nov 10 '21 16:11 dishutin

Ditto. Intraday data (5-min OHLCV) renders as barchart, daily frequency and lower renders as candlesticks. tidyquant v1.0.03

br00t999 avatar Jan 08 '22 16:01 br00t999

I found it may be the scaling problem on the candlestick vs. time, when I modify the time to time series and divided by 3000, the upper and lower shadow can be shown.

tidyquant::FANG %>% 
  filter(symbol=='FB') %>% 
  mutate(date=round(with_tz(Sys.time(), 'UTC'), units='hours') - 
           hours(seq_len(nrow(.))),
                date=as.POSIXct(date)) %>% 
  filter(date > Sys.time() - days(3)) %>% 
  ggplot(aes(x = as.ts(date)/3000, y = close, group = symbol)) +
  geom_candlestick(aes(open = open, high = high, low = low, close = close))

image

stephen5127 avatar Apr 24 '22 07:04 stephen5127