Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apply.paramset() No transactions returned for param.combo 1 out of 4 #148

Open
maxsach opened this issue Sep 13, 2022 · 1 comment
Open

Comments

@maxsach
Copy link

maxsach commented Sep 13, 2022

After i tried everything to make the optimization work with TTR:KST() I still couldnt figuere it out. So I created a function for KST myself with multiple n variables. It works just like the TTR::KST() when I just run the strategy but if I try to optimize it via apply.paramset it tells me:

[1] "No transactions returned for param.combo 1 out of 4"
[1] "No transactions returned for param.combo 2 out of 4"
[1] "No transactions returned for param.combo 3 out of 4"
[1] "No transactions returned for param.combo 4 out of 4"

It feels like it is just not meant to be

Any help will be appreciated.

my KST function:

kst_noob<-function (price, 
                    n1=10,
                    n2=10,
                    n3=10,
                    n4=15, 
                    nROC1=10,
                    nROC2=15,
                    nROC3=20,
                    nROC4=30, 
                    nSig = 9, 
                    wts = 1:4) 
{
  roc1<-ROC(price,nROC1 )
  roc2<-ROC(price,nROC2 )
  roc3<-ROC(price,nROC3 )
  roc4<-ROC(price,nROC4 )
  sma1<-SMA(roc1,n1)
  sma2<-SMA(roc2,n2)
  sma3<-SMA(roc3,n3)
  sma4<-SMA(roc4,n4)
  kst_line<-sma1*1+sma2*2+sma3*3+sma4*4
  signal<-SMA(kst_line,nSig)
  result <- cbind(kst_line, signal)*100
  colnames(result) <- c("kstt", "signall")
  as.matrix.data.frame(return(result))
}

my main strategy code:

library(quantstrat)
library(dplyr)
#library(data.table)
library(DT)
library(ggplot2)
library(htmltools)
library(htmlwidgets)
library(knitr)
library(lattice)
library(pander)
#kernvariablen
init_date<-"2009-12-31"
start_date<-"2010-01-01"
end_date<-"2017-12-31"
initEq<-10000
currency("USD")
adjustment<-TRUE
currency("USD")
Sys.setenv(TZ = "UTC")
basic_symbols<-function(){
  symbols<-c(
    #"FUTY",
    "XLY"#,
    # "VAW",
    #"PTH"
  )
}

portfolio.st<-"Port.KST"
account.st<-"Acct.KST"
strategy.st<-"Strat.KST"
#symbols<-c("XLY")
symbols<-basic_symbols()
getSymbols(Symbols = symbols,
           from= start_date,
           to= end_date,
           index.class="POSIXct")
stock(symbols,
      currency = "USD",
      multiplier = 1)
rm.strat(portfolio.st)
rm.strat(account.st)
rm.strat(strategy.st)
initPortf(name = portfolio.st,
          symbols = symbols,
          initDate = init_date)
initAcct(name = account.st,
         portfolios = portfolio.st,
         initDate = init_date,
         initEq = initEq)
initOrders(portfolio = portfolio.st,
           symbols = symbols,
           initDate = init_date)
strategy(strategy.st,
         store = TRUE)
a<-c(1,2,3,4)
i=nrow(n)
m=as.matrix(c(1,2,3,4))
m[1,]
#add.indicator(strategy = strategy.st, 
 #             name = "KST", 
  #            arguments = list(price = quote(Cl(mktdata)),
   #                            n=c(11,20,15,12),
    #                          
     #                          #n=as.matrix(c(10,10,10,15)),
      #                       #n=c(10,10,10,15),
       #                       wts=1:4),
        #    label = "kst")
add.indicator(strategy = strategy.st,
             name = "kst_noob",
              arguments = list(price = quote(Cl(mktdata)),
                               n1=15,
                               n2=13,
                              n3=11,
                               n4=14, 
                                nROC1=10,
                              nROC2=15,
                              nROC3=20,
                               nROC4=30, 
                               nSig = 9),
              label = "kstnoobb")
add.signal(strategy =strategy.st,
           name = "sigCrossover",
           arguments = list(
             columns =c("signall.kstnoobb","kstt.kstnoobb"),
             relationship="gte"),
           label = "long")
add.signal(strategy.st,
           name = "sigCrossover",
           arguments = list(columns =c("signall.kstnoobb","kstt.kstnoobb"),
                            relationship="lte"),
           label = "short")
add.rule(strategy = strategy.st,
         name = "ruleSignal",
         arguments = list(sigcol="long",
                          sigval=TRUE,
                          orderqty=100,
                          ordertype="stoplimit",
                          orderside="long",
                          threshold=0.0005,
                          prefer="High",
                          TxmFees=-10,
                          replace=FALSE),
         type = "enter",
         label = "EnterLONG")
