2

The csv file with OHLC (Open-High-Low-Close) and Volume data (hourly data with the format of DD.MM.YYYY HH:mm) of a currency-pair named XXXZZZ.csv:

Date;Open;High;Low;Close;Volume
02.01.2009 07:00;1,5326;1,539785;1,52896;1,5369;1083497,742
02.01.2009 08:00;1,5375;1,5379;1,53105;1,537;1191678,162

I load the quantstrat package and initialize:

library(quantstrat)
Sys.setenv(TZ="UTC")
currency(c('XXX', 'ZZZ'))
exchange_rate('XXXZZZ', tick_size=0.0001)

I read the csv file with read.zoo (as I could not make quantmod::getSymbols work):

XXXZZZ <- as.xts(read.zoo("XXXZZZ.csv", sep=';', tz='', header=TRUE, 
                          format='%d.%m.%Y %H:%M',
                          index.column = 1 
                          )
                 )

This results in a "xts" & "zoo" object with an index column being the Date column and 5 other columns being OHLC and Volume.

chart_Series(XXXZZZ)

Results in:

Error in chart_Series(XXXZZZ) : 'x' must be a time-series object

So how can I manipulate the XXXZZZ to be a time-series object? If different, can the answer cover not only hourly data but from 1-second to monthly data as well?

Suggestion.1: Changed decimal symbol from comma to dot, the problem still persists.

XXXZZZ <- gsub(",",".",XXXZZZ)
ehbeehefak
  • 23
  • 5
  • I suspect that you have some problems because the decimals of the OHLC values are separated by a comma (frequently used in Europe) instead of a dot (US standard). I suggest that you try to change the commas with `XXXZZZ <- gsub(",", ".", XXXZZZ)` and see whether the problem persists. – RHertel Jul 31 '15 at 17:14
  • @RHertel chart_Series() is still giving the same error. – ehbeehefak Jul 31 '15 at 17:24
  • An xts object can, in principle, cover any span of time, down to microseconds and up to several years if necessary. The limits are posed by the availability of data and the size of the file (you might have some obvious problems if you want to store and evaluate tick data on the microsecond scale over a period of several years). – RHertel Jul 31 '15 at 17:24
  • @RHertel Tried `getSymbols(XXXZZZ, src="csv", dir="XXXZZZ.csv")` but the reading took so long compared to other methods, I gave up eventually. – ehbeehefak Jul 31 '15 at 17:34
  • Is the OHLC data of this security available on the internet, as it is, e.g., for EOD data of US stocks? This would allow to reproduce the problem and maybe solve it. – RHertel Jul 31 '15 at 17:36
  • @RHertel Yes, it was from Dukascopy. It was a random exotic currency pair. The site is: https://www.dukascopy.com/swiss/english/marketwatch/historical/ You have to specify 1-Hour and the interval with the format you are looking for, and you should be able to download. – ehbeehefak Jul 31 '15 at 17:39
  • @RHertel Yeah, forgot about that, apologies. I used a temporary email account for that. I appreciate an example from quantmod. Thank you very much. – ehbeehefak Jul 31 '15 at 17:51

1 Answers1

1

RHertel's comment about the decimal separator is the likely issue. It's not enough to simply gsub(x, ",", ".") because the result is still character, not numeric. You need to set dec="," in your call to read.zoo.

The code below works for me, though I had to add a couple more observations to give chart_Series something to plot.

require(quantmod)
Lines <- "Date;Open;High;Low;Close;Volume
02.01.2009 07:00;1,5326;1,539785;1,52896;1,5369;1083497,742
02.01.2009 08:00;1,5375;1,5379;1,53105;1,537;1191678,162
02.01.2009 09:00;1,5375;1,5379;1,53105;1,537;1191678,162
02.01.2009 10:00;1,5375;1,5379;1,53105;1,537;1191678,162"
conn <- textConnection(Lines)
XXXZZZ <- as.xts(read.zoo(conn, sep=';', tz="", dec=",", header=TRUE,
                   format='%d.%m.%Y %H:%M', index.column=1))
close(conn)
chart_Series(XXXZZZ)
Community
  • 1
  • 1
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • Thank you Mr. Ulrich. Just replaced `conn` with `"XXXZZZ.csv"` (my csv data file's name) in `read.zoo()`, and it worked. Nothing else was needed other than the `as.xts(read.zoo())` line and the `chart_Series()` line. – ehbeehefak Jul 31 '15 at 18:22