R - Optimization results don't match - Matlab, R project and Python | futures io social day trading
futures io futures trading


R - Optimization results don't match
Updated: Views / Replies:521 / 1
Created: by Eric Hung Attachments:0

Welcome to futures io.

(If you already have an account, login at the top of the page)

futures io is the largest futures trading community on the planet, with over 90,000 members. At futures io, our goal has always been and always will be to create a friendly, positive, forward-thinking community where members can openly share and discuss everything the world of trading has to offer. The community is one of the friendliest you will find on any subject, with members going out of their way to help others. Some of the primary differences between futures io and other trading sites revolve around the standards of our community. Those standards include a code of conduct for our members, as well as extremely high standards that govern which partners we do business with, and which products or services we recommend to our members.

At futures io, our focus is on quality education. No hype, gimmicks, or secret sauce. The truth is: trading is hard. To succeed, you need to surround yourself with the right support system, educational content, and trading mentors Ė all of which you can find on futures io, utilizing our social trading environment.

With futures io, you can find honest trading reviews on brokers, trading rooms, indicator packages, trading strategies, and much more. Our trading review process is highly moderated to ensure that only genuine users are allowed, so you donít need to worry about fake reviews.

We are fundamentally different than most other trading sites:
  • We are here to help. Just let us know what you need.
  • We work extremely hard to keep things positive in our community.
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts.
  • We firmly believe in and encourage sharing. The holy grail is within you, we can help you find it.
  • We expect our members to participate and become a part of the community. Help yourself by helping others.

You'll need to register in order to view the content of the threads and start contributing to our community.  It's free and simple.

-- Big Mike, Site Administrator

Reply
 
Thread Tools Search this Thread
 

R - Optimization results don't match

  #1 (permalink)
Trading Apprentice
No.138, Xuelin Rd., Shulin Dist., New Taipei City
 
Futures Experience: Beginner
Platform: R, C++
Favorite Futures: Stock, Futures
 
Posts: 1 since Jan 2016
Thanks: 0 given, 0 received

R - Optimization results don't match

I am running the Quantstrat package to backtest my strategy with parameter optimization on Linux.

What I do first is to set up my indicators, signals and rules in my strategy.
Then I try to optimize my parameters which have about 200,000 combos. So I randomly pick up 100 to do the optimization.

If the best combo is, for example, 2-day rsi = 15, I put the optimized parameter into backtesting again; however, I got a different statistical result from the optimized one. On the other hand, if I do the optimization with only one combo, the trade statistics outputs are the same.

Did I do anything wrong?


Many thanks in advance!



Code is here:

 
Code
###  install packages
library(doMC)
library(devtools)
library(quantstrat)
library(zoo)
library(TTR)
library(IKTrading)
library(DSTrading)
library(lattice)




###########################################
###  custom transaction costs function  ###
###########################################

###  transaction costs
###  Buying: 14.25 basis point for brokerage fee
###  Selling: 14.25 basis point for brokerage fee and 30 basis point transaction tax
buyCost <- 0.001425
sellCost <- 0.004425

### custom transaction fee function based on value of transaction
buyFee <- function(TxnQty, TxnPrice, Symbol, ...)
{
  abs(TxnQty) * TxnPrice * -buyCost
}

sellFee <- function(TxnQty, TxnPrice, Symbol, ...)
{
  abs(TxnQty) * TxnPrice * -sellCost
}

###  download stock data from yahoo
###  replace the column names of 2330.TW to tsmc
###  use splits and dividends to adjust OHLC price
tsmc <- getSymbols("2330.TW", from = "2000-01-01", to = Sys.Date(), auto.assign = FALSE)
names(tsmc) <- gsub("2330.TW", "tsmc", names(tsmc))
tsmc <- adjustOHLC(tsmc, use.Adjusted = TRUE)
head(tsmc)




############################
###  set up working env  ###
############################

###  clean .blotter env, setting up currency, time zone and stock
rm(list = ls(.blotter), envir = .blotter)
currency("TWD")
Sys.setenv(TZ = "UTC")
symbols = "tsmc"
stock(symbols, currency = "TWD", multiplier = 1)


