[R-SIG-Finance] Quantstrat problem

Brian G. Peterson brian at braverock.com
Tue Dec 28 15:02:45 CET 2010



On 12/28/2010 05:16 AM, Emanuel Burgener wrote:
> Hi all,
>
> I would need your help to solve the following problem. I get this error when
> I run my code for quantstrat (cf hereunder):
> *
> Error in do.call(opr, list(data[, colNums[1]], data[, colNums[2]])) :
>    'what' must be a character string or a function*
>
> I would really appreciate your help.
> Maybe two comments regarding my code:
>
>     - the aim is to have an indicator that can take different values, for
>     example -2,-1,0,1,2 (currently only -1,0,1 are implemented). And according
>     to this indicator, I would like to have a position of Indicator*100 shares
>     (hence the maybe too complicated "minusOne", "minusZero", etc episode).

I really try not to code custom indicators to give integer results like 
this.  I find that they are typically difficult to understand and debug. 
  See the 'macd' and 'rsi' demos for examples of how to use an indicator 
that you believe has some predictive power without having to further 
transform your indicator.  In practice, we use more 'sigFormula' calls 
than the simpler sigThreshold and sigCrossover calls, since this lets us 
do things like combine indicators (or prior signals) to get more 
business-oriented logic into the strategy.

> Any suggestion to improve my not-so-elegant solution is welcome.

Your indicator function doesn't name the columns but your sigCrossover 
expects columns named:

"minusOne","minusZero","Indicator"

Which will never work.

traceback says:

 > out<-applyStrategy(strategy=stratTest , portfolios="Test")
Error in do.call(opr, list(data[, colNums[1]], data[, colNums[2]])) :
   'what' must be a character string or a function
 > traceback()
7: do.call(opr, list(data[, colNums[1]], data[, colNums[2]]))
6: sigComparison(label = label, data = data, columns = columns[c(i,
        lng)], relationship = relationship)
5: diff(sigComparison(label = label, data = data, columns = columns[c(i,
        lng)], relationship = relationship))
4: function (label, data = mktdata, columns, relationship = c("gt",
        "lt", "eq", "gte", "lte"))
    {
        ret_sig = FALSE
        lng <- length(columns)
        for (i in 1:(lng - 1)) {
            ret_sig = ret_sig | diff(sigComparison(label = label,
                data = data, columns = columns[c(i, lng)], relationship 
= relationship)) ==
                1
        }
        is.na(ret_sig) <- which(!ret_sig)
        colnames(ret_sig) <- label
        return(ret_sig)
    }(label = "signal.sell1", data = mktdata, columns = c("minusOne",
    "minusZero", "Indicator"), relationship = "op")
3: do.call(fun, .formals)
2: applySignals(strategy = strategy, mktdata = mktdata, sret$indicators,
        parameters = parameters, ...)
1: applyStrategy(strategy = stratTest, portfolios = "Test")

Please look at the 'macross' demo, which does what you are trying to do 
in your example here. You'll probably be better served by extending it 
than by writing from scratch.

>     - I actually first started to implement and test my strategies with
>     blotter. But, as it is rather slow in terms of testing, I decided to
>     transfer my Indicator into the quantstrat environment. So if there is an
>     easy solution to transfer "blotter"-code into "quantstrat"-code, this would
>     be the best solution for me (or any other way to speed things up with
>     blotter).

'blotter' is for transaction P&L.  I *never* call blotter by hand, 
except for the occasional addTxn call from inside a custom risk rule 
function or something similar.

We wrote quantstrat to simplify strategy modeling, and to reduce the 
amount of code needed to model and test a strategy.  I never 
contemplated using blotter for strategy modeling, just for P&L, so there 
in no 'simple' way to translate custom strategy code to quantstrat.  For 
an example of 'similar' things done in blotter and then again in 
quantstrat, see the 'longtrend' demo in blotter and the 'faber' example 
in quantstrat.  We'll be working on generalized capital-based order 
sizing functions in the next few weeks, so hopefully you'll have some 
examples of more sophisticated order sizing logic soon too.

> Again, thank you very much for your help, you're doing a great job with this
> forum.

Regards,

   - Brian

-- 
Brian G. Peterson
http://braverock.com/brian/
Ph: 773-459-4973
IM: bgpbraverock



More information about the R-SIG-Finance mailing list