[R-SIG-Finance] Open to Close to Open data transformation (quantmod, xts and possibly zoo) + chart.TimeSeries

Peter Carl peter at braverock.com
Tue Sep 27 18:54:50 CEST 2011


You may have already figured this out, but the dates have to appear in the
timeseries at the same granularity you are looking to match.

> chart.TimeSeries(newdata, date.format="%d-%m-%y", period.areas =
list(c("03-01-2011","05-06-2011")), period.color = "blue", lwd = 2)

should be...

> chart.TimeSeries(newdata, date.format="%d-%m-%y", period.areas =
list(c("2011-01-03","2011-06-06")), period.color = "blue", lwd = 2)

... where the list of areas is specified in 8601 format.  Also note that
the fifth of June does not appear in your data series.  If you are looking
to match at a higher granularity, then period.areas = list(c("2011-01",
"2011-05")) would also work.

HTH,

pcc
-- 
Peter Carl
http://www.braverock.com/~peter

> Thanks again.
>
> One more point:
>
> When I try
>
> chart.TimeSeries(newdata, date.format="%d-%m-%y", period.areas =
> list(c("03-01-2011","05-06-2011")), period.color = "blue", lwd = 2)
>
> I can't get the event period shadings.
>
> This has to do I guess with the fact that 2 prices have the same data
> though different hourly time signature.
>
> Is this correct and how do I rectify it?
>
> I've been trying around with formats but no result so far.
>
> On 23 September 2011 14:56, G See <gsee000 at gmail.com> wrote:
>> FWIW, I misread the docs for getSymbols.yahoo which says
>>      In the case of xts objects, the indexing will be by Date. This can
>>      be altered with the ‘index.class’ argument.  See ‘indexClass’ for
>>      more information on changing index classes.
>> So, this should be perfectly acceptable as well:
>> medata <- getSymbols("^GSPC",
>>         from=Sys.Date()-400,
>>         index.class='POSIXct',
>>         auto.assign=FALSE)
>> opens <- Op(medata)
>> closes <- Cl(medata)
>> index(opens) <- index(opens) + (60*60*8) + (60*30)
>> index(closes) <- index(closes) + (60*60*15)
>> newdata <- rbind(opens,closes)
>> colnames(newdata) <- "GSPC.OC"
>>> tail(newdata)
>>                     GSPC.OC
>> 2011-09-20 08:30:00 1204.50
>> 2011-09-20 15:00:00 1202.09
>> 2011-09-21 08:30:00 1203.63
>> 2011-09-21 15:00:00 1166.76
>> 2011-09-22 08:30:00 1164.55
>> 2011-09-22 15:00:00 1129.56
>>
>> On Fri, Sep 23, 2011 at 8:25 AM, G See <gsee000 at gmail.com> wrote:
>>>
>>> Hi Costas,
>>> You need an indexClass that can handle times.  By default
>>> getSymbols.yahoo
>>> gives you an object with 'Date' indexClass which you need to change to
>>> something like 'POSIXct' or 'POSIXlt'
>>> (You *can* do this directly in your getSymbols call using the argument
>>> index.class='POSIXct', but the documentation says only "Date" is
>>> supported,
>>> so I'll do it the other way)
>>> require(quantmod)
>>> getSymbols("^GSPC",from='1990-01-01')
>>> medata <- tail(GSPC, 400)
>>> indexClass(medata) <- "POSIXct"
>>> opens <- Op(medata)
>>> closes <- Cl(medata)
>>> index(opens) <- index(opens) + (60*60*8) + (60*30)
>>> index(closes) <- index(closes) + (60*60*15)
>>> newdata <- rbind(opens,closes)
>>> colnames(newdata) <- "GSPC.OC"
>>> > tail(newdata)
>>>                     GSPC.OC
>>> 2011-09-20 08:30:00 1204.50
>>> 2011-09-20 15:00:00 1202.09
>>> 2011-09-21 08:30:00 1203.63
>>> 2011-09-21 15:00:00 1166.76
>>> 2011-09-22 08:30:00 1164.55
>>> 2011-09-22 15:00:00 1129.56
>>> Regards,
>>> Garrett
>>> On Fri, Sep 23, 2011 at 4:50 AM, Costas Vorlow
>>> <costas.vorlow at gmail.com>
>>> wrote:
>>>>
>>>> Hello,
>>>>
>>>> I want to put in order (preferably as a zoo or xts object with a
>>>> suitable
>>>> timestamp)
>>>> the open anc closing prices of an (say) index downloaded from YAHOO
>>>> finance:
>>>>
>>>> require(quantmod)
>>>> getSymbols('^GSPC',from='1990-01-01')
>>>>
>>>> medata<- tail((GSPC),400)
>>>> opens<-Op(medata)
>>>> closes<-Cl(medata)
>>>>
>>>> i.e., I want  asingle sequence of  1095.89,    1105.24 , 1101.24 ,
>>>> 1102.94, and so on ... preferably with a timestamp (morning - evening
>>>> of
>>>> same day as for daily prices zoo does not like the same date in two
>>>> consecutive prices...
>>>>
>>>> > head(merge(opens,closes))
>>>>           GSPC.Open GSPC.Close
>>>> 2010-02-24   1095.89    1105.24
>>>> 2010-02-25   1101.24    1102.94
>>>> 2010-02-26   1103.10    1104.49
>>>> 2010-03-01   1105.36    1115.71
>>>> 2010-03-02   1117.01    1118.31
>>>> 2010-03-03   1119.36    1118.79
>>>> >
>>>> >
>>>> Probably I could use the OpCl in quantmod and caclulate the Cl price
>>>> from
>>>> Op, though Is there any easy way using some implicit zoo/xts function
>>>> for
>>>> this? I have problems in puting the timestamps in the xts vobject.
>>>>
>>>> Thanks,
>>>> Costas
>>>>
>>>> _______________________________________________
>>>> R-SIG-Finance at r-project.org mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>>>> -- Subscriber-posting only. If you want to post, subscribe first.
>>>> -- Also note that this is not the r-help list where general R
>>>> questions
>>>> should go.
>>>
>>
>>
>
>
>
> --
>
> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
> |c|o|s|t|a|s|@|v|o|r|l|o|w|.|o|r|g|
> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
>
> _______________________________________________
> R-SIG-Finance at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions
> should go.
>



More information about the R-SIG-Finance mailing list