2
votes

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)
1
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
@RHertel chart_Series() is still giving the same error. - ehbeehefak
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
@RHertel Tried getSymbols(XXXZZZ, src="csv", dir="XXXZZZ.csv") but the reading took so long compared to other methods, I gave up eventually. - ehbeehefak
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

1 Answers

1
votes

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)