[R-SIG-Finance] quantstrat - model transactions on specific dates

Mark Knecht markknecht at gmail.com
Thu Oct 9 22:26:04 CEST 2014


On Thu, Oct 9, 2014 at 1:05 PM, Mark Knecht <markknecht at gmail.com> wrote:
> Hi,
>    I suspect I'm making this far more difficult than it needs to be,
> however I've spent time looking on Stack Overflow and couldn't find an
> answer. Hopefully someone here can point me in the right direction.
> Thanks in advance.
>
>    I would like to do something in quantstrat like set up an initial
> buy for all symbols in a portfolio on a certain date. To that end I
> want a signal column that says the date is matching my buy date and
> then I'l execute a specific rule. However signals need to match
> columns which led me to wanting the index in a column as a value. To
> that end the code below basically does this but returns  a numerical
> value which (I think) is referenced to 1970-01-01.
>
>> tail(IndexAsXts)
>            xtsIndex
> 2014-10-01    16344
> 2014-10-02    16345
> 2014-10-03    16346
> 2014-10-06    16349
> 2014-10-07    16350
> 2014-10-08    16351
>
>>
>
>    Anyway, how do I turn these numeric values back into dates that I
> could test against 'testDate'?
>
> Thanks,
> Mark
>
>
>
> require(quantstrat)
>
> Sys.setenv(TZ="UTC")
>
> symbol = "SPY"
> getSymbols(symbol,src="yahoo",from="2014-01-01")
>
> testDate = "2014-03-11"
>
> xtsIndex = function(data){
>   xtsIndex = reclass(index(data), match.to=data)
>   colnames(xtsIndex)="xtsIndex"
>   return(xtsIndex)
> }
>
> IndexAsXts = xtsIndex(get(symbol))
>
> tail(IndexAsXts)
>
> #Find origin
>
> LastVal = last(IndexAsXts)
> OriginDate = index(LastVal) - as.numeric(LastVal)
> print(OriginDate)
>
> tail(as.POSIXct(coredata(IndexAsXts), origin=OriginDate,
> tz=Sys.getenv("TZ")), 5)

Humm...It gets considerably closer with

getSymbols(symbol,src="yahoo",from="2014-01-01",
           index.class=c("POSIXt","POSIXct"))



More information about the R-SIG-Finance mailing list