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")
}
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).