Search code examples
rxtsquantmod

Convert tick data to OHLC 4HR bars


I've imported tick data from .csv and can't seem to figure out how to convert to OHLC data. I'm looking to convert to 1 or 4HR OHLC bars.

Please help!

library(TFX)

library(quantmod)

library(zoo)

library(xts)

head(data)

                    Name      Close  

2014-05-01 00:00:00 "AUD/JPY" "94.874"

2014-05-01 00:00:00 "AUD/JPY" "94.876"

2014-05-01 00:00:00 "AUD/JPY" "94.876"

2014-05-01 00:00:00 "AUD/JPY" "94.877"

2014-05-01 00:00:00 "AUD/JPY" "94.877"

2014-05-01 00:00:00 "AUD/JPY" "94.877"

is.OHLC(data)

## [1] FALSE

periodicity(data)

## 0.161999940872192 seconds periodicity from 2014-05-01 00:00:00 to 2014-05-30 20:59:58

to.weekly(data)

## Error in to.period(x, "weeks", name = name, ...) : unsupported type

to.period(data,"seconds",5)

## Error in to.period(data, "seconds", 5) : unsupported type

bars <- period.apply(data,

+                     endpoints(data,"secs",60),

+                     function(xx){

+                       ticks=coredata(data$close)

+                       c( first(ticks),max(ticks), min(ticks),

+                          last(ticks) )

+                     })

## There were 50 or more warnings (use warnings() to see the first 50)

head(bars)

                    Name Close

2014-05-01 00:00:57 -Inf   Inf

2014-05-01 00:01:58 -Inf   Inf

2014-05-01 00:02:59 -Inf   Inf

2014-05-01 00:03:56 -Inf   Inf

2014-05-01 00:04:54 -Inf   Inf

2014-05-01 00:05:50 -Inf   Inf

to.period(data,"seconds")

## Error in to.period(data, "seconds") : unsupported type

xx<-to.minutes(data[,1],5,'minutes')

## Error in to.period(x, "minutes", k = k, name = name, ...) : unsupported type

head(data)

                    Name      Close  

2014-05-01 00:00:00 "AUD/JPY" "94.874"

2014-05-01 00:00:00 "AUD/JPY" "94.876"

2014-05-01 00:00:00 "AUD/JPY" "94.876"

2014-05-01 00:00:00 "AUD/JPY" "94.877"

2014-05-01 00:00:00 "AUD/JPY" "94.877"

2014-05-01 00:00:00 "AUD/JPY" "94.877"

Solution

  • Your problem is that your Close data is stored as character type -- don't you notice the quotes around the numbers printed from your xts data object? Most likely because you've included the name column, and when xts finds "coredata" containing character vectors it converts everything, including numeric vectors to character types. character type xts objects have their uses. For example, the orderbook object in the package quantstrat is a "Character" xts object. However, what you want to do is convert numeric data to a lower frequency. You need a 'numeric only' xts object to do that. So drop the name column in your data object (and convert the Close to numeric type).

    With xts loaded, read the documentation carefully for ?to.period

    to.weekly, to.daily etc are all essentially wrappers to to.period

    Here's a contrived example to get you started:

    library(xts)
    #create contrived 5 min xts object
    getSymbols("AAPL")
    n <- NROW(AAPL)
    time <- seq(as.POSIXct("2014-06-28"), as.POSIXct("2014-07-10"), by = "5 mins")
    
    #' Here is the 5 min xts data object created.  
    xtsdata <- xts(x = coredata(AAPL), order.by = time[1:n])
    
    #' Want to convert it to hourly? 
    
    xtsdata_hourly <- to.period(x = xtsdata, period = "hours", indexAt = 'startof')
    
    #' Want to convert it to 4 hour bars? k is the number of bars to aggregate over 
    xtsdata_4hours <- to.period(x = xtsdata, period = "hours", k = 4, indexAt = 'startof')