apply.paramset trade-by-trade PnL data

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

apply.paramset trade-by-trade PnL data

Jersey Fanatic
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]]

_______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: apply.paramset trade-by-trade PnL data

Jersey Fanatic
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 <[hidden email]>:

> 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]]

_______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: apply.paramset trade-by-trade PnL data

Jersey Fanatic
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 <[hidden email]>:

> 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 <[hidden email]>:
>
>> 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]]

_______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: apply.paramset trade-by-trade PnL data

braverock
On Tue, 2016-02-02 at 16:29 +0200, Jersey Fanatic wrote:
> results <- apply.paramset(strategy.st,paramset.label = "MACD_OPT",
>                           portfolio=strategy.st, account=strategy.st,
>                           nsamples=0,verbose = FALSE)
>
Your problem is here.  You're not setting an 'audit' environment.


paramsetenv<-new.env()

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

ls(pos=paramsetenv)

#note that paramsetenv will have the portfolio for each parameter set.
#results$tradeStats will give you summary statistics for each paramset.

# see: http://www.rinfinance.com/agenda/2013/workshop/Humme+Peterson.pdf
# p.42-43 on paramsets,
# and p.62-67 on walk forward testing, which uses the audit environment


>
> 2016-02-01 12:43 GMT+02:00 Jersey Fanatic <[hidden email]>:
>
> > 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 <[hidden email]>: > >> 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. >> > >

_______________________________________________
[hidden email] 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.