[R-SIG-Finance] Passing two distributions to one parameter

Joshua Ulrich josh.m.ulrich at gmail.com
Sun May 29 17:39:20 CEST 2016


If you want people on this list to help you, you need to stop sending
HTML.  Your message is unreadable and the code is not runable.
Configure your mail client to send only plain-text.

On Sat, May 28, 2016 at 8:14 AM, Atakan Okan <atakanokan at outlook.com> wrote:
> Hi r-sig-finance,
> I was trying to implement a strategy where parabolic SAR (from TTR package) is used. However one of the parameters of SAR "accel" takes two values, one for "accelaration factor" and one for "maximum acceleration factor". I was unable to pass two sequences as one parameter and I was wondering if it is possible to do that to be able to optimize those parameters.
> Reproducible code: (The commented out add.distributions did not work.)
>
> library(PerformanceAnalytics)library(quantstrat)                               no.cores <- 4
> .strategy <- new.env().blotter  <- new.env()
> symbol.name = "AAPL"                                        tick.size = 0.01 currency("USD")stock(symbol.name, currency="USD", multiplier=1,tick_size= tick.size)
> initialEquity = 1000                                           port.acct.currency <- "USD"strategy.keyword = "PSAR_D1"
> longStopLossDistance <- 0;longTrailingStopDistance <- 0;longTakeProfitDistance <- 0shortStopLossDistance <- 0;shortTrailingStopDistance <- 0;shortTakeProfitDistance <- 0
> symbol.data <- getSymbols(symbol.name)
> strategy.st <- paste(symbol.name,strategy.keyword,sep = "_")rm.strat(strategy.st)
> initDate = as.character(as.Date(index(symbol.data[1])-1))                    initPortf(strategy.st, symbol.name, initDate=initDate, currency = port.acct.currency)initAcct(strategy.st, portfolios=strategy.st, initDate=initDate,          initEq=initialEquity, currency = port.acct.currency)initOrders(portfolio=strategy.st,initDate=initDate)                          strategy(strategy.st,store=TRUE)summary(getStrategy(strategy.st))
> txn.model <- 0
> fixedSizeLong  =  100     fixedSizeShort =  -100
> paramset.label.name <- "PSAR_OPT"accel.factor <- seq(from=0.01, by=0.01, length.out = 3)max.accel.factor <- seq(from=0.1, by=0.05, length.out = 4)accels.df <- expand.grid(accel.factor,max.accel.factor)
>
> add.indicator(strategy.st,                                                                   name = "SAR",                                                                 arguments = list(HL=HLC(eval(parse(text = symbol.name)))                               ,accel=c(accel.factor,max.accel.factor)              ),                       label='sar')
>
> #apply.indicators.df <- applyIndicators(strategy.st, mktdata=eval(parse(text = symbol.name)))
>
> add.signal(strategy.st,name="sigCrossover",           arguments = list(columns=c("sar","Close"),relationship="gt"),           label="sar.gt.close")
> add.signal(strategy.st,name="sigCrossover",           arguments = list(columns=c("sar","Close"),relationship="lt"),           label="sar.lt.close")
> #apply.signals.df <- applySignals(strategy.st, mktdata=apply.indicators.df)
> add.rule(strategy.st,         name='ruleSignal',         arguments = list(sigcol="sar.gt.close",                          sigval=TRUE,                          prefer="Open",                           orderqty= fixedSizeLong,                           ordertype='market',                          orderside='long',                          orderset='ocolong',                          TxnFees = txn.model),         type='enter',         label='longenter',         enabled=TRUE)
> # Long Exit Rule-------------------------------------------------------------------add.rule(strategy.st,         name='ruleSignal',         arguments = list(sigcol="sar.lt.close",                          sigval=TRUE,                          prefer="Open",                           orderqty='all',                          ordertype='market',                          orderside='long',                          orderset='ocolong',                          TxnFees = txn.model),         type='exit',         label='longexit',         enabled=TRUE)
>
>
> # Short Entry Rule--------------------------------------------------------------------add.rule(strategy.st,         name='ruleSignal',         arguments = list(sigcol="sar.lt.close",                          sigval=TRUE,                          prefer="Open",                           orderqty= fixedSizeShort,                           ordertype='market',                          orderside='short',                          orderset='ocoshort',                          TxnFees = txn.model),         type='enter',         label='shortenter',         enabled=TRUE)
> # Short Exit Rule---------------------------------------------------------------------add.rule(strategy.st,         name='ruleSignal',         arguments = list(sigcol="sar.gt.close",                          sigval=TRUE,                          prefer="Open",                           orderqty='all',                          ordertype='market',                          orderside='short',                          orderset='ocoshort',                          TxnFees = txn.model),         type='exit',         label='shortexit',         enabled=TRUE)
>
>
>
> #SAR Optimization-------------------------------------------------------------#add.distribution(strategy.st, #                paramset.label = paramset.label.name,  #               component.type = 'indicator',   #              component.label = 'sar',                          #             variable = list( accel = accels.df),             #            label = "sar.accels")
>
>
> #add.distribution(strategy.st,#                 paramset.label = paramset.label.name,#                 component.type = 'indicator',#                 component.label = 'sar',                      #                 variable = list( accel[1] = accel.factor),        #                 label = "sar.accels.factor")
>
> #add.distribution(strategy.st,#                 paramset.label = paramset.label.name,#                 component.type = 'indicator',#                 component.label = 'sar',                      #                 variable = list( accel[2] = max.accel.factor),        #                 label = "sar.accels.max")
>
> #add.distribution(strategy.st,#                 paramset.label = paramset.label.name,#                 component.type = 'indicator',#                 component.label = 'sar',                      #                 variable = list( accel = accel.factor,max.accel.factor),        #                 label = "sar.accels.2")
>
>
>
> summary(getStrategy(strategy.st))
> paramsetenv<-new.env()results <- apply.paramset(strategy.st,paramset.label=paramset.label.name,                          portfolio=strategy.st, account=strategy.st,nsamples=0,verbose = TRUE,                          audit=paramsetenv)
> results.df <- data.frame(results$tradeStats)
>
> ------------------------------------------------------------------------------------------------------------------------------------------------
> Atakan Okan
>         [[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.



-- 
Joshua Ulrich  |  about.me/joshuaulrich
FOSS Trading  |  www.fosstrading.com
R/Finance 2016 | www.rinfinance.com



More information about the R-SIG-Finance mailing list