Tuesday, August 17, 2010

Interfacing C# .Net and R - Integrating the best of both worlds (Part 3) - Trader Desk Example

In this third and final part of the series (Part 1, Part 2), I am going to continue shaping the examples in the previous posts by quickly building a small application - a simple Trading Desk application. In the example I will use the same C# and R interface method together with a specific Quantitative Financial Modelling R library called Quantmod.

Example Objective
In the example the objective is to build a C# Web Form which acts as the main application controller that captures various user option parameters with the main functions being:

1. To automatically kick off an R routine to download data from Yahoo Finance.
2. To select specific dates of interest
3. To add or udpate different charts
4. To add different chart indicators
5. To calculate Period Return Statistics

Most of these functions are provided through the set of R functions exposed by the Quantmod R library.

Additions under the hood

Below I am showing some of the salient additions done to the code over and above the code in the previous posts. Since the underlying program structure is in line with the explanations in the previous posts I am not going to delve again into detailed explanations. The code should be pretty self explanatory.

//My RFacade Class - note the new addition to load Quantmod Library
public RFacade()
{
     rconn = new STATCONNECTORSRVLib.StatConnector();
     rconn.Init("R");


     //load R Quantmod Library
     rconn.EvaluateNoReturn("library(quantmod)");
}


//Get data from yahoo finance
public void RGetData(String symbol)
{
     String RCommand = "getSymbols('"+symbol+"',src='yahoo')";
     rconn.EvaluateNoReturn(RCommand);
}


//Draw Candle Chart for specific company symbol and date range
public void RCandleChart(String symbol, DateTime from, DateTime to)
{
     String dateRange = FormatDateRange(from, to);
     String RCommand = "candleChart(" + symbol +dateRange+",multi.col=TRUE,theme='white')";
     rconn.EvaluateNoReturn(RCommand);
}

//Draw Bar Chart for specific company symbol and date range
public void RBarChart(String symbol, DateTime from, DateTime to)
{
     String dateRange = FormatDateRange(from, to);
     String RCommand = "barChart(" + symbol + dateRange + ")";
     rconn.EvaluateNoReturn(RCommand);
}


//Draw ChartSeries plot for specific company symbol and date range
public void RTimeSeries(String symbol, DateTime from, DateTime to)
{
     String dateRange = FormatDateRange(from, to);
     String RCommand = "chartSeries(" + symbol + dateRange + ")";
     rconn.EvaluateNoReturn(RCommand);
}


//Create new R grahics window (/device)
public void RNewChart()
{
     String RCommand = "windows()";
     rconn.EvaluateNoReturn(RCommand);
}


//Add Indicators
public void addIndicatorSMA()
{
     String RCommand = "addSMA(20)";
     rconn.EvaluateNoReturn(RCommand);
}


public void addIndicatorDPO()
{
     String RCommand = "addDPO()";
     rconn.EvaluateNoReturn(RCommand);
}


public void addIndicatorBBands()
{
     String RCommand = "addBBands()";
     rconn.EvaluateNoReturn(RCommand);
}


//Get Mean Return
public double RGetMeanReturn(String symbol, DateTime from, DateTime to)
{
     String dateRange = FormatDateRange(from, to);
     String RCommand = "logRet<-diff(log(" + symbol + "))";
     rconn.EvaluateNoReturn(RCommand);
     RCommand = "mean(logRet" + dateRange + "[,4])";
     return (double)rconn.Evaluate(RCommand);
}

How it Looks
As you can see from the screen shot below, with this approach one can quickly start to mash up interesting applications by benefeting from the rich R platform and the flexible C# front end.

Hope you found this series of articles on C# and R interfacing using R(D)COM interesting ... and please anyone who wishes to continue discussing or collaborate or share experiences just drop me an email  or post a comment.


Click to Enlarge

Friday, August 13, 2010

Interfacing C# .Net and R - Integrating the best of both worlds (Part 2)

This post is a continuation from the previous post (Part I) focusing on interfacing C# with R using the R (D)COM. In this post I am going to enhance my previous exercise by creating a Facade .Net Class which facilitates access to specific functions in R.

Creating the R Facade Class

