[R-SIG-Finance] Custom Txnfee function in apply.paramset vs applyStrategy

Atakan Okan atakanokan at outlook.com
Mon Apr 25 18:47:31 CEST 2016


Oh sorry about that. Thanks for pointing it out Mr.Ulrich. If it did not work, I will use an gmail account  to send the code for the next try. 

Here is the code:

library(lattice);library(foreach);library(doSNOW);library(ggplot2)
library(gridExtra);library(reshape);library(beepr);library(quantstrat) 
library(doSNOW)
Sys.setenv(TZ="UTC")                                                                                 
.strategy<- new.env();.blotter<- new.env()                                                                                                                             

currency('USD')                                                                                                                                                                      
stock("AAPL", currency="USD", multiplier=1,tick_size= 0.01)

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

strategy.st <- paste("AAPL","MACD_D1",sep = "_")
rm.strat(strategy.st) 

initialEquity = 100000                                                                               
initDate = "2013-12-30" 
initPortf(strategy.st, "AAPL", 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)

customFees <- function (TxnQty, ...) 
{
  return(abs(TxnQty) * -0.01)
}
txn.model <- "customFees"    

positionSizeLong  =  1000     
positionSizeShort =  -1000     


paramset.label.name <- "SMA_OPT"
FastSMARange <- seq(5,21,by=8)
SlowSMARange <- seq(10,50,by=20)
StopLossDistanceRange <- seq(0.0025,0.005,by=0.0025)                               


add.indicator(strategy.st,                                               
              name = "SMA",                                               
              arguments = list(x=Cl(eval(parse(text = "AAPL")))
                               ,n=5   #fastsma of best combination by NetPnL
              ),                                                            
              label='fastsma')                                           

add.indicator(strategy.st,                                                 
              name = "SMA",                                                
              arguments = list(x=Cl(eval(parse(text = "AAPL")))
                               ,n=50    #slowsma of best combination by NetPnL
              ),                                                           
              label='slowsma')                                             

add.signal(strategy.st,
           name="sigCrossover",
           arguments = list(columns=c("fastsma","slowsma"),relationship="gt"),
           label="fastsma.gt.slowsma")                                                        

add.signal(strategy.st,
           name="sigCrossover",
           arguments = list(columns=c("fastsma","slowsma"),relationship="lt"),
           label="fastsma.lt.slowsma")

add.rule(strategy.st,
         name='ruleSignal',
         arguments = list(sigcol="fastsma.gt.slowsma",
                          sigval=TRUE,
                          prefer="Open", 
                          orderqty= positionSizeLong, 
                          #osFUN="osAllInLong",  
                          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="fastsma.lt.slowsma",
                          sigval=TRUE,
                          prefer="Open", 
                          orderqty='all',
                          ordertype='market',
                          orderside='long',
                          orderset='ocolong',
                          TxnFees = txn.model),
         type='exit',
         label='longexit',
         enabled=TRUE
)

# Long StopLoss Rule---------------------------------------------------------------------------
add.rule(strategy.st,name='ruleSignal',
         arguments = list( sigcol="fastsma.lt.slowsma", sigval=TRUE,
                           replace=FALSE,
                           orderside='long',
                           ordertype='stoplimit',
                           tmult=TRUE,
                           threshold=quote( longStopLossDistance ),
                           orderqty='all',
                           orderset='ocolong',
                           TxnFees = txn.model),
         type='chain', parent="longenter",
         label='StopLossLong',
         enabled=TRUE)

# Short Entry Rule--------------------------------------------------------------------
add.rule(strategy.st,
         name='ruleSignal',
         arguments = list(sigcol="fastsma.lt.slowsma",
                          sigval=TRUE,
                          prefer="Open", 
                          orderqty=positionSizeShort, 
                          #osFUN="osAllInShort",  
                          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="fastsma.gt.slowsma",
                          sigval=TRUE,
                          prefer="Open", 
                          orderqty='all',
                          ordertype='market',
                          orderside='short',
                          orderset='ocoshort',
                          TxnFees = txn.model),
         type='exit',
         label='shortexit',
         enabled=TRUE
)

