Amarjit,

I got it. While I didn't fix up the GBPUSD demo, I did this using the
google demo.

The critical issue is that in order to optimize a component, the component
needs a label. The reason you were getting your error (as far as I can
deduce) is that you didn't label the indicator, and therefore, had no label
to which to hook the indicator optimization distribution.

In any case, I've attached the salient aspects of the demo to demonstrate.


###INITIAL STRATEGY

library(quantstrat)

osMaxDollar <- function(data, timestamp, orderqty, ordertype, orderside,
                                portfolio, symbol, prefer="Open", tradeSize,
                                maxSize, integerShares=FALSE,
                                ...) {
  pos <- getPosQty(portfolio, symbol, timestamp)
  if(prefer=="Close") {
    price <- as.numeric(Cl(mktdata[timestamp,]))
  } else {
    price <- as.numeric(Op(mktdata[timestamp,]))
  }
  posVal <- pos*price
  if (orderside=="short") {
    dollarsToTransact <- max(tradeSize, posVal-maxSize)
  } else {
    dollarsToTransact <- min(tradeSize, maxSize-posVal)
  }
  qty <- dollarsToTransact/price
  if(integerShares) {
    if(qty > 0) {
      qty <- floor(qty)
    }
    if(qty < 0) {
      qty <- ceiling(qty)
    }
  }
  return(qty)
}

rm(list=ls(.blotter), envir=.blotter)
initDate='2000-12-31'
initEq=10000

currency('USD')
Sys.setenv(TZ="UTC")

symbols <- c("USO")

stock(symbols, currency="USD", multiplier=1)
getSymbols(symbols, src='yahoo',
           index.class=c("POSIXt","POSIXct"),
           from = "2009-01-01", to="2013-12-31", adjust=TRUE)

strategy.st="GOOGstoch"
portfolio.st="GOOGstoch"
account.st="GOOGstoch"

rm.strat(portfolio.st)
rm.strat(strategy.st)
initPortf(portfolio.st, symbols=symbols, initDate=initDate, currency='USD')
initAcct(account.st, portfolios=portfolio.st, initDate=initDate,
currency='USD', initEq=initEq)
initOrders(portfolio.st, initDate=initDate)
strategy(strategy.st, store=TRUE)

################## indicators and parameters ##############

nFastK = 20
nFastD = 3
nSlowD = 5
maType="SMA"
.orderqty=1000
.txn=0

add.indicator(strategy.st, name = "stoch",
              arguments = list(HLC = quote(HLC(mktdata)), nFastK=nFastK,
                               nFastD=nFastD,nSlowD=nSlowD, maType=maType),
              label="stoch")  #NOTICE THE LABEL HERE.


################### signals ###################3#######
add.signal(strategy.st, name="sigThreshold",
           arguments = list(threshold=0.20,
                            column="slowD.stoch",
                            relationship="gt",
                            cross=TRUE),
           label="slowD.stoch.ind.gt.20")

add.signal(strategy.st,
           name="sigThreshold",
           arguments = list(threshold=0.80,
                            column="slowD.stoch",
                            relationship="lt",
                            cross=TRUE),
           label="slowD.stoch.ind.lt.80")

add.signal(strategy.st,
           name="sigThreshold",
           arguments = list(threshold=0.80,
                            column="slowD.stoch",
                            relationship="gte",
                            cross=TRUE),
           label="slowD.stoch.ind.gte.80")

add.signal(strategy.st,
           name="sigThreshold",
           arguments = list(threshold=0.20,
                            column="slowD.stoch",
                            relationship="lte",
                            cross=TRUE),
           label="slowD.stoch.ind.lte.20")

################# long rules ########################

add.rule(strategy.st,
         name='ruleSignal',
         arguments = list(sigcol="slowD.stoch.ind.gte.80",
                          sigval=TRUE,
                          orderqty='all',
                          ordertype='market',
                          TxnFees=.txn,
                          orderside='long',
                          #pricemethod='market',
                          replace=FALSE),
         label='Exit2SHORT', type='exit', path.dep=TRUE)

stratstoch <- add.rule(strategy.st,
                       name='ruleSignal',
                       arguments = list(sigcol="slowD.stoch.ind.gt.20",
                                        sigval=TRUE,
                                        orderqty=100,
                                        ordertype='market',
                                        TxnFees=0,
                                        orderside='long',
                                        #pricemethod='market',
                                        tradeSize=.orderqty,
                                        maxSize=initEq,
                                        replace=FALSE,
                                        osFUN=osMaxDollar),
                       label='EnterLONG', type='enter', path.dep=TRUE)

