
Hi IIya,


Have reinstalled quantstrat and I get results! Hooorrraaayyy ;-)

I will try on GBPUSD, produce graphs, and then try a relevant intraday timefilter with optimization.


Amarjit

 




________________________________
 From: Ilya Kipnis <ilya.kipnis@gmail.com>
To: amarjit chandhial <a.chandhial@btinternet.com>; "r-sig-finance@r-project.org" <r-sig-finance@r-project.org> 
Sent: Sunday, 20 April 2014, 23:38
Subject: Re: [R-SIG-Finance] Fw: stochastic oscillator OBOS - intraday data & optimization



Amarjit,

Can you reinstall the quantstrat package and copy and paste the applyStrategy call from inside apply.paramset says?

Thanks.

-Ilya


On Sun, Apr 20, 2014 at 2:25 PM, amarjit chandhial <a.chandhial@btinternet.com> wrote:


>
>Hi IIya,
>
>
>
>
>Have run the program as in email and I cannot generate results on either windows or linux. 
>
>
>
>with 
>
>
>
>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.10, result.11, result.12,     result.13, result.14, result.15, result.16, result.17, result.18,     result.19, result.20, result.21, result.22, result.23, result.24,     result.25, result.26, result.27, result.28, result.29, result.30,     result.31, result.32, result.33, result.34, result.35, result.36,     result.37, result.38, result.39, result.40, result.41, result.42,     result.43, result.44, result.45, result.46, result.47, result.48,     result.49,
 result.50): attempt to select less than one element>
>
>
>
>
>Have also tried variations on labels e.g. add.distribution component.label="slowD.stoch.ind.gte.80", as the signal is applied in the add.rule.
>
>
>
>Here's the packages on linux with rev. 
>
>
>
>Please advise.
>
>
>
>
>
>Amarjit
>
>
>
>
>
>
>
>
>
>R version 3.1.0 (2014-04-10)
>Platform:
 x86_64-pc-linux-gnu (64-bit)
>
>locale:
> [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C              
 LC_TIME=en_GB.UTF-8        LC_COLLATE=en_GB.UTF-8     LC_MONETARY=en_GB.UTF-8   
> [6] LC_MESSAGES=en_GB.UTF-8    LC_PAPER=en_GB.UTF-8       LC_NAME=C                 
 LC_ADDRESS=C               LC_TELEPHONE=C            
>[11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C      
>
>attached base packages:
>[1] stats     graphics  grDevices utils     datasets  methods   base     
>
>other attached packages:
> [1] quantstrat_0.8.2 (1598)          foreach_1.4.2 (25)             blotter_0.8.18 (1599)            PerformanceAnalytics_1.1.4 (3362)     FinancialInstrument_1.1.9 (1529)
> [6] quantmod_0.4-1 (610)
            Defaults_1.1-1                  TTR_0.22-0.1 (177)              xts_0.9-7 (803)                                  zoo_1.7-11 (973)               
>
>loaded via a namespace (and not attached):
>[1] codetools_0.2-8 grid_3.1.0      iterators_1.0.7 lattice_0.20-29 tools_3.1.0    
>
>
>
>
>
>
>From: Ilya Kipnis <ilya.kipnis@gmail.com>
>To: amarjit chandhial <a.chandhial@btinternet.com>; "r-sig-finance@r-project.org" <r-sig-finance@r-project.org> 
>Sent: Sunday, 20 April 2014, 3:19
>Subject: Re: [R-SIG-Finance] Fw: stochastic oscillator OBOS - intraday data & optimization
> 
>
>
>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]]

