[R-SIG-Finance] Fw: stochastic oscillator OBOS - intraday data & optimization

amarjit chandhial a.chandhial at btinternet.com
Thu Apr 24 09:00:26 CEST 2014



Hi IIya,


I have run optimizations for GBPUSD 30min data, 3528 (8*21*21) combinations. Thereafter, I divided the tradeStats on the nSlowD variable and generated heat-maps & 3D-Graphs for various parameters, viewing lowThresh vs highThresh. Maybe there's a better way of doing this. I have checked the values given via the optimizations with an individual run of the strategy and the results equate. Please check?  As of only 10 days of data, the strategy does not generate many trades. It's up to the reader to determine values for nSlowD, lowThresh & highThresh given the plethora of graphs; indeed rationale for 'optimal' values along these lines is given on p51-p55  Jaekle & Tomasini (2009). With more data I'd like to do an IS vs OOS test, in addition to a periodic optimized walk-forward, that's for later; of course being mindful of the pitfalls of over-optimization and non-parsimony. 


Question:


(a) Are the inequalities correct with regard to add.distribution ?

The inequalities in the add.distributions for the long-only GBPUSD (enter a trade buy GBP sell USD; and to exit a trade sell GBP and buy USD) strategy are:
slowD.stoch.ind.gt.20
slowD.stoch.ind.gte.80


Likewise, to include a short-side (enter a trade sell GBP buy USD; and to exit a trade buy GBP and sell USD) the add.distributions would be:
slowD.stoch.ind.lte.20
slowD.stoch.ind.lt.80



Attached are:
(a) program for calculating the optimizations & generating the graphs. NB: half-way in the program the reader can load the RData tradeStats data and simply generate the graphs not running the optimizations.
(b) RData file with tradeStats of 3528 optimizations


Please check if all is okay with optimizations. If there's a better way of graphing be free to make the improvements.


Thereafter I'd like to get the timefilter optimized. 



Amarjit





 
 From: amarjit chandhial <a.chandhial at btinternet.com>
To: Ilya Kipnis <ilya.kipnis at gmail.com>; "r-sig-finance at r-project.org" <r-sig-finance at r-project.org> 
Sent: Monday, 21 April 2014, 8:03
Subject: Re: [R-SIG-Finance] Fw: stochastic oscillator OBOS - intraday data & optimization
  


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 at gmail.com>
To: amarjit chandhial <a.chandhial at btinternet.com>; "r-sig-finance at r-project.org" <r-sig-finance at 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 at 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 at gmail.com>
>To: amarjit chandhial <a.chandhial at btinternet.com>; "r-sig-finance at r-project.org" <r-sig-finance at 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 at btinternet.com> wrote:
>
>
>>
>>
>>
>>
>>
>>Can someone provide guidance on getting the optimization
 working.
>>
>>
>>
>>
>>Amarjit
>>
>>
>>
>>
>>
>>
>>
>>
>>----- Forwarded Message -----
>>From: amarjit chandhial <a.chandhial at btinternet.com>
>>To: "r-sig-finance at r-project.org" <r-sig-finance at 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 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.
>>
>>
>>_______________________________________________
>>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.
>>
>
>
>
   
 [[alternative HTML version deleted]]


_______________________________________________
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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20140424/e487e75e/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ac_stochOSC_OBOS_GBPUSD_vanilla_and_optimization.R
Type: application/octet-stream
Size: 12842 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20140424/e487e75e/attachment.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: GBPUSDStoch.ALLcombinations.RData
Type: application/x-gzip-compressed
Size: 212404 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20140424/e487e75e/attachment.bin>


More information about the R-SIG-Finance mailing list