[R-SIG-Finance] quantstrat parameters

Roger Trimble roger at turningbull.com
Wed Oct 19 19:01:30 CEST 2011


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



More information about the R-SIG-Finance mailing list