############# short rules ##################

#stratstoch <- add.rule(strategy.st,
#                       name='ruleSignal',
#                       arguments = list(sigcol="slowD.stoch.ind.lte.20",
#                                        sigval=TRUE,
#                                        orderqty='all',
#                                        ordertype='market',
#                                        TxnFees=.txn,
#                                        orderside='short',
#                                        #pricemethod='market',
#                                        replace=FALSE),
#                       label='Exit2LONG', type='exit', path.dep=TRUE)

#stratstoch <- add.rule(strategy.st,
#                       name='ruleSignal',
#                       arguments = list(sigcol="slowD.stoch.ind.lt.80",
#                                        sigval=TRUE,
#                                        orderqty=100,
#                                        ordertype='market',
#                                        TxnFees=0,
#                                        orderside='short',
#                                        #pricemethod='market',
#                                        replace=FALSE,
#                                        tradeSize=-.orderqty,
#                                        maxSize=initEq,
#                                        osFUN=osMaxDollar),
#                       label='EnterSHORT', type='enter', path.dep=TRUE)


Optimization:

.nSlowD <- seq(from=3, to=10, by=1)
.nLower <- seq(from=.10, to=.30, by=.05)
.nUpper <- seq(from=.70, to=.90, by=.05)
.nsamples = 50

add.distribution(strategy.st,
                 paramset.label="stochDist",
                 component.type='indicator',
                 component.label="stoch", #THE COMPONENT LABEL IS THE LABEL
ARGUMENT FROM THE ADD.INDICATOR FUNCTION.
                 variable=list(nSlowD=.nSlowD),
                 label="nSlowDdist")

add.distribution(strategy.st,
                 paramset.label="stochDist",
                 component.type="signal",
                 component.label="slowD.stoch.ind.gt.20",
                 variable=list(threshold=.nLower),
                 label="lowThreshDist"
                 )

add.distribution(strategy.st,
                 paramset.label="stochDist",
                 component.type="signal",
                 component.label="slowD.stoch.ind.lt.80",
                 variable=list(threshold=.nUpper),
                 label="highThreshDist"
)

save.strategy(strategy.st)

require(doMC)
registerDoMC(cores=8)

results <- apply.paramset(strategy.st,
                          paramset.label='stochDist',
                          portfolio.st=portfolio.st,
                          account.st=account.st,
                          nsamples = .nsamples,
                          audit = NULL,
                          verbose=TRUE)

print(results$tradeStats)



On Wed, Apr 16, 2014 at 11:41 PM, amarjit chandhial <
a.chandhial@btinternet.com> wrote:

>
>
>
> Can someone provide guidance on getting the optimization working.
>
>
> Amarjit
>
>
>
>
>   ----- Forwarded Message -----
>  *From:* amarjit chandhial <a.chandhial@btinternet.com>
> *To:* "r-sig-finance@r-project.org" <r-sig-finance@r-project.org>
> *Sent:* Thursday, 10 April 2014, 22:26
> *Subject:* [R-SIG-Finance] stochastic oscillator OBOS - intraday data &
> optimization
>
>
> All,
>
>
> Iiya and I have implemented a stochastic oscillator OBOS strategy within
> quantstrat.
>
> I have then applied quantstrat's intraday GBPUSD data, 30min frequency
> with the strategy, including transaction costs (as in the other demos).
>
> Thereafter I have attempted to optimize various values e.g.
>
> (i) nSlowD, vary from 1-to-10
> (ii) the lower level (over-sold) vary from .10-to-.30
> (ii) the upper level (over-bought) vary from .70-to-.90
>
>
> I get the error
>
> error calling combine function:
> <simpleError in fun(result.1, result.2, result.3, result.4, result.5,
> result.6, result.7, result.8, result.9,..., result.100): attempt to select
> less than one element>
>
> Attached are both programs: initial strategy and optimization.
>
>
>
> An error corresponding to this from last month
> http://r-forge.r-project.org/forum/forum.php?set=custom&forum_id=1032&style=nested&max_rows=100&submit=Change+View
>
>
> Please help!
>
> Amarjit
>
>
>
>
>
>
> _______________________________________________
> R-SIG-Finance@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.
>
>
> _______________________________________________
> R-SIG-Finance@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.
>

	[[alternative HTML version deleted]]

