[R-SIG-Finance] Extracting OHLC from trade price series

Jeff Ryan jeff.a.ryan at gmail.com
Wed Feb 20 16:43:33 CET 2008


Hi Yohan,

Not offense meant - sorry if that came across as such.  Rmetrics is
the gold standard when it comes to breadth and overall usefulness.
Always appreciative of you and your team's efforts.

The purpose of my comment was with respect to using timeDate as an
indexing class.  I'll clarify to the list with a more clear response,
but I think my reasoning is that even the primary timeSeries class
doesn't utilize timeDate for its @positions/charvec.  That coupled
with what seemed like a situation that could be handled with something
simpler in POSIXct was the _why_.

That said, timeDate as a tool seems great.  I have yet to fully grasp
its power (or place in a workflow), but I none-the-less interested in
it.  If you look at 'xts' you can see that one can easily allow an
'xts' object to use 'timeDate' for indexing.  [ indexClass(x) <-
'timeDate' ]

I have been meaning to email you about a few things with respect to my
'xts' package and 'timeSeries'/'timeDate':

A quick primer on 'xts':

Basically enforces a time-based
(Date,POSIXct,chron,timeDate,yearmon,yearqtr) index in a modified
'zoo' class, while allowing for arbitrary attributes to be appended
(like time zone, data source, comments...).  The latter is only unique
in that these attributes are displayed differently by using xts as
opposed to zoo.  In zoo and other classes, they just clutter up the
screen.  I like the additional features of timeSeries quite a bit -
but in reality the S4 object just doesn't work in many functions that
aren't built for it.

The main usefulness for developers is in the conversion scheme.
'as.xts' allows for lossless conversion of one object into 'xts', and
'reclass()' reverses the procedure.  Basically the parts of the
original object that need to be remembered for re-conversion are
assigned to xtsAttributes.

Inside functions it is possible to simply 'as.xts' the time-series
input (can be of any class: timeSeries, ts, zoo, its, irts, even
properly formed (rownames in suitable date-convertible format) matrix
and data.frames) and carry out all your internal tasks only having to
worry about interacting with a zoo-like (matrix) object.  No more
methods/conditionals or worse still, broken code.  It makes writing
and maintaining code much faster and easier.

Examples from xts include 'to.period' and 'periodicity', and in
quantmod 'chartSeries' currently converts objects to xts before doing
anything.



Ok - so I gave you some idea what I have been working on.  The issue I
have at present is converting a as.xts(timeSeries) object back to
timeSeries from 'xts'.  An example follows. But the basic issue is
that the @positions slot loses information when subset.

> data(sample_matrix)
> sample.timeSeries <- timeSeries(sample_matrix,charvec=as.Date(rownames(sample_matrix)))
> str(sample.timeSeries)
Formal class 'timeSeries' [package "fSeries"] with 8 slots
  ..@ Data         : num [1:180, 1:4] 50.0 50.2 50.4 50.4 50.2 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : atomic [1:180] 2007-01-02 2007-01-03 2007-01-04 2007-01-05 ...
  .. .. .. ..- attr(*, "control")= Named chr "GMT"
  .. .. .. .. ..- attr(*, "names")= chr "FinCenter"
  .. .. ..$ : chr [1:4] "Open" "High" "Low" "Close"
  ..@ positions    : atomic [1:180] 2007-01-02 2007-01-03 2007-01-04
2007-01-05 ...
  .. ..- attr(*, "control")= Named chr "GMT"
  .. .. ..- attr(*, "names")= chr "FinCenter"
  ..@ format       : chr "%Y-%m-%d"
  ..@ FinCenter    : chr "GMT"
  ..@ units        : chr [1:4] "Open" "High" "Low" "Close"
  ..@ recordIDs    :'data.frame':       0 obs. of  0 variables
  ..@ title        : chr "Time Series Object"
  ..@ documentation: chr "Wed Feb 20 15:20:47 2008"
> str(sample.timeSeries[1:10,1:2])
Formal class 'timeSeries' [package "fSeries"] with 8 slots
  ..@ Data         : num [1:10, 1:2] 50.0 50.2 50.4 50.4 50.2 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:10] "2007-01-02" "2007-01-03" "2007-01-04" "2007-01-05" ...
  .. .. ..$ : chr [1:2] "Open" "High"
  ..@ positions    : chr [1:10] "2007-01-02" "2007-01-03" "2007-01-04"
"2007-01-05" ...
  ..@ format       : chr "%Y-%m-%d"
  ..@ FinCenter    : chr "GMT"
  ..@ units        : chr [1:2] "Open" "High"
  ..@ recordIDs    :'data.frame':       0 obs. of  0 variables
  ..@ title        : chr "Time Series Object"
  ..@ documentation: chr "Wed Feb 20 15:20:47 2008"
>

I can only imagine that if the original had value - it can't be good
that it gets lost in the process of subsetting - as everything just
becomes a chr vector.

My issue happens in the conversion:

First what an 'as.xts(timeSeries)' looks like:

