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 returns zero #142

Closed
JohnLee987 opened this issue Jul 26, 2021 · 1 comment
Closed

apply.paramset returns zero #142

JohnLee987 opened this issue Jul 26, 2021 · 1 comment

Comments

@JohnLee987
Copy link

JohnLee987 commented Jul 26, 2021

Hi, I am replicating the reply given by FXQuantTrader in https://stackoverflow.com/questions/48595452/parameter-optimization-with-custom-indicators-quantstrat regarding using apply.paramset. I noticed that currently the final results i.e. resultsopt$tradeStats are all zero. Given the reply has been posted almost 3.5 years ago, has there been any update causing this? I am posting the code below in case needed. FYI, I also tried to use doParallel and registerDoParallel before running apply.paramset (as replied in another open issue here) with no success.

library(quantstrat)
.blotter <<- new.env()
.strategy <<- new.env()  

Sys.setenv(TZ = "UTC")

currency(c('USD'))

symbols <- "AAPL"
getSymbols(symbols)

stock(symbols, currency = "USD")


AAPL <- AAPL["2016/"]

init_date <- "2017-01-07"
start_date <- "2018-01-31"
end_date <- "2018-01-31"
init_equity <- 1e8 # $100,000,000
adjustment <- FALSE


portfolio.st <- "Luxor.Opt"  
account.st <- "Luxor.Opt"
strategy.st <- "Luxor.Opt"

rm.strat(name = portfolio.st)   

initPortf(name = portfolio.st, 
          symbols = symbols)

initAcct(name = account.st,
         portfolios = portfolio.st,
         initEq = init_equity)

initOrders(portfolio = portfolio.st,
           symbols = symbols)

strategy(strategy.st, store = TRUE) 


fastMA_custom2 = 12

slowMA_custom2 = 26

signalMA_custom2 = 9
maType="EMA"
MAforest = 3

forest <- function(x, fastMA_custom, slowMA_custom, signalMA_custom){
  step1 <- EMA(x,fastMA_custom)
  step2 <- EMA(x,slowMA_custom)
  step3 <- step1-step2
  step4 <- EMA(step3,signalMA_custom)
  step5 <- step3-step4
  return(step5)
}

smaforest <- function(x){
  step1 <- EMA(x,fastMA_custom2)
  step2 <- EMA(x,slowMA_custom2)
  step3 <- step1-step2
  step4 <- EMA(step3,signalMA_custom2)
  step5 <- step3-step4
  step6 <- EMA(step5,MAforest)
  return(step6)
}

add.indicator(strategy = strategy.st,
              name ="forest",
              arguments = list(x=quote(Cl(mktdata)),
                               fastMA_custom = 12,
                               slowMA_custom = 26,
                               signalMA_custom = 9
              ),
              label="forest")

add.indicator(strategy=strategy.st,
              name ="smaforest",
              arguments = list(x=quote(Cl(mktdata))),
              label="smaforest")


add.signal(strategy = strategy.st,
           name="sigCrossover",
           arguments = list(columns = c("forest", "smaforest"),
                            relationship = "gte"),
           label = "long")

add.signal(strategy = strategy.st,
           name="sigCrossover",
           arguments = list(columns = c("forest", "smaforest"),
                            relationship = "lte"),
           label = "short")

add.rule(strategy.st,
         name = "ruleSignal",
         arguments = list(sigcol = "long",
                          sigval = TRUE,
                          orderqty = 100000,
                          ordertype = "market",
                          orderside = "long", 
                          TxnFees = -1, 
                          replace = FALSE),
         type = "enter",
         label = "EnterLONG")

add.rule(strategy.st,
         name = "ruleSignal",
         arguments = list(sigcol = "short",
                          sigval = TRUE,
                          orderqty = -100000,
                          ordertype = "market",
                          orderside = "short", 
                          replace = FALSE, 
                          TxnFees = -1
         ),
         type = "enter",
         label = "EnterSHORT")

add.rule(strategy.st, 
         name = "ruleSignal", 
         arguments = list(sigcol = "short", 
                          sigval = TRUE, 
                          orderside = "long", 
                          ordertype = "market", 
                          orderqty = "all", 
                          TxnFees = -1, 
                          replace = TRUE), 
         type = "exit", 
         label = "Exit2SHORT")


add.rule(strategy.st, 
         name = "ruleSignal", 
         arguments = list(sigcol = "long", 
                          sigval = TRUE, 
                          orderside = "short", 
                          ordertype = "market", 
                          orderqty = "all", 
                          TxnFees = -1, 
                          replace = TRUE), 
         type = "exit", 
         label = "Exit2LONG")

addPosLimit(portfolio.st, symbols[], timestamp=init_date, maxpos=500, minpos=0)

# applyStrategy(strategy.st, portfolio.st)
# 
# updatePortf(portfolio.st)
# tradeStats(portfolio.st, symbols)

add.distribution(strategy.st,
                 paramset.label = "forestopt",  #The label we will use when we want to run this optimisation in paramset
                 component.type = "indicator", # The custom function is of indicator type (not other alternatives including signal or rule)
                 component.label = "forest", #this is the name of your custom function
                 variable = list(fastMA_custom = seq(8, 12, by = 2)),
                 label = "myForestOptLabel") #choose whatever you want



resultsopt <- apply.paramset(strategy.st,
                             paramset.label = "forestopt",
                             portfolio.st = portfolio.st,
                             account.st = account.st, 
                             nsamples = 0)

# Check, results are equal in the case where fastMA_custom = 12 compared to when running `applyStrategy`:

resultsopt$tradeStats

myForestOptLabel         Portfolio Symbol Num.Txns Num.Trades Total.Net.Profit Avg.Trade.PL Med.Trade.PL Largest.Winner
1                8 Luxor.Opt.train.1      0        0          0                0            0            0              0
2               10 Luxor.Opt.train.2      0        0          0                0            0            0              0
3               12 Luxor.Opt.train.3      0        0          0                0            0            0              0
  Largest.Loser Gross.Profits Gross.Losses Std.Dev.Trade.PL Std.Err.Trade.PL Percent.Positive Percent.Negative Profit.Factor
1             0             0            0                0                0                0                0             0
2             0             0            0                0                0                0                0             0
3             0             0            0                0                0                0                0             0
  Avg.Win.Trade Med.Win.Trade Avg.Losing.Trade Med.Losing.Trade Avg.Daily.PL Med.Daily.PL Std.Dev.Daily.PL Std.Err.Daily.PL
1             0             0                0                0            0            0                0                0
2             0             0                0                0            0            0                0                0
3             0             0                0                0            0            0                0                0
  Ann.Sharpe Max.Drawdown Profit.To.Max.Draw Avg.WinLoss.Ratio Med.WinLoss.Ratio Max.Equity Min.Equity End.Equity
1          0            0                  0                 0                 0          0          0          0
2          0            0                  0                 0                 0          0          0          0
3          0            0                  0                 0                 0          0          0          0
@jaymon0703
Copy link
Collaborator

Hi @JohnLee987 did you resolve this yourself? Sorry i had not taken a look yet.

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