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