[R-SIG-Finance] Passing two distributions to one parameter
Atakan Okan
atakanokan at outlook.com
Sun May 29 19:41:43 CEST 2016
I am so sorry, switched to Outlook.com, I always forget to put it into Plain Text mode.
Here is the code in plain-text format,(The commented out add.distributions did not work.)
>> 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.
#PSAR problem - passing two arguments to one parameter when creating distribution
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 <- 0
shortStopLossDistance <- 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")
#calismadi
#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")
#calismadi
#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")
#calismadi
#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")
#apply.paramset.df <- apply.paramset.signal.analysis(strategy.st = strategy.st, portfolio.st = strategy.st, paramset.label = paramset.label.name)
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)
#applyStrategy( strategy=strategy.st , portfolios=strategy.st
# ,verbose=TRUE)
#updatePortf(strategy.st)
#updateAcct(strategy.st)
#updateEndEq(strategy.st)
> From: josh.m.ulrich at gmail.com
> Date: Sun, 29 May 2016 10:39:20 -0500
> Subject: Re: [R-SIG-Finance] Passing two distributions to one parameter
> To: atakanokan at outlook.com
> CC: r-sig-finance at r-project.org
>
> 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 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.)
[[alternative HTML version deleted]]
More information about the R-SIG-Finance
mailing list