[R-SIG-Finance] blotter package, can't add new symbol to existing portfolio

ce zadig_1 at excite.com
Wed Nov 27 14:52:31 CET 2013


Dear all,

I am following blotter example in the http://artax.karlin.mff.cuni.cz/r-help/library/blotter/html/blotter-package.html
I create, initialize , and add transactions. but when it comes to add a new stock symbol to  portfolio it gives an error message. Example adds MMM, I get Error in if (nrow(PosData) > 1) { : argument is of length zero 

R version 3.0.2 

> library(blotter)
Loading required package: xts
Loading required package: zoo

Attaching package: ‘zoo’

The following objects are masked from ‘package:base’:

    as.Date, as.Date.numeric

Loading required package: FinancialInstrument
Loading required package: quantmod
Loading required package: Defaults
Loading required package: TTR
Version 0.4-0 included new data defaults. See ?getSymbols.
Loading required package: PerformanceAnalytics

Package PerformanceAnalytics (1.1.0) loaded.
Econometric tools for performance and risk analysis.
(c) 2004-2012 Peter Carl, Brian G. Peterson. License: GPL
http://r-forge.r-project.org/projects/returnanalytics/


> currency("USD")
[1] "USD"
> symbols = c("IBM","F")
> for(symbol in symbols){ # establish tradable instruments
+     stock(symbol, currency="USD", multiplier=1)
+ }
> 
> # Download price data
> require(quantmod)
> getSymbols(symbols, from='2007-01-01', to='2007-01-31', src='yahoo',
+ index.class=c("POSIXt","POSIXct"))
[1] "IBM" "F"  
> 
> # Initialize a portfolio object 'p'
> print('Creating portfolio \"p\"...')
[1] "Creating portfolio \"p\"..."
> initPortf('p', symbols=symbols, currency="USD")
[1] "p"
> 
> ## Trades must be made in date order.
> print('Adding trades to \"p\"...')
[1] "Adding trades to \"p\"..."
> # Make a couple of trades in IBM
> addTxn(Portfolio = "p", Symbol = "IBM", TxnDate = '2007-01-03', TxnQty = 50, 
+ TxnPrice = 96.5, TxnFees = -0.05*50)
[1] "2007-01-03 00:00:00 IBM 50 @ 96.5"
> addTxn("p", "IBM", '2007-01-04', 50, 97.1, TxnFees = -0.05*50)
[1] "2007-01-04 00:00:00 IBM 50 @ 97.1"
> 
> # ...a few in F...
> addTxn("p", "F", '2007-01-03', -100, 7.60, TxnFees = pennyPerShare(-100))
[1] "2007-01-03 00:00:00 F -100 @ 7.6"
> addTxn("p", "F", '2007-01-04', 50, 7.70, TxnFees = pennyPerShare(50))
[1] "2007-01-04 00:00:00 F 50 @ 7.7"
> addTxn("p", "F", '2007-01-10', 50, 7.78, TxnFees = pennyPerShare(50))
[1] "2007-01-10 00:00:00 F 50 @ 7.78"
> getSymbols("MMM", from='2007-01-01', to='2007-01-31', src='yahoo',
+ index.class=c("POSIXt","POSIXct")) # Download price data
[1] "MMM"
> stock("MMM", currency="USD", multiplier=1) # Add the instrument
[1] "MMM"
> 
> # Now we can add transactions:
> addTxn("p", "MMM", '2007-01-05', -50, 77.9, TxnFees = -0.05*50)
Error in if (nrow(PosData) > 1) { : argument is of length zero



More information about the R-SIG-Finance mailing list