Skip to content

Problem with updatePortf

5 messages · Worik Stanton, G See

#
Friends

I am running into trouble with updatePortf.

Stepping through it the problem is with the call to .updatePosPL, but I
cannot debug that function.

I am working on a small example that exhibits the problem but in the
mean time is there a way I can debug .updatePosPL?

cheers
Worik
#
If by "cannot debug that function" you mean you can't find the source
code, you can view the code with

    blotter:::.updatePosPL
or
    getAnywhere(".updatePosPL")

Or, your can checkout the code with svn to see a commented copy.

   svn checkout svn://svn.r-forge.r-project.org/svnroot/blotter/

and browse to the file pkg/blotter/R/updatePosPL.R

Or, you can view it online at
https://r-forge.r-project.org/scm/viewvc.php/*checkout*/pkg/blotter/R/updatePosPL.R?root=blotter

HTH,
Garrett
On Thu, Sep 20, 2012 at 9:10 PM, Worik Stanton <worik.stanton at gmail.com> wrote:
#
On 21/09/12 14:10, Worik Stanton wrote:
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...
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()
#
getPrice looks for a price column and it only works if the xts object
has colnames.
Error in getPrice(USDCHF) :
  subscript out of bounds, no price was discernible from the data

You need to add colnames.
Last.Price
2005-09-01     1.2343
2005-09-02     1.2280
2005-09-05     1.2335
2005-09-06     1.2365
2005-09-07     1.2426
2005-09-08     1.2449
2005-09-09     1.2442
2005-09-12     1.2592
2005-09-13     1.2619
2005-09-14     1.2585
2005-09-15     1.2671
2005-09-16     1.2687
2005-09-19     1.2772
2005-09-20     1.2809
2005-09-21     1.2718
2005-09-22     1.2795
2005-09-23     1.2916
2005-09-26     1.2896
2005-09-27     1.2959
2005-09-28     1.2940

Although getPrice is not documented, you can look at its code to see
what it does.  If you do not provide a value for the "prefer"
argument, it looks for a column name that contains the word "price".
If it cannot find that, it looks for "trade".  Finally, it looks for
"close".  If your colnames don't contain any of those words, or if you
want to use a different column, you can specify that with the "prefer"
argument
Bid
2005-09-01 1.2558
2005-09-02 1.2368
2005-09-05 1.2344
2005-09-06 1.2404
2005-09-07 1.2444
2005-09-08 1.2470
2005-09-09 1.2470
2005-09-12 1.2597
2005-09-13 1.2640
2005-09-14 1.2613
2005-09-15 1.2696
2005-09-16 1.2729
2005-09-19 1.2815
2005-09-20 1.2818
2005-09-21 1.2825
2005-09-22 1.2809
2005-09-23 1.2918
2005-09-26 1.2965
2005-09-27 1.2998
2005-09-28 1.2984

Garrett
On Thu, Sep 20, 2012 at 9:30 PM, Worik Stanton <worik.stanton at gmail.com> wrote:
#
On 21/09/12 14:43, G See wrote:
Now the error is:
2005-09-01
FOO
Error in `*.default`(TmpPeriods[, columns], CcyMult) :
  non-conformable arrays

Adding a transaction in the inner loop...

    equity = getEndEq(account, CurrentDate)

    addTxn(Portfolio=portfolio, Symbol="USDCHF",
           TxnDate=CurrentDate,
           TxnPrice=1.1, TxnQty = 10 ,
                 TxnFees=0, verbose=TRUE)
   
   
    cat("FOO \n")

gives the following error...
2005-09-01
[1] "2005-09-01 00:00:00 USDCHF 10 @ 1.1"
FOO
Error in `*.default`(TmpPeriods[, columns], CcyMult) :
  non-conformable arrays

cheers
W