Search code examples
rcsvquantmodquantitative-financequantstrat

Charting OHLC data with chart_Series function


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)

Solution

  • 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)