###  before first day of the strategy
###  naming the strategy, portfolio and account
###  determing the trade size and initial equity
initDate = "1999-01-01"
strategy.st <- portfolio.st <- account.st <- "myStrat"
tradeSize <- 150000
initEq <- tradeSize*length(symbols)


###  remove the changes of former portfolio and strategy if they have
###  initialize portfolio, account and orders
###  create strategy
rm.strat(portfolio.st)
rm.strat(strategy.st)
initPortf(portfolio.st, symbols = symbols, initDate = initDate, currency = "TWD")
initAcct(account.st, portfolios = portfolio.st, initDate = initDate, currency = "TWD", initEq = initEq)
initOrders(portfolio.st, initDate = initDate)
strategy(strategy.st, store = TRUE)




#########################
###  set up strategy  ###
#########################

### apply optimized parameters
#  RSI
nRSI = 2
thresh1 = 14
thresh2 = 9

#  MA
nSMAexit = 9
nSMAfilter = 234

#  average true range
period = 10
pctATR = .02 #  2% ATR, size the position based on risk
maxPct = .04 

##############################################
###  Notice the "arguments" in indicators  ###
###  "columns/column" in signals           ###
###  or applyStrategy may go wrong         ###
###  eg, column_name.label                 ###
##############################################

###  indicators
add.indicator(strategy.st, name = "lagATR",
              arguments = list(HLC=quote(HLC(mktdata)), n=period),
              label = "atrX")

add.indicator(strategy.st, name = "RSI",
              arguments = list(price=quote(Cl(mktdata)), n=nRSI),
              label = "rsi")

add.indicator(strategy.st, name = "SMA",
              arguments = list(x=quote(Cl(mktdata)), n=nSMAexit),
              label = "quickMA")

add.indicator(strategy.st, name = "SMA",
              arguments = list(x=quote(Cl(mktdata)), n=nSMAfilter),
              label = "filterMA")


###  signals
#  cross = FALSE, for current signal
#  cross = TRUE, for both previous day and current signal
add.signal(strategy.st, name = "sigThreshold",
           arguments = list(column = "rsi", threshold = thresh1,
                            relationship = "lt", cross = TRUE),
           label = "rsiThresh1")

#  first time enter long
add.signal(strategy.st, name = "sigAND",
           arguments = list(columns = c("rsiThresh1", "atr.atrX")),
           label = "longEntry1")

add.signal(strategy.st, name = "sigThreshold",
           arguments = list(column = "rsi", threshold = thresh2,
                            relationship = "lt", cross = TRUE),
           label = "rsiThresh2")

#  second time enter long
add.signal(strategy.st, name = "sigAND",
           arguments = list(columns = c("rsiThresh2", "atr.atrX")),
           label = "longEntry2")


#  first signal to exit
add.signal(strategy.st, name = "sigCrossover",
           arguments = list(columns = c("Close", "quickMA"), relationship = "gt"),
           label = "exitLongNormal")

#  second signal to exit
add.signal(strategy.st, name = "sigCrossover",
           arguments = list(columns = c("Close", "filterMA"), relationship = "gt"),
           label = "exitLongFilter1")

#  third signal to exit
add.signal(strategy.st, name = "sigCrossover",
           arguments = list(columns = c("Close", "filterMA"), relationship = "lt"),
           label = "exitLongFilter2")


###  rules
add.rule(strategy.st, name = "ruleSignal",
         arguments = list(sigcol = "longEntry1",
                          sigval = TRUE,
                          ordertype = "market",
                          orderside = "long",
                          replace = FALSE, #  replace = TRUE may choose just one rule
                          prefer = "Open", #  tomorrow because today has closed
                          osFUN = osDollarATR, #  order size function
                          tradeSize = tradeSize,
                          pctATR = pctATR,
                          maxPctATR = pctATR, #  set an upper limit of orders
                          atrMod = "X",
                          TxnFees = "buyFee"), #  atrx above
         type = "enter", path.dep = TRUE, label = "enterLong1")

