[R-SIG-Finance] quantstrat parameters

Joshua Ulrich josh.m.ulrich at gmail.com
Sat Oct 22 04:21:14 CEST 2011


Hi Roger,

Have you seen Brian's answer in this thread?
https://stat.ethz.ch/pipermail/r-sig-finance/2011q3/008470.html

I believe one of those two approaches should work for you.

Best,
--
Joshua Ulrich  |  FOSS Trading: www.fosstrading.com



On Wed, Oct 19, 2011 at 12:01 PM, Roger Trimble <roger at turningbull.com> wrote:
> Good afternoon,
>
> I have a question regarding the usage of parameters to quantstrat.  As an
> example, I have a strategy that uses three moving averages ('EMA'); slow,
> medium, and fast.  I would like to run this strategy through
> applyParameter, using different combinations of 'n' for the different EMA
> indicators.
>
> My apply strategy command looks something like this:
> out <- applyStrategy(strategy=strat, portfolios=st.name,
>    parameters=list(nfast=10,nmed=28,nslow=60))
>
> So, I am using nfast, nmed, and nslow to represent the 'n' argument for EMA.
>
> In my strategy definition, I am using this:
>
> ## fast ma
> strat <- add.indicator(strategy=strat,
>                       name='EMA',
>                       arguments=list(x=quote(Cl(mktdata))),
>                       parameters=list(n=quote(nfast)),
>                       label='fast.ma')
> ## med ma
> strat <- add.indicator(strategy=strat,
>                       name='EMA',
>                       arguments=list(x=quote(Cl(mktdata))),
>                       parameters=list(n=quote(nmed)),
>                       label='med.ma')
> ## slow ma
> strat <- add.indicator(strategy=strat,
>                       name='EMA',
>                       arguments=list(x=quote(Cl(mktdata))),
>                       parameters=list(n=quote(nslow)),
>                       label='slow.ma')
>
> The strategy runs without error, but the parameters do not appear to be
> evaluating correctly, and the model finishes without trading.  I believe
> this is because the indicators are defaulting to 10, and never firing a
> signal.  In viewing mktdata after the run, I can confirm the indicators
> are all equal.
>
> If I run the model explicitly setting the respective n arguments, e.g.
> arguements=list(x=quote(Cl(mktdata)),n=28)...,  then the strategy runs as
> expected with signals and trades.
>
>
> Looking into the code leads me to believe my intuition is wrong about how
> this works:
>
>        # see 'S Programming p. 67 for this matching
>        fun<-match.fun(indicator$name)
>        .formals  <- formals(fun)
>        onames <- names(.formals)
>
>        pm <- pmatch(names(indicator$arguments), onames, nomatch = 0L)
>        if (any(pm == 0L))
>            warning(paste("some arguments stored for",indicator$name,"do
> not match"))
>        names(indicator$arguments[pm > 0L]) <- onames[pm]
>        .formals[pm] <- indicator$arguments[pm > 0L]
>
>                # now add arguments from parameters
>                if(length(parameters)){
>                        pm <- pmatch(names(parameters), onames, nomatch = 0L)
>                        names(parameters[pm > 0L]) <- onames[pm]
>                        .formals[pm] <- parameters[pm > 0L]
>                }
>
>
> It appears to me that names(parameters) and onames need to be the same,
> which leaves me confused about how to specify different parameters to
> different indicator functions that are looking for the same named
> argument.
>
> I'm sure my error is operator head-space and timing...  I greatly
> appreciate any feedback here.  Thank you very much.
>
> Roger
>
> --
> Roger Trimble
> TurningBull
>
> _______________________________________________
> 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