[R-SIG-Finance] Adding "Stoploss disabled" to the parameter distribution for apply.paramset
Atakan Okan
atakanokan at outlook.com
Sat May 27 20:11:52 CEST 2017
Tried doing a simple MACD Long-only strategy but couldn't manage to get it working. The take profit rule doesn't seem to work as well, since changing its threshold does not affect the net pnl. Below is a minimal (as much as I could) reproducible code:
#AAPL google finance data
#MACD cross
library(quantmod)
library(xts)
library(quantstrat)
getSymbols("AAPL", src = "google")
plot(Cl(AAPL))
.strategy <- new.env()
.blotter <- new.env()
currency('USD')
stock("AAPL", currency="USD", multiplier=1,tick_size= 0.01)
strategy.st <- "reproducible_strategy"
rm.strat(strategy.st)
initDate = as.character(as.Date(index(AAPL[1])-1))
initPortf(strategy.st,
"AAPL",
initDate=initDate,
currency = "USD")
initAcct(strategy.st,
portfolios=strategy.st,
initDate=initDate,
initEq=1000,
currency = "USD")
initOrders(portfolio=strategy.st,
initDate=initDate)
strategy(strategy.st,store=TRUE)
txn.model <- 0
add.indicator(strategy.st,
name = "MACD",
arguments = list(x=Cl(AAPL)),
label='macd')
add.signal(strategy.st,name="sigCrossover",
arguments = list(columns=c("macd.macd","signal.macd"),
relationship="gt"),
label="macd.gt.signal")
add.signal(strategy.st,name="sigCrossover",
arguments = list(columns=c("macd.macd","signal.macd"),
relationship="lt"),
label="macd.lt.signal")
# Long Entry Rule-----------------------------------------------------------------------------
add.rule(strategy.st,
name='ruleSignal',
arguments = list(sigcol="macd.gt.signal",
sigval=TRUE,
prefer="Open",
orderqty= 10,
ordertype='market',
orderside='long',
orderset='ocolong',
TxnFees = 0),
type='enter',
label='longenter',
enabled=FALSE
)
# Long Exit Rule-------------------------------------------------------------------
add.rule(strategy.st,
name='ruleSignal',
arguments = list(sigcol="macd.lt.signal",
sigval=TRUE,
prefer="Open",
orderqty='all',
ordertype='market',
orderside='long',
orderset='ocolong',
TxnFees = 0),
type='exit',
label='longexit',
enabled=FALSE
)
# Long Take Profit Rule
TakeProfitDistanceRANGE <- seq(1,5,by=4)
longTakeProfitDistance <- TakeProfitDistanceRANGE[1]
add.rule(strategy.st, name = "ruleSignal",
arguments = list(sigcol="macd.lt.signal",
sigval=TRUE,
ordertype="limit",
orderside="long",
replace=FALSE,
tmult=FALSE,
threshold=quote(longTakeProfitDistance),
orderqty="all",
orderset="ocolong",
TxnFees = 0),
type = "chain",
parent="longenter",
label = "TakeProfitLong",
enabled = FALSE
)
#Optimization----------------------------------------------------
paramset.label.name <- "MACD_OPT"
macdSignalRANGE <- seq(50,100,by=10)
#MACD Signal Distribution
add.distribution(strategy.st,
paramset.label = paramset.label.name,
component.type = 'indicator',
component.label = "macd",
variable = list( nSig = macdSignalRANGE ),
label = "macdSignalRANGE")
#Long Take Profit Optimization
add.distribution(strategy.st,
paramset.label = paramset.label.name,
component.type = "chain",
component.label = "TakeProfitLong",
variable = list( threshold = TakeProfitDistanceRANGE ),
label = "TakeProfitLONG")
#Long TP On/Off
add.distribution( strategy=strategy.st,
paramset.label = paramset.label.name,
component.type = 'chain',
component.label = 'TakeProfitLong',
variable = list(enabled = c(TRUE,FALSE)),
label = 'TPOnOff'
)
enable.rule(strategy.st,type="enter",label="longenter", enable = TRUE)
enable.rule(strategy.st,type="exit",label="longexit", enable = TRUE)
enable.rule(strategy.st,type="chain",label="TakeProfitLong", enable = TRUE)
summary(getStrategy(strategy.st))
if (Sys.info()["sysname"] == "Windows"){
library(doSNOW)
cl <- makeCluster(4)
registerDoSNOW(cl)
}
if(Sys.info()["sysname"] == "Linux") {
library(doMC)
registerDoMC(cores=4)
getDoParWorkers()
}
results <- apply.paramset(strategy.st,
paramset.label=paramset.label.name,
portfolio=strategy.st,
account=strategy.st,
nsamples=0,
verbose = TRUE,
calc = "slave")
if(Sys.info()["sysname"] == "Windows"){ snow::stopCluster(cl) }
results.df <- data.frame(results$tradeStats)
More information about the R-SIG-Finance
mailing list