# Short Stop Loss Rule-----------------------------------------------------------------
add.rule(strategy.st,name='ruleSignal',
         arguments = list( sigcol="fastsma.gt.slowsma", sigval=TRUE,
                           replace=FALSE,
                           orderside='short',
                           ordertype='stoplimit',
                           tmult=TRUE,
                           threshold=quote( shortStopLossDistance ),
                           orderqty='all',
                           orderset='ocoshort',
                           TxnFees = txn.model),
         type='chain', parent="shortenter",
         label='StopLossShort',
         enabled=TRUE)


#Indicator Optimization-------------------------------------------------------------
add.distribution(strategy.st,
                 paramset.label = paramset.label.name,
                 component.type = 'indicator',
                 component.label = "fastsma",
                 variable = list( n = FastSMARange ),               
                 label = "FastSMARANGE")


add.distribution(strategy.st,
                 paramset.label = paramset.label.name,
                 component.type = 'indicator',
                 component.label = "slowsma",
                 variable = list( n = SlowSMARange ),
                 label = "SlowSMARANGE")

add.distribution.constraint(strategy.st,
                            paramset.label = 'SMA_OPT',
                            distribution.label.1 = 'FastSMARANGE',
                            distribution.label.2 = 'SlowSMARANGE',
                            operator = '<',
                            label = 'FastSMA<- snow::makeCluster(4, type = "SOCK")
registerDoSNOW(cl)
results <- apply.paramset(strategy.st,paramset.label=paramset.label.name,
                          portfolio=strategy.st, account=strategy.st,nsamples=0,verbose = TRUE,
                          audit=paramsetenv)
snow::stopCluster(cl)
results.df <- data.frame(results$tradeStats)


#Second run applyStrategy()
longStopLossDistance <- 0.005   #SL of best combination by NetPnL
shortStopLossDistance <- 0.005  #SL of best combination by NetPnL

applyStrategy( strategy=strategy.st , portfolios=strategy.st
               #,parameters=list(n = 5)
               ,verbose=TRUE)
updatePortf(strategy.st)
updateAcct(strategy.st)
updateEndEq(strategy.st)

results.df.2 <- data.frame(tradeStats(strategy.st))

#Check NetPnL apply.paramset vs applyStrategy
results.df$Net.Trading.PL[12]  == results.df.2$Net.Trading.PL






> From: josh.m.ulrich at gmail.com
> Date: Mon, 25 Apr 2016 11:20:29 -0500
> Subject: Re: [R-SIG-Finance] Custom Txnfee function in apply.paramset vs applyStrategy
> To: atakanokan at outlook.com
> CC: r-sig-finance at r-project.org
> 
> The problem is that you're sending HTML, which the posting guide tells
> you not to do:
> https://www.r-project.org/posting-guide.html
> See also the first paragraph in the "General Instructions" section at
> the bottom of:
> https://www.r-project.org/mail.html
> 
> On Mon, Apr 25, 2016 at 10:43 AM, Atakan Okan  wrote:
>> So the code seems a bit odd, the indentation must have somehow caused some problems when emailing.
>> This a better and a little bit more compact one:
> 
>> From: atakanokan at outlook.com
>> To: r-sig-finance at r-project.org
>> Subject: Custom Txnfee function in apply.paramset vs applyStrategy
>> Date: Sun, 24 Apr 2016 21:36:33 +0300
>>
>>
>>
>>
>> Hi,
>> I have been experimenting with different custom transaction fee models for different assets and I realized that apply.paramset and applyStrategy yields different results when a custom transaction fee function is used.
>> The reproducible example is below, and even though apply.paramset yields a NetPnL result of 21779 with the custom transaction fee model, applyStrategy yields 21509, which makes me believe that apply.paramset somehow is not incorporating the fees. The comparison of NetPnL results were made with the parameter combination of FastSMA=5, SlowSMA=50 and Stoploss=0.005.
>> Any help is appreciated, thanks.
>> Atakan Okan
>> Code:
> 
>> [[alternative HTML version deleted]]
>>
> ^^^ There's your problem.
> 
>> _______________________________________________
>> 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
 		 	   		  
	[[alternative HTML version deleted]]



More information about the R-SIG-Finance mailing list