[R-SIG-Finance] apply.paramset trade-by-trade PnL data

Jersey Fanatic jerseyfanatic1 at gmail.com
Tue Feb 2 15:29:53 CET 2016


Reproducible code:

library(lattice);library(foreach);library(doSNOW);library(ggplot2)
library(gridExtra);library(reshape);library(beepr);library(quantstrat)

Sys.setenv(TZ="UTC")

.strategy<- new.env();.blotter<- new.env()



currency('USD')
symbol.name = "AAPL"


stock(symbol.name, currency="USD", multiplier=1,tick_size= 0.01)

initialEquity = 100000

strategy.keyword = "MACD_D1"

constantTxnFee = 0

getSymbols('AAPL',src = 'yahoo', from="2014-01-01", to="2015-05-31")
AAPL <- adjustOHLC(AAPL)

strategy.st <- paste(symbol.name,strategy.keyword,sep = "_")
rm.strat(strategy.st)

initDate = "2013-12-30"
initPortf(strategy.st, symbol.name, initDate=initDate, currency = "USD")
initAcct(strategy.st, portfolios=strategy.st, initDate=initDate,
initEq=initialEquity, currency = "USD")
initOrders(portfolio=strategy.st,initDate=initDate)
strategy(strategy.st,store=TRUE)

txn.model <- constantTxnFee

macdFastMARange <- seq(5,35,by=10)
macdSlowMARange <- seq(2,62,by=20)
macdSignalRange <- seq(2,26,by=6)
strategy.indicator.name <- "MACD"

strategy.name <- strategy.st
add.indicator(strategy.name,
              name = strategy.indicator.name,
              arguments = list(x=Cl(eval(parse(text = symbol.name)))),
              label='macd')

add.signal(strategy.name,name="sigCrossover",
           arguments =
list(columns=c("macd.macd","signal.macd"),relationship="gt"),
           label="macd.gt.signal")
add.signal(strategy.name,name="sigCrossover",
           arguments =
list(columns=c("macd.macd","signal.macd"),relationship="lt"),
           label="macd.lt.signal")

add.rule(strategy.name,
         name='ruleSignal', arguments = list(sigcol="macd.gt.signal",
sigval=TRUE,  prefer="Open",
                          orderqty= 1000,  ordertype='market',
orderside='long',
                          orderset='ocolong',  TxnFees = txn.model),
         type='enter', label='longenter', enabled=FALSE)
add.rule(strategy.name,
         name='ruleSignal', arguments = list(sigcol="macd.lt.signal",
sigval=TRUE, prefer="Open",  orderqty='all',
                          ordertype='market', orderside='long',
orderset='ocolong', TxnFees = txn.model),
         type='exit', label='longexit', enabled=FALSE)
add.rule(strategy.name, name='ruleSignal', arguments =
list(sigcol="macd.lt.signal", sigval=TRUE,
                          prefer="Open",  orderqty=1000,
 ordertype='market',
                          orderside='short', orderset='ocoshort', TxnFees =
txn.model),
         type='enter', label='shortenter', enabled=FALSE)
add.rule(strategy.name, name='ruleSignal', arguments =
list(sigcol="macd.gt.signal",  sigval=TRUE,
                          prefer="Open",  orderqty='all',
ordertype='market',  orderside='short',
                          orderset='ocoshort',  TxnFees = txn.model),
type='exit', label='shortexit', enabled=FALSE)

add.distribution(strategy.name, paramset.label = "MACD_OPT", component.type
= 'indicator',
                 component.label = "macd", variable = list( nFast =
macdFastMARange ), label = "macdFastMARANGE")
add.distribution(strategy.name, paramset.label = "MACD_OPT", component.type
= 'indicator',
                 component.label = "macd", variable = list( nSlow =
macdSlowMARange ), label = "macdSlowMARANGE")
add.distribution(strategy.name, paramset.label = "MACD_OPT", component.type
= 'indicator',
                 component.label = "macd", variable = list( nSig =
macdSignalRange ), label = "macdSignalRANGE")
add.distribution.constraint(strategy.name, paramset.label = 'MACD_OPT',
                            distribution.label.1 = 'macdFastMARANGE',
distribution.label.2 = 'macdSlowMARANGE',
                            operator = '<', label = 'FastMA<SlowMA')

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="enter",label="shortenter", enable = TRUE)
enable.rule(strategy.st,type="exit",label="shortexit", enable = TRUE)

results <- apply.paramset(strategy.st,paramset.label = "MACD_OPT",
                          portfolio=strategy.st, account=strategy.st,
nsamples=0,verbose = FALSE)


2016-02-01 12:43 GMT+02:00 Jersey Fanatic <jerseyfanatic1 at gmail.com>:

> Transaction table from applyStrategy() lists transaction dates and PnL in
> trade-by-trade basis. However, after executing apply.paramset() on a
> distribution of parameters, same trade-by-trade PnL statistics is not
> retrievable via getTxns(). Is there a way to access trade-by-trade PnL data
> results from *apply.paramset()* for each combination tried in the
> optimization range? My end goal is to construct a time-aligned data table
> with hourly/daily/weekly PnL for each combination of parameters.
>
>
> 2016-02-01 9:12 GMT+02:00 Jersey Fanatic <jerseyfanatic1 at gmail.com>:
>
>> Transaction table from applyStrategy() lists transaction dates and PnL in
>> trade-by-trade basis. However, after executing apply.paramset() on a
>> distribution of parameters, same trade-by-trade PnL statistics is not
>> retrievable via getTxns(). Is there a way to access trade-by-trade PnL data
>> results from applyStrategy for each combination tried in the optimization
>> range? My end goal is to construct a time-aligned data table with
>> hourly/daily/weekly PnL for each combination of parameters.
>>
>> Thanks for the help in advance.
>>
>
>

	[[alternative HTML version deleted]]



More information about the R-SIG-Finance mailing list