[R-SIG-Finance] quantstrat demo(faber_rebal)

Simon Otziger simon at volemont.com
Sat Mar 29 10:45:16 CET 2014


Dear Gei,

Thanks for the swift reply. Replacing timestamp by pindex[i] in the
add.rule doesn't solve the problem. Does the quick fix work for you?
The downloaded data doesn't have any missing values.

Simon

library(quantstrat)
Sys.setenv(TZ = "America/New_York")
source("faber_rebal.R")

[1] "setup completed"
Error in if (is.na(price)) stop("order at timestamp ", timestamp, "
must not have price of NA") :
  argument is of length zero

# add quaterly rebalancing
add.rule('faber', 'rulePctEquity',
        arguments=list(rebalance_on='quarters',
                trade.percent=1/length(symbols),

refprice=quote(last(getPrice(mktdata)[paste('::',pindex[i],sep='')][,1])),
                digits=0
        ),
        type='rebalance',
        label='rebalance'
)

On Fri, Mar 28, 2014 at 11:36 PM, Gei Lin <gmonaie at gmail.com> wrote:
> Looking at applyStrategy.rebalancing(), timestamp is being passed here to
> the function rulePctEquity, so it can be used *inside* rulePctEquity, but
> you can't reference it in passing arguments because it isn't defined inside
> applyStrategy.rebalancing(), it is floating around as the value pindex[i]
>
> ruleProc(s$rules$rebalance, timestamp = pindex[i],
>                       path.dep = TRUE, ruletype = "rebalance",
>                       ..., mktdata = md_subset, parameters = parameters,
>                       portfolio = portfolio, symbol = symbol)
>
> Quick fix, replace "timestamp" in the add.rule with "pindex[i]" which has
> the intended value and won't be missing.
>
> # add quarterly rebalancing
>> add.rule('faber', 'rulePctEquity',
> +         arguments=list(rebalance_on='quarters',
> +                 trade.percent=1/length(symbols),
> +
> refprice=quote(last(getPrice(mktdata)[paste('::',timestamp,sep='')][,1])),
> +                 digits=0
> +         ),
> +         type='rebalance',
> +         label='rebalance'
> + )
>
>
> On Fri, Mar 28, 2014 at 5:00 PM, Simon Otziger <simon at volemont.com> wrote:
>>
>> Dear list members,
>>
>> I'm trying to run the demo(faber_rebal) from the Quantstrat package
>> (R-Forge Rev. 1590):
>> library(quantstrat)
>> Sys.setenv(TZ = "America/New_York")
>> demo(faber_rebal)
>>
>> The demo stops on line 133 with the error message:
>> > out<-applyStrategy.rebalancing(strategy='faber' , portfolios='faber')
>> Error in paste("::", timestamp, sep = "") :
>>   cannot coerce type 'closure' to vector of type 'character'
>>
>> I guess it's a namespace/masking clash with the timestamp() function
>> of the R.utils package. Another quantstrat user posted the same issue
>> in the R-Forge forums, see
>>
>> https://r-forge.r-project.org/forum/forum.php?thread_id=28525&forum_id=1032&group_id=316
>>
>> Any idea how to fix it?
>>
>> Thanks,
>> Simon
>>
>>
>> > sessionInfo()
>> R version 3.0.3 (2014-03-06)
>> Platform: x86_64-pc-linux-gnu (64-bit)
>>
>> locale:
>>  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
>>  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
>>  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
>>  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
>>  [9] LC_ADDRESS=C               LC_TELEPHONE=C
>> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
>>
>> attached base packages:
>> [1] stats     graphics  grDevices utils     datasets  methods   base
>>
>> other attached packages:
>>  [1] quantstrat_0.8.0           foreach_1.4.1
>>  [3] blotter_0.8.17             PerformanceAnalytics_1.1.0
>>  [5] FinancialInstrument_1.1.9  quantmod_0.4-0
>>  [7] Defaults_1.1-1             TTR_0.22-0
>>  [9] xts_0.9-7                  zoo_1.7-11
>>
>> loaded via a namespace (and not attached):
>> [1] codetools_0.2-8 grid_3.0.3      iterators_1.0.6 lattice_0.20-27
>> [5] tools_3.0.3
>>
>> _______________________________________________
>> 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.
>
>
>
>
> --
>
> Don't quit. Suffer now and live the rest of your life as a champion.
>
>                                                 - Muhammad Ali
>
> The world is full of obvious things which nobody by any chance observes.
>
>                                                 - Sir Arthur Conan Doyle
>
> Towering genius disdains a beaten path. It seeks regions hitherto
> unexplored.
>
>                                                 - Abraham Lincoln



More information about the R-SIG-Finance mailing list