[R-SIG-Finance] Time in Force conditions with Quantstrat

Joshua Ulrich josh.m.ulrich at gmail.com
Sat Mar 12 17:19:13 CET 2016


On Sun, Feb 28, 2016 at 9:10 PM, Ryan <suchislife27 at gmail.com> wrote:
> I'm having some difficulty getting time in force to work with quanstrat.
>
> I've studied the documentation and it states that:
>
> timestamp time-in-force; either a time stamp, or a number of seconds, or
> 'GTC' / ”, 'GTC' and ” both meaning 'Good Till Canceled'; order expires if
> still 'open' at this timestamp, default is ”
>
> I have been trying to enter a limit order, 5% below the Close price, as
> when I study the orderbook and mktdata object, it appears that a limit
> order is based on the bar in which the signal occurs. Hence the limit price
> would be 5% below the Close.
>
> I've tried to enter a time in seconds, which is the equivalent to two days,
> for the sake of the example. If I remove timestamp it runs however
> time.in.force remains blank in the orderbook as I would expect timestamp +
> time.in.force is the time when the order is cancelled.
>
> As laid out below I get the following error:
>
> Error in if (prefer == "Close") { : argument is of length zero
>
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.

>
> Below is a reproducible example that produces the error. Any assistance
> would be greatly appreciated.
>
<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 )


-- 
Joshua Ulrich  |  about.me/joshuaulrich
FOSS Trading  |  www.fosstrading.com
R/Finance 2016 | www.rinfinance.com



More information about the R-SIG-Finance mailing list