[R-SIG-Finance] Problem with updatePortf
Worik Stanton
worik.stanton at gmail.com
Fri Sep 21 04:30:05 CEST 2012
On 21/09/12 14:10, Worik Stanton wrote:
> [snip]
> I am working on a small example that exhibits the problem but in the
> mean time is there a way I can debug .updatePosPL?
So here is an example without transactions.
I am a novice with blotter so tis example could probably be smaller. I
have tried to distill what are the most important points (hence no
transactions).
When I run this I get...
> go()
2005-09-01
FOO
Error in getPrice(get(Symbol, pos = env), symbol = symbol, prefer =
prefer) :
subscript out of bounds, no price was discernible from the data
>
require(quantmod)
require(TTR)
require(blotter)
go <- function(){
suppressWarnings(try(rm("account.turtles","portfolio.turtles",
pos=.blotter),silent=TRUE))
suppressWarnings(try(rm("portfolio","account","N","symbol",
"symbols","ClosePrice",
"CurrentDate","equity","Units",
"maxUnits","size","Stop",
"equity","TxnPrice","initDate",
"initEq","Posn","verbose"),silent=TRUE)
)
currency("USD")
currency("CHF")
exchange_rate("USDCHF", "CHF", "USD", 0.00001)
## Load all the symbols into memory
USDCHF <- xts(matrix(ncol=3,c(1.2558, 1.2368, 1.2344, 1.2404,
1.2444, 1.2470, 1.2470, 1.2597, 1.2640,
1.2613, 1.2696, 1.2729, 1.2815, 1.2818,
1.2825, 1.2809, 1.2918, 1.2965, 1.2998,
1.2984, 1.2311, 1.2250, 1.2239, 1.2317,
1.2309, 1.2385, 1.2385, 1.2430, 1.2542,
1.2543, 1.2574, 1.2598, 1.2734, 1.2745,
1.2680, 1.2660, 1.2777, 1.2885, 1.2883,
1.2910, 1.2343, 1.2280, 1.2335, 1.2365,
1.2426, 1.2449, 1.2442, 1.2592, 1.2619,
1.2585, 1.2671, 1.2687, 1.2772, 1.2809,
1.2718, 1.2795, 1.2916, 1.2896, 1.2959,
1.2940)), as.Date(c("2005-09-01",
"2005-09-02", "2005-09-05", "2005-09-06",
"2005-09-07", "2005-09-08", "2005-09-09",
"2005-09-12", "2005-09-13", "2005-09-14",
"2005-09-15", "2005-09-16", "2005-09-19",
"2005-09-20", "2005-09-21", "2005-09-22",
"2005-09-23", "2005-09-26", "2005-09-27",
"2005-09-28")))
## Set initial values
initDate=start(USDCHF)
initEq=100000
## Set up a portfolio object and an account object
portfolio = "turtles"
assign("portfolio", portfolio, pos=1)
initPortf(name=portfolio,symbols, initDate=initDate)
account = "turtles"
assign("account", account, pos=1)
initAcct(name=account,portfolios="turtles", initDate=initDate,
initEq=initEq)
Portfolio<-getPortfolio(portfolio)
Portfolio[["USDCHF"]]$strat <- xts( as.matrix(t(c(0))),
order.by=as.POSIXct(initDate) )
colnames(Portfolio[["USDCHF"]]$strat) <- c('Txn')
## now put it back where it belongs
assign( "portfolio.turtles", Portfolio , envir=.blotter )
rm("Portfolio")
for( i in index(USDCHF)) {
i <- as.Date(i)
CurrentDate <- i
cat(as.character(CurrentDate), "\n")
equity = getEndEq(account, CurrentDate)
cat("FOO \n")
updatePortf(Portfolio = portfolio, Dates = CurrentDate)
updateAcct(account, Dates = CurrentDate)
updateEndEq(account, Dates = CurrentDate)
} # End dates loop
getEndEq(account,Sys.time())
}
##go()
--
it does not matter I think that I shall never see
how much I dig and dig A billboard lovely as a tree
this hole just Indeed, unless the billboards fall
keeps getting deeper I'll never see a tree at all
More information about the R-SIG-Finance
mailing list