> str(as.xts(sample.timeSeries[1:10,1:2]))
An 'xts' object from 2007-01-02 to 2007-01-11 containing:
  Data: num [1:10, 1:2] 50.0 50.2 50.4 50.4 50.2 ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:10] "2007-01-02" "2007-01-03" "2007-01-04" "2007-01-05" ...
  ..$ : chr [1:2] "Open" "High"
  Indexed by:  POSIXct[1:10], format: "2007-01-02" "2007-01-03"
"2007-01-04" "2007-01-05" ...
  Original class: 'timeSeries'
  xts Attributes:
List of 5
 $ format       : chr "%Y-%m-%d"
 $ FinCenter    : chr "GMT"
 $ recordIDs    :'data.frame':  0 obs. of  0 variables
 $ title        : chr "Time Series Object"
 $ documentation: chr "Wed Feb 20 15:20:47 2008"
>


# Next we are taking timeSeries, converting it to 'xts', then putting
it back as a timeSeries
> str(reclass(as.xts(sample.timeSeries)))
Formal class 'timeSeries' [package "fSeries"] with 8 slots
  ..@ Data         : num [1:180, 1:4] 50.0 50.2 50.4 50.4 50.2 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : atomic [1:180] 2007-01-02 2007-01-03 2007-01-04 2007-01-05 ...
  .. .. .. ..- attr(*, "control")= Named chr "GMT"
  .. .. .. .. ..- attr(*, "names")= chr "FinCenter"
  .. .. ..$ : chr [1:4] "Open" "High" "Low" "Close"
  ..@ positions    : atomic [1:180] 2007-01-02 2007-01-03 2007-01-04
2007-01-05 ...
  .. ..- attr(*, "control")= Named chr "GMT"
  .. .. ..- attr(*, "names")= chr "FinCenter"
  ..@ format       : chr "%Y-%m-%d"
  ..@ FinCenter    : chr "GMT"
  ..@ units        : chr [1:4] "Open" "High" "Low" "Close"
  ..@ recordIDs    :'data.frame':       0 obs. of  0 variables
  ..@ title        : chr "Time Series Object"
  ..@ documentation: chr "Wed Feb 20 15:20:47 2008"

# Now the same thing - only first subsetting the timeSeries object.
The point to notice is that your 'dimnames' and '@positions' from the
non-xts example above change form after subsetting.  The xts reclass
process simply rebuilds, so everything is still the way it should be
(only subset).
> str(reclass(as.xts(sample.timeSeries[1:10,1:2])))
Formal class 'timeSeries' [package "fSeries"] with 8 slots
  ..@ Data         : num [1:10, 1:2] 50.0 50.2 50.4 50.4 50.2 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : atomic [1:10] 2007-01-02 2007-01-03 2007-01-04 2007-01-05 ...
  .. .. .. ..- attr(*, "control")= Named chr "GMT"
  .. .. .. .. ..- attr(*, "names")= chr "FinCenter"
  .. .. ..$ : chr [1:2] "Open" "High"
  ..@ positions    : atomic [1:10] 2007-01-02 2007-01-03 2007-01-04
2007-01-05 ...
  .. ..- attr(*, "control")= Named chr "GMT"
  .. .. ..- attr(*, "names")= chr "FinCenter"
  ..@ format       : chr "%Y-%m-%d"
  ..@ FinCenter    : chr "GMT"
  ..@ units        : chr [1:2] "Open" "High"
  ..@ recordIDs    :'data.frame':       0 obs. of  0 variables
  ..@ title        : chr "Time Series Object"
  ..@ documentation: chr "Wed Feb 20 15:20:47 2008"
>
If you notice, the dimnames and positions haven't changed form.

My issue - and it may only be my concern :) - is that

> identical(sample.timeSeries[1:10,1:2], reclass(as.xts(sample.timeSeries[1:10,1:2])))

fails.  I have a hard time reconciling the idea of making it pass by
_removing_ information, only because timeSeries does.

I am not too sure if this makes sense.  If not please tell me so.

On a slightly different note I would like to learn more about the
timeDate class, and how maybe _it_ should be the default indexing
class (currently Date or POSIXct has that role) in an 'xts' object.
Are there methods that might be of use?

Thanks for your reply - and hopefully this email didn't take you too
long to read :)

Jeff Ryan

On Feb 20, 2008 5:56 AM, Yohan Chalabi <chalabi at phys.ethz.ch> wrote:
> >>>> "JR" == "Jeff Ryan" <jeff.a.ryan at gmail.com>
> >>>> on Tue, 19 Feb 2008 18:39:27 -0600
>
>    JR> You are probably better off _not_
>    JR> using timeDate - but I wrote xts so I wouldn't have to - to
>    JR> each his
>    JR> own.
>
> Hi Jeff,
>
> I am currently working on the different Rmetrics packages I found
> your advice to not use fCalendar interesting.
> Could you please give me the reasons why you would not recommend it.
> It will help us to improve the package.
>
> regards,
> Yohan
>



-- 
There's a way to do it better - find it.
Thomas A. Edison



More information about the Rmetrics-core mailing list