[R-SIG-Finance] Potential bug in quantstrat in how it handles "F" symbol

Sergey Pisarenko drseergio at gmail.com
Thu Mar 1 20:10:58 CET 2012


Hi,

I have modified quantstrat to use ".GlobalEnv" and it works fine so
far. Not sure if this is something that should be considered to be
changed permanently.

/Sergey

On Thu, Mar 1, 2012 at 6:47 PM, Sergey Pisarenko <drseergio at gmail.com> wrote:
> Hi,
>
> Just a follow-up. When I step through applyStrategy with a debugger
> and I get to the lines:
>
>    for (symbol in symbols) {
>        if (isTRUE(load.mktdata))
>            mktdata <- get(symbol)
>
>
> "get(symbol)" returns FALSE
>
> "get(symbol, envir=.GlobalEnv)" returns market data for Ford.
>
> /Sergey
>
> On Thu, Mar 1, 2012 at 6:35 PM, Sergey Pisarenko <drseergio at gmail.com> wrote:
>> Hi Brian and rex,
>>
>> Thank you for looking into this. This is strange because that's
>> exactly what's happening. Here's how I load the data:
>>
>> watched   <- c('F')
>>
>> symbols <- vector()
>> for (symbol in watched) {
>>  ubars    <- tryCatch(getSymbols(symbol, src='MySQL',
>> auto.assign=FALSE), error=function(e) NULL)
>>  # skip if we don't have enough data
>>  if (is.null(ubars) || nrow(ubars) < startDay) next
>>  symbols  <- c(symbols, symbol)
>>  bars     <- ubars[index(ubars) >= as.Date(from)]
>>  assign(symbol, bars)
>> }
>>
>> When I do:
>>> F
>>
>> I get the data but "get" inside of applyStrategy still fails with an
>> error. It fails with:
>> Error in mktdata[, keep] : incorrect number of dimensions
>>
>> I attach a sample runnable script to this e-mail.
>>
>> --
>> Kind Regards,
>> Sergey Pisarenko.
>>
>> On Thu, Mar 1, 2012 at 3:04 PM, Brian G. Peterson <brian at braverock.com> wrote:
>>> On Wed, 2012-02-29 at 23:53 -0800, rex wrote:
>>>> Sergey Pisarenko <drseergio at gmail.com> [2012-02-29 22:29]:
>>>> >
>>>> >The issue here is that get('F') will return FALSE because F is defined
>>>> >as FALSE in R. This will obviously cause a failure down the road where
>>>> >market data is read and manipulated.
>>>>
>>>> Hello Sergey,
>>>>
>>>> Easy fix:
>>>>
>>>> > if (F == FALSE) print('F == FALSE')
>>>> [1] "F == FALSE"
>>>> > F <- 'F'
>>>> > if (F == FALSE) print('F == FALSE')
>>>> >
>>>
>>> According to the R FAQ[1]:
>>> "In R, T and F are just variables being set to TRUE and FALSE,
>>> respectively, but are not reserved words as in S and hence can be
>>> overwritten by the user. (This helps e.g. when you have factors with
>>> levels "T" or "F".) Hence, when writing code you should always use TRUE
>>> and FALSE."
>>>
>>> I think you'll find that blotter, quantstrat, FinancialInstrument all
>>> use TRUE and FALSE correctly.
>>>
>>> If you are trying to load data for Ford, you would already have locally
>>> overwritten the F variable with your data for Ford.
>>>
>>>> require(quantmod, quietly=TRUE)
>>>> getSymbols('F')
>>> [1] "F"
>>>> head(F)
>>>           F.Open F.High F.Low F.Close F.Volume F.Adjusted
>>> 2007-01-03   7.56   7.67  7.44    7.51 78652200       7.48
>>> 2007-01-04   7.56   7.72  7.43    7.70 63454900       7.67
>>> 2007-01-05   7.72   7.75  7.57    7.62 40562100       7.59
>>> 2007-01-08   7.63   7.75  7.62    7.73 48938500       7.70
>>> 2007-01-09   7.75   7.86  7.73    7.79 56732200       7.76
>>> 2007-01-10   7.79   7.79  7.67    7.73 42397100       7.70
>>>> head(get('F'))
>>>           F.Open F.High F.Low F.Close F.Volume F.Adjusted
>>> 2007-01-03   7.56   7.67  7.44    7.51 78652200       7.48
>>> 2007-01-04   7.56   7.72  7.43    7.70 63454900       7.67
>>> 2007-01-05   7.72   7.75  7.57    7.62 40562100       7.59
>>> 2007-01-08   7.63   7.75  7.62    7.73 48938500       7.70
>>> 2007-01-09   7.75   7.86  7.73    7.79 56732200       7.76
>>> 2007-01-10   7.79   7.79  7.67    7.73 42397100       7.70
>>>
>>> so, get('F') will only return FALSE if you don't have any data loaded.
>>>
>>> That is a problem on your end, I think, and not a problem in the
>>> package.
>>>
>>> Regards,
>>>
>>>   - Brian
>>>
>>> Ref:
>>> [1]
>>> http://sites.stat.psu.edu/~dhunter/R/doc/manual/R-FAQ.html#What-are-the-differences-between-R-and-S_003f
>>> (see section 3.3.3)
>>>
>>> --
>>> Brian G. Peterson
>>> http://braverock.com/brian/
>>> Ph: 773-459-4973
>>> IM: bgpbraverock
>>>
>>> _______________________________________________
>>> 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