[R-SIG-Finance] Blotter package - problem with example.

Jan Vandermeer nordicgnome at gmail.com
Wed Oct 14 04:22:48 CEST 2009


Hi all;

I've been following the discussion here on backtesting
([R-SIG-Finance] Backtesting framework package -
<https://stat.ethz.ch/pipermail/r-sig-finance/2009q2/004404.html> and
the discussion  [R-SIG-Finance] QuantMod trading models docs?
initiated by Mark Knecht and decided to give the blotter package a
try.
I tried running  through the example provided by the blotter package.
</usr/lib64/R/library/blotter/html/blotter-package.html>
I installed the latest version from R-forge. I am running R version
2.9.1 (2009-06-26) on an AMD Turion 64 with a Gentoo 2.6.31 kernel.

Things ran smoothly as shown below until I got to the "p =
updatePortf(p,'2007-01')" step, where R kicked out

"Error in if (mid != 0 && key < vec[mid]) { :
  missing value where TRUE/FALSE needed".

So I went along and looked at the
"/usr/lib64/R/library/blotter/html/00Index.html", which sent me along
to "/usr/lib64/R/library/blotter/html/updatePosPL.html" and attempted
to adjust some of the values because the usage seems to indicate that
additional values may be required

"Usage
updatePosPL(Portfolio, Symbol, StartDate, EndDate, Prices = Cl(get(Symbol)))
updatePortf(Portfolio, StartDate, EndDate)"

I added an EndDate using the same format as in the getSymbols command.
and received the following error.

> p = updatePortf(p,'2007-01-01`, '2007-01-31')
Error: unexpected numeric constant in "p = updatePortf(p,'2007-01-01`, '2007"


I tried a couple of variations on the updatePosPL()

> updatePosPL(p, "IBM", '2007-01-01', '2007-01-31', Prices = Cl(get(Symbol)))
Error in updatePosPL(p, "IBM", "2007-01-01", "2007-01-31", Prices =
Cl(get(Symbol))) :
  unused argument(s) ("2007-01-31")
> updatePosPL(p, "IBM", '2007-01-01', '2007-01-31', Prices = Cl(get(Symbol)))
Error in updatePosPL(p, "IBM", "2007-01-01", "2007-01-31", Prices =
Cl(get(Symbol))) :
  unused argument(s) ("2007-01-31")
> updatePosPL(p, "IBM", '2007-01-01', Prices = Cl(get(Symbol)))
Error in get(Symbol) : object 'Symbol' not found
> updatePosPL(p, "IBM", '2007-01-01', Prices = Cl(get(symbol)))
Error in get(symbol) : object 'symbol' not found

I also tried:
> updatePosPL(p, "IBM", '2007-01-01', Prices = Cl(IBM))
Error in if (any(i < 0)) { : missing value where TRUE/FALSE needed


At this point I am at a bit of a loss. I tried looking through some of
the stuff on R-Project to see if there are guidelines for code
examples and where and if they should work. I guess I would expect the
code examples include in the library or
"/usr/lib64/R/library/blotter/R-ex" to just work.

So, I am I doing something glaringly wrong? Do I need to update my
version of R, xts, zoo or quantmod? I know I am on still on the steep
part of the learning curve as far as R in general is concerned and
specifically with time series and financial data with R but R texts
I've seen suggest that working through examples with real data and
known steps is a great way to learn. Successfully working through
examples allows one small sign posts of success along the way

Any assistance would be appreciated.

Jan



Blotter example entered into R and responses after the first _________

I tried looking at the output for "p", see below the second ______. It
appears to be included in the data.frame

Values obtained through quantmod are below the third _______

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

Attaching package: 'zoo'


        The following object(s) are masked from package:base :

         as.Date.numeric

xts now requires a valid TZ environment variable to be set
 no TZ var is set, setting to TZ=GMT
> symbols = c("IBM","F","MMM")
> require(quantmod)
Loading required package: quantmod
Loading required package: Defaults
Loading required package: TTR
> getSymbols(symbols, from='2007-01-01', to='2007-01-31', index.class="POSIXct")
[1] "IBM" "F"   "MMM"
> print('Creating portfolio \"p\"...')
[1] "Creating portfolio \"p\"..."
> p = initPortf(symbols=symbols)
> print('Adding trades to \"p\"...')
[1] "Adding trades to \"p\"..."
> # Make a couple of trades in IBM
> p = addTxn(p, "IBM", '2007-01-03', 50, 96.5, -0.05*50)
[1] "IBM 2007-01-03 50 @ 96.5"
> p = addTxn(p, "IBM", '2007-01-04', 50, 97.1, -0.05*50)
[1] "IBM 2007-01-04 50 @ 97.1"
> # ...a few in F.
> p = addTxn(p, "F", '2007-01-03', -100, 7.60, -0.05*100)
[1] "F 2007-01-03 -100 @ 7.6"
> p = addTxn(p, "F", '2007-01-04', 50, 7.70, -0.05*50)
[1] "F 2007-01-04 50 @ 7.7"
> p = addTxn(p, "F", '2007-01-10', 50, 7.78, -0.05*50)
[1] "F 2007-01-10 50 @ 7.78"
> # ...and some in MMM
> p = addTxn(p, "MMM", '2007-01-05', -50, 77.9, -0.05*50)
[1] "MMM 2007-01-05 -50 @ 77.9"
> p = addTxn(p, "MMM", '2007-01-08', 50, 77.6, -0.05*50)
[1] "MMM 2007-01-08 50 @ 77.6"
> p = addTxn(p, "MMM", '2007-01-09', 50, 77.6, -0.05*50)
[1] "MMM 2007-01-09 50 @ 77.6"
___________


It appears that the transactions have been posted correctly

> p
$IBM
$IBM$txn
           Txn.Qty Txn.Price Txn.Fees Txn.Value Txn.Avg.Cost Pos.Qty
1950-01-01       0       0.0      0.0       0.0         0.00       0
2007-01-03      50      96.5     -2.5    4827.5        96.55      50
2007-01-04      50      97.1     -2.5    4857.5        97.15     100
           Pos.Avg.Cost Realized.PL
1950-01-01         0.00           0
2007-01-03        96.55           0
2007-01-04        96.85           0

$IBM$posPL
           Pos.Qty Pos.Value Txn.Value Txn.Fees Realized.PL Unrealized.PL
1950-01-01       0         0         0        0           0             0
           Trading.PL
1950-01-01          0


$F
$F$txn
           Txn.Qty Txn.Price Txn.Fees Txn.Value Txn.Avg.Cost Pos.Qty
1950-01-01       0      0.00      0.0       0.0         0.00       0
2007-01-03    -100      7.60     -5.0    -755.0         7.55    -100
2007-01-04      50      7.70     -2.5     387.5         7.75     -50
2007-01-10      50      7.78     -2.5     391.5         7.83       0
           Pos.Avg.Cost Realized.PL
1950-01-01         0.00           0
2007-01-03         7.55           0
2007-01-04         7.35         -10
2007-01-10         0.00         -24

$F$posPL
           Pos.Qty Pos.Value Txn.Value Txn.Fees Realized.PL Unrealized.PL
1950-01-01       0         0         0        0           0             0
           Trading.PL
1950-01-01          0


$MMM
$MMM$txn
           Txn.Qty Txn.Price Txn.Fees Txn.Value Txn.Avg.Cost Pos.Qty
1950-01-01       0       0.0      0.0       0.0         0.00       0
2007-01-05     -50      77.9     -2.5   -3892.5        77.85     -50
2007-01-08      50      77.6     -2.5    3882.5        77.65       0
2007-01-09      50      77.6     -2.5    3882.5        77.65      50
           Pos.Avg.Cost Realized.PL
1950-01-01         0.00           0
2007-01-05        77.85           0
2007-01-08         0.00          10
2007-01-09        77.65           0

$MMM$posPL
           Pos.Qty Pos.Value Txn.Value Txn.Fees Realized.PL Unrealized.PL
1950-01-01       0         0         0        0           0             0
           Trading.PL
1950-01-01

_________________________________________

> IBM
           IBM.Open IBM.High IBM.Low IBM.Close IBM.Volume IBM.Adjusted
2007-01-03    97.18    98.40   96.26     97.27    9196800        92.84
2007-01-04    97.25    98.79   96.88     98.31   10524500        93.83
2007-01-05    97.60    97.95   96.91     97.42    7221300        92.98
2007-01-08    98.50    99.50   98.35     98.90   10340000        94.40
2007-01-09    99.08   100.33   99.07    100.07   11108200        95.51
2007-01-10    98.50    99.05   97.93     98.89    8744800        94.39
2007-01-11    99.00    99.90   98.50     98.65    8000700        94.16
2007-01-12    98.99    99.69   98.50     99.34    6636500        94.82
2007-01-16    99.40   100.84   99.30    100.82    9602200        96.23
2007-01-17   100.69   100.90   99.90    100.02    8200700        95.47
2007-01-18    99.80    99.95   98.91     99.45   14725000        94.92
2007-01-19    95.00    96.85   94.55     96.17   26035800        91.79
2007-01-22    96.42    97.23   96.12     97.11   13539300        92.69
2007-01-23    96.91    97.38   96.20     97.08   10337400        92.66
2007-01-24    97.08    97.58   96.58     97.40    5700000        92.96
2007-01-25    97.22    97.92   97.22     97.51    6201300        93.07
2007-01-26    97.52    97.83   96.84     97.45    5771100        93.01
2007-01-29    97.70    98.66   97.45     98.54    7294800        94.05
2007-01-30    98.57    99.45   98.50     99.37    7177900        94.84
2007-01-31    98.80    99.48   98.35     99.15    6432600        94.63
> F
           F.Open F.High F.Low F.Close  F.Volume F.Adjusted
2007-01-03   7.56   7.67  7.44    7.51  78652200       7.51
2007-01-04   7.56   7.72  7.43    7.70  63454900       7.70
2007-01-05   7.72   7.75  7.57    7.62  40562100       7.62
2007-01-08   7.63   7.75  7.62    7.73  48938500       7.73
2007-01-09   7.75   7.86  7.73    7.79  56732200       7.79
2007-01-10   7.79   7.79  7.67    7.73  42397100       7.73
2007-01-11   7.73   7.80  7.68    7.77  40020800       7.77
2007-01-12   7.77   7.92  7.76    7.89  57053800       7.89
2007-01-16   7.89   8.01  7.87    7.94  66699800       7.94
2007-01-17   7.97   8.10  7.97    8.04  63728700       8.04
2007-01-18   8.06   8.24  8.06    8.18  78375700       8.18
2007-01-19   8.24   8.32  8.17    8.30  66023300       8.30
2007-01-22   8.33   8.43  8.25    8.41  53554600       8.41
2007-01-23   8.39   8.62  8.24    8.30 116406900       8.30
2007-01-24   8.31   8.35  8.11    8.20  76821900       8.20
2007-01-25   8.15   8.52  8.00    8.22  99688700       8.22
2007-01-26   8.15   8.48  8.14    8.42  54421900       8.42
2007-01-29   8.44   8.51  8.35    8.37  55791000       8.37
2007-01-30   8.38   8.42  8.19    8.20  28763200       8.20
2007-01-31   8.17   8.20  8.04    8.13  65038600       8.13
> MMM
           MMM.Open MMM.High MMM.Low MMM.Close MMM.Volume MMM.Adjusted
2007-01-03    77.53    78.85   77.38     78.26    3781500        72.40
2007-01-04    78.40    78.41   77.45     77.95    2968400        72.11
2007-01-05    77.89    77.90   77.01     77.42    2765200        71.62
2007-01-08    77.42    78.04   76.97     77.59    2434500        71.78
2007-01-09    78.00    78.23   77.44     77.68    1896800        71.86
2007-01-10    77.31    77.96   77.04     77.85    1787500        72.02
2007-01-11    78.05    79.03   77.88     78.65    2372500        72.76
2007-01-12    78.41    79.50   78.22     79.36    2582200        73.42
2007-01-16    79.48    79.62   78.92     79.56    2526600        73.60
2007-01-17    79.33    79.51   78.75     78.91    2711300        73.00
2007-01-18    78.71    79.70   78.60     78.81    1963000        72.91
2007-01-19    79.09    79.88   78.82     79.25    2912100        73.32
2007-01-22    79.25    79.29   78.13     78.49    2139300        72.61
2007-01-23    78.71    79.15   78.33     78.85    1949500        72.95
2007-01-24    78.85    79.66   78.85     79.49    1772700        73.54
2007-01-25    79.56    79.75   78.83     79.01    2888700        73.09
2007-01-26    78.97    79.19   78.23     78.69    1996300        72.80
2007-01-29    78.54    79.20   78.25     78.96    3420300        73.05
2007-01-30    74.92    76.18   74.24     74.70   15657900        69.11
2007-01-31    74.21    74.68   73.09     74.30    9284500        68.74



More information about the R-SIG-Finance mailing list