add.rule(strategy.st, name = "ruleSignal",
         arguments = list(sigcol = "longEntry2",
                          sigval = TRUE,
                          ordertype = "market",
                          orderside = "long",
                          replace = FALSE, #  replace = TRUE may choose just one rule
                          prefer = "Open", #  tomorrow because today has closed
                          osFUN = osDollarATR, #  order size function
                          tradeSize = tradeSize,
                          pctATR = pctATR,
                          maxPctATR = maxPct, #  set an upper limit of orders
                          atrMod = "X",
                          TxnFees = "buyFee"), #  atrx above
         type = "enter", path.dep = TRUE, label = "enterLong2")

#  exit rules
add.rule(strategy.st, name = "ruleSignal",
         arguments = list(sigcol = "exitLongNormal",
                          sigval = TRUE,
                          orderqty = "all", #  order quantity, in all and out all
                          ordertype = "market",
                          orderside = "long",
                          replace = FALSE,
                          prefer = "Open",
                          TxnFees = "sellFee",
                          orderset = 'ocolong'),
         type = "exit", path.dep = TRUE, label = "normalExitLong")

add.rule(strategy.st, name = "ruleSignal",
         arguments = list(sigcol = "exitLongFilter1",
                          sigval = TRUE,
                          orderqty = "all",
                          ordertype = "market",
                          orderside = "long",
                          replace = FALSE,
                          prefer = "Open",
                          TxnFees = "sellFee",
                          orderset = 'ocolong'),
         type = "exit", path.dep = TRUE, label = "filterExitLong1")

add.rule(strategy.st, name = "ruleSignal",
         arguments = list(sigcol = "exitLongFilter2",
                          sigval = TRUE,
                          orderqty = "all",
                          ordertype = "market",
                          orderside = "long",
                          replace = FALSE,
                          prefer = "Open",
                          TxnFees = "sellFee",
                          orderset = 'ocolong'),
         type = "exit", path.dep = TRUE, label = "filterExitLong2")

add.rule(strategy.st, name='ruleSignal', 
         arguments = list(sigcol="longEntry1",
                          sigval=TRUE, 
                          orderqty="all", 
                          ordertype='stoplimit', 
                          orderside='long',
                          prefer = "Open",
                          threshold=0.3, 
                          tmult=TRUE,
                          TxnFees = "sellFee",
                          orderset = 'ocolong'), 
         type='chain', parent = 'enterLong1', 
         path.dep = TRUE, label = "stopLossExit1")

#  stop loss
add.rule(strategy.st, name='ruleSignal', 
         arguments = list(sigcol="longEntry2",
                          sigval=TRUE, 
                          orderqty="all", 
                          ordertype='stoplimit', 
                          orderside='long', 
                          threshold=0.3,
                          prefer = "Open",
                          tmult=TRUE,
                          TxnFees = "sellFee",
                          orderset = 'ocolong'), 
         type='chain', parent = 'enterLong2', 
         path.dep = TRUE, label = "stopLossExit2")


###  apply strategy
t1 <- Sys.time()
out <- applyStrategy(strategy = strategy.st, portfolios = portfolio.st)
t2 <- Sys.time()
print(t2-t1)


###  set up analytics
updatePortf(portfolio.st)

tradeDetails <- getPortfolio(portfolio.st)
posPL <- tradeDetails$symbols$tsmc$posPL

dateRange <- time(tradeDetails$summary)[-1]
updateAcct(portfolio.st, dateRange)
updateEndEq(account.st)



##########################
###  trade statistics  ###
##########################

tStats <- tradeStats(Portfolios = portfolio.st, use = "trades", inclZeroDays = FALSE)
tStats[, 4:ncol(tStats)] <- round(tStats[, 4:ncol(tStats)], 2)
View(data.frame(t(tStats))) #  general output




######################
###  Optimization  ###
######################

