3

I have a data frame with dates and prices as:

>df
Price  Date
1.25   2012-01-05
...

I create a currency and and stock:

currency("USD")
stock("GSPC", "USD")

I then create an xts object:

GSPC <- xts(df$Price, df$Date)
colnames(GSPC) <- "Close"

The intended use of is to create a blotter portfolio -- this works. But when I try to

updatePortf(portfolio, Symbols="GSPC", Dates = current.date)

I get the following error:

Error in get(Symbol, pos = env) : object 'GSPC' not found

GSPC doesn't show up in "showSymbols()" so I assume that it needs to be registered somewhere. Is there any way to register the symbol?

A very hacky workaround, inspired by another stackoverflow answer is:

GSPC$GSPC.High <- GSPC$Open
GSPC$GSPC.Low <- GSPC$Open
GSPC$GSPC.Close <- GSPC$Open
GSPC$GSPC.Volume <- GSPC$Open
GSPC$GSPC.Adjusted <- GSPC$Open

write.zoo(GSPC, file="GSPC.csv", sep=",")
setSymbolLookup(GSPC=list(src="csv",format="%Y-%m-%d"))

getSymbols("GSPC")

Is there any better way to create the above? I don't have (need) Volume, high, low, close and adjusted - do I still need them for blotter?

UPDATE I've managed to reproduce the issue and then understant why it is. It seems like you cannot declare the xts objects in the local function environment. Here is a reproducible script:

library(xts)
library(FinancialInstrument)
library(blotter)
library(lubridate)
rm(list=ls(envir=.blotter),envir=.blotter)

runme <- function() {
  currency("USD")
  stock("GSPC", "USD")

  dates <-ymd("2012-03-02") + seq(0,9) * ddays(1)
  prices <- abs(rnorm(10))

  GSPC <- xts(prices, dates)
  colnames(GSPC) <- "Close"

  # Initialise
  initPortf("p", symbols="GSPC", initDate=ymd("2012-01-01"), currency="USD")
  initAcct("a", portfolios="p", initDate=ymd("2012-01-01"), initEq=2e6, currency="USD")

  trade.date <- ymd("2012-03-04")
  addTxn("p", "GSPC", trade.date, 1, GSPC[trade.date])

  updatePortf("p", Symbols="GSPC", Dates = trade.date)
  updateAcct("a", Dates = trade.date)

  updateEndEq("a", Dates = trade.date)

  chart.Posn("p")

}
svenski
  • 543
  • 4
  • 14
  • What is in `portfolio`? What is `current.date`? What is the **exact** error message you get? If you need help making it reproducible, see http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – GSee Aug 09 '12 at 14:20
  • Thank you for adding reproducible code! :-) – GSee Aug 09 '12 at 19:21

2 Answers2

2

The blotter package makes use of the FinancialInstrument package. You probably need to define your instrument. I think if you do something like this it should work.

synthetic("GSPC", currency("USD"))

If not, perhaps you could provide a reproducible example. (we don't have your portfolio)

EDIT:

To more directly answer the question in your title, getSymbols stores a list called .getSymbols in your .GlobalEnv of all Symbols that you have used getSymbols to load. However, I don't think blotter looks at it.

EDIT 2:

The error message you get is telling you that there is no data in your .GlobalEnv named "GSPC". You have to either have an xts object in your .GlobalEnv, or pass an xts object to updatePortf in the Prices argument. Again, I could give better help if your example were reproducible (not to mention that it would make it more useful for future visitors of this site).

GSee
  • 48,880
  • 13
  • 125
  • 145
  • Sorry, I didn't include the first lines that defined the stock. I'll update my post. – svenski Aug 09 '12 at 14:15
  • 1
    When you do, go ahead and make the whole thing reproducible! – GSee Aug 09 '12 at 14:16
  • It could be that you don't have any data on `current.date`. You could try using `Dates=NULL` or `Dates=paste0("/", current.date)`, but you haven't provided the actual error you received, or shown what data are in your objects. – GSee Aug 09 '12 at 14:27
  • Thank you, you've made your point about the reproducibility several times. I agree and I'm trying. I have a script where this happens that is very long and additionally I cannot share it. The condensed version of the same script is similar to what you see above with one mock trade does not show the same error. So for some reason the "GSPC" is not seen in the main script although if I debug at the point it's clearly there as an xts object. I'll update the issue if I manage to reproduce it in a shorter script. – svenski Aug 09 '12 at 15:45
  • Thanks the update about .GlobalEnv helped me solve the issue. – svenski Aug 09 '12 at 16:38
  • If you comment out the `chart.Posn` line, you could use `updatePortf("p", Symbols="GSPC", Dates = trade.date, Prices=GSPC)` instead. However, `chart.Posn` requires that the data be in the `.GlobalEnv` which you can either call a missing feature or a bug depending on your mood. ;-) – GSee Aug 09 '12 at 17:03
1

Just for completeness, to make the snippet work above you need to assign GSPC to the .GlobalEnv:

assign("GSPC", GSPC, envir=.GlobalEnv)

The full script is:

library(xts)
library(FinancialInstrument)
library(blotter)
library(lubridate)
rm(list=ls(envir=.blotter),envir=.blotter)

runme <- function() {
  currency("USD")
  stock("GSPC", "USD")

  dates <-ymd("2012-03-02") + seq(0,9) * ddays(1)
  prices <- abs(rnorm(10))

  GSPC <- xts(prices, dates)
  colnames(GSPC) <- "Close"
  assign("GSPC", GSPC, envir=.GlobalEnv)

  # Initialise
  initPortf("p", symbols="GSPC", initDate=ymd("2012-01-01"), currency="USD")
  initAcct("a", portfolios="p", initDate=ymd("2012-01-01"), initEq=2e6, currency="USD")

  trade.date <- ymd("2012-03-04")
  addTxn("p", "GSPC", trade.date, 1, GSPC[trade.date])

  updatePortf("p", Symbols="GSPC", Dates = trade.date)
  updateAcct("a", Dates = trade.date)

  updateEndEq("a", Dates = trade.date)

  chart.Posn("p")

}
svenski
  • 543
  • 4
  • 14