Question

Asked this question question recently and was advised to use the 'to.minutes5()' function. However, upon doing so I get the following error:

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

My original text file has the date and time as to separate columns:

GBPUSD<- read.table("GBPUSD.txt", sep=',')

head(GBPUSD)

  V1           V2     V3     V4     V5     V6      V7    V8               
1 <TICKER> <DTYYYYMMDD> <TIME> <OPEN> <HIGH>  <LOW> <CLOSE> <VOL> 
2   GBPUSD     20010102 230100 1.5021 1.5021 1.5018  1.5018     4   
3   GBPUSD     20010102 230200 1.5019 1.5019 1.5019  1.5019     4     
4   GBPUSD     20010102 230300 1.5018 1.5019 1.5018  1.5019     4     
5   GBPUSD     20010102 230400 1.5019 1.5019 1.5019  1.5019     4     
6   GBPUSD     20010102 230500 1.5017 1.5018 1.5017  1.5018     4   

So the first step I undertook was to blend them into one column using the paste() function in a new 'TIME' column:

GBPUSD$Time <-paste(GBPUSD[,2],GBPUSD[,3],sep=",") 

After which I created a POSIXct time stamp using the new merged 'TIME' (9th column):

time<- as.POSIXct(GBPUSD[,9], format="%Y%m%d,%H%M%S")

Then I got around to creating an xts time series using the closing prices and time:

gbp<- xts(GBPUSD[7], time)

However, when I call to.minutes5(gbp), I get an error.

How do I convert the one minute closing prices in my text file to 5 minute prices without running a loop?

Additionally, how would I go about converting 1min OHLC to 5min OHLC?

UPDATE:

When I try the advice put forth by khoxsey, I get an error on the following line:

raw_cable <- read.table(header=FALSE, text=data, col.names=cn, colClasses=cl)
"Error in textConnection(text) : invalid 'text' argument"

I presume this happens because I first imported a tabular data frame and deleted the first line:

data<- read.table("GBPUSD.txt", sep=',')
data <- data[-1,] #deleted the initial header to add at a later step, as suggested
Was it helpful?

Solution

You're not quite providing all of your code, so it's hard to tell exactly where you're going wrong, but you're very close. I copied some of your data to get a few more observations and used your formatting to get the time string. (BTW, time is a function in R so you might call your variable something else).

The first chunk of code is optional, simply some setup to get a data frame containing representative data, and call your setup to as.POSIXct to create our index:

forex_data <- "GBPUSD 20010102 230000 1.5021 1.5021 1.5018 1.5018 4
GBPUSD 20010102 230100 1.5021 1.5021 1.5018 1.5018 4
GBPUSD 20010102 230200 1.5019 1.5019 1.5019 1.5019 4
GBPUSD 20010102 230300 1.5018 1.5019 1.5018 1.5019 4
GBPUSD 20010102 230400 1.5019 1.5019 1.5019 1.5019 4
GBPUSD 20010102 230500 1.5017 1.5018 1.5017 1.5018 4
GBPUSD 20010102 230600 1.5019 1.5019 1.5019 1.5019 4
GBPUSD 20010102 230700 1.5018 1.5019 1.5018 1.5019 4
GBPUSD 20010102 230800 1.5019 1.5019 1.5019 1.5019 4
GBPUSD 20010102 230900 1.5017 1.5018 1.5017 1.5018 4
GBPUSD 20010102 231000 1.5017 1.5018 1.5017 1.5018 4"

cn <- c('TICKER','DTYYYYMMDD','TIME','OPEN','HIGH','LOW','CLOSE','Volume')
cl <- c(rep('character', 3), rep('numeric', 5))
raw_cable <- read.table(header=FALSE, text=forex_data, col.names=cn, colClasses=cl)

dt_tm <-as.POSIXct(paste(raw_cable[,2], raw_cable[,3],sep=","), format="%Y%m%d,%H%M%S")

Now that we have a data.frame containing our data, and an appropriate index vector, we create an xts object using only the price and volume data. Here, that means the last 5 columns.

require(xts)  
cable <- xts(raw_cable[,4:8], order.by=dt_tm)

Finally, with a well-formed xts object, it is really easy to roll up to 5-minute periods. See the docs for to.period for a bunch of other possibilities. One key thing to note here is setting the indexAt parameter, which allows us to choose which ending minute to use. For fun, try out the other possibilities in the documentation.

to.minutes5(cable, indexAt='startof', name=NULL)

                      Open   High    Low  Close Volume
2001-01-02 23:00:00 1.5021 1.5021 1.5018 1.5019     20
2001-01-02 23:05:00 1.5017 1.5019 1.5017 1.5018     20
2001-01-02 23:10:00 1.5017 1.5018 1.5017 1.5018      4
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top