Skip to content
Prev 13754 / 15274 Next

Time in Force conditions with Quantstrat

On Sun, Feb 28, 2016 at 9:10 PM, Ryan <suchislife27 at gmail.com> wrote:
The error is because you included `timestamp` as one of the arguments
to `ruleSignal`. `timestamp` is a function in the utils package, so
you're essentially passing a function via '...' which is causing
problems.  I'm not sure why this is causing this particular issue, but
the fix is "don't do that."

You also specified the time.in.force argument incorrectly.  It's
supposed to be either: a timestamp (e.g. a Date or POSIXct object), a
number of seconds, or "GTC".  You specified time.in.force="172800",
which is a character string.
<snip>

Here's a modified version of your example that works for me using the
latest development versions of quantstrat and xts.

require(quantstrat)
require(IKTrading)

# Set the currency and the timezone
currency('USD')
Sys.setenv(TZ = "UTC")

# Define symbols of interest
symbols <- c("AMP.AX", "BHP.AX", "ANZ.AX",
   "CBA.AX", "BXB.AX", "CSL.AX", "IAG.AX",
   "MQG.AX", "NAB.AX", "ORG.AX", "QBE.AX",
   "RIO.AX", "SCG.AX", "SUN.AX", "TLS.AX",
   "WBC.AX", "WES.AX", "WOW.AX", "WPL.AX",
   "TCL.AX", "WFD.AX", "AMC.AX")

#Get Symbols
getSymbols(Symbols=symbols, from="2010-01-01", to="2015-12-31")

# Define the instrument type
stock(symbols, currency = "USD", multiplier = 1)

#Boilerplate
from = "2010-01-01"
to = "2015-12-31"

#trade sizing and initial equity settings
tradeSize <- 2500
initEq <- 100000

strategy.st <- portfolio.st <- account.st <- "Timeinforce"
rm.strat(portfolio.st)
rm.strat(strategy.st)
initAcct(account.st, portfolio.st, currency='USD', initEq=initEq)
initPortf(portfolio.st, symbols, currency='USD')
initOrders(portfolio.st)
strategy(strategy.st, store=TRUE)

#Add Indicators
add.indicator(strategy.st, name="SMA",
              arguments=list(x=quote(Cl(mktdata)), n=20),
              label="sma")

#Add Entry and Exit Signals
add.signal(strategy.st, name="sigComparison",
  arguments=list(columns=c("Close", "SMA.sma"),
                 relationship="gt"),
  label="longentry")

#enter signal rule
add.rule(strategy.st, name="ruleSignal",
  arguments=list(sigcol="longentry",
                 sigval=TRUE,
                 ordertype="limit",
                 orderside="long",
                 replace=TRUE,
                 prefer="Close",
                 tmult = TRUE,
                 threshold = 0.05,
                 time.in.force=172800,
                 orderqty=tradeSize,
                 osFUN=osMaxDollar,
                 tradeSize=tradeSize,
                 maxSize=tradeSize),
  type="enter",
  path.dep=TRUE,
  label="enterlong")

#stop loss.
add.rule(strategy.st, name="ruleSignal",
  arguments=list(sigcol="longentry",
                 sigval=TRUE,
                 ordertype="stoptrailing",
                 orderside="long",
                 replace=FALSE,
                 orderqty="all",
                 threshold=0.05,
                 tmult=TRUE,
                 orderset="ocolong"),
  type="chain",
  parent="enterlong",
  label="stopLossLong",
  path.dep=TRUE,
  enable=TRUE)

#apply strategy
out2 <- applyStrategy(strategy=strategy.st,portfolios=portfolio.st )
Message-ID: <CAPPM_gTenMQX8F6CQtrAgEiepU=dZMfFicT4=xDShJF8HwOrpw@mail.gmail.com>
In-Reply-To: <CANf4qfjuqJ2Ps1+nwd+=hZXUbMpY2oNL_SnkiHxucderLXy_mw@mail.gmail.com>