###  add distribution and distribution constraints
#  rsi
add.distribution(strategy.st,
                 paramset.label = 'allParam',
                 component.type = 'signal',
                 component.label = 'rsiThresh1',
                 variable = list(threshold = 2:15),
                 label = 'up.rsi'
)

add.distribution(strategy.st,
                 paramset.label = 'allParam',
                 component.type = 'signal',
                 component.label = 'rsiThresh2',
                 variable = list(threshold = 2:15),
                 label = 'dn.rsi'
)

add.distribution.constraint(strategy.st,
                            paramset.label = 'allParam',
                            distribution.label.1 = 'up.rsi',
                            distribution.label.2 = 'dn.rsi',
                            operator = '>',
                            label = 'RSI'
)

#  MA
add.distribution(strategy.st,
                 paramset.label = 'allParam',
                 component.type = 'indicator',
                 component.label = 'quickMA',
                 variable = list(threshold = 5:15),
                 label = 'quickMA'
)

add.distribution(strategy.st,
                 paramset.label = 'allParam',
                 component.type = 'indicator',
                 component.label = 'filterMA',
                 variable = list(threshold = 220:260),
                 label = 'filterMA'
)

#  stop loss rate
stopPct = seq(0.05, 0.3, 0.05)
add.distribution(strategy.st,
                 paramset.label = 'allParam',
                 component.type = 'chain',
                 component.label = 'stopLossExit1',
                 variable = list(threshold = stopPct),
                 label = 'stopLossLong1'
)

add.distribution(strategy.st,
                 paramset.label = 'allParam',
                 component.type = 'chain',
                 component.label = 'stopLossExit2',
                 variable = list(threshold = stopPct),
                 label = 'stopLossLong2'
)

add.distribution.constraint(strategy.st,
                            paramset.label = 'allParam',
                            distribution.label.1 = 'stopLossLong1',
                            distribution.label.2 = 'stopLossLong2',
                            operator = '==',
                            label = 'stopLoss'
)

###  optimize all parameters above
registerDoMC(cores=2)
results <- apply.paramset(strategy.st,
                          account.st,
                          paramset.label='allParam',
                          portfolio.st=portfolio.st,
                          nsamples = 100)

tS <- results$tradeStats
#View(tS)
idx <- order(tS$Net.Trading.PL, decreasing = TRUE)
tS <- tS[idx,]
View(t(tS)[,1:3]) #  optimized output

Reply With Quote
 
  #2 (permalink)
Quick Summary
Quick Summary Post

Quick Summary is created and edited by users like you... Add FAQ's, Links and other Relevant Information by clicking the edit button in the lower right hand corner of this message.


Reply



futures io > > > > R - Optimization results don't match

Thread Tools Search this Thread
Search this Thread:

Advanced Search



Upcoming Webinars and Events (4:30PM ET unless noted)

Jigsaw Trading: TBA

Elite only

FuturesTrader71: TBA

Elite only

NinjaTrader: TBA

Jan 18

RandBots: TBA

Jan 23

GFF Brokers & CME Group: Futures & Bitcoin

Elite only

Adam Grimes: TBA

Elite only

Ran Aroussi: TBA

Elite only
     

Similar Threads
Thread Thread Starter Forum Replies Last Post
renko backtest/optimization results magicly changing... Momozo NinjaTrader 5 December 14th, 2015 11:12 AM
No Optimization Results? NJAMC NinjaTrader 8 July 5th, 2013 05:21 PM
Strategy Optimization and trusting the results petronick Elite Automated NinjaTrader Trading 11 July 31st, 2011 02:50 AM
Same Feed on Diff Platforms: Charts Don't Match??? tdaman Reviews of Brokers and Data Feeds 4 May 17th, 2011 01:43 PM
When not to trust optimization results? molecool Traders Hideout 10 September 6th, 2010 06:39 PM


All times are GMT -4. The time now is 10:57 AM.

Copyright © 2017 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, +507 833-9432, info@futures.io
All information is for educational use only and is not investment advice.
There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
no new posts
Page generated 2017-12-18 in 0.08 seconds with 19 queries on phoenix via your IP 54.226.113.250