Creating a Facade Class (or a set of .Net classes) which acts as a .Net wrapper to R functions greatly facilitate the use of R functions and their integration within the .Net programming environment. Below I am showing an excerpt from the class RFacade that I have created in this example.

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using StatConnectorCommonLib;
using STATCONNECTORSRVLib;
using System.Runtime.InteropServices;


namespace R
{


class RFacade : IDisposable
{


     private StatConnector rconn;
     private bool disposed = false;


     public RFacade()
     {
          rconn = new STATCONNECTORSRVLib.StatConnector();
          rconn.Init("R");
     }


     public void Dispose()
     {
          Dispose(true);
          GC.SuppressFinalize(this);
     }


     protected virtual void Dispose(bool disposing)
     {
          if (!disposed)
          {
               if (disposing)
               {
                    if (rconn != null)
                    {
                         Marshal.ReleaseComObject(this.rconn);
                         rconn = null;
                    }
               }
               disposed = true;
          }
     }



.... continued

The RFacade constructor handles the creation of the COM connection and initialisation. As you can notice the above example presents also a slight improvement from the previous post when it comes to closing the connection. The RFacade class implements the IDisposable interface to ensure that all unmanaged resources are released when the wrapper is no longer used.

Setting and Getting Data from R

Although in the previous post I showed examples how this can be done, in this example I am going to provide methods in my RFacade class which further simplify the setting and getting of data from R. The two RFacade methods are show below. The first method, RGetRandomNormalVector, creates a vector on R which in return is populated by R with a set of random numbers from a Standard Normal Distribution. The second method, RSetVector, provides similar functionality but this time the data is provided by the C# front end. These two methods show how vectors can be passed easily back and forth between the two layers.

//Function to create an R vector of name [VectorName] and size [size]
//and generates random numbers from normal distribution


public double[] RGetRandomNormalVector (String VectorName, int size)


{
     String RCommand = VectorName + "<-rnorm(" + size + ")";
     double[] data = (double[])rconn.Evaluate(RCommand);
     return data;
}

//Function to create an R vector of name [VectorName] with specific data [data]

public void RSetVector(String VectorName, double[] data)
{
     int vectorSize = data.Count();
     StringBuilder stringData = new StringBuilder(vectorSize);
     for (int c=0; c<=(vectorSize-1); c++) {
          stringData.Append(data[c]);
          if (c < (vectorSize-1) ) {
               stringData.Append(",");
          }
     }
     String RCommand = VectorName + "<-c(" + stringData + ")";
     rconn.EvaluateNoReturn(RCommand);
}

... continued

Exposing R functions

At this stage we have everything ready to start using some R functions. In this example I am going to provide some simple functions which provide a good indication of how one can extend this functionality.


//Function to Calculate Correlation using R cor function

public double RCorrelate(String var1, String Var2)
{
     String RCommand = "cor(" + var1 + "," + Var2 + ")";
     return (double)rconn.Evaluate(RCommand);
}




//Function to plot a simple XY graph using R graphics
public void RPlot(String var1, String Var2)
{
     String RCommand = "plot(" + var1 + "," + Var2 + ")";
     rconn.EvaluateNoReturn(RCommand.ToString());
}




//Function to Calculate Linear Regression using R lm function
//Returns a double array, [0] intercept [1] gradient
public double[] RLinearRegression(String dependentVar, String independentVar)
{
     String RCommand = "fit <- lm("+ dependentVar + " ~ " + independentVar +")";
     rconn.EvaluateNoReturn(RCommand);
     return (double[])rconn.Evaluate("fit$coefficients");
}

Using R from C# .Net is easier...

Once we have our Facade class and also a set of common functions, making use of R becomes much easier from C#. The exerpt below provides examples of method calls from my main Form which accesses a number of exposed R functions via the RFacade object instance RInstance.

double[] year = new double[] { 2000, 2001, 2002, 2003, 2004 };

double[] rate = new double[] { 9.34, 8.50, 7.62, 6.93, 6.60 };


RInstance.RSetVector("year", year);
RInstance.RSetVector("rate", rate);

double corr = RInstance.RCorrelate("year", "rate");

RInstance.RPlot("year", "rate");

double[] regressionCoeff = RInstance.RLinearRegression("rate", "year");


double[] data = RInstance.RGetRandomNormalVector("mydata", dataSize);

This article is continued in a third and final post Part 3.

Thursday, August 12, 2010

Interfacing C# .Net and R - Integrating the best of both worlds

In specific software areas like in quantitative finance or else in other mathematical domains, data centric programming typically requires a good balance between three requirements - (1) a solid platform with rich mathematical/statistical functionality (2) having an easy to use, contemporary, programming environment which permits easy and flexible front end code development and (3) an easy to use interface between the two environments.

In this artcile I am going to explain how such a balance can be attained by using two of the best products in their specific worlds - using the rich R library as the mathematical/statistical component but then interfacing with C# for the front end application design. As an interfacing option I banked on using R (D)COM which provides an easy to use interfacing method which keeps you away from spending hours identifying interfacing problems.

The software required for this tutorial is the following:
1. R software (download from here)
2. R (D)COM Interface (download from here)
3. If you dont have a C# IDE you can download Visual Studio Express for free (download from here)

Once you installed all your software we can start focusing on the small example. Albeit a simple example, the example will show the flexibility that one can attain by interfacing R and C#. In the example we have a C# Windows Form which fires a request to R in order to generate a random data set from a standard normal distribution, calculate some basic statistics, and then display the data and results back on our form.

STEP 1 - Creating the Windows Form

In Visual Studio create a new Windows Form application.

STEP 2 - Add project reference to the R (D)COM library

From the solution explorer add reference to the R (D)COM library. In the COM components list you typically find the component name listed as "Repository for R COM Server Instances".

STEP 3 - Add library references to your form code

This is done by adding references as follows:

using StatConnectorCommonLib;
using STATCONNECTORSRVLib;

STEP 4 - Setting and Initializing Connection

Two variables were defined in the form class. The variable rconn of type StatConnector represents the connection to the R COM component. The variable dataSize is used to store the size of the data array that we are going to randomly generate from R.

private StatConnector rconn;
private int dataSize;

Both these variables are initialized in the default constructor of my form, in my case named frmMain.

public frmMain()
{
InitializeComponent();
dataSize = 100;
rconn = new STATCONNECTORSRVLib.StatConnector();
rconn.Init("R");
}

STEP 5 - Invoking R commands and displaying results

The form that I created in my example had the following components:

1. A listbox which is used to present the random numbers generated from R
2. Two labels which present the Mean and Standard Deviation of the data calculated from R
3. A button which kicks off the process

The button clicked handler code is presented below:

private void btnGenData_Click(object sender, EventArgs e)
{
        rconn.SetSymbol("sdataSize",dataSize);


       //Generate Data in R
       rconn.Evaluate("sdata<-rnorm(sdataSize)");
       double[] data = (double[])rconn.GetSymbol("sdata");


      //Calculate Statistics in R
      rconn.Evaluate("saverage<-mean(sdata)");
      rconn.Evaluate("sstdev<-sd(sdata)");
     double average = (double)rconn.GetSymbol("saverage");
     double stdev = (double)rconn.GetSymbol("sstdev");




     // Display Data and Results on Windows Form
     lbDataList.Items.Clear();
     for (int c = 0; c < dataSize; c++)
    {
       lbDataList.Items.Add(data[c]);
    }
    lblAverage.Text = "Average: " + average;
    lblStdDev.Text = "Standard Deviation: " + stdev;


}

As it can be seen from the above code, the interfacing mechanism is quite simple and intuitive. The only important thing to note is that the COM object returns back general object references which need to be typcasted as required. In the case of the data variable which holds the random data vetor, this is typecasted into a double[] type. In the case of a single return value form average and standard deviation this is typecasted into a double.

Pretty easy right? I found this method really easy to use and keeps you focused on the problem that you wish to solve, rather than spending hours debugging code. Using C# for your front end application also provides a lot of flexibility when it comes to building world class applications and user interfaces. Testing the method with large amounts of data, running in the hundreds of thousands, also proved to be very efficient.

A continuation of this article is available in Part 2