2

I am trying to save as a CSV file the orderbook generated after running a portfolio on quantstrat.

order_book <- getOrderBook(qs.portfolio)
write.csv(order_book, "orderbook.csv")

I am getting the following error message:

Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : cannot coerce class ""order_book"" to a data.frame

I am new to R so I believe there is a simple solution, but I haven't been able to figure it out.

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
Yago
  • 63
  • 5

1 Answers1

3

The order book is a list of data by strategy and instrument. So you need to subset the list by the strategy and instrument you want the order book for. Here's an example:

demo("bbands", package = "quantstrat", ask = FALSE)
order_book <- getOrderBook("bbands")
# Now subset
ibm_order_book <- order_book[["bbands"]][["IBM"]]
ibm_order_book[1:3,1:3]
#                     Order.Qty Order.Price Order.Type
# 2007-02-26 00:00:00 "100"     "96.910004" "market"  
# 2007-03-20 00:00:00 "all"     "94.5"      "market"  
# 2007-04-03 00:00:00 "-100"    "96.099998" "market"  
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • You're welcome. And welcome to StackOverlow. Be sure to read [What do to when someone answers my question](http://stackoverflow.com/help/someone-answers). – Joshua Ulrich May 12 '17 at 22:57