Quantcast
Channel: Vince Vella's Blog
Viewing all articles
Browse latest Browse all 29

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

$
0
0
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

Viewing all articles
Browse latest Browse all 29

Trending Articles