GetOrders with Quantstrat
On Tue, Apr 5, 2016 at 4:26 AM, Ryan Crawford <suchislife27 at gmail.com> wrote:
Hello I have been trying to view "open" orders from the orderbook in Quantstrat and keep running into the same error. It seems a reasonably simple process however the error I get is: oB <- getOrders(portfolio.st, symbols, status="open") Error in orderbook[[portfolio]][[symbol]] : recursive indexing failed at level 2 I've searched extensively and I can't seem to pinpoint the issue. Below is a reproducible example that generates the error. Thank you in advance for any help.
While reproducible, a strategy with 22 symbols isn't minimal. Regardless, I ran your code, but no orders are created and lots of warnings are generated by IKTrading function(s). I also get a different error when calling getOrders(): "Error in orderbook[[portfolio]][[symbol]] : no such index at level 2". ...which is reasonable, given there are no orders.
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="2016-04-04")
# Define the instrument type
stock(symbols, currency = "USD", multiplier = 1)
#Boilerplate
from = "2010-01-01"
to = "2016-04-04"
#trade sizing and initial equity settings
tradeSize <- 2500
initEq <- 100000
strategy.st <- portfolio.st <- account.st <- "GetOrderBook"
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.20.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 )
#get open orders
oB <- getOrders(portfolio.st, symbols, status="open")
[[alternative HTML version deleted]]
_______________________________________________ R-SIG-Finance at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Joshua Ulrich | about.me/joshuaulrich FOSS Trading | www.fosstrading.com R/Finance 2016 | www.rinfinance.com