[R-SIG-Finance] Time in Force conditions with Quantstrat
Ryan
suchislife27 at gmail.com
Mon Feb 29 04:10:11 CET 2016
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
Below is a reproducible example that produces the error. Any assistance
would be greatly appreciated.
require(quantstrat)
require(quantmod)
require(PerformanceAnalytics)
require(TTR)
require(blotter)
require(IKTrading)
# Suppresses warnings
options("getSymbols.warning4.0" = FALSE)
# 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
initDate = "2005-01-01"
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)
initPortf(portfolio.st, symbols=symbols, initDate=initDate, currency='USD')
initAcct(account.st, portfolios=portfolio.st, initDate=initDate,
currency='USD',initEq=initEq)
initOrders(portfolio.st, initDate=initDate)
strategy(strategy.st, store=TRUE)
#parameters
nSMA=20
#Add Indicators
add.indicator(strategy.st, name="SMA",
arguments=list(x=quote(Cl(mktdata)), n=nSMA),
label="sma")
#Add Entry and Exit Signals
add.signal(strategy.st, name="sigComparison",
arguments=list(columns=c("Close", "SMA.20.sma"),
relationship="gt"),
label="longentry")
#add.signal(strategy.st, name="sigComparison",
# arguments=list(columns=c("Close", "SMA.20.sma"),
relationship="lt"),
# label="longexit")
#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, timestamp, time.in.force='172800',
orderqty=tradeSize, osFUN=osMaxDollar, tradeSize=tradeSize,
maxSize=tradeSize),
type="enter", path.dep=TRUE, label="enterlong")
#add.rule(strategy.st, name="ruleSignal",
# arguments=list(sigcol="longexit", sigval=TRUE, ordertype="market",
# orderside="long", replace=FALSE, prefer="Open",
orderqty="all", TxnFees=-12),
# type="exit", path.dep=TRUE, label="exitlong")
#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
t1 <- Sys.time()
out2 <- applyStrategy(strategy=strategy.st,portfolios=portfolio.st )
t2 <- Sys.time()
print(t2-t1)
[[alternative HTML version deleted]]
More information about the R-SIG-Finance
mailing list