8. C++ and Portfolio Probe

The Strategy

Given that the real work of optimization and generating random portfolios in Portfolio Probe is done in C, it would seem that going directly from C++ to C would be the thing to do.  That is the most direct approach but far, far from easy.  There are hundreds of lines of R code that would need to be reproduced in C++ in order to pull that off.

The approach taken here is to use the RInside package in R to call R functions from Portfolio Probe.  So C++ calls R which calls C.  Thanks to the great efforts of the authors of RInside and Rcpp this is quite easy and powerful.

The Example

The example is done in Windows.  At present there are a few rough spots in RInside under Windows.  But if you follow the hints here, you should not have trouble.  It is likely to go smoothly in Linux.

Preliminaries

The requirements are:

  • On Windows you need Rtools installed, and hence on your PATH.
  • You need the RInside R package (which depends on Rcpp).
  • R_HOME needs to be set.  The makefile may or may not be able to set this correctly (the attempt is commented out in the version of Makefile.win here).
  • The location of the DLLs that R uses needs to be on your PATH
  • This will probably be fixed at some point, but you may need to set R_LIBS_USER.

The example was done with R version 2.14.1 (32-bit), Rtools214 and RInside 0.2.6.  This combination of tools has been successful under both Windows 7 and XP.

The value of R_HOME used was:  C:/PROGRA~1/R/R-214~1.1

The directory added to PATH to say where R’s DLLs are:  C:\Program Files\R\R-2.14.1\bin\i386

My value of  R_LIBS_USER is:  C:/Users/pat/Documents/R/win-library/2.14

Running the example

Put into a directory the two files:

In a command window, go to that directory and do:

make -f Makefile.win

Assuming that works (there may be a warning at the start about “MS-DOS style path detected” that you can ignore), then you can do:

rinside_portprobe1.exe

This should print out some results from a tiny optimization based on a variance matrix of some random data. Here is an example of the output.

The print out includes a warning from the optimizer that the utility was changed from the default value.  This warning could have been suppressed in at least three ways, but it shows what happens with warnings.

Trouble Spots

If you run into problems, here are some hints.

R not found

If you get messages on the order of:

/bin/R not found

Then R_HOME is probably not set, or not set properly.

R.dll not found

Error messages saying that there is no R.dll on the machine indicates that the location of R.dll and friends is not on your PATH.

No Rcpp package

Error messages like:

There is no package called Rcpp

This means that R is not able to find the library of installed packages.  The cause of this is that R_LIBS_USER is not set, or not set properly.

Do not mix 32-bit and 64-bit

It’s going to get grumpy if you have a mix of 32-bit and 64-bit resources.

Errors in R calls

It is probably best to have complex R calls (like to trade.optimizer) wrapped in calls to try or tryCatch.  This catches the errors in R so that you can deal with them sensibly.

Navigate

Back to the top level of “Portfolio Probe Cookbook”