add.rule(strategy = strategy.st,
         name = "ruleSignal",
         arguments = list(sigcol="short",
                          sigval=TRUE,
                          orderqty=-100,
                          ordertype="stoplimit",
                          orderside="short",
                          threshold=-0.005,
                          prefer="Low",
                          TxmFees=-10,
                          replace=FALSE),
         type = "enter",
         label = "EnterLONG")
KST(Cl(XLY))
add.rule(strategy = strategy.st,
         name = "ruleSignal",
         arguments = list(sigcol="short",
                          sigval=TRUE,
                          orderside="short",
                          ordertype="market",
                          orderqty="all",
                          TynFees=-10,
                          replace=TRUE),
         type = "exit",
         label = "Exit2SHORT")
add.rule(strategy = strategy.st,
         name = "ruleSignal",
         arguments = list(sigcol="long",
                          sigval=TRUE,
                          orderside="short",
                          ordertype="market",
                          orderqty="all",
                          TynFees=-10,
                          replace=TRUE),
         type = "exit",
         label = "Exit2LONG")
checkBlotterUpdate <- function(port.st = portfolio.st, 
                               account.st = account.st, 
                               verbose = TRUE) {
  
  ok <- TRUE
  p <- getPortfolio(port.st)
  a <- getAccount(account.st)
  syms <- names(p$symbols)
  port.tot <- sum(
    sapply(
      syms, 
      FUN = function(x) eval(
        parse(
          text = paste("sum(p$symbols", 
                       x, 
                       "posPL.USD$Net.Trading.PL)", 
                       sep = "$")))))
  
  port.sum.tot <- sum(p$summary$Net.Trading.PL)
  
  if(!isTRUE(all.equal(port.tot, port.sum.tot))) {
    ok <- FALSE
    if(verbose) print("portfolio P&L doesn't match sum of symbols P&L")
  }
  
  initEq <- as.numeric(first(a$summary$End.Eq))
  endEq <- as.numeric(last(a$summary$End.Eq))
  
  if(!isTRUE(all.equal(port.tot, endEq - initEq)) ) {
    ok <- FALSE
    if(verbose) print("portfolio P&L doesn't match account P&L")
  }
  
  if(sum(duplicated(index(p$summary)))) {
    ok <- FALSE
    if(verbose)print("duplicate timestamps in portfolio summary")
    
  }
  
  if(sum(duplicated(index(a$summary)))) {
    ok <- FALSE
    if(verbose) print("duplicate timestamps in account summary")
  }
  return(ok)
}
cwd <- getwd()
results_file <- paste("results", strategy.st, "RData", sep = ".")
if( file.exists(results_file) ) {
  load(results_file)
} else {
  results <- applyStrategy(strategy.st, portfolios = portfolio.st)
  updatePortf(portfolio.st)
  updateAcct(account.st)
  updateEndEq(account.st)
  if(checkBlotterUpdate(portfolio.st, account.st, verbose = TRUE)) {
    save(list = "results", file = results_file)
    save.strategy(strategy.st)
  }
}
setwd(cwd)
applyStrategy(strategy.st, portfolios = portfolio.st)
updatePortf(portfolio.st)
updateAcct(account.st)
updateEndEq(account.st)
tstats <- tradeStats(portfolio.st)
kable(t(tstats))
chart.Posn(portfolio.st)

And my optimization code:

.kstDistr2 <- matrix(runif(16,10,16),4,4)%>%round()
library(parallel)
if( Sys.info()['sysname'] == "Windows") {
  library(doParallel)
  registerDoParallel(cores=detectCores())
} else {
  library(doMC)
  registerDoMC(cores=detectCores())
}
#Add Distribution
nn1<-c(10:20)
nn2<-10:15
nn3<-10:15
nn4<-10:15

#delete.paramset(strategy.st,"KST6")
add.distribution(strategy.st,
                 paramset.label = "KST6",
                 component.type = "indicator",
                 component.label = "kstnoobb",
                 variable = list(n1 = .kstDistr2),
                 label = ".n1")





library(parallel)

if( Sys.info()['sysname'] == "Windows") {
  library(doParallel)
  registerDoParallel(cores=detectCores())
} else {
  library(doMC)
  registerDoMC(cores=detectCores())
}



set.seed(20201312)
.nsamples <-4
rm(results)
results <- apply.paramset(strategy.st,
                          paramset.label = "KST6",
                          portfolio.st = portfolio.st,
                          account.st = account.st,
                          #nsamples = .nsamples,
                          paramsets = .kstDistr2,
                          verbose = TRUE)
closeAllConnections()

unregister_dopar()
@jaymon0703
Copy link
Collaborator

Hi @maxsach i am not exactly sure why (and i dont have the time right now to take much more of a closer look) but i get results when running your code with registerDoSEQ().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants