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

Costas Vorlow costas.vorlow at gmail.com
Thu Sep 29 08:54:44 CEST 2011


Dear Peter,

Still no hope. I am doing something wrong. I noticed that the times
are also going through. Maybe that's the problem?
I tried to include them to the area-event-dates list in the function
call but still no shading....

> tail(newdata,50)
                    SP500  Close
2011-08-22 08:30:00      1123.55
2011-08-22 15:00:00      1123.82
2011-08-23 08:30:00      1124.36
2011-08-23 15:00:00      1162.35
2011-08-24 08:30:00      1162.16
2011-08-24 15:00:00      1177.60
2011-08-25 08:30:00      1176.69
2011-08-25 15:00:00      1159.27
2011-08-26 08:30:00      1158.85
2011-08-26 15:00:00      1176.80
2011-08-29 08:30:00      1177.91
2011-08-29 15:00:00      1210.08
2011-08-30 08:30:00      1209.76
2011-08-30 15:00:00      1212.92
2011-08-31 08:30:00      1213.00
2011-08-31 15:00:00      1218.89
2011-09-01 08:30:00      1219.12
2011-09-01 15:00:00      1204.42
2011-09-02 08:30:00      1203.90
2011-09-02 15:00:00      1173.97
2011-09-06 08:30:00      1173.97
2011-09-06 15:00:00      1165.24
2011-09-07 08:30:00      1165.85
2011-09-07 15:00:00      1198.62
2011-09-08 08:30:00      1197.98
2011-09-08 15:00:00      1185.90
2011-09-09 08:30:00      1185.37
2011-09-09 15:00:00      1154.23
2011-09-12 08:30:00      1153.50
2011-09-12 15:00:00      1162.27
2011-09-13 08:30:00      1162.59
2011-09-13 15:00:00      1172.87
2011-09-14 08:30:00      1173.32
2011-09-14 15:00:00      1188.68
2011-09-15 08:30:00      1189.44
2011-09-15 15:00:00      1209.11
2011-09-16 08:30:00      1209.21
2011-09-16 15:00:00      1216.01
2011-09-19 08:30:00      1214.99
2011-09-19 15:00:00      1204.09
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
2011-09-23 08:30:00      1128.82
2011-09-23 15:00:00      1136.43
2011-09-26 08:30:00      1136.91
2011-09-26 15:00:00      1162.95

On 28 September 2011 14:08, Peter Carl <peter at braverock.com> wrote:
> Linux, although I don't think that should have a bearing on this issue.
> Windows issues are more common around timezones.  That said, I'm not a
> Windows expert...
>
> Did you also try a lower time granularity, such as:
> chart.TimeSeries(newdata, date.format="%d-%m-%y", period.areas =
> list(c("2011-01", "2011-05")), period.color = "blue", lwd = 2)
>
> I'm using PerformanceAnalytcs_1.0.3.3.  This behavior did change in a
> recent CRAN release, so it might also be a version issue.  Check with:
> sessionInfo()
>
> HTH,
>
> pcc
> --
> Peter Carl
> http://www.braverock.com/~peter
>
>> Thanks but I am not getting anything. Are you using Linux or Windows
>> platforms?
>>
>> On 27 September 2011 19:54, Peter Carl <peter at braverock.com> wrote:
>>> 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.
>>>>
>>>
>>>
>>>
>>
>>
>>
>> --
>>
>> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
>> |c|o|s|t|a|s|@|v|o|r|l|o|w|.|o|r|g|
>> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
>>
>
>
>



-- 

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|c|o|s|t|a|s|@|v|o|r|l|o|w|.|o|r|g|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+



More information about the R-SIG-Finance mailing list