Skip to content

Don't success to create xts from lines in code

5 messages · pierre8r-list at yahoo.fr, Joshua Ulrich

#
Hi,

Don't success to create xts from lines in code



Here my R code :
---------------

library(quantmod)
library(xts)

Lines <-
"01/08/2007 00:59:00,1.930250,1.930350,1.929300,1.929600,-60
01/08/2007 01:59:00,1.929600,1.930100,1.926250,1.928050,-60
01/08/2007 02:59:00,1.928050,1.929700,1.927700,1.927800,-60
01/08/2007 03:59:00,1.927800,1.928350,1.927200,1.928250,-60
01/08/2007 04:59:00,1.928250,1.929600,1.928250,1.929000,-60
01/08/2007 05:59:00,1.929000,1.929250,1.928650,1.929250,-60
01/08/2007 06:59:00,1.929250,1.930850,1.928700,1.930650,-60
01/08/2007 07:59:00,1.930650,1.933100,1.930250,1.933000,-60
01/08/2007 08:59:00,1.933000,1.934350,1.931050,1.931100,-60
01/08/2007 09:59:00,1.931100,1.934750,1.930350,1.934000,-60
01/08/2007 10:59:00,1.934000,1.934650,1.932300,1.932400,-60
01/08/2007 11:59:00,1.932400,1.933500,1.931200,1.933050,-60
01/08/2007 12:59:00,1.933050,1.933850,1.932100,1.933600,-60"

quotes <- read.zoo(textConnection(Lines))

x <- xts(as.matrix(quotes[,-1]),as.POSIXct(paste(quotes[,1]),
format='%m/%d/%Y %H:%M:%S'))

colnames(x) <- c('Open','High','Low','Close','Volume')

x


The message error :
-------------------
Warning message:
In zoo(rval, ix) :
  some methods for ?zoo? objects do not work if the index entries in ?order.by? are not unique
+ format='%m/%d/%Y %H:%M:%S'))
Warning messages:
1: In zoo(rval[i], x.index[i]) :
  some methods for ?zoo? objects do not work if the index entries in ?order.by? are not unique
2: In zoo(x = x, order.by = order.by, frequency = frequency) :
  some methods for ?zoo? objects do not work if the index entries in ?order.by? are not unique
3: In zoo(rval[i], x.index[i]) :
  some methods for ?zoo? objects do not work if the index entries in ?order.by? are not unique







      ____________________________________________________________
ente http://mail.yahoo.fr
#
See ?read.zoo

1) You need to specify the correct delimiter for your file.
2) read.zoo is converting your index to Date (the default), use
POSIXct instead and be sure to specify the correct format of the index
in your file.

quotes <- read.zoo(textConnection(Lines), sep=',',
FUN=as.POSIXct, format='%m/%d/%Y %H:%M:%S')
x <- as.xts(quotes)
colnames(x) <- c('Open','High','Low','Close','Volume')

--
http://quantemplation.blogspot.com
On Thu, Jul 24, 2008 at 11:54 AM, <pierre8r-list at yahoo.fr> wrote:
#
Hi Josh,

Thanks, it works.

If it don't boring you, I want to read a other data format.
There is a comma between Date and Time. 

2008.07.14,04:00,1.5894,1.5916,1.5893,1.5914,276
2008.07.14,05:00,1.5912,1.5912,1.5902,1.5904,205
2008.07.14,06:00,1.5903,1.5911,1.5898,1.5899,239
2008.07.14,07:00,1.5898,1.5902,1.5865,1.5882,541
2008.07.14,08:00,1.5883,1.5910,1.5868,1.5870,689
2008.07.14,09:00,1.5869,1.5890,1.5860,1.5877,763
2008.07.14,10:00,1.5878,1.5881,1.5841,1.5871,835
2008.07.14,11:00,1.5870,1.5876,1.5854,1.5859,533

Thanks,

Pierre8r
____________________________________________________________
ente http://mail.yahoo.fr
#
I would read it as a data.frame, since the index has a comma, then
convert to xts.

quotes <- read.csv(textConnection(lines), header=FALSE)
x <- as.xts(quotes[,-(1:2)],
as.POSIXct(paste(quotes[,1],quotes[,2]),format='%Y.%m.%d %H:%M'))
colnames(x) <- c('Open','High','Low','Close','Volume')

--
http://quantemplation.blogspot.com
On Thu, Jul 24, 2008 at 1:59 PM, <pierre8r-list at yahoo.fr> wrote:
#
Thanks Josh. 
I just changed lines to Lines and it works.

This data format is the data format for historical quotations from MetaTrader 4 :
http://www.metaquotes.net/

Anybody can download MT4 client and open a account with a e-mail for free.
Then download historical Forex datas for free.

In french :
Menu / Outils / Archives

In english something near that :
Menu / Tools / Historical data

Maybe this R code could be added to show how to import for free Forex quotations into R.

Regards,

Pierre8r



      ____________________________________________________________
ente http://mail.yahoo.fr