[R-SIG-Finance] Extracting OHLC from trade price series
chalabi at phys.ethz.ch
chalabi at phys.ethz.ch
Fri Feb 29 14:13:04 CET 2008
"Jeff Ryan" <jeff.a.ryan at gmail.com> writes:
> 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.
I know there was no offence :). I was really interested to understand the
why of your comment.
>
> 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_.
Although @positions in timeSeries is a character, timeDate is
used to generate these positions. Otherwise it would not be possible to
to handle situation where series are recorded at different financial
centers with different time zones.
> 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.
we made few changes to timeSeries to make it more compatible with other
functions. And if it does not work, you can write a new method for the
function you want to use or convert the timeSeries to a matrix (see
as.matrix.timeSeries). So, I do not get the point "in reality the S4
object just doesn't work...".
>
> 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
In timeSeries, @positions are defined as
@positions <- c(as.character(timeDate))
This also returns the attribute FinCenter to @positions.
As you mentioned this attribute is lost when we subbsets the positions.
In fact there is no reason to have this attribute because the financial
center is already stored in @FinCenter.
To avoid this issue, I changed the following in dev-version of timeSeries
@positions <- c(as.character(timeDate))
this should fix your issue. Thanks to point this out!
>
>> 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?
>
We are extending the documentation of fCalendar package with an overview
of timeDate. It should not be too long until we release the new documentation.
thanks for your feedback!
Yohan
> 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
--
Yohan Chalabi
More information about the Rmetrics-core
mailing list